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
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/**
 * Integrated Trading System V2
 * Combines all components into a unified intelligent trading system
 * Features: Advanced strategies, market monitoring, notifications, regime detection
 */

import { EnhancedMarketMonitor } from './enhanced-market-monitor.js';
import { NotificationManager, NOTIFICATION_PRIORITY } from './enhanced-notification-system.js';
import { AdaptiveRegimeDetector, MARKET_REGIMES } from './adaptive-regime-detector.js';
import { analyzeWithAdvancedStrategy, ADVANCED_STRATEGIES_V2 } from './advanced-strategies-v2.js';
import { analyzeWithStrategy, HYBRID_STRATEGIES } from './trading-strategies.js';

/**
 * Integrated Trading System
 */
export class IntegratedTradingSystem {
    constructor(config = {}) {
        this.config = {
            symbol: config.symbol || 'BTC',
            strategy: config.strategy || 'ict-market-structure',
            useAdaptiveStrategy: config.useAdaptiveStrategy !== false,
            interval: config.interval || 60000,
            enableNotifications: config.enableNotifications !== false,
            notificationChannels: config.notificationChannels || ['browser'],
            telegram: config.telegram || null,
            riskLevel: config.riskLevel || 'medium'
        };

        // Initialize components
        this.monitor = new EnhancedMarketMonitor({
            symbol: this.config.symbol,
            strategy: this.config.strategy,
            interval: this.config.interval,
            useWebSocket: true
        });

        this.notificationManager = new NotificationManager({
            enabled: this.config.enableNotifications,
            channels: this.config.notificationChannels,
            telegram: this.config.telegram
        });

        this.regimeDetector = new AdaptiveRegimeDetector({
            lookbackPeriod: 100,
            volatilityPeriod: 20,
            trendPeriod: 50
        });

        // State
        this.isRunning = false;
        this.currentRegime = null;
        this.lastAnalysis = null;
        this.performanceStats = {
            totalSignals: 0,
            successfulSignals: 0,
            failedSignals: 0,
            avgConfidence: 0,
            startTime: null
        };

        // Setup event handlers
        this.setupEventHandlers();
    }

    /**
     * Start the integrated trading system
     * @returns {Promise<Object>} Start result
     */
    async start() {
        if (this.isRunning) {
            return { success: false, message: 'Already running' };
        }

        console.log('[IntegratedSystem] Starting...');

        try {
            // Start market monitor
            const monitorResult = await this.monitor.start();
            
            if (!monitorResult.success) {
                throw new Error(`Monitor failed to start: ${monitorResult.message}`);
            }

            this.isRunning = true;
            this.performanceStats.startTime = Date.now();

            // Send startup notification
            if (this.config.enableNotifications) {
                await this.notificationManager.send({
                    type: 'system',
                    priority: NOTIFICATION_PRIORITY.LOW,
                    title: '✅ Trading System Started',
                    message: `Monitoring ${this.config.symbol} with ${this.config.strategy} strategy`,
                    data: {
                        symbol: this.config.symbol,
                        strategy: this.config.strategy,
                        adaptive: this.config.useAdaptiveStrategy
                    }
                });
            }

            console.log('[IntegratedSystem] Started successfully');
            return { success: true, message: 'System started successfully' };
        } catch (error) {
            console.error('[IntegratedSystem] Start error:', error);
            return { success: false, message: error.message };
        }
    }

    /**
     * Stop the integrated trading system
     */
    stop() {
        if (!this.isRunning) return;

        console.log('[IntegratedSystem] Stopping...');
        
        this.monitor.stop();
        this.isRunning = false;

        // Send shutdown notification
        if (this.config.enableNotifications) {
            this.notificationManager.send({
                type: 'system',
                priority: NOTIFICATION_PRIORITY.LOW,
                title: '🛑 Trading System Stopped',
                message: `Stopped monitoring ${this.config.symbol}`,
                data: this.getPerformanceStats()
            });
        }

        console.log('[IntegratedSystem] Stopped');
    }

    /**
     * Setup event handlers for monitor
     */
    setupEventHandlers() {
        // Handle signals from monitor
        this.monitor.on('Signal', async (analysis) => {
            await this.handleSignal(analysis);
        });

        // Handle price updates
        this.monitor.on('PriceUpdate', (priceData) => {
            this.handlePriceUpdate(priceData);
        });

        // Handle errors
        this.monitor.on('Error', (error) => {
            this.handleError(error);
        });

        // Handle connection changes
        this.monitor.on('ConnectionChange', (status) => {
            this.handleConnectionChange(status);
        });
    }

