Add momentum strategy with QQQ-based TQQQ/SQQQ trading
Implements MomentumIndicator (samples QQQ direction) and MomentumStrategy (buys TQQQ on up, SQQQ on down, exits at +1% target or 1hr timeout). Wired into Bot via index.ts. Also fixes vitest config to exclude dist/ from test discovery. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
46
src/momentum-indicator.ts
Normal file
46
src/momentum-indicator.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Alpaca } from "./alpaca";
|
||||
import { Indicator } from "./indicator";
|
||||
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> {
|
||||
await wait(this.config.settleDelay);
|
||||
|
||||
const before = await alpaca.getLatestQuote(this.config.symbol);
|
||||
const priceBefore = before.ap;
|
||||
|
||||
await wait(this.config.sampleDelay);
|
||||
|
||||
const after = await alpaca.getLatestQuote(this.config.symbol);
|
||||
const priceAfter = after.ap;
|
||||
|
||||
const direction = priceAfter >= priceBefore ? 'up' : 'down';
|
||||
|
||||
return { direction, priceBefore, priceAfter };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user