continuation

This commit is contained in:
Boki 2026-10-20 16:57:59 -04:00
parent 32e43c9243
commit 879e9b4228
42 changed files with 2607 additions and 1306 deletions

View file

@ -66,17 +66,15 @@ export class DataManager extends EventEmitter {
for (const symbol of symbols) {
try {
// Load raw data
let data = await this.storageService.getHistoricalBars(
const data = await this.storageService.getHistoricalBars(
symbol,
startDate,
endDate,
resolution
);
// If no data found, generate mock data
if (!data || data.length === 0) {
logger.warn(`No historical data found for ${symbol}, generating mock data`);
data = this.generateMockBars(symbol, startDate, endDate, resolution);
if (!data.length) {
throw new Error(`No historical data for ${symbol}`);
}
// Apply corporate actions
@ -84,6 +82,9 @@ export class DataManager extends EventEmitter {
// Quality checks
const cleanedData = this.performQualityChecks(symbol, adjustedData);
if (!cleanedData.length) {
throw new Error(`No valid historical data for ${symbol}`);
}
// Convert to MarketData format
const marketData = this.convertToMarketData(symbol, cleanedData);
@ -95,6 +96,8 @@ export class DataManager extends EventEmitter {
} catch (error) {
logger.error(`Failed to load data for ${symbol}:`, error);
this.emit('dataError', { symbol, error });
this.dataCache.clear();
throw error;
}
}
@ -437,68 +440,4 @@ export class DataManager extends EventEmitter {
this.aggregatedCache.clear();
this.dataQualityIssues = [];
}
private generateMockBars(
symbol: string,
startDate: Date,
endDate: Date,
resolution: string
): HistoricalBar[] {
const bars: HistoricalBar[] = [];
const resolutionMs = DataManager.RESOLUTIONS[resolution]?.milliseconds || 86400000; // Default to 1 day
let currentTime = startDate.getTime();
const endTime = endDate.getTime();
// Base price varies by symbol
let basePrice = 100;
if (symbol === 'AAPL') {
basePrice = 150;
} else if (symbol === 'GOOGL') {
basePrice = 140;
} else if (symbol === 'MSFT') {
basePrice = 380;
} else if (symbol === 'TSLA') {
basePrice = 250;
}
let price = basePrice;
while (currentTime <= endTime) {
// Generate realistic intraday movement
const volatility = 0.02; // 2% daily volatility
const trend = 0.0001; // Slight upward trend
// Random walk with trend
const changePercent = (Math.random() - 0.5) * volatility + trend;
price = price * (1 + changePercent);
// Generate OHLC
const open = price;
const intraDayVolatility = volatility / 4;
const high = price * (1 + Math.random() * intraDayVolatility);
const low = price * (1 - Math.random() * intraDayVolatility);
const close = low + Math.random() * (high - low);
// Volume with some randomness
const baseVolume = 10000000; // 10M shares
const volume = baseVolume * (0.5 + Math.random());
bars.push({
symbol,
timestamp: new Date(currentTime),
open: Number(open.toFixed(2)),
high: Number(high.toFixed(2)),
low: Number(low.toFixed(2)),
close: Number(close.toFixed(2)),
volume: Math.floor(volume),
vwap: Number(((high + low + close) / 3).toFixed(2)),
});
currentTime += resolutionMs;
price = close; // Next bar opens at previous close
}
return bars;
}
}