    /**
     * Handle trading signal
     * @param {Object} analysis - Analysis results
     */
    async handleSignal(analysis) {
        try {
            console.log('[IntegratedSystem] Signal received:', analysis);

            // Update stats
            this.performanceStats.totalSignals++;
            this.lastAnalysis = analysis;

            // Filter signals based on risk level
            if (!this.shouldExecuteSignal(analysis)) {
                console.log('[IntegratedSystem] Signal filtered based on risk level');
                return;
            }

            // Send notification
            if (this.config.enableNotifications && analysis.signal !== 'hold') {
                await this.notificationManager.sendSignal(analysis);
            }

            // Emit event for UI
            this.emitEvent('signal', analysis);
        } catch (error) {
            console.error('[IntegratedSystem] Signal handling error:', error);
        }
    }

    /**
     * Handle price updates
     * @param {Object} priceData - Price data
     */
    handlePriceUpdate(priceData) {
        // Emit event for UI
        this.emitEvent('priceUpdate', priceData);
    }

    /**
     * Handle errors
     * @param {Error} error - Error object
     */
    async handleError(error) {
        console.error('[IntegratedSystem] Error:', error);

        // Send error notification for critical errors
        if (this.config.enableNotifications) {
            await this.notificationManager.sendError(error, 'Trading System');
        }

        // Emit event for UI
        this.emitEvent('error', error);
    }

    /**
     * Handle connection status changes
     * @param {Object} status - Connection status
     */
    handleConnectionChange(status) {
        console.log('[IntegratedSystem] Connection change:', status);

        // Emit event for UI
        this.emitEvent('connectionChange', status);

        // Notify on circuit breaker
        if (status.status === 'circuit-breaker-open' && this.config.enableNotifications) {
            this.notificationManager.send({
                type: 'warning',
                priority: NOTIFICATION_PRIORITY.HIGH,
                title: '⚠️ Circuit Breaker Activated',
                message: 'Too many errors detected. System paused temporarily.',
                data: status
            });
        }
    }

    /**
     * Perform analysis with adaptive strategy selection
     * @param {Array<Object>} ohlcvData - OHLCV data
     * @returns {Promise<Object>} Analysis results
     */
    async performAnalysis(ohlcvData) {
        try {
            let strategy = this.config.strategy;

            // Detect market regime if adaptive mode enabled
            if (this.config.useAdaptiveStrategy) {
                const regimeAnalysis = this.regimeDetector.detectRegime(ohlcvData);
                this.currentRegime = regimeAnalysis;

                // Get recommended strategies for this regime
                const recommendedStrategies = this.regimeDetector.getRecommendedStrategies();
                
                // Use first recommended strategy
                if (recommendedStrategies && recommendedStrategies.length > 0) {
                    strategy = recommendedStrategies[0];
                    console.log(`[IntegratedSystem] Regime: ${regimeAnalysis.regime}, Using: ${strategy}`);
                }
            }

            // Perform analysis
            let analysis;
            
            if (ADVANCED_STRATEGIES_V2[strategy]) {
                analysis = await analyzeWithAdvancedStrategy(this.config.symbol, strategy, ohlcvData);
            } else if (HYBRID_STRATEGIES[strategy]) {
                const marketData = {
                    price: ohlcvData[ohlcvData.length - 1].close,
                    volume: ohlcvData[ohlcvData.length - 1].volume,
                    high24h: Math.max(...ohlcvData.slice(-24).map(c => c.high)),
                    low24h: Math.min(...ohlcvData.slice(-24).map(c => c.low))
                };
                analysis = analyzeWithStrategy(this.config.symbol, strategy, marketData);
            } else {
                throw new Error(`Unknown strategy: ${strategy}`);
            }

            // Enrich with regime data
            if (this.currentRegime) {
                analysis.regime = this.currentRegime.regime;
                analysis.regimeConfidence = this.currentRegime.confidence;
            }

            return analysis;
        } catch (error) {
            console.error('[IntegratedSystem] Analysis error:', error);
            throw error;
        }
    }

    /**
     * Determine if signal should be executed based on risk level
     * @param {Object} analysis - Analysis results
     * @returns {boolean} Should execute
     */
    shouldExecuteSignal(analysis) {
        const riskLevels = {
            'very-low': { minConfidence: 50 },
            'low': { minConfidence: 60 },
            'medium': { minConfidence: 70 },
            'high': { minConfidence: 80 },
            'very-high': { minConfidence: 85 }
        };

        const levelConfig = riskLevels[this.config.riskLevel] || riskLevels.medium;
        
        // Don't execute hold signals
        if (analysis.signal === 'hold') {
            return false;
        }

        // Check confidence threshold
        return analysis.confidence >= levelConfig.minConfidence;
    }

    /**
     * Emit custom event
     * @param {string} eventName - Event name
     * @param {*} data - Event data
     */
    emitEvent(eventName, data) {
        window.dispatchEvent(new CustomEvent(`tradingSystem:${eventName}`, {
            detail: data
        }));
    }

    /**
     * Update system configuration
     * @param {Object} newConfig - New configuration
     */
    updateConfig(newConfig) {
        const needsRestart = this.isRunning && (
            newConfig.symbol !== this.config.symbol ||
            newConfig.interval !== this.config.interval
        );

        // Update configuration
        Object.assign(this.config, newConfig);

        // Update components
        if (newConfig.symbol || newConfig.strategy || newConfig.interval) {
            this.monitor.updateConfig({
                symbol: this.config.symbol,
                strategy: this.config.strategy,
                interval: this.config.interval
            });
        }

        if (newConfig.notificationChannels || newConfig.telegram) {
            this.notificationManager.updateConfig({
                channels: this.config.notificationChannels,
                telegram: this.config.telegram
            });
        }

        // Restart if necessary
        if (needsRestart) {
            this.stop();
            this.start();
        }
    }

    /**
     * Get current system status
     * @returns {Object} System status
     */
    getStatus() {
        return {
            isRunning: this.isRunning,
            config: this.config,
            monitorStatus: this.monitor.getStatus(),
            currentRegime: this.currentRegime,
            lastAnalysis: this.lastAnalysis,
            performanceStats: this.getPerformanceStats()
        };
    }

    /**
     * Get performance statistics
     * @returns {Object} Performance stats
     */
    getPerformanceStats() {
        const runtime = this.performanceStats.startTime
            ? Date.now() - this.performanceStats.startTime
            : 0;

        return {
            ...this.performanceStats,
            runtime,
            runtimeFormatted: this.formatDuration(runtime),
            successRate: this.performanceStats.totalSignals > 0
                ? (this.performanceStats.successfulSignals / this.performanceStats.totalSignals) * 100
                : 0
        };
    }

    /**
     * Format duration in milliseconds
     * @param {number} ms - Duration in milliseconds
     * @returns {string} Formatted duration
     */
    formatDuration(ms) {
        const seconds = Math.floor(ms / 1000);
        const minutes = Math.floor(seconds / 60);
        const hours = Math.floor(minutes / 60);
        const days = Math.floor(hours / 24);

        if (days > 0) return `${days}d ${hours % 24}h`;
        if (hours > 0) return `${hours}h ${minutes % 60}m`;
        if (minutes > 0) return `${minutes}m ${seconds % 60}s`;
        return `${seconds}s`;
    }

    /**
     * Test all components
     * @returns {Promise<Object>} Test results
     */
    async test() {
        console.log('[IntegratedSystem] Running system test...');

        const results = {
            monitor: false,
            notifications: false,
            regimeDetection: false,
            strategy: false
        };

        try {
            // Test monitor
            const monitorStatus = this.monitor.getStatus();
            results.monitor = !!monitorStatus;

            // Test notifications
            const notifResult = await this.notificationManager.test();
            results.notifications = notifResult.success;

            // Test regime detection with sample data
            const sampleData = this.generateSampleData();
            const regimeResult = this.regimeDetector.detectRegime(sampleData);
            results.regimeDetection = !!regimeResult.regime;

            // Test strategy analysis
            const analysisResult = await this.performAnalysis(sampleData);
            results.strategy = !!analysisResult.signal;

            console.log('[IntegratedSystem] Test results:', results);
            return {
                success: Object.values(results).every(r => r),
                results
            };
        } catch (error) {
            console.error('[IntegratedSystem] Test error:', error);
            return {
                success: false,
                results,
                error: error.message
            };
        }
    }

    /**
     * Generate sample data for testing
     * @returns {Array<Object>} Sample OHLCV data
     */
    generateSampleData() {
        const data = [];
        let price = 50000;

        for (let i = 0; i < 100; i++) {
            const volatility = price * 0.02;
            const open = price + (Math.random() - 0.5) * volatility;
            const close = open + (Math.random() - 0.5) * volatility;
            const high = Math.max(open, close) + Math.random() * volatility * 0.5;
            const low = Math.min(open, close) - Math.random() * volatility * 0.5;
            const volume = Math.random() * 1000000;

            data.push({
                timestamp: Date.now() - (99 - i) * 3600000,
                open, high, low, close, volume
            });

            price = close;
        }

        return data;
    }

    /**
     * Get available strategies
     * @returns {Object} Available strategies
     */
    static getAvailableStrategies() {
        return {
            advanced: ADVANCED_STRATEGIES_V2,
            hybrid: HYBRID_STRATEGIES
        };
    }

    /**
     * Get market regimes
     * @returns {Object} Market regimes
     */
    static getMarketRegimes() {
        return MARKET_REGIMES;
    }
}

export default IntegratedTradingSystem;