Spaces:
Running on Zero
Running on Zero
File size: 6,395 Bytes
3e33e2e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | import type {
AttributionItem,
BacktestRunRequest,
CoverageTimeline,
DatasetSummary,
DrawdownPoint,
ExperimentDetail,
ExperimentSummary,
Health,
MarketBarItem,
MonthlyReturns,
PortfolioAllocationRequest,
PortfolioAllocationResponse,
SeriesPoint,
StrategyCatalogItem,
SymbolCatalogItem,
TradePageResponse,
} from "./types";
async function request<T>(path: string, options: RequestInit = {}): Promise<T> {
const response = await fetch(path, {
...options,
headers: {
"Content-Type": "application/json",
...(options.headers || {}),
},
});
if (!response.ok) {
let errorDetail = `HTTP ${response.status}`;
try {
const errorJson = await response.json();
if (errorJson.detail) {
errorDetail = typeof errorJson.detail === "string" ? errorJson.detail : JSON.stringify(errorJson.detail);
}
} catch {
// ignore json parse error on non-json error responses
}
throw new Error(errorDetail);
}
return response.json() as Promise<T>;
}
export const api = {
// System
health: () => request<Health>("/health"),
// Dataset
datasetSummary: () => request<DatasetSummary>("/api/v1/dataset/summary"),
symbols: (search?: string, sector?: string) => {
const params = new URLSearchParams();
if (search) params.append("search", search);
if (sector && sector !== "ALL") params.append("sector", sector);
const qs = params.toString() ? `?${params.toString()}` : "";
return request<SymbolCatalogItem[]>(`/api/v1/dataset/symbols${qs}`);
},
coverageTimeline: () => request<CoverageTimeline>("/api/v1/dataset/coverage-timeline"),
// Market Data
bars: (
symbol: string,
start?: string,
end?: string,
resolution: "15m" | "1h" | "1d" = "15m",
limit = 1500
) => {
const params = new URLSearchParams({
symbol,
resolution,
limit: limit.toString(),
});
if (start) params.append("start", start);
if (end) params.append("end", end);
return request<MarketBarItem[]>(`/api/v1/market/bars?${params.toString()}`);
},
// Strategies & Backtesting
strategies: () => request<StrategyCatalogItem[]>("/api/v1/strategies"),
runBacktest: (payload: BacktestRunRequest) =>
request<ExperimentDetail>("/api/v1/backtest/run", {
method: "POST",
body: JSON.stringify(payload),
}),
streamBacktest: async (
payload: BacktestRunRequest,
onProgress: (progress: {
progress_pct: number;
current_date: string;
current_equity: number;
trades_count: number;
point?: { timestamp: string; equity: number };
last_trade?: { symbol: string; net_pnl: number; exit_time: string; exit_reason: string };
}) => void,
onComplete: (detail: ExperimentDetail) => void,
onError: (err: Error) => void
) => {
try {
const response = await fetch("/api/v1/backtest/stream", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const reader = response.body?.getReader();
if (!reader) throw new Error("ReadableStream not supported");
const decoder = new TextDecoder();
let buffer = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n\n");
buffer = lines.pop() || "";
for (const block of lines) {
const line = block.trim();
if (line.startsWith("data: ")) {
try {
const data = JSON.parse(line.slice(6));
if (data.type === "progress") {
onProgress(data);
} else if (data.type === "complete") {
onComplete(data);
}
} catch (e) {
console.error("Error parsing stream chunk:", e);
}
}
}
}
} catch (err: any) {
onError(err instanceof Error ? err : new Error(String(err)));
}
},
// Experiments
experiments: (offset = 0, limit = 50) =>
request<{ items: ExperimentSummary[]; total: number }>(
`/api/v1/experiments?offset=${offset}&limit=${limit}`
),
experiment: (id: string) =>
request<ExperimentDetail>(`/api/v1/experiments/${encodeURIComponent(id)}`),
equity: (id: string, maxPoints = 1500) =>
request<SeriesPoint[]>(
`/api/v1/experiments/${encodeURIComponent(id)}/equity?maximum_points=${maxPoints}`
),
drawdown: (id: string, maxPoints = 1500) =>
request<DrawdownPoint[]>(
`/api/v1/experiments/${encodeURIComponent(id)}/drawdown?maximum_points=${maxPoints}`
),
trades: (
id: string,
options: {
symbol?: string;
outcome?: "win" | "loss";
exitReason?: string;
offset?: number;
limit?: number;
} = {}
) => {
const params = new URLSearchParams({
offset: (options.offset ?? 0).toString(),
limit: (options.limit ?? 100).toString(),
});
if (options.symbol) params.append("symbol", options.symbol);
if (options.outcome) params.append("outcome", options.outcome);
if (options.exitReason) params.append("exit_reason", options.exitReason);
return request<TradePageResponse>(
`/api/v1/experiments/${encodeURIComponent(id)}/trades?${params.toString()}`
);
},
attribution: (id: string) =>
request<AttributionItem[]>(`/api/v1/experiments/${encodeURIComponent(id)}/attribution`),
monthly: (id: string) =>
request<MonthlyReturns>(`/api/v1/experiments/${encodeURIComponent(id)}/monthly`),
compare: (ids: string[]) => {
const params = new URLSearchParams();
ids.forEach((id) => params.append("ids", id));
return request<ExperimentSummary[]>(`/api/v1/experiments/compare?${params.toString()}`);
},
// Portfolio Allocation
allocatePortfolio: (req: PortfolioAllocationRequest) =>
request<PortfolioAllocationResponse>("/api/v1/portfolio/allocate", {
method: "POST",
body: JSON.stringify(req),
}),
// Export URLs
exportTradesCsvUrl: (id: string) =>
`/api/v1/reports/${encodeURIComponent(id)}/export-csv?kind=trades`,
exportEquityCsvUrl: (id: string) =>
`/api/v1/reports/${encodeURIComponent(id)}/export-csv?kind=equity`,
};
|