-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngine.ts
95 lines (85 loc) · 3.24 KB
/
Engine.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { NotificationService } from './NotificationService';
export class TradingEngine extends EventEmitter {
// ... existing properties ...
private notificationService: NotificationService;
private constructor() {
super();
this.components = new Map();
this.initializeComponents();
this.setupEventHandlers();
}
private initializeComponents(): void {
// ... existing initializations ...
this.notificationService = new NotificationService();
this.register('notificationService', this.notificationService);
}
private setupEventHandlers(): void {
// Existing monitoring handlers
this.monitoringService.on('metrics', (metrics) => {
this.logger.info('System metrics update:', metrics);
});
// WebSocket event handlers
this.wsManager.on('error', (symbol: string, error: Error) => {
this.notificationService.sendAlert(
`WebSocket error for ${symbol}: ${error.message}`,
'error'
);
});
this.wsManager.on('connected', (symbol: string) => {
this.notificationService.sendAlert(
`WebSocket connected for ${symbol}`,
'info'
);
});
// Performance tracking notifications
this.performanceTracker.on('performanceUpdate', (metrics: any) => {
if (metrics.profitLoss < -1000) { // Example threshold
this.notificationService.sendAlert(
`High loss detected: ${metrics.profitLoss}`,
'warning'
);
}
});
// Risk management notifications
this.riskManager.on('riskThresholdBreached', (data: any) => {
this.notificationService.sendAlert(
`Risk threshold breached: ${data.message}`,
'error'
);
});
}
public start(): void {
try {
this.isRunning = true;
this.monitoringService.startMonitoring();
const wsUrl = process.env.WEBSOCKET_URL || 'wss://stream.binance.com:9443/ws';
this.wsManager.connect(wsUrl, 'yourSymbol');
this.notificationService.sendAlert('Trading Engine started', 'info');
this.logger.info('Trading Engine started');
this.emit('engineStart');
} catch (error) {
this.logger.error('Failed to start Trading Engine:', error);
this.notificationService.sendAlert(
`Failed to start Trading Engine: ${error.message}`,
'error'
);
this.stop();
}
}
public stop(): void {
try {
this.isRunning = false;
this.monitoringService.stopMonitoring();
this.wsManager.disconnect();
this.notificationService.sendAlert('Trading Engine stopped', 'info');
this.logger.info('Trading Engine stopped');
this.emit('engineStop');
} catch (error) {
this.logger.error('Error stopping Trading Engine:', error);
this.notificationService.sendAlert(
`Error stopping Trading Engine: ${error.message}`,
'error'
);
}
}
}