/** * Event names for consistency across services * * This constant object defines all trading system events in a centralized location. * All services must use these constants instead of hardcoding event names to ensure * consistency and prevent typos. * * Event Naming Convention: * - Use dot notation: 'domain.action' (e.g., 'signal.generated') * - Use lowercase for consistency * - Keep names descriptive and specific * * Event Categories: * - Signal Events: ML signal generation and processing * - Model Events: ML model lifecycle and updates * - Trade Events: Trade execution and management */ export const TRADING_EVENTS = { SIGNAL_GENERATED: 'signal.generated', // ML service generates new trading signal SIGNAL_PROCESSED: 'signal.processed', // Frontend processes/accepts signal MODEL_UPDATED: 'model.updated', // ML model performance updated TRADE_EXECUTED: 'trade.executed', // Trade successfully executed SIGNAL_EXPIRED: 'signal.expired', // Signal expired without action MODEL_TRAINING_STARTED: 'model.training.started', // Model training initiated MODEL_TRAINING_COMPLETED: 'model.training.completed', // Model training finished MODEL_TRAINING_FAILED: 'model.training.failed' // Model training failed } as const; export type TradingEventName = typeof TRADING_EVENTS[keyof typeof TRADING_EVENTS]; /** * Event payload types * * These interfaces define the structure of event data for each trading event. * All events follow a consistent pattern with 'event' and 'data' properties. * * Event Structure: * - event: The event name constant from TRADING_EVENTS * - data: Event-specific payload with relevant information * - timestamp: When the event occurred (ISO 8601 format) */ /** * Signal Generated Event * * Emitted when the ML service generates a new trading signal. * This event triggers the frontend to evaluate and potentially act on the signal. * * Data Fields: * - signalId: Unique identifier for the generated signal * - modelId: ML model that generated the signal * - symbol: Trading pair (e.g., 'EURUSD') * - timeframe: Market data timeframe used * - signal: Trading action ('BUY', 'SELL', 'HOLD') * - confidence: Model confidence score (0.0-1.0) * - price: Entry price for the signal * - timestamp: When signal was generated */ export interface SignalGeneratedEvent { event: typeof TRADING_EVENTS.SIGNAL_GENERATED; data: { signalId: string; modelId: string; symbol: string; timeframe: string; signal: 'BUY' | 'SELL' | 'HOLD'; confidence: number; price: number; timestamp: string; }; } export interface SignalProcessedEvent { event: typeof TRADING_EVENTS.SIGNAL_PROCESSED; data: { signalId: string; status: 'accepted' | 'rejected' | 'expired'; reason?: string; timestamp: string; }; } export interface ModelUpdatedEvent { event: typeof TRADING_EVENTS.MODEL_UPDATED; data: { modelId: string; version: string; accuracy: number; lastTraining: string; timestamp: string; }; } export interface TradeExecutedEvent { event: typeof TRADING_EVENTS.TRADE_EXECUTED; data: { tradeId: string; signalId: string; symbol: string; side: 'BUY' | 'SELL'; quantity: number; price: number; timestamp: string; }; } // Union type for all events export type TradingEvent = | SignalGeneratedEvent | SignalProcessedEvent | ModelUpdatedEvent | TradeExecutedEvent; /** * Event Bus Interface * * Defines the contract for the event bus system that enables loose coupling * between services. Services can publish events without knowing who's listening, * and subscribe to events without knowing who's publishing. * * Interface Methods: * - publish: Emit an event to all subscribers * - subscribe: Register a handler for a specific event type * * Usage Pattern: * ```typescript * // Subscribe to events * const unsubscribe = eventBus.subscribe(TRADING_EVENTS.SIGNAL_GENERATED, handleSignal); * * // Publish events * await eventBus.publish({ * event: TRADING_EVENTS.SIGNAL_GENERATED, * data: { /* signal data *\/ } * }); * * // Clean up subscription * unsubscribe(); * ``` */ export interface EventBus { publish(event: T): Promise; subscribe( eventName: T['event'], handler: (event: T) => Promise ): () => void; }