added initial py analytics / rust core / ts orchestrator services
This commit is contained in:
parent
680b5fd2ae
commit
c862ed496b
62 changed files with 13459 additions and 0 deletions
162
apps/stock/orchestrator/src/api/rest/backtest.ts
Normal file
162
apps/stock/orchestrator/src/api/rest/backtest.ts
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
import { Hono } from 'hono';
|
||||
import { z } from 'zod';
|
||||
import { logger } from '@stock-bot/logger';
|
||||
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 {
|
||||
const app = new Hono();
|
||||
const backtestEngine = container.get('BacktestEngine') as BacktestEngine;
|
||||
const modeManager = container.get('ModeManager') as ModeManager;
|
||||
|
||||
// Run new backtest
|
||||
app.post('/run', 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);
|
||||
}
|
||||
|
||||
logger.error('Error running backtest:', error);
|
||||
return c.json({
|
||||
error: error instanceof Error ? error.message : 'Failed to run backtest'
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
|
||||
// Stop running backtest
|
||||
app.post('/stop', async (c) => {
|
||||
try {
|
||||
await backtestEngine.stopBacktest();
|
||||
|
||||
return c.json({
|
||||
message: 'Backtest stop requested',
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Error stopping backtest:', error);
|
||||
return c.json({
|
||||
error: error instanceof Error ? error.message : 'Failed to stop backtest'
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
|
||||
// Get backtest progress
|
||||
app.get('/progress', async (c) => {
|
||||
try {
|
||||
// In real implementation, would track progress
|
||||
return c.json({
|
||||
status: 'running',
|
||||
progress: 0.5,
|
||||
processed: 10000,
|
||||
total: 20000,
|
||||
currentTime: new Date().toISOString()
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Error getting backtest progress:', error);
|
||||
return c.json({
|
||||
error: error instanceof Error ? error.message : 'Failed to get progress'
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
|
||||
// Stream backtest events (Server-Sent Events)
|
||||
app.get('/stream', async (c) => {
|
||||
c.header('Content-Type', 'text/event-stream');
|
||||
c.header('Cache-Control', 'no-cache');
|
||||
c.header('Connection', 'keep-alive');
|
||||
|
||||
const stream = new ReadableStream({
|
||||
start(controller) {
|
||||
// Listen for backtest events
|
||||
const onProgress = (data: any) => {
|
||||
controller.enqueue(`data: ${JSON.stringify(data)}\n\n`);
|
||||
};
|
||||
|
||||
const onComplete = (data: any) => {
|
||||
controller.enqueue(`data: ${JSON.stringify({ event: 'complete', data })}\n\n`);
|
||||
controller.close();
|
||||
};
|
||||
|
||||
backtestEngine.on('progress', onProgress);
|
||||
backtestEngine.on('complete', onComplete);
|
||||
|
||||
// Cleanup on close
|
||||
c.req.raw.signal.addEventListener('abort', () => {
|
||||
backtestEngine.off('progress', onProgress);
|
||||
backtestEngine.off('complete', onComplete);
|
||||
controller.close();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return new Response(stream);
|
||||
});
|
||||
|
||||
// Validate backtest configuration
|
||||
app.post('/validate', async (c) => {
|
||||
try {
|
||||
const body = await c.req.json();
|
||||
const config = BacktestConfigSchema.parse(body);
|
||||
|
||||
// Additional validation logic
|
||||
const validation = {
|
||||
valid: true,
|
||||
warnings: [] as string[],
|
||||
estimatedDuration: 0
|
||||
};
|
||||
|
||||
// Check data availability
|
||||
const startDate = new Date(config.startDate);
|
||||
const endDate = new Date(config.endDate);
|
||||
const days = (endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24);
|
||||
|
||||
if (days > 365) {
|
||||
validation.warnings.push('Large date range may take significant time to process');
|
||||
}
|
||||
|
||||
if (config.symbols.length > 100) {
|
||||
validation.warnings.push('Large number of symbols may impact performance');
|
||||
}
|
||||
|
||||
// Estimate duration (simplified)
|
||||
validation.estimatedDuration = days * config.symbols.length * 0.1; // seconds
|
||||
|
||||
return c.json(validation);
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return c.json({
|
||||
valid: false,
|
||||
error: 'Invalid configuration',
|
||||
details: error.errors
|
||||
}, 400);
|
||||
}
|
||||
|
||||
return c.json({
|
||||
valid: false,
|
||||
error: error instanceof Error ? error.message : 'Validation failed'
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue