continuation

This commit is contained in:
Boki 2026-10-20 16:57:59 -04:00
parent 32e43c9243
commit 879e9b4228
42 changed files with 2607 additions and 1306 deletions

View file

@ -1,12 +1,10 @@
import { randomUUID as uuidv4 } from 'node:crypto';
import { at } from '@stock-bot/invariants';
import { getLogger } from '@stock-bot/logger';
import { requireServices, type IServiceContainer } from '@stock-bot/services';
const logger = getLogger('backtest-service');
// Use environment variable or default
const ORCHESTRATOR_URL = process.env.ORCHESTRATOR_URL || 'http://localhost:2004';
import type { IServiceContainer } from '@stock-bot/services';
import {
BacktestServiceV2,
type Backtest,
type BacktestTransport,
type Run,
} from './backtest-v2.service';
export interface BacktestRequest {
strategy: string;
@ -16,10 +14,10 @@ export interface BacktestRequest {
initialCapital: number;
config?: Record<string, unknown>;
}
export interface BacktestJob {
id: string;
status: 'pending' | 'running' | 'completed' | 'failed' | 'cancelled';
runId?: string;
status: Run['status'];
strategy: string;
symbols: string[];
startDate: Date;
@ -31,271 +29,56 @@ export interface BacktestJob {
error?: string;
}
/** Compatibility facade: V1 uses the same definitions, runs, and result reader as V2. */
export class BacktestService {
private container: IServiceContainer & { postgres: NonNullable<IServiceContainer['postgres']> };
constructor(container: IServiceContainer) {
this.container = requireServices(container, 'postgres');
logger.info('BacktestService initialized', {
hasPostgres: !!container.postgres,
containerKeys: Object.keys(container),
});
private readonly service: BacktestServiceV2;
constructor(container: IServiceContainer, request?: BacktestTransport) {
this.service = new BacktestServiceV2(container, request);
}
async createBacktest(request: BacktestRequest): Promise<BacktestJob> {
const backtestId = uuidv4();
// Insert into PostgreSQL
const result = await this.container.postgres.query(
`INSERT INTO backtests
(id, name, strategy, symbols, start_date, end_date, initial_capital, config, status)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'pending')
RETURNING *`,
[
backtestId,
request.config?.name || `Backtest ${new Date().toISOString()}`,
request.strategy,
JSON.stringify(request.symbols),
request.startDate,
request.endDate,
request.initialCapital,
JSON.stringify(request.config || {}),
]
);
const backtest: BacktestJob = {
id: at(result.rows, 0).id,
status: at(result.rows, 0).status,
strategy: at(result.rows, 0).strategy,
symbols: at(result.rows, 0).symbols,
startDate: at(result.rows, 0).start_date,
endDate: at(result.rows, 0).end_date,
initialCapital: parseFloat(at(result.rows, 0).initial_capital),
config: at(result.rows, 0).config,
createdAt: at(result.rows, 0).created_at,
updatedAt: at(result.rows, 0).updated_at,
};
// Call orchestrator to run backtest
try {
const response = await fetch(`${ORCHESTRATOR_URL}/api/backtest/run`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
mode: 'backtest',
startDate: new Date(request.startDate).toISOString(),
endDate: new Date(request.endDate).toISOString(),
symbols: request.symbols,
strategy: request.strategy,
initialCapital: request.initialCapital,
dataFrequency: '1d', // Default to daily
commission: request.config?.commission,
slippage: request.config?.slippage,
speed: 'max', // Default speed
fillModel: {
slippage: 'realistic',
marketImpact: true,
partialFills: true,
},
}),
});
if (!response.ok) {
throw new Error(`Orchestrator returned ${response.status}`);
}
const result = await response.json();
// Store result directly without transformation
if (result.status === 'completed') {
// Update backtest status
await this.container.postgres.query(
'UPDATE backtests SET status = $1, updated_at = NOW() WHERE id = $2',
['completed', backtestId]
);
// Store results in backtest_results table
await this.saveBacktestResults(backtestId, result);
logger.info('Backtest completed', {
backtestId,
trades: result.metrics?.totalTrades,
return: result.metrics?.totalReturn,
});
} else {
// Update status to running if not completed
await this.container.postgres.query(
'UPDATE backtests SET status = $1, updated_at = NOW() WHERE id = $2',
['running', backtestId]
);
}
logger.info('Backtest started in orchestrator', { backtestId, result });
} catch (error) {
logger.error('Failed to start backtest in orchestrator', { backtestId, error });
await this.container.postgres.query(
'UPDATE backtests SET status = $1, error = $2, updated_at = NOW() WHERE id = $3',
['failed', error instanceof Error ? error.message : String(error), backtestId]
);
backtest.status = 'failed';
backtest.error = error instanceof Error ? error.message : String(error);
}
return backtest;
}
async getBacktest(id: string): Promise<BacktestJob | null> {
const result = await this.container.postgres.query('SELECT * FROM backtests WHERE id = $1', [
id,
]);
if (result.rows.length === 0) {
return null;
}
const row = at(result.rows, 0);
return {
id: row.id,
status: row.status,
strategy: row.strategy,
symbols: row.symbols,
startDate: row.start_date,
endDate: row.end_date,
initialCapital: parseFloat(row.initial_capital),
config: row.config,
createdAt: row.created_at,
updatedAt: row.updated_at,
error: row.error,
};
}
async getBacktestResults(id: string) {
const result = await this.container.postgres.query(
`SELECT
br.*,
b.strategy,
b.symbols,
b.start_date,
b.end_date,
b.initial_capital,
b.config as backtest_config
FROM backtest_results br
JOIN backtests b ON b.id = br.backtest_id
WHERE br.backtest_id = $1`,
[id]
);
if (result.rows.length === 0) {
return null;
}
const row = at(result.rows, 0);
// Reconstruct the result format expected by frontend
return {
backtestId: row.backtest_id,
status: 'completed',
completedAt: row.completed_at.toISOString(),
config: {
name: row.backtest_config?.name || 'Backtest',
strategy: row.strategy,
symbols: row.symbols,
startDate: row.start_date.toISOString(),
endDate: row.end_date.toISOString(),
initialCapital: parseFloat(row.initial_capital),
commission: row.backtest_config?.commission ?? 0.001,
slippage: row.backtest_config?.slippage ?? 0.0001,
dataFrequency: row.backtest_config?.dataFrequency || '1d',
const backtest = await this.service.createBacktest(
{
...request,
name: typeof request.config?.name === 'string' ? request.config.name : 'Backtest',
},
metrics: row.metrics,
equity: row.equity_curve,
ohlcData: row.ohlc_data,
trades: row.trades,
positions: row.positions,
analytics: row.analytics,
executionTime: row.execution_time,
};
}
async listBacktests(params: { limit: number; offset: number }): Promise<BacktestJob[]> {
const result = await this.container.postgres.query(
`SELECT * FROM backtests
ORDER BY created_at DESC
LIMIT $1 OFFSET $2`,
[params.limit, params.offset]
{ autoStart: false }
);
return result.rows.map(row => ({
id: row.id,
status: row.status,
strategy: row.strategy,
symbols: row.symbols,
startDate: row.start_date,
endDate: row.end_date,
initialCapital: parseFloat(row.initial_capital),
config: row.config,
createdAt: row.created_at,
updatedAt: row.updated_at,
error: row.error,
}));
const run = await this.service.createRun({ backtestId: backtest.id, speedMultiplier: null });
return this.toJob(backtest, run);
}
async updateBacktestStatus(
id: string,
status: BacktestJob['status'],
error?: string
): Promise<void> {
if (error) {
await this.container.postgres.query(
'UPDATE backtests SET status = $1, error = $2, updated_at = NOW() WHERE id = $3',
[status, error, id]
);
} else {
await this.container.postgres.query(
'UPDATE backtests SET status = $1, updated_at = NOW() WHERE id = $2',
[status, id]
);
async getBacktest(id: string): Promise<BacktestJob | null> {
const backtest = await this.service.getBacktest(id);
if (!backtest) {
return null;
}
return this.toJob(backtest, (await this.service.listRuns(id))[0]);
}
async listBacktests(params: { limit: number; offset: number }): Promise<BacktestJob[]> {
return Promise.all(
(await this.service.listBacktests(params)).map(async backtest =>
this.toJob(backtest, (await this.service.listRuns(backtest.id))[0])
)
);
}
async getBacktestResults(id: string) {
const run = (await this.service.listRuns(id))[0];
return run ? this.service.getRunResults(run.id) : null;
}
async cancelBacktest(id: string): Promise<void> {
await this.updateBacktestStatus(id, 'cancelled');
// Call orchestrator to stop backtest
try {
await fetch(`${ORCHESTRATOR_URL}/api/backtest/stop`, {
method: 'POST',
});
} catch (error) {
logger.error('Failed to stop backtest in orchestrator', { backtestId: id, error });
const run = (await this.service.listRuns(id))[0];
if (!run) {
throw new Error('Backtest run not found');
}
await this.service.cancelRun(run.id);
}
private async saveBacktestResults(
backtestId: string,
result: Record<string, unknown>
): Promise<void> {
try {
await this.container.postgres.query(
`INSERT INTO backtest_results
(backtest_id, completed_at, metrics, equity_curve, ohlc_data, trades, positions, analytics, execution_time)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
[
backtestId,
result.completedAt || new Date(),
JSON.stringify(result.metrics || {}),
JSON.stringify(result.equity || result.equityCurve || []),
JSON.stringify(result.ohlcData || {}),
JSON.stringify(result.trades || []),
JSON.stringify(result.positions || result.finalPositions || {}),
JSON.stringify(result.analytics || {}),
result.executionTime || 0,
]
);
} catch (error) {
logger.error('Failed to save backtest results', { backtestId, error });
throw error;
}
private toJob(backtest: Backtest, run?: Run): BacktestJob {
return {
...backtest,
runId: run?.id,
status: run?.status ?? 'pending',
updatedAt: run?.updatedAt ?? backtest.updatedAt,
error: run?.error,
};
}
}