stock-bot/apps/stock/orchestrator/test-full-flow.ts

93 lines
No EOL
2.4 KiB
TypeScript

#!/usr/bin/env bun
/**
* Test the full flow from market data to trade execution
*/
import { SimpleMovingAverageCrossover } from './src/strategies/examples/SimpleMovingAverageCrossover';
import { BaseStrategy } from './src/strategies/BaseStrategy';
import { MarketData } from './src/types';
import { getLogger } from '@stock-bot/logger';
const logger = getLogger('test-flow');
async function testFullFlow() {
console.log('Testing full flow from market data to orders...\n');
// Create strategy
const config = {
id: 'test-sma',
name: 'Test SMA',
enabled: true,
symbols: ['AAPL'],
allocation: 1.0
};
const strategy = new SimpleMovingAverageCrossover(config, null, null);
let signalCount = 0;
let orderCount = 0;
// Listen for signals
strategy.on('signal', (signal) => {
signalCount++;
logger.info(`Signal #${signalCount}:`, signal);
});
// Listen for orders
strategy.on('order', (order) => {
orderCount++;
logger.info(`Order #${orderCount}:`, order);
});
await strategy.start();
// Generate 50 days of data with a clear uptrend after day 20
logger.info('Generating market data with uptrend...');
for (let day = 0; day < 50; day++) {
const basePrice = 100;
let price = basePrice;
// Create a clear uptrend after day 20
if (day > 20) {
price = basePrice + (day - 20) * 0.5; // 50 cents per day uptrend
} else {
price = basePrice + Math.sin(day * 0.3) * 2; // Sideways
}
const marketData: MarketData = {
type: 'bar',
data: {
symbol: 'AAPL',
open: price - 0.5,
high: price + 0.5,
low: price - 1,
close: price,
volume: 1000000,
timestamp: Date.now() + day * 86400000
}
};
// Process the data
await strategy.onMarketData(marketData);
if (day === 19) {
logger.info(`Day 19: Last day before uptrend, price = ${price}`);
}
if (day === 25) {
logger.info(`Day 25: Should see golden cross soon, price = ${price}`);
}
}
await strategy.stop();
console.log('\n=== Test Results ===');
console.log(`Total signals generated: ${signalCount}`);
console.log(`Total orders generated: ${orderCount}`);
const perf = strategy.getPerformance();
console.log('Strategy performance:', perf);
}
testFullFlow().catch(console.error);