messy work. backtests / mock-data
This commit is contained in:
parent
4e4a048988
commit
fa70ada2bb
51 changed files with 2576 additions and 887 deletions
|
|
@ -1,21 +1,48 @@
|
|||
import { Hono } from 'hono';
|
||||
import { z } from 'zod';
|
||||
import { logger } from '@stock-bot/logger';
|
||||
import { IServiceContainer } from '@stock-bot/di';
|
||||
import { BacktestConfigSchema } from '../../types';
|
||||
import { BacktestEngine } from '../../backtest/BacktestEngine';
|
||||
import { ModeManager } from '../../core/ModeManager';
|
||||
import { container } from '../../container';
|
||||
|
||||
const BacktestIdSchema = z.object({
|
||||
backtestId: z.string()
|
||||
});
|
||||
|
||||
export function createBacktestRoutes(): Hono {
|
||||
export function createBacktestRoutes(container: IServiceContainer): Hono {
|
||||
const app = new Hono();
|
||||
const backtestEngine = container.get('BacktestEngine') as BacktestEngine;
|
||||
const modeManager = container.get('ModeManager') as ModeManager;
|
||||
const backtestEngine = container.custom?.BacktestEngine as BacktestEngine;
|
||||
const modeManager = container.custom?.ModeManager as ModeManager;
|
||||
|
||||
// Run new backtest
|
||||
// Default POST to / is the same as /run for backward compatibility
|
||||
app.post('/', async (c) => {
|
||||
try {
|
||||
const body = await c.req.json();
|
||||
const config = BacktestConfigSchema.parse(body);
|
||||
|
||||
// Initialize backtest mode
|
||||
await modeManager.initializeMode(config);
|
||||
|
||||
// Run backtest
|
||||
const result = await backtestEngine.runBacktest(config);
|
||||
|
||||
return c.json(result, 201);
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return c.json({
|
||||
error: 'Invalid backtest configuration',
|
||||
details: error.errors
|
||||
}, 400);
|
||||
}
|
||||
|
||||
container.logger.error('Error running backtest:', error);
|
||||
return c.json({
|
||||
error: error instanceof Error ? error.message : 'Failed to run backtest'
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
|
||||
// Run new backtest (same as above but at /run)
|
||||
app.post('/run', async (c) => {
|
||||
try {
|
||||
const body = await c.req.json();
|
||||
|
|
@ -36,7 +63,7 @@ export function createBacktestRoutes(): Hono {
|
|||
}, 400);
|
||||
}
|
||||
|
||||
logger.error('Error running backtest:', error);
|
||||
container.logger.error('Error running backtest:', error);
|
||||
return c.json({
|
||||
error: error instanceof Error ? error.message : 'Failed to run backtest'
|
||||
}, 500);
|
||||
|
|
@ -53,7 +80,7 @@ export function createBacktestRoutes(): Hono {
|
|||
timestamp: new Date().toISOString()
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Error stopping backtest:', error);
|
||||
container.logger.error('Error stopping backtest:', error);
|
||||
return c.json({
|
||||
error: error instanceof Error ? error.message : 'Failed to stop backtest'
|
||||
}, 500);
|
||||
|
|
@ -72,7 +99,7 @@ export function createBacktestRoutes(): Hono {
|
|||
currentTime: new Date().toISOString()
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Error getting backtest progress:', error);
|
||||
container.logger.error('Error getting backtest progress:', error);
|
||||
return c.json({
|
||||
error: error instanceof Error ? error.message : 'Failed to get progress'
|
||||
}, 500);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue