work on integrating new system

This commit is contained in:
Boki 2025-07-03 21:13:02 -04:00
parent 083dca500c
commit 063f4c8e27
8 changed files with 221 additions and 66 deletions

View file

@ -71,6 +71,25 @@ export class RustBacktestAdapter extends EventEmitter {
const resultJson = this.currentEngine.run();
const rustResult = JSON.parse(resultJson);
// Store OHLC data for each symbol
const ohlcData: Record<string, any[]> = {};
for (const symbol of config.symbols) {
const bars = await this.storageService.getHistoricalBars(
symbol,
new Date(config.startDate),
new Date(config.endDate),
config.dataFrequency || '1d'
);
ohlcData[symbol] = bars.map(bar => ({
timestamp: bar.timestamp.getTime(),
open: bar.open,
high: bar.high,
low: bar.low,
close: bar.close,
volume: bar.volume,
}));
}
// Convert Rust result to orchestrator format
const result: BacktestResult = {
backtestId: `rust-${Date.now()}`,
@ -109,6 +128,7 @@ export class RustBacktestAdapter extends EventEmitter {
dailyReturns: this.calculateDailyReturns(rustResult.equity_curve),
finalPositions: rustResult.final_positions || {},
executionTime: Date.now() - startTime,
ohlcData,
};
this.emit('complete', result);
@ -199,10 +219,16 @@ export class RustBacktestAdapter extends EventEmitter {
// Create state for the strategy
const priceHistory: Map<string, number[]> = new Map();
const positions: Map<string, number> = new Map();
const fastPeriod = parameters.fastPeriod || 10;
const slowPeriod = parameters.slowPeriod || 30;
const fastPeriod = parameters.fastPeriod || 5;
const slowPeriod = parameters.slowPeriod || 15;
// Create a simple strategy based on the name
this.container.logger.info('Registering TypeScript strategy', {
strategyName,
fastPeriod,
slowPeriod
});
// Create a TypeScript strategy callback
const callback = (callJson: string) => {
const call = JSON.parse(callJson);
@ -212,7 +238,7 @@ export class RustBacktestAdapter extends EventEmitter {
// Debug log first few data points
if (priceHistory.size === 0) {
console.log('First market data received:', JSON.stringify(marketData, null, 2));
this.container.logger.debug('First market data received:', marketData);
}
// For SMA crossover strategy
@ -255,10 +281,12 @@ export class RustBacktestAdapter extends EventEmitter {
// Golden cross - buy signal
if (prevFastSMA <= prevSlowSMA && fastSMA > slowSMA && currentPosition <= 0) {
this.container.logger.info(`Golden cross detected for ${symbol} at price ${price}`);
signals.push({
symbol,
signal_type: 'Buy',
strength: 1.0,
quantity: 100, // Fixed quantity for testing
reason: 'Golden cross'
});
positions.set(symbol, 1);
@ -266,10 +294,12 @@ export class RustBacktestAdapter extends EventEmitter {
// Death cross - sell signal
else if (prevFastSMA >= prevSlowSMA && fastSMA < slowSMA && currentPosition >= 0) {
this.container.logger.info(`Death cross detected for ${symbol} at price ${price}`);
signals.push({
symbol,
signal_type: 'Sell',
strength: 1.0,
quantity: 100, // Fixed quantity for testing
reason: 'Death cross'
});
positions.set(symbol, -1);

View file

@ -304,18 +304,18 @@ export class StorageService {
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
// Random walk with trend - increased volatility for testing
const trend = 0.0002; // Slight upward trend
const volatility = 0.01; // 1% volatility (increased from 0.2%)
const change = (Math.random() - 0.5 + trend) * volatility;
basePrice *= (1 + change);
// Generate OHLC data
const open = basePrice * (1 + (Math.random() - 0.5) * 0.001);
// Generate OHLC data with more realistic volatility
const open = basePrice * (1 + (Math.random() - 0.5) * 0.005);
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 high = Math.max(open, close) * (1 + Math.random() * 0.008);
const low = Math.min(open, close) * (1 - Math.random() * 0.008);
const volume = 1000000 + Math.random() * 500000;
bars.push({