165 lines
6.5 KiB
TypeScript
165 lines
6.5 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 { createBuiltinStrategyRegistry } from './strategies/builtin-strategies';
|
|
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
|
|
);
|
|
|
|
// Callbacks are invoked after composition: the feed drains the manager, and
|
|
// factories receive the current mode without either service locating its peers.
|
|
const marketDataService: MarketDataService = new MarketDataService({
|
|
logger: services.logger,
|
|
storage: storageService,
|
|
drainStrategies: () => strategyManager.drain(),
|
|
});
|
|
const executionService = new ExecutionService(services, storageService);
|
|
const analyticsService = new AnalyticsService(services);
|
|
const registry = options.strategyRegistry ?? createBuiltinStrategyRegistry();
|
|
const strategyManager: StrategyManager = new StrategyManager({
|
|
logger: services.logger,
|
|
marketData: marketDataService,
|
|
execution: executionService,
|
|
createStrategy: config => registry.create(config, { modeManager, execution: executionService }),
|
|
});
|
|
const modeManager: ModeManager = new ModeManager({
|
|
logger: services.logger,
|
|
marketData: marketDataService,
|
|
execution: executionService,
|
|
storage: storageService,
|
|
strategies: strategyManager,
|
|
});
|
|
const backtestEngine = options.useRustEngine
|
|
? new RustBacktestAdapter(
|
|
{ logger: services.logger, storage: storageService },
|
|
{
|
|
timeoutMs: options.nativeExecutionTimeoutMs,
|
|
}
|
|
)
|
|
: new BacktestEngine({
|
|
logger: services.logger,
|
|
storage: storageService,
|
|
strategies: strategyManager,
|
|
execution: executionService,
|
|
getTradingEngine: () => modeManager.getTradingEngine(),
|
|
});
|
|
const paperTradingManager = new PaperTradingManager(
|
|
services,
|
|
storageService,
|
|
marketDataService,
|
|
executionService
|
|
);
|
|
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(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),
|
|
});
|
|
|
|
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.'
|
|
);
|
|
}
|