Freeze historical datasets and qualify backtest instrument identities
This commit is contained in:
parent
63c3856797
commit
d6b09eb635
43 changed files with 2249 additions and 1230 deletions
|
|
@ -3,6 +3,7 @@ import { defined } from '@stock-bot/invariants';
|
|||
import { at } from '@stock-bot/utils';
|
||||
import { PerformanceAnalyzer, type PerformanceMetrics } from '../analytics/PerformanceAnalyzer';
|
||||
import { DataManager } from '../data/DataManager';
|
||||
import { resolveDataset, type DatasetResolver } from '../data/historical-dataset';
|
||||
import { StorageService } from '../services/StorageService';
|
||||
import type { Signal } from '../strategies/BaseStrategy';
|
||||
import { StrategyManager } from '../strategies/StrategyManager';
|
||||
|
|
@ -62,7 +63,7 @@ export class BacktestEngine extends EventEmitter implements BacktestExecutionEng
|
|||
// this.performanceAnalyzer = new PerformanceAnalyzer(this.initialCapital);
|
||||
}
|
||||
|
||||
async runBacktest(config: unknown): Promise<BacktestResult> {
|
||||
async runBacktest(config: unknown, resolver?: DatasetResolver): Promise<BacktestResult> {
|
||||
// Validate config
|
||||
const validatedConfig = BacktestConfigSchema.parse(config);
|
||||
|
||||
|
|
@ -126,24 +127,20 @@ export class BacktestEngine extends EventEmitter implements BacktestExecutionEng
|
|||
}
|
||||
|
||||
try {
|
||||
// Load historical data with multi-resolution support
|
||||
const dataMap = await this.dataManager.loadHistoricalData(
|
||||
validatedConfig.symbols,
|
||||
new Date(validatedConfig.startDate),
|
||||
new Date(validatedConfig.endDate),
|
||||
validatedConfig.dataFrequency,
|
||||
true // Include extended hours
|
||||
);
|
||||
// Both engines execute exactly these frozen, already-normalized bars.
|
||||
const dataset = await resolveDataset(this.storageService, validatedConfig, resolver);
|
||||
this.dataManager.inspectDataset(dataset);
|
||||
this.marketSimulator.resetWithSeed(Number.parseInt(dataset.manifest.hash.slice(0, 8), 16));
|
||||
|
||||
// Load market microstructure for each symbol
|
||||
await this.loadMarketMicrostructure(validatedConfig.symbols);
|
||||
|
||||
// Convert to flat array and sort by time
|
||||
const marketData: MarketData[] = [];
|
||||
dataMap.forEach((data, _symbol) => {
|
||||
marketData.push(...data);
|
||||
});
|
||||
marketData.sort((a, b) => a.data.timestamp - b.data.timestamp);
|
||||
const marketData: MarketData[] = dataset.bars.map(bar => ({
|
||||
type: 'bar',
|
||||
data: bar,
|
||||
eventId: `${dataset.manifest.hash}:${bar.symbol}:${bar.timestamp}`,
|
||||
}));
|
||||
|
||||
this.container.logger.info(`Loaded ${marketData.length} market data points`);
|
||||
|
||||
|
|
@ -157,12 +154,13 @@ export class BacktestEngine extends EventEmitter implements BacktestExecutionEng
|
|||
allocation: 1 / (validatedConfig.strategies?.length ?? 1),
|
||||
})) || [
|
||||
{
|
||||
id: `${validatedConfig.strategy}-${Date.now()}`,
|
||||
id: `${validatedConfig.strategy}-0`,
|
||||
name: validatedConfig.strategy,
|
||||
enabled: true,
|
||||
parameters: {
|
||||
symbols: validatedConfig.symbols,
|
||||
initialCapital: validatedConfig.initialCapital,
|
||||
...validatedConfig.config,
|
||||
// Add any default strategy parameters here
|
||||
},
|
||||
symbols: validatedConfig.symbols,
|
||||
|
|
@ -784,10 +782,10 @@ export class BacktestEngine extends EventEmitter implements BacktestExecutionEng
|
|||
for (const symbol of symbols) {
|
||||
const microstructure: MarketMicrostructure = {
|
||||
symbol,
|
||||
avgSpreadBps: 2 + Math.random() * 3, // 2-5 bps
|
||||
dailyVolume: 10_000_000 * (1 + Math.random() * 9), // 10-100M shares
|
||||
avgTradeSize: 100 + Math.random() * 400, // 100-500 shares
|
||||
volatility: 0.15 + Math.random() * 0.25, // 15-40% annual vol
|
||||
avgSpreadBps: 3,
|
||||
dailyVolume: 10_000_000,
|
||||
avgTradeSize: 100,
|
||||
volatility: 0.2,
|
||||
tickSize: 0.01,
|
||||
lotSize: 1,
|
||||
intradayVolumeProfile: this.generateIntradayProfile(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue