Introduce logger infrastructure and add structured logging to every layer: alpaca.ts (buy/sell/quote/clock), momentum-indicator.ts (evaluate flow), momentum-strategy.ts (poll loop), index.ts (startup/cycle), and trading.ts (market timing). Replace raw console.log calls with leveled logger. Update all tests with appropriate console spies. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import { Alpaca } from "./alpaca";
|
|
import { Indicator } from "./indicator";
|
|
import { logger } from "./logger";
|
|
import { wait } from "./trading";
|
|
|
|
export interface MomentumResult {
|
|
direction: 'up' | 'down';
|
|
priceBefore: number;
|
|
priceAfter: number;
|
|
}
|
|
|
|
export interface MomentumIndicatorConfig {
|
|
symbol: string;
|
|
settleDelay: number;
|
|
sampleDelay: number;
|
|
}
|
|
|
|
const defaultConfig: MomentumIndicatorConfig = {
|
|
symbol: 'QQQ',
|
|
settleDelay: 60_000,
|
|
sampleDelay: 1_000,
|
|
};
|
|
|
|
export class MomentumIndicator implements Indicator<MomentumResult> {
|
|
name = 'momentum-indicator';
|
|
private config: MomentumIndicatorConfig;
|
|
|
|
constructor(config: Partial<MomentumIndicatorConfig> = {}) {
|
|
this.config = { ...defaultConfig, ...config };
|
|
}
|
|
|
|
async evaluate(alpaca: Alpaca): Promise<MomentumResult> {
|
|
logger.debug(`waiting ${this.config.settleDelay}ms for market to settle`);
|
|
await wait(this.config.settleDelay);
|
|
|
|
const priceBefore = await alpaca.getLatestAsk(this.config.symbol);
|
|
logger.debug(`${this.config.symbol} priceBefore: ${priceBefore}`);
|
|
|
|
await wait(this.config.sampleDelay);
|
|
|
|
const priceAfter = await alpaca.getLatestAsk(this.config.symbol);
|
|
const direction = priceAfter >= priceBefore ? 'up' : 'down';
|
|
logger.debug(`${this.config.symbol} priceAfter: ${priceAfter} → direction: ${direction}`);
|
|
|
|
return { direction, priceBefore, priceAfter };
|
|
}
|
|
}
|