finished initial backtest / engine
This commit is contained in:
parent
55b4ca78c9
commit
c106a719e8
18 changed files with 1571 additions and 180 deletions
89
apps/stock/orchestrator/test-strategy-signals.ts
Normal file
89
apps/stock/orchestrator/test-strategy-signals.ts
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
#!/usr/bin/env bun
|
||||
|
||||
/**
|
||||
* Test strategy signal generation
|
||||
*/
|
||||
|
||||
import { SimpleMovingAverageCrossover } from './src/strategies/examples/SimpleMovingAverageCrossover';
|
||||
import { MarketData } from './src/types';
|
||||
|
||||
async function testStrategySignals() {
|
||||
console.log('Testing strategy signal generation...\n');
|
||||
|
||||
// Create strategy with mock config
|
||||
const config = {
|
||||
id: 'test-sma',
|
||||
name: 'Test SMA Strategy',
|
||||
enabled: true,
|
||||
symbols: ['AAPL'],
|
||||
allocation: 1.0
|
||||
};
|
||||
|
||||
const strategy = new SimpleMovingAverageCrossover(config, null, null);
|
||||
await strategy.start();
|
||||
|
||||
// Generate 100 days of mock data with a clear trend
|
||||
const basePrice = 100;
|
||||
let price = basePrice;
|
||||
let signalCount = 0;
|
||||
|
||||
console.log('Feeding market data to strategy...\n');
|
||||
|
||||
for (let day = 0; day < 100; day++) {
|
||||
// Create uptrend for days 30-50, downtrend for days 60-80
|
||||
if (day >= 30 && day < 50) {
|
||||
price += 0.5; // Uptrend
|
||||
} else if (day >= 60 && day < 80) {
|
||||
price -= 0.5; // Downtrend
|
||||
} else {
|
||||
price += (Math.random() - 0.5) * 0.2; // Small random movement
|
||||
}
|
||||
|
||||
const marketData: MarketData = {
|
||||
type: 'bar',
|
||||
data: {
|
||||
symbol: 'AAPL',
|
||||
open: price - 0.1,
|
||||
high: price + 0.2,
|
||||
low: price - 0.2,
|
||||
close: price,
|
||||
volume: 1000000,
|
||||
timestamp: Date.now() + day * 86400000
|
||||
}
|
||||
};
|
||||
|
||||
// Listen for signals
|
||||
strategy.once('signal', (signal) => {
|
||||
signalCount++;
|
||||
console.log(`Day ${day}: Signal generated!`);
|
||||
console.log(` Type: ${signal.type}`);
|
||||
console.log(` Strength: ${signal.strength}`);
|
||||
console.log(` Reason: ${signal.reason}`);
|
||||
console.log(` Metadata:`, signal.metadata);
|
||||
});
|
||||
|
||||
// Listen for orders
|
||||
strategy.once('order', (order) => {
|
||||
console.log(`Day ${day}: Order generated!`);
|
||||
console.log(` Side: ${order.side}`);
|
||||
console.log(` Quantity: ${order.quantity}`);
|
||||
console.log(` Type: ${order.orderType}`);
|
||||
});
|
||||
|
||||
// Process the market data
|
||||
await strategy.onMarketData(marketData);
|
||||
|
||||
if (day % 10 === 0) {
|
||||
console.log(`Day ${day}: Price = ${price.toFixed(2)}, Total signals = ${signalCount}`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\n✅ Test completed. Total signals generated: ${signalCount}`);
|
||||
|
||||
const perf = strategy.getPerformance();
|
||||
console.log('\nStrategy Performance:', perf);
|
||||
|
||||
await strategy.stop();
|
||||
}
|
||||
|
||||
testStrategySignals().catch(console.error);
|
||||
Loading…
Add table
Add a link
Reference in a new issue