stock-bot/apps/stock/orchestrator/src/strategies/examples/SimpleMovingAverageCrossover.ts

359 lines
12 KiB
TypeScript

import { defined } from '@stock-bot/invariants';
import { getLogger } from '@stock-bot/logger';
import type { SmaParameters } from '../../contracts/strategies';
import type { ModeManager } from '../../core/ModeManager';
import { movingAverageCrossover } from '../../indicators/window-statistics';
import type { ExecutionService, StrategyFill } from '../../services/ExecutionService';
import type { OrderRequest, StrategyConfig } from '../../types';
import { type MarketData } from '../../types';
import { BaseStrategy, type Signal } from '../BaseStrategy';
import { crossoverCheckpointSchema } from '../checkpoint-schemas';
import { smaConfig } from '../strategy-config';
const logger = getLogger('SimpleMovingAverageCrossover');
export class SimpleMovingAverageCrossover extends BaseStrategy {
private priceHistory = new Map<string, number[]>();
private lastTradeTime = new Map<string, number>();
private barCount = new Map<string, number>();
private totalSignals = 0;
override getCheckpointVersion() {
return 'sma-crossover-original/v2';
}
protected override captureIndicators() {
return {
priceHistory: [...this.priceHistory],
lastSignalBar: [...this.lastTradeTime],
barCount: [...this.barCount],
totalSignals: this.totalSignals,
lastCrossoverType: [],
};
}
protected override restoreIndicators(input: unknown) {
const state = crossoverCheckpointSchema(this.settings.slowPeriod * 2).parse(input);
this.priceHistory = new Map(state.priceHistory);
this.lastTradeTime = new Map(state.lastSignalBar);
this.barCount = new Map(state.barCount);
this.totalSignals = state.totalSignals;
}
private settings: SmaParameters;
private readonly DEBUG_INTERVAL = 10;
constructor(
config: StrategyConfig,
modeManager: ModeManager,
executionService: ExecutionService
) {
const validated = smaConfig(config, 1);
super(validated, modeManager, executionService);
this.settings = validated.parameters;
logger.info(
`SimpleMovingAverageCrossover initialized with Fast=${this.settings.fastPeriod}, Slow=${this.settings.slowPeriod}`
);
}
protected updateIndicators(data: MarketData): void {
if (data.type !== 'bar') {
return;
}
const symbol = data.data.symbol;
const price = data.data.close;
// Update price history
if (!this.priceHistory.has(symbol)) {
this.priceHistory.set(symbol, []);
this.barCount.set(symbol, 0);
logger.info(`📊 Starting to track ${symbol} @ ${price}`);
}
const history = defined(this.priceHistory.get(symbol));
history.push(price);
// Increment bar count
const currentBar = (this.barCount.get(symbol) || 0) + 1;
this.barCount.set(symbol, currentBar);
// Keep only needed history
if (history.length > this.settings.slowPeriod * 2) {
history.shift();
}
// Log when we have enough data to start trading
if (history.length === this.settings.slowPeriod) {
logger.info(`✅ ${symbol} now has enough history (${history.length} bars) to start trading`);
}
}
protected async generateSignal(data: MarketData): Promise<Signal | null> {
if (data.type !== 'bar') {
return null;
}
const symbol = data.data.symbol;
const history = this.priceHistory.get(symbol);
if (!history || history.length <= this.settings.slowPeriod) {
if (history && history.length % 5 === 0) {
logger.debug(
`${symbol} - Not enough history: ${history.length}/${this.settings.slowPeriod + 1} bars`
);
}
return null;
}
const crossover = movingAverageCrossover(
history,
this.settings.fastPeriod,
this.settings.slowPeriod
);
if (!crossover) {
return null;
}
const { fastMA, slowMA, prevFastMA, prevSlowMA, goldenCross, deathCross } = crossover;
const currentPosition = this.getPosition(symbol);
const currentPrice = data.data.close;
const timestamp = new Date(data.data.timestamp).toISOString().split('T')[0];
// Check minimum holding period
const currentBar = this.barCount.get(symbol) || 0;
const lastTradeBar = this.lastTradeTime.get(symbol) || 0;
const barsSinceLastTrade =
lastTradeBar > 0 ? currentBar - lastTradeBar : Number.MAX_SAFE_INTEGER;
// Enhanced debugging - log more frequently and when MAs are close
const maDiff = fastMA - slowMA;
const maDiffPct = (maDiff / slowMA) * 100;
const masAreClose = Math.abs(maDiffPct) < 1.0; // Within 1%
if (history.length % this.DEBUG_INTERVAL === 0 || masAreClose || goldenCross || deathCross) {
logger.info(`${symbol} @ ${timestamp} [Bar ${currentBar}]:`);
logger.info(` Price: $${currentPrice.toFixed(2)}`);
logger.info(` Fast MA (${this.settings.fastPeriod}): $${fastMA.toFixed(2)}`);
logger.info(` Slow MA (${this.settings.slowPeriod}): $${slowMA.toFixed(2)}`);
logger.info(` MA Diff: ${maDiff.toFixed(2)} (${maDiffPct.toFixed(2)}%)`);
logger.info(` Position: ${currentPosition} shares`);
logger.info(` Bars since last trade: ${barsSinceLastTrade}`);
if (goldenCross) {
logger.info(` 🟢 GOLDEN CROSS DETECTED!`);
}
if (deathCross) {
logger.info(` 🔴 DEATH CROSS DETECTED!`);
}
}
// Note: We removed the MIN_HOLDING_BARS check here to allow closing and opening on same bar
// The check is now done individually for new position entries
if (goldenCross) {
logger.info(`🟢 Golden cross detected for ${symbol} @ ${timestamp}`);
logger.info(` Current position: ${currentPosition} shares`);
// For golden cross, we want to be long
if (currentPosition < 0) {
// Close short position first
logger.info(` Closing short position of ${Math.abs(currentPosition)} shares`);
const signal: Signal = {
type: 'buy',
symbol,
strength: 0.8,
reason: 'Golden cross - Closing short position',
metadata: {
fastMA,
slowMA,
prevFastMA,
prevSlowMA,
crossoverType: 'golden',
price: currentPrice,
quantity: Math.abs(currentPosition), // Buy to close short
},
};
this.lastTradeTime.set(symbol, currentBar);
this.totalSignals++;
logger.info(`👉 Total signals generated: ${this.totalSignals}`);
return signal;
} else if (currentPosition === 0) {
// No position, open long
logger.info(` No current position, opening long`);
logger.info(
` Price: ${currentPrice.toFixed(2)}, Fast MA: ${fastMA.toFixed(2)} > Slow MA: ${slowMA.toFixed(2)}`
);
logger.info(
` Prev Fast MA: ${prevFastMA.toFixed(2)} <= Prev Slow MA: ${prevSlowMA.toFixed(2)}`
);
// Calculate position size
const positionSize = await this.calculateCrossoverPositionSize(currentPrice);
logger.info(` Opening long position: ${positionSize} shares`);
const signal: Signal = {
type: 'buy',
symbol,
strength: 0.8,
reason: 'Golden cross - Fast MA crossed above Slow MA',
metadata: {
fastMA,
slowMA,
prevFastMA,
prevSlowMA,
crossoverType: 'golden',
price: currentPrice,
quantity: positionSize,
},
};
this.lastTradeTime.set(symbol, currentBar);
this.totalSignals++;
logger.info(`👉 Total signals generated: ${this.totalSignals}`);
return signal;
} else {
logger.info(` ⚠️ Already long, skipping buy signal`);
}
} else if (deathCross) {
logger.info(`🔴 Death cross detected for ${symbol} @ ${timestamp}`);
logger.info(` Current position: ${currentPosition} shares`);
// For death cross, we want to be flat or short
if (currentPosition > 0) {
// Close long position
logger.info(` Closing long position of ${currentPosition} shares`);
logger.info(
` Price: ${currentPrice.toFixed(2)}, Fast MA: ${fastMA.toFixed(2)} < Slow MA: ${slowMA.toFixed(2)}`
);
logger.info(
` Prev Fast MA: ${prevFastMA.toFixed(2)} >= Prev Slow MA: ${prevSlowMA.toFixed(2)}`
);
const signal: Signal = {
type: 'sell',
symbol,
strength: 0.8,
reason: 'Death cross - Fast MA crossed below Slow MA',
metadata: {
fastMA,
slowMA,
prevFastMA,
prevSlowMA,
crossoverType: 'death',
price: currentPrice,
quantity: currentPosition,
},
};
this.lastTradeTime.set(symbol, currentBar);
this.totalSignals++;
logger.info(`👉 Total signals generated: ${this.totalSignals}`);
return signal;
} else if (currentPosition === 0) {
// Open short position for long/short strategy
logger.info(` No current position, opening short`);
const positionSize = await this.calculateCrossoverPositionSize(currentPrice);
logger.info(` Opening short position: ${positionSize} shares`);
const signal: Signal = {
type: 'sell',
symbol,
strength: 0.8,
reason: 'Death cross - Opening short position',
metadata: {
fastMA,
slowMA,
prevFastMA,
prevSlowMA,
crossoverType: 'death',
price: currentPrice,
quantity: positionSize,
},
};
this.lastTradeTime.set(symbol, currentBar);
this.totalSignals++;
logger.info(`👉 Total signals generated: ${this.totalSignals}`);
return signal;
} else {
logger.info(` ⚠️ Already short, skipping sell signal`);
}
}
// Log near-crossover conditions
const fastApproachingSlow = Math.abs(fastMA - slowMA) / slowMA < 0.01; // Within 1%
if (fastApproachingSlow && history.length % 20 === 0) {
logger.debug(
`${symbol} - MAs converging: Fast MA ${fastMA.toFixed(2)} ~ Slow MA ${slowMA.toFixed(2)} (${((Math.abs(fastMA - slowMA) / slowMA) * 100).toFixed(2)}% diff)`
);
}
return null;
}
protected override validateConfig(config: StrategyConfig): StrategyConfig {
const validated = smaConfig(config, 1);
this.settings = validated.parameters;
return validated;
}
private async calculateCrossoverPositionSize(price: number): Promise<number> {
if (this.settings.quantity !== undefined) {
return this.settings.quantity;
}
const accountBalance = (await this.executionService.getAccount()).equity;
const positionValue = accountBalance * this.settings.positionFraction;
const shares = Math.floor(positionValue / price);
logger.debug(
`Position sizing: Balance=$${accountBalance}, Position Size=${this.settings.positionFraction}, Price=$${price}, Shares=${shares}`
);
return Math.max(1, shares); // At least 1 share
}
protected onOrderFilled(fill: StrategyFill): void {
const { symbol, side, quantity, price } = fill;
logger.info(`✅ ${side.toUpperCase()} filled: ${symbol} - ${quantity} shares @ ${price}`);
// Position tracking is handled by BaseStrategy.onOrderUpdate
// Just log the current position
const currentPosition = this.getPosition(symbol);
logger.info(`Position for ${symbol}: ${currentPosition} shares`);
}
// Override to provide custom order generation
protected override async signalToOrder(signal: Signal): Promise<OrderRequest | null> {
logger.info(`🔄 Converting signal to order:`, signal);
const _currentPosition = this.getPosition(signal.symbol);
// Get position sizing from metadata
const quantity = typeof signal.metadata?.quantity === 'number' ? signal.metadata.quantity : 100;
if (signal.type === 'buy') {
const order: OrderRequest = {
symbol: signal.symbol,
side: 'buy',
quantity,
orderType: 'market',
timeInForce: 'DAY',
};
logger.info(`📈 Generated BUY order:`, order);
return order;
} else if (signal.type === 'sell') {
const order: OrderRequest = {
symbol: signal.symbol,
side: 'sell',
quantity,
orderType: 'market',
timeInForce: 'DAY',
};
logger.info(`📉 Generated SELL order:`, order);
return order;
}
return null;
}
}