Refactor execution boundaries and add durable strategy recovery
This commit is contained in:
parent
d6b09eb635
commit
694c48bbdb
36 changed files with 3077 additions and 491 deletions
|
|
@ -1,4 +1,11 @@
|
|||
import type { Account, Fill, StoredOrder } from './model';
|
||||
import type {
|
||||
MarketInput,
|
||||
StrategyCheckpoint,
|
||||
StrategyJournalEvent,
|
||||
StrategyJournalPayload,
|
||||
StrategyOrderIntent,
|
||||
} from './strategy-recovery';
|
||||
|
||||
export interface ExecutionTransaction {
|
||||
account: Account;
|
||||
|
|
@ -8,69 +15,17 @@ export interface ExecutionTransaction {
|
|||
putOrder(order: StoredOrder): Promise<void>;
|
||||
insertFill(orderId: string, fill: Fill): Promise<boolean>;
|
||||
recordEvent(eventId: string, fingerprint: string): Promise<boolean>;
|
||||
appendJournal(payload: StrategyJournalPayload): Promise<void>;
|
||||
nextJournal(cursor: number): Promise<StrategyJournalEvent | undefined>;
|
||||
strategies(): Promise<StrategyCheckpoint[]>;
|
||||
putStrategy(checkpoint: StrategyCheckpoint): Promise<void>;
|
||||
putIntent(intent: StrategyOrderIntent): Promise<void>;
|
||||
nextIntent(): Promise<StrategyOrderIntent | undefined>;
|
||||
nextInput(): Promise<MarketInput | undefined>;
|
||||
putInput(input: MarketInput): Promise<void>;
|
||||
}
|
||||
export interface ExecutionRepository {
|
||||
initialize(accountId: string, initial: Account): Promise<void>;
|
||||
transact<T>(accountId: string, work: (tx: ExecutionTransaction) => Promise<T>): Promise<T>;
|
||||
}
|
||||
export class ExecutionConflictError extends Error {}
|
||||
|
||||
/** Explicit run-local backtest store, never a fallback for paper or live execution. */
|
||||
export class BacktestExecutionRepository implements ExecutionRepository {
|
||||
private state?: {
|
||||
account: Account;
|
||||
orders: Record<string, StoredOrder>;
|
||||
fills: Record<string, Fill>;
|
||||
events: Record<string, string>;
|
||||
};
|
||||
private tail: Promise<unknown> = Promise.resolve();
|
||||
async initialize(_id: string, account: Account) {
|
||||
this.state = { account: structuredClone(account), orders: {}, fills: {}, events: {} };
|
||||
}
|
||||
transact<T>(_id: string, work: (tx: ExecutionTransaction) => Promise<T>): Promise<T> {
|
||||
const task = this.tail.then(async () => {
|
||||
if (!this.state) {
|
||||
throw new Error('Execution account is not initialized');
|
||||
}
|
||||
const state = structuredClone(this.state);
|
||||
const result = await work({
|
||||
account: state.account,
|
||||
async findOrder(id) {
|
||||
return Object.values(state.orders).find(o => o.report.clientOrderId === id);
|
||||
},
|
||||
async getOrder(id) {
|
||||
return state.orders[id];
|
||||
},
|
||||
async openOrders() {
|
||||
return Object.values(state.orders).filter(o =>
|
||||
['accepted', 'partiallyFilled'].includes(o.report.status)
|
||||
);
|
||||
},
|
||||
async putOrder(order) {
|
||||
state.orders[order.report.orderId] = order;
|
||||
},
|
||||
async insertFill(_orderId, fill) {
|
||||
if (state.fills[fill.id]) {
|
||||
return false;
|
||||
}
|
||||
state.fills[fill.id] = fill;
|
||||
return true;
|
||||
},
|
||||
async recordEvent(id, hash) {
|
||||
if (state.events[id] && state.events[id] !== hash) {
|
||||
throw new ExecutionConflictError('Conflicting market event identity');
|
||||
}
|
||||
if (state.events[id]) {
|
||||
return false;
|
||||
}
|
||||
state.events[id] = hash;
|
||||
return true;
|
||||
},
|
||||
});
|
||||
this.state = state;
|
||||
return structuredClone(result);
|
||||
});
|
||||
this.tail = task.catch(() => {});
|
||||
return task;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue