149 lines
5.9 KiB
TypeScript
149 lines
5.9 KiB
TypeScript
import { asValue, type AwilixContainer } from 'awilix';
|
|
import type { ServiceCradle as ServiceDefinitions } from '@stock-bot/di';
|
|
import { BacktestEngine } from './backtest/BacktestEngine';
|
|
import { BacktestRunner } from './backtest/BacktestRunner';
|
|
import type { BacktestExecutionEngine } from './backtest/execution-engine';
|
|
import { PostgresBacktestRunRepository } from './backtest/PostgresBacktestRunRepository';
|
|
import { UnavailableBacktestRunRepository } from './backtest/run-repository';
|
|
import { RustBacktestAdapter } from './backtest/RustBacktestAdapter';
|
|
import { ModeManager } from './core/ModeManager';
|
|
import { BacktestHandler } from './handlers/backtest/backtest.handler';
|
|
import { PaperTradingManager } from './paper/PaperTradingManager';
|
|
import { AnalyticsService } from './services/AnalyticsService';
|
|
import { ExecutionService } from './services/ExecutionService';
|
|
import { MarketDataService } from './services/MarketDataService';
|
|
import { StorageService } from './services/StorageService';
|
|
import { StrategyManager } from './strategies/StrategyManager';
|
|
import type { StrategyRegistry } from './strategies/StrategyRegistry';
|
|
import type { OrchestratorServices as IServiceContainer } from './types';
|
|
|
|
/**
|
|
* Register orchestrator-specific services in the DI container
|
|
*/
|
|
export async function registerOrchestratorServices(
|
|
container: AwilixContainer<ServiceDefinitions>,
|
|
options: {
|
|
initializeMode?: boolean;
|
|
useRustEngine?: boolean;
|
|
allowSyntheticData?: boolean;
|
|
syntheticSeed?: number;
|
|
nativeExecutionTimeoutMs?: number;
|
|
strategyRegistry?: StrategyRegistry;
|
|
} = {}
|
|
): Promise<void> {
|
|
// Create a service adapter that provides the expected interface
|
|
const services = container.resolve('serviceContainer') as IServiceContainer;
|
|
const custom = services.custom ?? {};
|
|
if (!services.custom) {
|
|
Object.defineProperty(services, 'custom', { value: custom, enumerable: true });
|
|
}
|
|
|
|
// Create storage service first as it's needed by other services
|
|
const storageService = new StorageService(
|
|
services,
|
|
services.mongodb,
|
|
services.postgres,
|
|
services.questdb || null,
|
|
options
|
|
);
|
|
|
|
// Create other services
|
|
const marketDataService = new MarketDataService(services);
|
|
const executionService = new ExecutionService(services, storageService);
|
|
const analyticsService = new AnalyticsService(services, storageService);
|
|
const strategyManager = new StrategyManager(services, options.strategyRegistry);
|
|
Object.assign(custom, { StorageService: storageService });
|
|
const backtestEngine = options.useRustEngine
|
|
? new RustBacktestAdapter(services, { timeoutMs: options.nativeExecutionTimeoutMs })
|
|
: new BacktestEngine(services, storageService, strategyManager);
|
|
const paperTradingManager = new PaperTradingManager(
|
|
services,
|
|
storageService,
|
|
marketDataService,
|
|
executionService
|
|
);
|
|
const modeManager = new ModeManager(
|
|
services,
|
|
marketDataService,
|
|
executionService,
|
|
storageService
|
|
);
|
|
|
|
const backtestRunner = new BacktestRunner({
|
|
engine: backtestEngine,
|
|
engineName: options.useRustEngine ? 'native' : 'typescript',
|
|
repository: services.postgres
|
|
? new PostgresBacktestRunRepository(services.postgres)
|
|
: new UnavailableBacktestRunRepository(),
|
|
logger: services.logger,
|
|
prepare: options.useRustEngine ? undefined : config => modeManager.initializeMode(config),
|
|
});
|
|
|
|
// Store custom services
|
|
Object.assign(services.custom ?? {}, {
|
|
StorageService: storageService,
|
|
MarketDataService: marketDataService,
|
|
ExecutionService: executionService,
|
|
AnalyticsService: analyticsService,
|
|
StrategyManager: strategyManager,
|
|
BacktestEngine: backtestEngine,
|
|
BacktestRunner: backtestRunner,
|
|
PaperTradingManager: paperTradingManager,
|
|
ModeManager: modeManager,
|
|
});
|
|
|
|
// Register services in the Awilix container for resolution
|
|
const orchestratorContainer = container as AwilixContainer<
|
|
ServiceDefinitions & {
|
|
StorageService: StorageService;
|
|
MarketDataService: MarketDataService;
|
|
ExecutionService: ExecutionService;
|
|
AnalyticsService: AnalyticsService;
|
|
StrategyManager: StrategyManager;
|
|
BacktestEngine: BacktestExecutionEngine;
|
|
BacktestRunner: BacktestRunner;
|
|
PaperTradingManager: PaperTradingManager;
|
|
ModeManager: ModeManager;
|
|
orchestratorServices: IServiceContainer;
|
|
}
|
|
>;
|
|
orchestratorContainer.register({
|
|
StorageService: asValue(storageService),
|
|
MarketDataService: asValue(marketDataService),
|
|
ExecutionService: asValue(executionService),
|
|
AnalyticsService: asValue(analyticsService),
|
|
StrategyManager: asValue(strategyManager),
|
|
BacktestEngine: asValue(backtestEngine),
|
|
BacktestRunner: asValue(backtestRunner),
|
|
PaperTradingManager: asValue(paperTradingManager),
|
|
ModeManager: asValue(modeManager),
|
|
orchestratorServices: asValue(services),
|
|
});
|
|
|
|
// Update the serviceContainer to include our custom services
|
|
const serviceContainer = container.cradle.serviceContainer;
|
|
if (serviceContainer && serviceContainer.custom) {
|
|
Object.assign(serviceContainer.custom, services.custom);
|
|
}
|
|
|
|
container
|
|
.resolve('handlerScanner')
|
|
.registerHandlerClass(BacktestHandler, { serviceName: 'orchestrator' });
|
|
|
|
// Setup event listeners after all services are registered
|
|
strategyManager.setupEventListeners();
|
|
|
|
// Initialize mode manager with default paper trading mode
|
|
if (options.initializeMode !== false) {
|
|
await modeManager.initializeMode({ mode: 'paper', startingCapital: 100000 });
|
|
}
|
|
// Start job consumption only after the shared graph and initial mode are ready.
|
|
services.queueManager?.getQueue('orchestrator', { workers: 1, concurrency: 1 });
|
|
}
|
|
|
|
// For backward compatibility, export a container getter
|
|
export function getContainer(): IServiceContainer {
|
|
throw new Error(
|
|
'Container should be accessed through ServiceApplication. Update your code to use dependency injection.'
|
|
);
|
|
}
|