stock-bot/apps/stock/orchestrator/src/backtest/native-result.ts
2026-10-20 14:35:45 -04:00

89 lines
2.4 KiB
TypeScript

import { z } from 'zod';
/** Serialized contract of engine/src/backtest/simple_results.rs. */
export const nativeResultSchema = z
.object({
backtestId: z.string(),
status: z.enum(['completed', 'failed', 'cancelled']),
completedAt: z.string(),
config: z
.object({
name: z.string(),
symbols: z.array(z.string()),
startDate: z.string(),
endDate: z.string(),
initialCapital: z.number(),
})
.passthrough(),
metrics: z
.object({
totalReturn: z.number(),
sharpeRatio: z.number(),
maxDrawdown: z.number(),
winRate: z.number(),
totalTrades: z.number(),
profitFactor: z.number().nullable(),
avgWin: z.number(),
avgLoss: z.number(),
})
.passthrough(),
equity: z.array(z.object({ date: z.string(), value: z.number() })),
trades: z.array(
z.object({
id: z.string(),
symbol: z.string(),
timestamp: z.string(),
side: z.enum(['buy', 'sell']),
price: z.number(),
quantity: z.number(),
commission: z.number(),
positionAfter: z.number(),
pnl: z.number().optional(),
})
),
positions: z.array(
z.object({
symbol: z.string(),
quantity: z.number(),
averagePrice: z.number(),
currentPrice: z.number(),
unrealizedPnl: z.number(),
realizedPnl: z.number(),
})
),
analytics: z.object({ exposureTime: z.number() }).passthrough(),
ohlcData: z.record(z.array(z.record(z.unknown()))),
executionTime: z.number(),
error: z.string().optional(),
})
.passthrough();
export type NativeBacktestResult = z.infer<typeof nativeResultSchema>;
/** Serialized contracts of engine/src/positions/mod.rs. */
export const closedTradesSchema = z.array(
z.object({
id: z.string(),
symbol: z.string(),
entry_time: z.string(),
exit_time: z.string(),
entry_price: z.number(),
exit_price: z.number(),
quantity: z.number(),
side: z.enum(['Buy', 'Sell']),
pnl: z.number(),
pnl_percent: z.number(),
commission: z.number(),
duration_ms: z.number(),
})
);
export type ClosedTrade = z.infer<typeof closedTradesSchema>[number];
export const openPositionsSchema = z.array(
z.object({
symbol: z.string(),
quantity: z.number(),
average_price: z.number(),
realized_pnl: z.number(),
unrealized_pnl: z.number(),
})
);