messy work. backtests / mock-data
This commit is contained in:
parent
4e4a048988
commit
fa70ada2bb
51 changed files with 2576 additions and 887 deletions
|
|
@ -1,5 +1,7 @@
|
|||
import { EventEmitter } from 'events';
|
||||
import { logger } from '@stock-bot/logger';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
|
||||
const logger = getLogger('BaseStrategy');
|
||||
import { MarketData, StrategyConfig, OrderRequest } from '../types';
|
||||
import { ModeManager } from '../core/ModeManager';
|
||||
import { ExecutionService } from '../services/ExecutionService';
|
||||
|
|
|
|||
|
|
@ -1,39 +1,41 @@
|
|||
import { logger } from '@stock-bot/logger';
|
||||
import { EventEmitter } from 'events';
|
||||
import { IServiceContainer } from '@stock-bot/di';
|
||||
import { MarketData, StrategyConfig, OrderRequest } from '../types';
|
||||
import { BaseStrategy } from './BaseStrategy';
|
||||
import { ModeManager } from '../core/ModeManager';
|
||||
import { MarketDataService } from '../services/MarketDataService';
|
||||
import { ExecutionService } from '../services/ExecutionService';
|
||||
import { TradingEngine } from '../../core';
|
||||
import { TradingEngine } from '@stock-bot/core';
|
||||
|
||||
export class StrategyManager extends EventEmitter {
|
||||
private strategies = new Map<string, BaseStrategy>();
|
||||
private activeStrategies = new Set<string>();
|
||||
private tradingEngine: TradingEngine | null = null;
|
||||
private container: IServiceContainer;
|
||||
|
||||
constructor(
|
||||
private modeManager: ModeManager,
|
||||
private marketDataService: MarketDataService,
|
||||
private executionService: ExecutionService
|
||||
) {
|
||||
constructor(container: IServiceContainer) {
|
||||
super();
|
||||
this.setupEventListeners();
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
private setupEventListeners(): void {
|
||||
setupEventListeners(): void {
|
||||
const marketDataService = this.container.custom?.MarketDataService;
|
||||
const executionService = this.container.custom?.ExecutionService;
|
||||
|
||||
if (!marketDataService || !executionService) {
|
||||
this.container.logger.error('Required services not found in container');
|
||||
return;
|
||||
}
|
||||
|
||||
// Listen for market data
|
||||
this.marketDataService.on('marketData', (data: MarketData) => {
|
||||
marketDataService.on('marketData', (data: MarketData) => {
|
||||
this.handleMarketData(data);
|
||||
});
|
||||
|
||||
// Listen for market data batches (more efficient)
|
||||
this.marketDataService.on('marketDataBatch', (batch: MarketData[]) => {
|
||||
marketDataService.on('marketDataBatch', (batch: MarketData[]) => {
|
||||
this.handleMarketDataBatch(batch);
|
||||
});
|
||||
|
||||
// Listen for fills
|
||||
this.executionService.on('fill', (fill: any) => {
|
||||
executionService.on('fill', (fill: any) => {
|
||||
this.handleFill(fill);
|
||||
});
|
||||
}
|
||||
|
|
@ -47,7 +49,10 @@ export class StrategyManager extends EventEmitter {
|
|||
this.activeStrategies.clear();
|
||||
|
||||
// Get trading engine from mode manager
|
||||
this.tradingEngine = this.modeManager.getTradingEngine();
|
||||
const modeManager = this.container.custom?.ModeManager;
|
||||
if (modeManager) {
|
||||
this.tradingEngine = modeManager.getTradingEngine();
|
||||
}
|
||||
|
||||
// Initialize new strategies
|
||||
for (const config of configs) {
|
||||
|
|
@ -59,9 +64,9 @@ export class StrategyManager extends EventEmitter {
|
|||
await this.enableStrategy(config.id);
|
||||
}
|
||||
|
||||
logger.info(`Initialized strategy: ${config.name} (${config.id})`);
|
||||
this.container.logger.info(`Initialized strategy: ${config.name} (${config.id})`);
|
||||
} catch (error) {
|
||||
logger.error(`Failed to initialize strategy ${config.name}:`, error);
|
||||
this.container.logger.error(`Failed to initialize strategy ${config.name}:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -71,8 +76,8 @@ export class StrategyManager extends EventEmitter {
|
|||
// For now, create a base strategy instance
|
||||
const strategy = new BaseStrategy(
|
||||
config,
|
||||
this.modeManager,
|
||||
this.executionService
|
||||
this.container.custom?.ModeManager,
|
||||
this.container.custom?.ExecutionService
|
||||
);
|
||||
|
||||
// Set up strategy event handlers
|
||||
|
|
@ -80,12 +85,10 @@ export class StrategyManager extends EventEmitter {
|
|||
this.handleStrategySignal(config.id, signal);
|
||||
});
|
||||
|
||||
strategy.on('order', (order: OrderRequest) => {
|
||||
this.handleStrategyOrder(config.id, order);
|
||||
strategy.on('error', (error: Error) => {
|
||||
this.container.logger.error(`Strategy ${config.id} error:`, error);
|
||||
});
|
||||
|
||||
await strategy.initialize();
|
||||
|
||||
return strategy;
|
||||
}
|
||||
|
||||
|
|
@ -94,10 +97,10 @@ export class StrategyManager extends EventEmitter {
|
|||
if (!strategy) {
|
||||
throw new Error(`Strategy ${strategyId} not found`);
|
||||
}
|
||||
|
||||
await strategy.start();
|
||||
|
||||
await strategy.initialize();
|
||||
this.activeStrategies.add(strategyId);
|
||||
logger.info(`Enabled strategy: ${strategyId}`);
|
||||
this.container.logger.info(`Enabled strategy: ${strategyId}`);
|
||||
}
|
||||
|
||||
async disableStrategy(strategyId: string): Promise<void> {
|
||||
|
|
@ -105,119 +108,80 @@ export class StrategyManager extends EventEmitter {
|
|||
if (!strategy) {
|
||||
throw new Error(`Strategy ${strategyId} not found`);
|
||||
}
|
||||
|
||||
await strategy.stop();
|
||||
|
||||
await strategy.shutdown();
|
||||
this.activeStrategies.delete(strategyId);
|
||||
logger.info(`Disabled strategy: ${strategyId}`);
|
||||
this.container.logger.info(`Disabled strategy: ${strategyId}`);
|
||||
}
|
||||
|
||||
private async handleMarketData(data: MarketData): Promise<void> {
|
||||
// Forward to active strategies
|
||||
// Forward to all active strategies
|
||||
for (const strategyId of this.activeStrategies) {
|
||||
const strategy = this.strategies.get(strategyId);
|
||||
if (strategy && strategy.isInterestedInSymbol(data.data.symbol)) {
|
||||
if (strategy) {
|
||||
try {
|
||||
await strategy.onMarketData(data);
|
||||
} catch (error) {
|
||||
logger.error(`Strategy ${strategyId} error processing market data:`, error);
|
||||
this.container.logger.error(`Error processing market data for strategy ${strategyId}:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async handleMarketDataBatch(batch: MarketData[]): Promise<void> {
|
||||
// Group by symbol for efficiency
|
||||
const bySymbol = new Map<string, MarketData[]>();
|
||||
|
||||
for (const data of batch) {
|
||||
const symbol = data.data.symbol;
|
||||
if (!bySymbol.has(symbol)) {
|
||||
bySymbol.set(symbol, []);
|
||||
}
|
||||
bySymbol.get(symbol)!.push(data);
|
||||
}
|
||||
|
||||
// Forward to strategies
|
||||
// Process batch more efficiently
|
||||
for (const strategyId of this.activeStrategies) {
|
||||
const strategy = this.strategies.get(strategyId);
|
||||
if (!strategy) continue;
|
||||
|
||||
const relevantData: MarketData[] = [];
|
||||
for (const [symbol, data] of bySymbol) {
|
||||
if (strategy.isInterestedInSymbol(symbol)) {
|
||||
relevantData.push(...data);
|
||||
}
|
||||
}
|
||||
|
||||
if (relevantData.length > 0) {
|
||||
if (strategy) {
|
||||
try {
|
||||
await strategy.onMarketDataBatch(relevantData);
|
||||
await strategy.onMarketDataBatch(batch);
|
||||
} catch (error) {
|
||||
logger.error(`Strategy ${strategyId} error processing batch:`, error);
|
||||
this.container.logger.error(`Error processing market data batch for strategy ${strategyId}:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async handleFill(fill: any): Promise<void> {
|
||||
// Notify relevant strategies about fills
|
||||
for (const strategyId of this.activeStrategies) {
|
||||
const strategy = this.strategies.get(strategyId);
|
||||
if (strategy && strategy.hasPosition(fill.symbol)) {
|
||||
// Forward fill to the strategy that created the order
|
||||
for (const [strategyId, strategy] of this.strategies) {
|
||||
if (strategy.hasOrder(fill.orderId)) {
|
||||
try {
|
||||
await strategy.onFill(fill);
|
||||
} catch (error) {
|
||||
logger.error(`Strategy ${strategyId} error processing fill:`, error);
|
||||
this.container.logger.error(`Error processing fill for strategy ${strategyId}:`, error);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async handleStrategySignal(strategyId: string, signal: any): Promise<void> {
|
||||
logger.debug(`Strategy ${strategyId} generated signal:`, signal);
|
||||
this.container.logger.info(`Strategy ${strategyId} generated signal:`, signal);
|
||||
|
||||
// Emit for monitoring/logging
|
||||
this.emit('strategySignal', {
|
||||
strategyId,
|
||||
signal,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
// Convert signal to order request
|
||||
const orderRequest: OrderRequest = {
|
||||
symbol: signal.symbol,
|
||||
quantity: signal.quantity,
|
||||
side: signal.side,
|
||||
type: signal.orderType || 'market',
|
||||
timeInForce: signal.timeInForce || 'day',
|
||||
strategyId
|
||||
};
|
||||
|
||||
private async handleStrategyOrder(strategyId: string, order: OrderRequest): Promise<void> {
|
||||
logger.info(`Strategy ${strategyId} placing order:`, order);
|
||||
|
||||
try {
|
||||
// Submit order through execution service
|
||||
const result = await this.executionService.submitOrder(order);
|
||||
|
||||
// Notify strategy of order result
|
||||
const strategy = this.strategies.get(strategyId);
|
||||
if (strategy) {
|
||||
await strategy.onOrderUpdate(result);
|
||||
}
|
||||
|
||||
// Emit for monitoring
|
||||
this.emit('strategyOrder', {
|
||||
strategyId,
|
||||
order,
|
||||
result,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
logger.error(`Failed to submit order from strategy ${strategyId}:`, error);
|
||||
|
||||
// Notify strategy of failure
|
||||
const strategy = this.strategies.get(strategyId);
|
||||
if (strategy) {
|
||||
await strategy.onOrderError(order, error);
|
||||
// Submit order through execution service
|
||||
const executionService = this.container.custom?.ExecutionService;
|
||||
if (executionService) {
|
||||
try {
|
||||
const result = await executionService.submitOrder(orderRequest);
|
||||
this.container.logger.info(`Order submitted for strategy ${strategyId}:`, result);
|
||||
} catch (error) {
|
||||
this.container.logger.error(`Failed to submit order for strategy ${strategyId}:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async onMarketData(data: MarketData): Promise<void> {
|
||||
// Called by backtest engine
|
||||
await this.handleMarketData(data);
|
||||
}
|
||||
|
||||
|
|
@ -225,52 +189,25 @@ export class StrategyManager extends EventEmitter {
|
|||
return this.tradingEngine;
|
||||
}
|
||||
|
||||
getActiveStrategies(): string[] {
|
||||
return Array.from(this.activeStrategies);
|
||||
}
|
||||
|
||||
getStrategy(strategyId: string): BaseStrategy | undefined {
|
||||
return this.strategies.get(strategyId);
|
||||
}
|
||||
|
||||
getAllStrategies(): Map<string, BaseStrategy> {
|
||||
return new Map(this.strategies);
|
||||
}
|
||||
|
||||
getActiveStrategies(): Set<string> {
|
||||
return new Set(this.activeStrategies);
|
||||
}
|
||||
|
||||
async updateStrategyConfig(strategyId: string, updates: Partial<StrategyConfig>): Promise<void> {
|
||||
const strategy = this.strategies.get(strategyId);
|
||||
if (!strategy) {
|
||||
throw new Error(`Strategy ${strategyId} not found`);
|
||||
}
|
||||
|
||||
await strategy.updateConfig(updates);
|
||||
logger.info(`Updated configuration for strategy ${strategyId}`);
|
||||
}
|
||||
|
||||
async getStrategyPerformance(strategyId: string): Promise<any> {
|
||||
const strategy = this.strategies.get(strategyId);
|
||||
if (!strategy) {
|
||||
throw new Error(`Strategy ${strategyId} not found`);
|
||||
}
|
||||
|
||||
return strategy.getPerformance();
|
||||
}
|
||||
|
||||
async shutdown(): Promise<void> {
|
||||
logger.info('Shutting down strategy manager...');
|
||||
this.container.logger.info('Shutting down strategy manager...');
|
||||
|
||||
// Disable all strategies
|
||||
for (const strategyId of this.activeStrategies) {
|
||||
await this.disableStrategy(strategyId);
|
||||
}
|
||||
|
||||
// Shutdown all strategies
|
||||
for (const [id, strategy] of this.strategies) {
|
||||
await strategy.shutdown();
|
||||
}
|
||||
|
||||
// Clear all strategies
|
||||
this.strategies.clear();
|
||||
this.activeStrategies.clear();
|
||||
this.removeAllListeners();
|
||||
this.tradingEngine = null;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
import { BaseStrategy, Signal } from '../BaseStrategy';
|
||||
import { MarketData } from '../../types';
|
||||
import { logger } from '@stock-bot/logger';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
|
||||
const logger = getLogger('MLEnhancedStrategy');
|
||||
import * as tf from '@tensorflow/tfjs-node';
|
||||
|
||||
interface MLModelConfig {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import { BaseStrategy, Signal } from '../BaseStrategy';
|
||||
import { MarketData } from '../../types';
|
||||
import { logger } from '@stock-bot/logger';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
|
||||
const logger = getLogger('MeanReversionStrategy');
|
||||
|
||||
interface MeanReversionIndicators {
|
||||
sma20: number;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue