finished initial backtest / engine
This commit is contained in:
parent
55b4ca78c9
commit
c106a719e8
18 changed files with 1571 additions and 180 deletions
93
apps/stock/orchestrator/test-full-flow.ts
Normal file
93
apps/stock/orchestrator/test-full-flow.ts
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
#!/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);
|
||||
Loading…
Add table
Add a link
Reference in a new issue