60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { EventEmitter } from 'node:events';
|
|
import type { ExecutionService } from '../services/ExecutionService';
|
|
import type { MarketDataService } from '../services/MarketDataService';
|
|
import type { StorageService } from '../services/StorageService';
|
|
import type { OrchestratorServices, OrderRequest } from '../types';
|
|
|
|
/** Compatibility facade: no independent simulator, balance, or position book. */
|
|
export class PaperTradingManager extends EventEmitter {
|
|
constructor(
|
|
_container: OrchestratorServices,
|
|
_storage: StorageService,
|
|
_market: MarketDataService,
|
|
private readonly execution: ExecutionService,
|
|
_initialBalance = 100000
|
|
) {
|
|
super();
|
|
}
|
|
executeOrder(order: OrderRequest) {
|
|
return this.execution.submitOrder(order);
|
|
}
|
|
updateMarketPrice(
|
|
symbol: string,
|
|
bid: number,
|
|
ask: number,
|
|
eventId: string,
|
|
timestamp: number,
|
|
size: number
|
|
) {
|
|
return this.execution.onMarketData({
|
|
type: 'quote',
|
|
eventId,
|
|
data: { symbol, bid, ask, bidSize: size, askSize: size, timestamp },
|
|
});
|
|
}
|
|
getAccount() {
|
|
return this.execution.getAccount();
|
|
}
|
|
getAllPositions() {
|
|
return this.execution.getPositions();
|
|
}
|
|
async getPosition(symbol: string) {
|
|
return (await this.getAllPositions()).find(p => p.symbol === symbol);
|
|
}
|
|
async getPerformanceMetrics() {
|
|
const account = await this.getAccount();
|
|
return {
|
|
cash: account.cash,
|
|
currentEquity: account.equity,
|
|
netPnl: account.realizedPnl + account.unrealizedPnl,
|
|
realizedPnl: account.realizedPnl,
|
|
unrealizedPnl: account.unrealizedPnl,
|
|
};
|
|
}
|
|
checkPendingOrders() {
|
|
throw new Error('Pending orders are processed only by ordered market events');
|
|
}
|
|
reset() {
|
|
throw new Error('Implicit paper account reset is disabled; use a new account ID');
|
|
}
|
|
}
|