63 lines
2.7 KiB
TypeScript
63 lines
2.7 KiB
TypeScript
import { ServiceContainerBuilder } from '@stock-bot/di';
|
|
import type { StockAppConfig } from '@stock-bot/stock-config';
|
|
import { paperConfigurationSchema } from './contracts/paper-runtime';
|
|
import { validatePaperConfiguration } from './paper/feed';
|
|
import {
|
|
assertSupportedComposition,
|
|
registerOrchestratorServices,
|
|
type OrchestratorRole,
|
|
} from './container';
|
|
import type { OrchestratorServices } from './types';
|
|
|
|
/** Use the shared builder for connection setup and adapter ownership. */
|
|
export async function createContainer(
|
|
config: StockAppConfig,
|
|
options: { role?: OrchestratorRole } = {}
|
|
) {
|
|
const settings = config.services?.orchestrator;
|
|
if (settings?.defaultMode === 'live') {
|
|
throw new Error('Live trading is disabled: no real broker adapter exists');
|
|
}
|
|
const role =
|
|
options.role === undefined
|
|
? settings?.defaultMode === 'backtest'
|
|
? 'research-only'
|
|
: 'paper'
|
|
: options.role;
|
|
const configuredEngine = config.services?.orchestrator?.backtesting?.useRustEngine;
|
|
const useRustEngine = configuredEngine === undefined ? true : configuredEngine;
|
|
// Fail before opening connections as well as before registering runtime services.
|
|
assertSupportedComposition(role, useRustEngine);
|
|
if (settings?.canonicalPaper !== undefined && role !== 'paper') {
|
|
throw new Error('Canonical paper configuration requires a paper composition');
|
|
}
|
|
const canonicalPaper =
|
|
settings?.canonicalPaper === undefined
|
|
? undefined
|
|
: validatePaperConfiguration(paperConfigurationSchema.parse(settings.canonicalPaper));
|
|
if (canonicalPaper && !config.database?.postgres?.enabled) {
|
|
throw new Error('Canonical paper requires durable PostgreSQL custody');
|
|
}
|
|
const container = await new ServiceContainerBuilder()
|
|
.withConfig(config)
|
|
.withOptions({
|
|
enableMongoDB: config.database?.mongodb?.enabled ?? false,
|
|
enablePostgres: config.database?.postgres?.enabled ?? false,
|
|
enableCache: config.database?.dragonfly?.enabled ?? false,
|
|
enableQueue: config.queue?.enabled ?? false,
|
|
enableQuestDB: config.database?.questdb?.enabled ?? false,
|
|
enableBrowser: false,
|
|
enableProxy: false,
|
|
})
|
|
.build();
|
|
await registerOrchestratorServices(container, {
|
|
role,
|
|
useRustEngine,
|
|
allowSyntheticData: config.services?.orchestrator?.backtesting?.allowSyntheticData ?? false,
|
|
syntheticSeed: config.services?.orchestrator?.backtesting?.syntheticSeed ?? 42,
|
|
nativeExecutionTimeoutMs: config.services?.orchestrator?.backtesting?.executionTimeoutMs,
|
|
canonicalPaper,
|
|
paperTradingCapital: settings?.paperTradingCapital,
|
|
});
|
|
return container.resolve('serviceContainer') as OrchestratorServices;
|
|
}
|