Remove executor, add buy/sell to Alpaca, move capital allocation to Bot
Replace the Executor logging placeholder with real buy/sell methods on the Alpaca class that place market orders via createOrder and return the fill price. Strategies now receive their capital amount directly and place orders themselves. Bot accepts StrategyAllocation[] to decouple capital allocation from strategy definition. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
29
src/bot.ts
29
src/bot.ts
@@ -1,23 +1,25 @@
|
||||
import { Alpaca } from "./alpaca";
|
||||
import { Strategy } from "./strategy";
|
||||
import { Executor } from "./executor";
|
||||
import { isMarketOpen, waitForNextOpen } from "./trading";
|
||||
|
||||
export interface StrategyAllocation {
|
||||
strategy: Strategy;
|
||||
capitalAllocation: number;
|
||||
}
|
||||
|
||||
export class Bot {
|
||||
private alpaca: Alpaca;
|
||||
private strategies: Strategy[];
|
||||
private executor: Executor;
|
||||
private allocations: StrategyAllocation[];
|
||||
|
||||
constructor(alpaca: Alpaca, strategies: Strategy[]) {
|
||||
const totalAllocation = strategies.reduce((sum, s) => sum + s.capitalAllocation, 0);
|
||||
constructor(alpaca: Alpaca, allocations: StrategyAllocation[]) {
|
||||
const totalAllocation = allocations.reduce((sum, a) => sum + a.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);
|
||||
this.allocations = allocations;
|
||||
}
|
||||
|
||||
async runDay(): Promise<void> {
|
||||
@@ -26,14 +28,13 @@ export class Bot {
|
||||
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);
|
||||
const account = await this.alpaca.getAccount();
|
||||
const totalCapital = parseFloat(account.cash);
|
||||
|
||||
await this.executor.executeSignals(strategy, signals, totalCapital);
|
||||
await Promise.all(
|
||||
this.allocations.map(async ({ strategy, capitalAllocation }) => {
|
||||
const capitalAmount = totalCapital * capitalAllocation;
|
||||
await strategy.execute(this.alpaca, capitalAmount);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user