55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
import { TradingEngine } from '@stock-bot/engine';
|
|
import { getLogger, setLoggerConfig } from '@stock-bot/logger';
|
|
import type { ExecutionRepository } from '../../apps/stock/orchestrator/src/execution/repository';
|
|
import { ExecutionService } from '../../apps/stock/orchestrator/src/services/ExecutionService';
|
|
import type { StorageService } from '../../apps/stock/orchestrator/src/services/StorageService';
|
|
import {
|
|
BacktestConfigSchema,
|
|
type MarketData,
|
|
type OrderRequest,
|
|
} from '../../apps/stock/orchestrator/src/types';
|
|
|
|
setLoggerConfig({ logLevel: 'fatal', logConsole: false });
|
|
export const logger = getLogger('execution-test');
|
|
export const start = Date.parse('2024-01-01T00:00:00Z');
|
|
export const config = BacktestConfigSchema.parse({
|
|
mode: 'backtest',
|
|
strategy: 'sma-crossover',
|
|
symbols: ['A.US', 'B.US'],
|
|
startDate: new Date(start).toISOString(),
|
|
endDate: '2025-01-01T00:00:00Z',
|
|
initialCapital: 10000,
|
|
dataFrequency: '1d',
|
|
commission: 0.001,
|
|
slippage: 0,
|
|
});
|
|
export function quote(
|
|
id: string,
|
|
price = 100,
|
|
size = 100,
|
|
timestamp = start,
|
|
symbol = 'A'
|
|
): MarketData {
|
|
return {
|
|
type: 'quote',
|
|
eventId: id,
|
|
data: { symbol, bid: price, ask: price, bidSize: size, askSize: size, timestamp },
|
|
};
|
|
}
|
|
export function order(
|
|
clientOrderId: string,
|
|
side: 'buy' | 'sell' = 'buy',
|
|
quantity = 10
|
|
): OrderRequest {
|
|
return { symbol: 'A', side, quantity, orderType: 'market', timeInForce: 'DAY', clientOrderId };
|
|
}
|
|
export function makeExecution(repository?: ExecutionRepository, clock = () => start) {
|
|
const engine = new TradingEngine('backtest', {
|
|
startTime: start,
|
|
endTime: Date.parse(config.endDate),
|
|
speedMultiplier: 0,
|
|
initialCapital: 10000,
|
|
});
|
|
const execution = new ExecutionService({ logger }, {} as StorageService, { repository, clock });
|
|
return { execution, engine };
|
|
}
|