Add bot orchestrator, wire up index.ts, remove old runDay
Bot validates strategy capital allocations, waits for market open, runs all strategies concurrently, and passes signals to the executor. The main loop in index.ts now delegates to Bot.runDay(). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
37
src/bot.ts
Normal file
37
src/bot.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { Alpaca } from "./alpaca";
|
||||
import { Strategy } from "./strategy";
|
||||
import { Executor } from "./executor";
|
||||
import { waitForNextOpen } from "./trading";
|
||||
|
||||
export class Bot {
|
||||
private alpaca: Alpaca;
|
||||
private strategies: Strategy[];
|
||||
private executor: Executor;
|
||||
|
||||
constructor(alpaca: Alpaca, strategies: Strategy[]) {
|
||||
const totalAllocation = strategies.reduce((sum, s) => sum + s.capitalAllocation, 0);
|
||||
if (totalAllocation > 1.0) {
|
||||
throw new Error(
|
||||
`Capital allocations sum to ${totalAllocation}, which exceeds 1.0`
|
||||
);
|
||||
}
|
||||
this.alpaca = alpaca;
|
||||
this.strategies = strategies;
|
||||
this.executor = new Executor(alpaca);
|
||||
}
|
||||
|
||||
async runDay(): Promise<void> {
|
||||
console.log('waiting for open');
|
||||
await waitForNextOpen(this.alpaca);
|
||||
|
||||
const account = await this.alpaca.getAccount();
|
||||
const totalCapital = parseFloat(account.cash);
|
||||
|
||||
await Promise.all(
|
||||
this.strategies.map(async (strategy) => {
|
||||
const signals = await strategy.execute(this.alpaca);
|
||||
await this.executor.executeSignals(strategy, signals, totalCapital);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user