messy work. backtests / mock-data

This commit is contained in:
Boki 2025-07-03 08:37:23 -04:00
parent 4e4a048988
commit fa70ada2bb
51 changed files with 2576 additions and 887 deletions

View file

@ -1,10 +1,9 @@
import { Server as SocketIOServer, Socket } from 'socket.io';
import { logger } from '@stock-bot/logger';
import { z } from 'zod';
import { IServiceContainer } from '@stock-bot/di';
import { MarketDataService } from '../../services/MarketDataService';
import { ExecutionService } from '../../services/ExecutionService';
import { ModeManager } from '../../core/ModeManager';
import { Container } from '@stock-bot/di';
const SubscribeSchema = z.object({
symbols: z.array(z.string()),
@ -15,16 +14,16 @@ const UnsubscribeSchema = z.object({
symbols: z.array(z.string())
});
export function setupWebSocketHandlers(io: SocketIOServer, container: Container): void {
const marketDataService = container.get('MarketDataService') as MarketDataService;
const executionService = container.get('ExecutionService') as ExecutionService;
const modeManager = container.get('ModeManager') as ModeManager;
export function setupWebSocketHandlers(io: SocketIOServer, container: IServiceContainer): void {
const marketDataService = container.custom?.MarketDataService as MarketDataService;
const executionService = container.custom?.ExecutionService as ExecutionService;
const modeManager = container.custom?.ModeManager as ModeManager;
// Track client subscriptions
const clientSubscriptions = new Map<string, Set<string>>();
io.on('connection', (socket: Socket) => {
logger.info(`WebSocket client connected: ${socket.id}`);
container.logger.info(`WebSocket client connected: ${socket.id}`);
clientSubscriptions.set(socket.id, new Set());
// Send initial connection info
@ -44,13 +43,13 @@ export function setupWebSocketHandlers(io: SocketIOServer, container: Container)
subscriptions.add(symbol);
}
logger.debug(`Client ${socket.id} subscribed to: ${symbols.join(', ')}`);
container.logger.debug(`Client ${socket.id} subscribed to: ${symbols.join(', ')}`);
if (callback) {
callback({ success: true, symbols });
}
} catch (error) {
logger.error('Subscription error:', error);
container.logger.error('Subscription error:', error);
if (callback) {
callback({
success: false,
@ -83,13 +82,13 @@ export function setupWebSocketHandlers(io: SocketIOServer, container: Container)
}
}
logger.debug(`Client ${socket.id} unsubscribed from: ${symbols.join(', ')}`);
container.logger.debug(`Client ${socket.id} unsubscribed from: ${symbols.join(', ')}`);
if (callback) {
callback({ success: true, symbols });
}
} catch (error) {
logger.error('Unsubscribe error:', error);
container.logger.error('Unsubscribe error:', error);
if (callback) {
callback({
success: false,
@ -107,7 +106,7 @@ export function setupWebSocketHandlers(io: SocketIOServer, container: Container)
callback({ success: true, result });
}
} catch (error) {
logger.error('Order submission error:', error);
container.logger.error('Order submission error:', error);
if (callback) {
callback({
success: false,
@ -127,7 +126,7 @@ export function setupWebSocketHandlers(io: SocketIOServer, container: Container)
callback({ success: true, positions });
}
} catch (error) {
logger.error('Error getting positions:', error);
container.logger.error('Error getting positions:', error);
if (callback) {
callback({
success: false,
@ -139,7 +138,7 @@ export function setupWebSocketHandlers(io: SocketIOServer, container: Container)
// Handle disconnection
socket.on('disconnect', async () => {
logger.info(`WebSocket client disconnected: ${socket.id}`);
container.logger.info(`WebSocket client disconnected: ${socket.id}`);
// Unsubscribe from all symbols for this client
const subscriptions = clientSubscriptions.get(socket.id);
@ -191,5 +190,5 @@ export function setupWebSocketHandlers(io: SocketIOServer, container: Container)
});
});
logger.info('WebSocket handlers initialized');
container.logger.info('WebSocket handlers initialized');
}