File size: 28,204 Bytes
01369a3
 
66b7a93
f43243e
66b7a93
8560ddd
66b7a93
 
 
 
 
 
 
 
 
 
 
 
f43243e
66b7a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9b0611
 
66b7a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3a66fe
a547315
66b7a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01369a3
 
 
 
 
 
 
d3a66fe
01369a3
 
 
 
 
 
 
 
 
 
 
 
 
 
66b7a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a547315
66b7a93
 
 
cafa0b3
 
66b7a93
 
 
 
 
 
 
 
 
 
 
01369a3
 
 
 
 
 
 
66b7a93
 
a547315
462ac4d
66b7a93
 
 
 
01369a3
66b7a93
 
 
 
 
a547315
462ac4d
66b7a93
 
 
 
01369a3
66b7a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a547315
 
 
 
 
 
 
 
 
 
 
 
 
01369a3
 
66b7a93
 
 
01369a3
 
a547315
 
 
 
 
 
 
 
 
01369a3
a547315
 
 
 
01369a3
a547315
01369a3
 
a547315
01369a3
 
 
66b7a93
 
 
 
01369a3
 
 
66b7a93
 
 
 
 
 
 
 
 
 
cafa0b3
 
 
66b7a93
 
 
 
 
 
 
 
 
 
 
 
 
01369a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b1907f
01369a3
 
 
 
 
 
66b7a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01369a3
 
 
 
66b7a93
 
 
 
 
01369a3
 
 
 
 
 
 
 
 
 
 
 
 
 
66b7a93
 
01369a3
66b7a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3a66fe
66b7a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5692dc
66b7a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a6768e
66b7a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
034b7dd
 
66b7a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
import { sendTelegramMessage } from '../data/telegramNotifier.js';

// Spread configuration
export function getSpread(symbol) {
  switch (symbol) {
    case 'BTCUSDT': return 25.0;
    case 'ETHUSDT': return 5.0;
    case 'XAUUSD': return 0.7;
    case 'EURUSD':
    case 'GBPUSD':
    case 'USDCAD':
      return 0.00007; // 0.7 pips
    default:
      return 0;
  }
}

// Commission configuration
export function calculateCommission(symbol, entryPrice, lots) {
  if (symbol === 'BTCUSDT' || symbol === 'ETHUSDT') {
    // Crypto: 0.04% per trade = 0.08% round-turn commission
    return 0.0008 * entryPrice * lots;
  } else {
    // Forex/Metals: $5 per lot round-turn commission
    return 5.0 * lots;
  }
}

export class TradeManager {
  constructor({ onDailyLossUpdate, isServer = false, db = null }) {
    this.isServer = isServer;
    this.db = db;
    this.onDailyLossUpdate = onDailyLossUpdate;
    
    this.activeTrades = [];
    this.tradeHistory = [];
    this.dailyLosses = 0;
    this.accountBalance = 5000.0;
    this.lastExecutedCandleTime = {};
    this.lastClosedTime = {};
    this.lastResetDate = new Date().toDateString();
  }

  /**
   * Add a trade from a signal
   */
  async takeTrade(signal, candleTime = null) {
    if (!this.isServer) {
      try {
        const response = await fetch('/api/take-trade', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ signal }),
        });
        const result = await response.json();
        if (result.success && result.trade) {
          console.log('[Client TradeManager] Trade submitted successfully to server:', result.trade);
          if (globalThis.syncTerminalState) await globalThis.syncTerminalState();
          return result.trade;
        }
      } catch (err) {
        console.error('[Client TradeManager] Failed to submit trade to server:', err);
      }
      return null;
    }

    this._checkDailyReset();
    
    // Exclude if already running an active trade on this symbol
    const active = this.activeTrades.some(t => t.symbol === signal.symbol && t.status === 'active');
    if (active) return null;

    // Exclude if the symbol is in a 10-minute cooldown
    if (this.isSymbolCoolingDown(signal.symbol)) {
      console.warn(`[TradeManager] Cannot take trade on ${signal.symbol}: cooling down.`);
      return null;
    }

    const spread = getSpread(signal.symbol);
    const commission = calculateCommission(signal.symbol, signal.entry, signal.lotSize);

    // Entry price is pre-adjusted for spread during signal generation
    const entryPrice = signal.entry;

    const trade = {
      // Non-colliding dynamic ID prevents duplicate entries on rapid executions
      id: Date.now() + Math.floor(Math.random() * 100000),
      type: signal.type,
      symbol: signal.symbol,
      entry: entryPrice,
      sl: signal.sl,
      tp1: signal.tp1,
      tp2: signal.tp2,
      lotSize: signal.lotSize,
      riskAmount: signal.riskAmount,
      time: Date.now(),
      // Initial current price: LONG trades exit at Bid (signal.entry), SHORT trades exit at Ask (signal.entry + spread)
      currentPrice: signal.type === 'LONG' ? signal.entry : signal.entry + spread,
      pnl: 0,
      currentRR: 0,
      commission: commission,
      status: 'active',
      slMoved: false,
      quality: signal.quality || 'A',
      initialRiskDist: Math.abs(signal.entry - signal.sl),
    };

    // Debit commission immediately from the closed balance
    this.accountBalance -= commission;
    this._saveAccountBalance();

    this.activeTrades.push(trade);
    this._saveActiveTrades();

    // Record execution candle time to block same-candle re-entry
    if (candleTime) {
      this.lastExecutedCandleTime[signal.symbol] = candleTime;
      this._saveLastExecutedCandleTime();
    }

    // Send Telegram Entry Notification
    const decs = this._getDecimals(trade.symbol);
    const confluencesHtml = signal.confluences && signal.confluences.length > 0 
      ? signal.confluences.map(c => `β€’ ${c}`).join('\n')
      : 'β€’ Smart Money Confluence Setup';

    const msg = `🚨 <b>NEW AUTOPILOT TRADE EXECUTED</b>\n\n` +
      `<b>Setup Quality:</b> <code>Grade ${trade.quality} Setup</code>\n` +
      `<b>Symbol:</b> <code>${trade.symbol}</code>\n` +
      `<b>Direction:</b> <code>${trade.type}</code>\n` +
      `<b>Lot Size:</b> <code>${trade.lotSize.toFixed(2)} lots</code>\n` +
      `<b>Entry Price:</b> <code>$${trade.entry.toFixed(decs)}</code>\n` +
      `<b>Stop Loss (SL):</b> <code>$${trade.sl.toFixed(decs)}</code>\n` +
      `<b>Take Profit 1 (TP1):</b> <code>$${trade.tp1.toFixed(decs)}</code>\n` +
      `<b>Take Profit 2 (TP2):</b> <code>$${trade.tp2.toFixed(decs)}</code>\n\n` +
      `πŸ“Š <b>Risk Configuration:</b>\n` +
      `β€’ <b>Expected Risk:</b> <code>$${trade.riskAmount.toFixed(2)}</code> (FundingPips Compliant)\n` +
      `β€’ <b>Stop Loss Distance:</b> <code>${Math.abs(trade.entry - trade.sl).toFixed(decs)} price units</code>\n\n` +
      `πŸ’‘ <b>Trade Confluences Scanned:</b>\n${confluencesHtml}`;

    sendTelegramMessage(msg);

    return trade;
  }

  /**
   * Update all trades with current prices
   */
  updatePrices(currentPrice, symbol) {
    this._checkDailyReset();
    const spread = getSpread(symbol);

    for (const trade of this.activeTrades) {
      if (trade.symbol !== symbol) continue;
      if (trade.status !== 'active') continue; // Skip already closed trades in memory loop

      // SHORT trades exit/value at Ask price (Bid + Spread)
      const valuationPrice = trade.type === 'SHORT' ? currentPrice + spread : currentPrice;
      trade.currentPrice = valuationPrice;

      const direction = trade.type === 'LONG' ? 1 : -1;
      const priceDiff = (valuationPrice - trade.entry) * direction;
      const slDist = trade.initialRiskDist || Math.abs(trade.entry - trade.sl);

      // Calculate total P&L (realized + remaining)
      const remainingPnL = this._calculatePnL(trade, valuationPrice);
      // Net of commission so per-trade P&L matches the actual balance impact.
      trade.pnl = (trade.realizedPnL || 0) + remainingPnL - (trade.commission || 0);
      
      // Guard against micro-stop division anomalies in older trades
      let minThreshold = 0.00005; // half a pip for Forex
      if (trade.symbol === 'BTCUSDT' || trade.symbol === 'ETHUSDT') {
        minThreshold = 0.1; // 10 cents for crypto
      } else if (trade.symbol === 'XAUUSD') {
        minThreshold = 0.05; // 5 cents for gold
      }

      trade.currentRR = slDist >= minThreshold ? priceDiff / slDist : 0;

      // Only the authoritative server engine opens/closes positions. The browser
      // client is display-only (refreshed from the server every few seconds), so
      // it must never close trades or mutate balance/history locally.
      if (!this.isServer) continue;

      // Check auto SL/TP hit or partial close at TP1. Closes fill at the EXACT
      // SL/TP level (not an overshooting tick) so realized risk stays bounded.
      if (trade.type === 'LONG') {
        if (valuationPrice <= trade.sl) {
          const reason = trade.partialClosed ? 'Trailing Stop' : 'SL Hit';
          this._closeTrade(trade, reason, trade.sl);
        } else if (valuationPrice >= trade.tp2) {
          if (!trade.partialClosed) {
            this._triggerPartialClose(trade, trade.tp1);
          }
          this._closeTrade(trade, 'TP2 Hit', trade.tp2);
        } else if (valuationPrice >= trade.tp1 && !trade.partialClosed) {
          this._triggerPartialClose(trade, trade.tp1);
        }
      } else {
        if (valuationPrice >= trade.sl) {
          const reason = trade.partialClosed ? 'Trailing Stop' : 'SL Hit';
          this._closeTrade(trade, reason, trade.sl);
        } else if (valuationPrice <= trade.tp2) {
          if (!trade.partialClosed) {
            this._triggerPartialClose(trade, trade.tp1);
          }
          this._closeTrade(trade, 'TP2 Hit', trade.tp2);
        } else if (valuationPrice <= trade.tp1 && !trade.partialClosed) {
          this._triggerPartialClose(trade, trade.tp1);
        }
      }
    }
    this._render();
  }

  /**
   * Update exit analysis from the exit manager
   */
  updateExitAnalysis(tradeId, analysis) {
    const trade = this.activeTrades.find(t => t.id === tradeId);
    if (!trade || !analysis || trade.status !== 'active') return; // Guard against updating closed trades

    if (analysis.warnings && analysis.warnings.length > 0) {
      trade.warnings = analysis.warnings;
    } else {
      trade.warnings = [];
    }

    if (analysis.suggestion) {
      trade.suggestion = analysis.suggestion;
      trade.suggestionText = analysis.reason;
    } else {
      trade.suggestion = null;
      trade.suggestionText = null;
    }

    // Apply a trailed stop on the runner (ratchet only β€” never loosen, never cross
    // back to the loss side of breakeven). Authoritative on the server engine.
    if (analysis.newSL != null && trade.partialClosed && this.isServer) {
      const better = trade.type === 'LONG'
        ? (analysis.newSL > trade.sl && analysis.newSL >= trade.entry)
        : (analysis.newSL < trade.sl && analysis.newSL <= trade.entry);
      if (better) {
        trade.sl = analysis.newSL;
        trade.slMoved = true;
        this._saveActiveTrades();
      }
    }

    // Warnings/suggestions are ephemeral display state recomputed every tick, so
    // just re-render β€” no need to write them to the database on every tick.
    this._render();
  }

  _generatePostMortem(trade) {
    const decs = this._getDecimals(trade.symbol);
    const lines = [];

    // Factual invalidation summary β€” no fabricated narrative.
    lines.push(`β€’ <b>Invalidation:</b> Price hit the Stop Loss at <code>$${trade.sl.toFixed(decs)}</code> (exit <code>$${(trade.exitPrice ?? trade.sl).toFixed(decs)}</code>), invalidating the ${trade.type} setup.`);

    if (trade.partialClosed) {
      const realized = trade.realizedPnL || 0;
      lines.push(`β€’ <b>Partial Banked First:</b> TP1 was reached β€” 70% closed for <code>+$${realized.toFixed(2)}</code> before the runner was stopped, so this was not a full-risk loss.`);
    }

    // Real warning signals the exit analyser flagged before the stop (from analyzeExit).
    if (Array.isArray(trade.warnings) && trade.warnings.length > 0) {
      lines.push(`β€’ <b>Warning signs flagged before the stop:</b>`);
      for (const w of trade.warnings) lines.push(`   – ${w}`);
    } else {
      lines.push(`β€’ <b>No reversal warnings were flagged</b> before the stop β€” price simply traded to the predefined invalidation level.`);
    }

    return lines.join('\n');
  }

  _closeTrade(trade, reason, exitPriceOverride = null) {
    if (trade.status === 'closed') return; // Double close safety lock guard
    trade.status = 'closed';
    trade.closeReason = reason;
    trade.closeTime = Date.now();
    // Fill at the exact SL/TP level when provided, so realized P&L matches the
    // intended risk instead of an overshooting live tick.
    trade.exitPrice = (exitPriceOverride !== null) ? exitPriceOverride : trade.currentPrice;

    // Record close time to trigger 10-minute cooldown
    this.lastClosedTime[trade.symbol] = Date.now();
    this._saveLastClosedTime();

    // Credit/debit ONLY the remaining portion P&L to balance
    const remainingPnL = this._calculatePnL(trade, trade.exitPrice);
    this.accountBalance += remainingPnL;
    this._saveAccountBalance();

    // Final P&L = realized partial + remaining portion, NET of commission (so stats,
    // win/loss counts and the Telegram "Net Realized P&L" are actually net).
    trade.pnl = (trade.realizedPnL || 0) + remainingPnL - (trade.commission || 0);

    if (trade.pnl < 0) {
      this.dailyLosses++;
      this._saveDailyLosses();
      if (this.onDailyLossUpdate) {
        this.onDailyLossUpdate(this.dailyLosses);
      }
    }

    this.tradeHistory.push({ ...trade });
    this.activeTrades = this.activeTrades.filter(t => t.id !== trade.id);
    this._saveActiveTrades();
    this._saveTradeHistory();

    // Send Telegram Exit Notification
    const decs = this._getDecimals(trade.symbol);
    const profitSign = trade.pnl >= 0 ? '+' : '';
    let postMortemHtml = '';
    
    if (reason.includes('SL Hit')) {
      postMortemHtml = `\n\nπŸ” <b>Smart Post-Mortem Audit (What went wrong?):</b>\n` + this._generatePostMortem(trade);
    }

    const header = trade.pnl >= 0
      ? `🏁 <b>TRADE CLOSED (${reason.toUpperCase()})</b>`
      : `❌ <b>TRADE CLOSED (${reason.toUpperCase()})</b>`;

    const msg = `${header}\n\n` +
      `<b>Symbol:</b> <code>${trade.symbol}</code>\n` +
      `<b>Exit Price:</b> <code>$${trade.exitPrice.toFixed(decs)}</code>\n` +
      `<b>Exit Reason:</b> <code>${trade.closeReason}</code>\n` +
      `<b>Net Realized P&amp;L:</b> <code>${profitSign}$${trade.pnl.toFixed(2)}</code>\n\n` +
      `πŸ“ˆ <b>Account Update:</b>\n` +
      `β€’ <b>New Balance:</b> <code>$${this.accountBalance.toFixed(2)}</code>\n` +
      `β€’ <b>Status:</b> Position fully liquidated. Cooldown period active for 10 minutes.${postMortemHtml}`;

    sendTelegramMessage(msg);

    this._render();
  }

  /**
   * Manually close a trade
   */
  async manualClose(tradeId) {
    if (!this.isServer) {
      try {
        const response = await fetch('/api/manual-close', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ tradeId }),
        });
        const result = await response.json();
        if (result.success) {
          console.log(`[Client TradeManager] Close requested for trade ${tradeId}.`);
          if (globalThis.syncTerminalState) await globalThis.syncTerminalState();
        }
      } catch (err) {
        console.error(`[Client TradeManager] Failed to close trade ${tradeId}:`, err);
      }
      return;
    }

    const trade = this.activeTrades.find(t => t.id === tradeId);
    if (trade) {
      this._closeTrade(trade, 'Manual Close');
    }
  }


  _calculatePnL(trade, currentPrice) {
    return this._calculatePnLForLots(trade, currentPrice, trade.lotSize);
  }

  _calculatePnLForLots(trade, currentPrice, lotSize) {
    const direction = trade.type === 'LONG' ? 1 : -1;
    const diff = (currentPrice - trade.entry) * direction;
    const sym = trade.symbol;

    if (sym === 'BTCUSDT' || sym === 'ETHUSDT') {
      return diff * lotSize;
    } else if (sym === 'XAUUSD') {
      return diff * lotSize * 100;
    } else if (sym === 'EURUSD' || sym === 'GBPUSD') {
      return (diff / 0.0001) * lotSize * 10;
    } else if (sym === 'USDCAD') {
      return (diff / 0.0001) * lotSize * (10 / currentPrice);
    }
    return 0;
  }

  _triggerPartialClose(trade, exitPrice) {
    if (trade.partialClosed) return;

    const partialLotSize = trade.lotSize * 0.7;
    const partialPnL = this._calculatePnLForLots(trade, exitPrice, partialLotSize);

    trade.realizedPnL = (trade.realizedPnL || 0) + partialPnL;

    // Credit realized partial P&L to account balance
    this.accountBalance += partialPnL;
    this._saveAccountBalance();

    // Reduce remaining lot size by 70% (leaving 30% active)
    trade.lotSize = trade.lotSize * 0.3;
    trade.partialClosed = true;
    trade.partialExitPrice = exitPrice;
    trade.partialExitTime = Date.now();

    // Trail Stop Loss to Breakeven (entry price) to secure a risk-free trade
    trade.sl = trade.entry;
    trade.slMoved = true;

    // Persist the reduced lot size + breakeven SL right away so a server restart
    // between the partial and the final close keeps the correct state.
    this._saveActiveTrades();

    trade.suggestion = 'MOVE_SL';
    trade.suggestionText = `TP1 reached β€” 70% quantity closed at $${exitPrice.toFixed(this._getDecimals(trade.symbol))} (+$${partialPnL.toFixed(2)}). SL moved to Breakeven.`;

    console.log(`[TradeManager] 70% Partial close triggered for ${trade.symbol} at $${exitPrice}: realized +$${partialPnL.toFixed(2)}. SL moved to Breakeven ($${trade.entry}).`);

    // Send Telegram TP1 Partial Close Notification
    const decs = this._getDecimals(trade.symbol);
    const msg = `πŸ’° <b>TP1 PARTIAL CLOSE REACHED</b>\n\n` +
      `<b>Symbol:</b> <code>${trade.symbol}</code>\n` +
      `<b>Target Hit:</b> TP1 reached at <code>$${exitPrice.toFixed(decs)}</code>\n` +
      `<b>Realized Profit:</b> <code>+$${partialPnL.toFixed(2)}</code>\n\n` +
      `πŸ“¦ <b>Volume Realization Details:</b>\n` +
      `β€’ <b>Closed Quantity (70%):</b> <code>${partialLotSize.toFixed(2)} lots</code>\n` +
      `β€’ <b>Remaining Quantity (30%):</b> <code>${trade.lotSize.toFixed(2)} lots</code>\n\n` +
      `πŸ›‘οΈ <b>Risk-Free Status Active:</b>\n` +
      `β€’ Stop Loss has been automatically trailed to <b>Breakeven</b> (<code>$${trade.entry.toFixed(decs)}</code>).\n` +
      `β€’ Maximum risk on this position is now <code>$0.00</code>.`;

    sendTelegramMessage(msg);
  }

  
  _render() {
    if (this.isServer) return;
    const list = document.getElementById('trade-list');
    if (!list) return;

    if (this.activeTrades.length === 0) {
      list.innerHTML = `
        <div class="empty-state">
          <span class="empty-icon">🎯</span>
          <p>No active trades</p>
        </div>`;
      return;
    }

    list.innerHTML = this.activeTrades.map(trade => {
      const isPositive = trade.pnl >= 0;
      const decimals = this._getDecimals(trade.symbol);

      return `
        <div class="trade-card" id="trade-${trade.id}">
          <div class="signal-header">
            <span class="signal-direction ${trade.type.toLowerCase()}">
              ${trade.type} ${trade.symbol}
              ${trade.partialClosed ? '<span class="partial-badge" style="font-size: 0.62rem; padding: 1px 4px; background: rgba(59, 130, 246, 0.2); color: var(--accent-cyan); border-radius: 4px; margin-left: 6px; border: 1px solid rgba(59, 130, 246, 0.3);">70% exit</span>' : ''}
              <span class="signal-quality grade-${(trade.quality || 'A').toLowerCase()}" style="font-size: 0.6rem; border-width: 1px; width: 15px; height: 15px; margin-left: 6px; display: inline-flex; vertical-align: middle; align-items: center; justify-content: center;">${trade.quality || 'A'}</span>
            </span>
            <span class="trade-pnl ${isPositive ? 'positive' : 'negative'}">
              ${isPositive ? '+' : ''}$${trade.pnl.toFixed(2)}
            </span>
          </div>
          <div class="signal-levels" style="margin-top: 8px;">
            <div class="signal-level">
              <span class="signal-level-label">Entry</span>
              <span class="signal-level-value entry">${trade.entry.toFixed(decimals)}</span>
            </div>
            <div class="signal-level">
              <span class="signal-level-label">Current</span>
              <span class="signal-level-value" style="color: ${isPositive ? 'var(--bullish)' : 'var(--bearish)'}">
                ${trade.currentPrice.toFixed(decimals)}
              </span>
            </div>
            <div class="signal-level">
              <span class="signal-level-label">SL</span>
              <span class="signal-level-value sl">${trade.sl.toFixed(decimals)}</span>
            </div>
            <div class="signal-level">
              <span class="signal-level-label">TP2</span>
              <span class="signal-level-value tp">${trade.tp2.toFixed(decimals)}</span>
            </div>
            <div class="signal-level">
              <span class="signal-level-label">R:R</span>
              <span class="signal-level-value" style="color: var(--accent-cyan)">${trade.currentRR.toFixed(2)}</span>
            </div>
            <div class="signal-level">
              <span class="signal-level-label">Lots</span>
              <span class="signal-level-value lots">${trade.lotSize.toFixed(2)}</span>
            </div>
          </div>
          ${trade.suggestion ? `
            <div class="trade-suggestion ${this._suggestionClass(trade.suggestion)}">
              πŸ’‘ ${trade.suggestionText || trade.suggestion}
            </div>
          ` : ''}
          ${trade.warnings && trade.warnings.length > 0 ? `
            <div class="trade-suggestion warning">
              ⚠️ ${trade.warnings[0]}
            </div>
          ` : ''}
          <div class="signal-actions" style="margin-top: 8px;">
            <button class="btn btn-danger btn-sm btn-close-trade" data-trade-id="${trade.id}">
              Close Trade
            </button>
          </div>
        </div>
      `;
    }).join('');

    // Attach close listeners
    list.querySelectorAll('.btn-close-trade').forEach(btn => {
      btn.addEventListener('click', () => {
        this.manualClose(parseInt(btn.dataset.tradeId));
      });
    });
  }

  _suggestionClass(suggestion) {
    switch (suggestion) {
      case 'HOLD': return 'hold';
      case 'PARTIAL_CLOSE': return 'partial';
      case 'CLOSE': return 'close';
      case 'MOVE_SL': return 'hold';
      default: return 'hold';
    }
  }

  _getDecimals(symbol) {
    if (symbol === 'XAUUSD') return 2;
    if (symbol === 'BTCUSDT' || symbol === 'ETHUSDT') return 2;
    return 5;
  }

  getDailyLosses() {
    return this.dailyLosses;
  }

  shouldStopTrading() {
    this._checkDailyReset();
    return false; // Removed 3 daily losses limit - trade anytime
  }

  _checkDailyReset() {
    const today = new Date().toDateString();
    if (this.isServer) {
      if (this.lastResetDate !== today) {
        console.log(`[Server TradeManager] Midnight system date rollover detected. Resetting losses count to 0.`);
        this.dailyLosses = 0;
        this.lastResetDate = today;
        this._saveDailyLosses();
        if (this.onDailyLossUpdate) {
          this.onDailyLossUpdate(0);
        }
      }
      return;
    }

    const stored = localStorage.getItem('tradeAnalyzer_dailyLosses');
    let storedDate = today;
    if (stored) {
      const data = JSON.parse(stored);
      storedDate = data.date;
    }

    if (storedDate !== today) {
      console.log(`[TradeManager] Midnight system date rollover detected. Resetting losses count to 0.`);
      this.dailyLosses = 0;
      this._saveDailyLosses();
      if (this.onDailyLossUpdate) {
        this.onDailyLossUpdate(0);
      }
      const overlay = document.getElementById('daily-stop-overlay');
      if (overlay) overlay.classList.add('hidden');
    }
  }

  _loadDailyLosses() {
    if (this.isServer) return this.dailyLosses;
    const today = new Date().toDateString();
    const stored = localStorage.getItem('tradeAnalyzer_dailyLosses');
    if (stored) {
      const data = JSON.parse(stored);
      if (data.date === today) return data.count;
    }
    return 0;
  }

  _saveDailyLosses() {
    if (this.isServer) {
      if (this.db) this.db.saveDailyLosses(this.dailyLosses, this.lastResetDate);
    } else {
      localStorage.setItem('tradeAnalyzer_dailyLosses', JSON.stringify({
        date: new Date().toDateString(),
        count: this.dailyLosses,
      }));
    }
  }

  resetDailyLosses() {
    this.dailyLosses = 0;
    this._saveDailyLosses();
    if (!this.isServer && this.onDailyLossUpdate) {
      this.onDailyLossUpdate(0);
    }
  }

  _loadActiveTrades() {
    if (this.isServer) return this.activeTrades;
    const stored = localStorage.getItem('tradeAnalyzer_activeTrades');
    return stored ? JSON.parse(stored) : [];
  }

  _saveActiveTrades() {
    if (this.isServer) {
      if (this.db) this.db.saveActiveTrades(this.activeTrades);
    } else {
      localStorage.setItem('tradeAnalyzer_activeTrades', JSON.stringify(this.activeTrades));
    }
  }

  _loadTradeHistory() {
    if (this.isServer) return this.tradeHistory;
    const stored = localStorage.getItem('tradeAnalyzer_tradeHistory');
    return stored ? JSON.parse(stored) : [];
  }

  _saveTradeHistory() {
    if (this.isServer) {
      if (this.db) this.db.saveTradeHistory(this.tradeHistory);
    } else {
      localStorage.setItem('tradeAnalyzer_tradeHistory', JSON.stringify(this.tradeHistory));
    }
  }

  getSymbolStats() {
    const stats = {};
    const symbols = ['BTCUSDT', 'XAUUSD', 'GBPUSD', 'USDCAD'];
    for (const sym of symbols) {
      stats[sym] = {
        total: 0,
        wins: 0,
        losses: 0,
        winRate: 0,
        profit: 0,
        grossProfit: 0,
        grossLoss: 0,
        profitFactor: 0,
      };
    }

    for (const trade of this.tradeHistory) {
      const sym = trade.symbol;
      if (!stats[sym]) continue;

      stats[sym].total++;
      stats[sym].profit += trade.pnl;

      if (trade.pnl >= 0) {
        stats[sym].wins++;
        stats[sym].grossProfit += trade.pnl;
      } else {
        stats[sym].losses++;
        stats[sym].grossLoss += Math.abs(trade.pnl);
      }
    }

    for (const sym of symbols) {
      const s = stats[sym];
      s.winRate = s.total > 0 ? (s.wins / s.total) * 100 : 0;
      s.profitFactor = s.grossLoss > 0 ? s.grossProfit / s.grossLoss : s.grossProfit > 0 ? 99.9 : 0;
    }

    return stats;
  }
  _loadAccountBalance() {
    if (this.isServer) return this.accountBalance;
    const stored = localStorage.getItem('tradeAnalyzer_accountBalance');
    if (stored) {
      const val = parseFloat(stored);
      if (!isNaN(val)) return val;
    }
    return 5000.0;
  }

  _saveAccountBalance() {
    if (this.isServer) {
      if (this.db) this.db.saveAccountBalance(this.accountBalance);
    } else {
      localStorage.setItem('tradeAnalyzer_accountBalance', this.accountBalance.toString());
    }
  }

  getAccountSummary() {
    let unrealizedPnL = 0;
    for (const trade of this.activeTrades) {
      unrealizedPnL += this._calculatePnL(trade, trade.currentPrice);
    }
    return {
      balance: this.accountBalance,
      equity: this.accountBalance + unrealizedPnL,
    };
  }

  async resetAccount() {
    if (!this.isServer) {
      try {
        const response = await fetch('/api/reset-account', { method: 'POST' });
        const result = await response.json();
        if (result.success) {
          console.log('[Client TradeManager] Account reset successfully on server.');
          if (globalThis.syncTerminalState) await globalThis.syncTerminalState();
        }
      } catch (err) {
        console.error('[Client TradeManager] Failed to reset account on server:', err);
      }
      return;
    }

    this.accountBalance = 5000.0;
    this._saveAccountBalance();
    this.resetDailyLosses();
    this.activeTrades = [];
    this._saveActiveTrades();
    this.tradeHistory = [];
    this._saveTradeHistory();
    
    // Clear cooldowns and executions on reset
    this.lastExecutedCandleTime = {};
    this.lastClosedTime = {};
    this._saveLastExecutedCandleTime();
    this._saveLastClosedTime();
  }

  wasAlreadyExecuted(symbol, candleTime) {
    return this.lastExecutedCandleTime[symbol] === candleTime;
  }

  isSymbolCoolingDown(symbol) {
    const closedTime = this.lastClosedTime[symbol];
    if (!closedTime) return false;
    const elapsed = Date.now() - closedTime;
    return elapsed < 10 * 60 * 1000; // 10 minutes cooldown
  }

  _loadLastExecutedCandleTime() {
    if (this.isServer) return this.lastExecutedCandleTime;
    const stored = localStorage.getItem('tradeAnalyzer_lastExecutedCandleTime');
    return stored ? JSON.parse(stored) : {};
  }

  _saveLastExecutedCandleTime() {
    if (this.isServer) {
      if (this.db) this.db.saveLastExecutedCandleTime(this.lastExecutedCandleTime);
    } else {
      localStorage.setItem('tradeAnalyzer_lastExecutedCandleTime', JSON.stringify(this.lastExecutedCandleTime));
    }
  }

  _loadLastClosedTime() {
    if (this.isServer) return this.lastClosedTime;
    const stored = localStorage.getItem('tradeAnalyzer_lastClosedTime');
    return stored ? JSON.parse(stored) : {};
  }

  _saveLastClosedTime() {
    if (this.isServer) {
      if (this.db) this.db.saveLastClosedTime(this.lastClosedTime);
    } else {
      localStorage.setItem('tradeAnalyzer_lastClosedTime', JSON.stringify(this.lastClosedTime));
    }
  }
}

export default TradeManager;