fixed backtest i think

This commit is contained in:
Boki 2025-07-03 20:41:42 -04:00
parent 16ac28a565
commit 083dca500c
7 changed files with 663 additions and 56 deletions

View file

@ -265,17 +265,85 @@ export class StorageService {
endTime: Date,
interval: string = '1m'
): Promise<any[]> {
if (!this.questdb) return [];
// If QuestDB is available, use real data
if (this.questdb) {
const result = await this.questdb.query(`
SELECT * FROM bars_${interval}
WHERE symbol = '${symbol}'
AND timestamp >= '${startTime.toISOString()}'
AND timestamp < '${endTime.toISOString()}'
ORDER BY timestamp
`);
return result;
}
const result = await this.questdb.query(`
SELECT * FROM bars_${interval}
WHERE symbol = '${symbol}'
AND timestamp >= '${startTime.toISOString()}'
AND timestamp < '${endTime.toISOString()}'
ORDER BY timestamp
`);
// Otherwise, generate mock data for testing
this.container.logger.info('Generating mock data for backtest', {
symbol,
startTime: startTime.toISOString(),
endTime: endTime.toISOString(),
interval
});
return result;
return this.generateMockBars(symbol, startTime, endTime, interval);
}
private generateMockBars(
symbol: string,
startTime: Date,
endTime: Date,
interval: string
): any[] {
const bars = [];
const msPerInterval = this.getIntervalMilliseconds(interval);
let currentTime = new Date(startTime);
// Starting price based on symbol
let basePrice = symbol === 'AAPL' ? 150 :
symbol === 'MSFT' ? 300 :
symbol === 'GOOGL' ? 120 : 100;
while (currentTime <= endTime) {
// Random walk with trend
const trend = 0.0001; // Slight upward trend
const volatility = 0.002; // 0.2% volatility
const change = (Math.random() - 0.5 + trend) * volatility;
basePrice *= (1 + change);
// Generate OHLC data
const open = basePrice * (1 + (Math.random() - 0.5) * 0.001);
const close = basePrice;
const high = Math.max(open, close) * (1 + Math.random() * 0.002);
const low = Math.min(open, close) * (1 - Math.random() * 0.002);
const volume = 1000000 + Math.random() * 500000;
bars.push({
symbol,
timestamp: new Date(currentTime),
open,
high,
low,
close,
volume,
vwap: (high + low + close) / 3
});
currentTime = new Date(currentTime.getTime() + msPerInterval);
}
return bars;
}
private getIntervalMilliseconds(interval: string): number {
switch (interval) {
case '1m': return 60 * 1000;
case '5m': return 5 * 60 * 1000;
case '15m': return 15 * 60 * 1000;
case '1h': return 60 * 60 * 1000;
case '1d': return 24 * 60 * 60 * 1000;
default: return 60 * 1000; // Default to 1 minute
}
}
async shutdown(): Promise<void> {