File size: 46,466 Bytes
b190b45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
/**

 * Advanced Technical Analysis Page

 * TradingView-like features with harmonic patterns, Elliott Wave, etc.

 */

import { apiClient } from '/static/shared/js/core/api-client.js';
import { logger } from '../../shared/js/utils/logger.js';
import { escapeHtml, safeFormatNumber, safeFormatCurrency } from '../../shared/js/utils/sanitizer.js';

class TechnicalAnalysisPage {
  constructor() {
    this.symbol = 'BTC';
    this.timeframe = '4h'; // Default for TA_QUICK
    this.currentMode = 'TA_QUICK';
    this.chart = null;
    this.candlestickSeries = null;
    this.volumeSeries = null;
    this.rsiSeries = null;
    this.macdSeries = null;
    this.trendLineSeries = null;
    this.supportLineSeries = null;
    this.resistanceLineSeries = null;
    this.fibonacciLevels = [];
    this.indicators = {
      rsi: true,
      macd: true,
      volume: false,
      ichimoku: false,
      elliott: false
    };
    this.patterns = {
      gartley: true,
      butterfly: true,
      bat: true,
      crab: true,
      candlestick: true
    };
    this.ohlcvData = [];
    this.analysisData = null;
    this.fundamentalData = null;
    this.onchainData = null;
    this.riskData = null;
    this.retryConfig = {
      maxRetries: 3,
      baseDelay: 1000,
      maxDelay: 5000
    };
  }

  async init() {
    try {
      console.log('[TechnicalAnalysis] Initializing...');
      this.bindEvents();
      await this.loadChart();
      await this.analyze();
      console.log('[TechnicalAnalysis] Ready');
    } catch (error) {
      logger.error('TechnicalAnalysis', 'Init error:', error);
    }
  }

  bindEvents() {
    // Mode tabs
    document.querySelectorAll('.mode-tab').forEach(tab => {
      tab.addEventListener('click', (e) => {
        const mode = e.currentTarget.dataset.mode;
        this.switchMode(mode);
      });
    });

    // Symbol input
    document.getElementById('symbol-input')?.addEventListener('change', (e) => {
      this.symbol = e.target.value.toUpperCase();
      this.runCurrentModeAnalysis();
    });

    // Timeframe select
    document.getElementById('timeframe-select')?.addEventListener('change', (e) => {
      this.timeframe = e.target.value;
      this.runCurrentModeAnalysis();
    });

    // Indicator checkboxes
    Object.keys(this.indicators).forEach(key => {
      const checkbox = document.getElementById(`indicator-${key}`);
      if (checkbox) {
        checkbox.addEventListener('change', (e) => {
          this.indicators[key] = e.target.checked;
          this.updateChart();
        });
      }
    });

    // Pattern checkboxes
    Object.keys(this.patterns).forEach(key => {
      const checkbox = document.getElementById(`pattern-${key}`);
      if (checkbox) {
        checkbox.addEventListener('change', (e) => {
          this.patterns[key] = e.target.checked;
          this.analyze();
        });
      }
    });

    // Analyze button
    document.getElementById('analyze-btn')?.addEventListener('click', () => {
      this.analyze();
    });

    // Chart controls
    document.getElementById('zoom-in')?.addEventListener('click', () => {
      this.chart?.timeScale().zoomIn();
    });
    document.getElementById('zoom-out')?.addEventListener('click', () => {
      this.chart?.timeScale().zoomOut();
    });
    document.getElementById('reset-chart')?.addEventListener('click', () => {
      this.chart?.timeScale().fitContent();
    });
  }

  async loadChart() {
    const container = document.getElementById('tradingview-chart');
    if (!container) return;

    // Create chart
    if (!window.LightweightCharts) {
      throw new Error('LightweightCharts library not loaded');
    }
    this.chart = window.LightweightCharts.createChart(container, {
      width: container.clientWidth,
      height: 600,
      layout: {
        background: { color: '#0f172a' },
        textColor: '#94a3b8',
      },
      grid: {
        vertLines: { color: '#1e293b' },
        horzLines: { color: '#1e293b' },
      },
      timeScale: {
        timeVisible: true,
        secondsVisible: false,
      },
    });

    // Create candlestick series with fallback for different library versions
    const seriesOptions = {
      upColor: '#22c55e',
      downColor: '#ef4444',
      borderVisible: false,
      wickUpColor: '#22c55e',
      wickDownColor: '#ef4444',
    };

    // Try multiple methods for compatibility
    if (typeof this.chart.addCandlestickSeries === 'function') {
      this.candlestickSeries = this.chart.addCandlestickSeries(seriesOptions);
    } else if (typeof this.chart.addSeries === 'function' && window.LightweightCharts && window.LightweightCharts.SeriesType && window.LightweightCharts.SeriesType.Candlestick) {
      this.candlestickSeries = this.chart.addSeries(window.LightweightCharts.SeriesType.Candlestick, seriesOptions);
    } else if (typeof this.chart.addSeries === 'function') {
      try {
        this.candlestickSeries = this.chart.addSeries('Candlestick', seriesOptions);
      } catch (e) {
        console.error('Failed to create candlestick series:', e);
        throw new Error('Could not create candlestick series');
      }
    } else {
      throw new Error('No compatible method to create candlestick series found');
    }

    if (!this.candlestickSeries) {
      throw new Error('Failed to create candlestick series');
    }

    // Create volume series (if enabled)
    if (this.indicators.volume) {
      this.volumeSeries = this.chart.addHistogramSeries({
        color: '#3b82f6',
        priceFormat: {
          type: 'volume',
        },
        priceScaleId: '',
        scaleMargins: {
          top: 0.8,
          bottom: 0,
        },
      });
    }
  }

  async analyze() {
    try {
      // Fetch OHLCV data with retry logic
      let response;
      let retries = 0;
      const maxRetries = 2;
      
      while (retries <= maxRetries) {
        try {
          // Use relative URL
          const url = `/api/ohlcv?symbol=${encodeURIComponent(this.symbol)}&timeframe=${encodeURIComponent(this.timeframe)}&limit=500`;
          response = await fetch(url, {
            signal: AbortSignal.timeout(15000)
          });

          if (response.ok) {
            break;
          }
          
          if (retries < maxRetries && response.status >= 500) {
            const delay = Math.min(1000 * Math.pow(2, retries), 5000);
            await this.delay(delay);
            retries++;
            continue;
          }
          
          throw new Error(`Failed to fetch OHLCV data: HTTP ${response.status}`);
        } catch (error) {
          if (retries < maxRetries && (error.message.includes('timeout') || error.message.includes('network'))) {
            const delay = Math.min(1000 * Math.pow(2, retries), 5000);
            await this.delay(delay);
            retries++;
            continue;
          }
          throw error;
        }
      }

      if (!response || !response.ok) {
        throw new Error('Failed to fetch OHLCV data after retries');
      }

      const data = await response.json();
      if (!data || typeof data !== 'object') {
        throw new Error('Invalid response format');
      }
      
      // Handle error responses
      if (data.success === false || data.error === true) {
        throw new Error(data.message || 'Failed to fetch OHLCV data');
      }
      
      // Validate data structure
      const ohlcvData = data.data || data.ohlcv || [];
      if (!Array.isArray(ohlcvData) || ohlcvData.length === 0) {
        throw new Error('No OHLCV data available');
      }
      
      // Validate first candle has required fields
      const firstCandle = ohlcvData[0];
      if (!firstCandle || (typeof firstCandle.open === 'undefined' && typeof firstCandle.o === 'undefined')) {
        throw new Error('Invalid OHLCV data structure - missing required fields');
      }

      this.ohlcvData = ohlcvData;

      // Fetch technical analysis with error handling
      let analysisResponse;
      try {
        analysisResponse = await apiClient.fetch(
          '/api/technical/analyze',
          {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
              symbol: this.symbol,
              timeframe: this.timeframe,
              ohlcv: this.ohlcvData,
              indicators: this.indicators,
              patterns: this.patterns
            })
          },
          20000
        );

        if (analysisResponse.ok) {
          const analysisJson = await analysisResponse.json();
          if (analysisJson && typeof analysisJson === 'object') {
            this.analysisData = analysisJson;
          } else {
            throw new Error('Invalid analysis response format');
          }
        } else {
          // Fallback: calculate locally
          logger.warn('TechnicalAnalysis', `Analysis API returned ${analysisResponse.status}, using local calculation`);
          this.analysisData = this.calculateTechnicalAnalysis();
        }
      } catch (error) {
        logger.warn('TechnicalAnalysis', 'Analysis API error, using local calculation:', error);
        // Fallback: calculate locally
        this.analysisData = this.calculateTechnicalAnalysis();
      }

      this.updateChart();
      this.renderAnalysis();
    } catch (error) {
      logger.error('TechnicalAnalysis', 'Analysis error:', error);
      this.showError('Failed to load analysis. Using fallback calculations.');
      this.analysisData = this.calculateTechnicalAnalysis();
      this.updateChart();
      this.renderAnalysis();
    }
  }

  calculateTechnicalAnalysis() {
    // Fallback local calculations
    return {
      support_resistance: this.calculateSupportResistance(),
      harmonic_patterns: this.detectHarmonicPatterns(),
      elliott_wave: this.analyzeElliottWave(),
      candlestick_patterns: this.detectCandlestickPatterns(),
      indicators: this.calculateIndicators(),
      signals: this.generateSignals()
    };
  }

  calculateSupportResistance() {
    const closes = this.ohlcvData.map(c => parseFloat(c.c || c.close));
    const highs = this.ohlcvData.map(c => parseFloat(c.h || c.high));
    const lows = this.ohlcvData.map(c => parseFloat(c.l || c.low));

    // Pivot-based calculation
    const pivots = this.findPivotPoints(highs, lows, closes);
    
    return {
      support: pivots.support,
      resistance: pivots.resistance,
      levels: pivots.levels
    };
  }

  findPivotPoints(highs, lows, closes, period = 5) {
    const pivotHighs = [];
    const pivotLows = [];
    const levels = [];

    for (let i = period; i < highs.length - period; i++) {
      // Pivot High
      let isPivotHigh = true;
      for (let j = i - period; j <= i + period; j++) {
        if (j !== i && highs[j] >= highs[i]) {
          isPivotHigh = false;
          break;
        }
      }
      if (isPivotHigh) {
        pivotHighs.push({ index: i, value: highs[i] });
        levels.push({ type: 'resistance', value: highs[i], strength: this.calculateLevelStrength(highs[i], highs) });
      }

      // Pivot Low
      let isPivotLow = true;
      for (let j = i - period; j <= i + period; j++) {
        if (j !== i && lows[j] <= lows[i]) {
          isPivotLow = false;
          break;
        }
      }
      if (isPivotLow) {
        pivotLows.push({ index: i, value: lows[i] });
        levels.push({ type: 'support', value: lows[i], strength: this.calculateLevelStrength(lows[i], lows) });
      }
    }

    // Get strongest levels
    const support = pivotLows.length > 0 
      ? pivotLows.sort((a, b) => a.value - b.value)[0].value 
      : Math.min(...lows.slice(-50));
    
    const resistance = pivotHighs.length > 0
      ? pivotHighs.sort((a, b) => b.value - a.value)[0].value
      : Math.max(...highs.slice(-50));

    return { support, resistance, levels: levels.slice(-10) };
  }

  calculateLevelStrength(level, prices) {
    const touches = prices.filter(p => Math.abs(p - level) / level < 0.01).length;
    return Math.min(touches / 3, 1);
  }

  detectHarmonicPatterns() {
    const patterns = [];
    const closes = this.ohlcvData.map(c => parseFloat(c.c || c.close));
    
    // Gartley Pattern
    const gartley = this.detectGartley(closes);
    if (gartley) patterns.push(gartley);

    // Butterfly Pattern
    const butterfly = this.detectButterfly(closes);
    if (butterfly) patterns.push(butterfly);

    // Bat Pattern
    const bat = this.detectBat(closes);
    if (bat) patterns.push(bat);

    // Crab Pattern
    const crab = this.detectCrab(closes);
    if (crab) patterns.push(crab);

    return patterns;
  }

  detectGartley(prices) {
    // Simplified Gartley detection
    if (prices.length < 5) return null;
    
    const X = prices[prices.length - 5];
    const A = prices[prices.length - 4];
    const B = prices[prices.length - 3];
    const C = prices[prices.length - 2];
    const D = prices[prices.length - 1];

    const AB = Math.abs((B - A) / (A - X));
    const BC = Math.abs((C - B) / (B - A));
    const CD = Math.abs((D - C) / (C - B));

    // Gartley ratios: AB ~ 0.618, BC ~ 0.382-0.886, CD ~ 0.786
    if (Math.abs(AB - 0.618) < 0.1 && 
        BC > 0.3 && BC < 0.9 && 
        Math.abs(CD - 0.786) < 0.1) {
      return {
        type: 'Gartley',
        pattern: 'Bullish',
        confidence: 0.75,
        points: { X, A, B, C, D }
      };
    }
    return null;
  }

  detectButterfly(prices) {
    if (prices.length < 5) return null;
    
    const X = prices[prices.length - 5];
    const A = prices[prices.length - 4];
    const B = prices[prices.length - 3];
    const C = prices[prices.length - 2];
    const D = prices[prices.length - 1];

    const AB = Math.abs((B - A) / (A - X));
    const BC = Math.abs((C - B) / (B - A));
    const CD = Math.abs((D - C) / (C - B));

    // Butterfly ratios: AB ~ 0.786, BC ~ 0.382-0.886, CD ~ 1.27-1.618
    if (Math.abs(AB - 0.786) < 0.1 && 
        BC > 0.3 && BC < 0.9 && 
        CD > 1.2 && CD < 1.7) {
      return {
        type: 'Butterfly',
        pattern: 'Bearish',
        confidence: 0.70,
        points: { X, A, B, C, D }
      };
    }
    return null;
  }

  detectBat(prices) {
    if (prices.length < 5) return null;
    
    const X = prices[prices.length - 5];
    const A = prices[prices.length - 4];
    const B = prices[prices.length - 3];
    const C = prices[prices.length - 2];
    const D = prices[prices.length - 1];

    const AB = Math.abs((B - A) / (A - X));
    const BC = Math.abs((C - B) / (B - A));
    const CD = Math.abs((D - C) / (C - B));

    // Bat ratios: AB ~ 0.382-0.5, BC ~ 0.382-0.886, CD ~ 0.886
    if (AB > 0.3 && AB < 0.55 && 
        BC > 0.3 && BC < 0.9 && 
        Math.abs(CD - 0.886) < 0.1) {
      return {
        type: 'Bat',
        pattern: 'Bullish',
        confidence: 0.72,
        points: { X, A, B, C, D }
      };
    }
    return null;
  }

  detectCrab(prices) {
    if (prices.length < 5) return null;
    
    const X = prices[prices.length - 5];
    const A = prices[prices.length - 4];
    const B = prices[prices.length - 3];
    const C = prices[prices.length - 2];
    const D = prices[prices.length - 1];

    const AB = Math.abs((B - A) / (A - X));
    const BC = Math.abs((C - B) / (B - A));
    const CD = Math.abs((D - C) / (C - B));

    // Crab ratios: AB ~ 0.382-0.618, BC ~ 0.382-0.886, CD ~ 1.618
    if (AB > 0.3 && AB < 0.65 && 
        BC > 0.3 && BC < 0.9 && 
        Math.abs(CD - 1.618) < 0.15) {
      return {
        type: 'Crab',
        pattern: 'Bearish',
        confidence: 0.68,
        points: { X, A, B, C, D }
      };
    }
    return null;
  }

  analyzeElliottWave() {
    const closes = this.ohlcvData.map(c => parseFloat(c.c || c.close));
    if (closes.length < 34) return null;

    // Simplified Elliott Wave analysis
    const waves = this.identifyWaves(closes);
    return {
      wave_count: waves.length,
      current_wave: waves[waves.length - 1],
      pattern: this.determineElliottPattern(waves),
      target: this.calculateElliottTarget(waves)
    };
  }

  identifyWaves(prices) {
    const waves = [];
    let direction = null;
    let startIdx = 0;

    for (let i = 1; i < prices.length; i++) {
      const change = prices[i] - prices[i - 1];
      const currentDir = change > 0 ? 'up' : 'down';

      if (direction === null) {
        direction = currentDir;
      } else if (direction !== currentDir) {
        waves.push({
          direction,
          start: startIdx,
          end: i - 1,
          magnitude: Math.abs(prices[i - 1] - prices[startIdx])
        });
        startIdx = i - 1;
        direction = currentDir;
      }
    }

    return waves;
  }

  determineElliottPattern(waves) {
    if (waves.length < 5) return 'Incomplete';
    
    // Check for 5-wave impulse pattern
    const impulse = waves.slice(-5);
    if (impulse.length === 5) {
      const wave3 = impulse[2];
      const wave1 = impulse[0];
      
      // Wave 3 should be the longest
      if (wave3.magnitude > wave1.magnitude * 1.618) {
        return 'Impulse Wave (5-3-5-3-5)';
      }
    }
    
    return 'Corrective Wave';
  }

  calculateElliottTarget(waves) {
    if (waves.length < 3) return null;
    
    const lastWave = waves[waves.length - 1];
    const prevWave = waves[waves.length - 2];
    
    // Fibonacci extension target
    const target = lastWave.magnitude * 1.618;
    return {
      price: target,
      type: lastWave.direction === 'up' ? 'resistance' : 'support'
    };
  }

  detectCandlestickPatterns() {
    const patterns = [];
    
    for (let i = 4; i < this.ohlcvData.length; i++) {
      const candles = this.ohlcvData.slice(i - 4, i + 1);
      
      // Doji
      if (this.isDoji(candles[candles.length - 1])) {
        patterns.push({ type: 'Doji', index: i, signal: 'Reversal' });
      }
      
      // Hammer
      if (this.isHammer(candles[candles.length - 1])) {
        patterns.push({ type: 'Hammer', index: i, signal: 'Bullish' });
      }
      
      // Engulfing
      const engulfing = this.isEngulfing(candles[candles.length - 2], candles[candles.length - 1]);
      if (engulfing) {
        patterns.push({ type: engulfing, index: i, signal: engulfing.includes('Bullish') ? 'Bullish' : 'Bearish' });
      }
    }

    return patterns.slice(-10);
  }

  isDoji(candle) {
    const body = Math.abs(parseFloat(candle.c || candle.close) - parseFloat(candle.o || candle.open));
    const range = parseFloat(candle.h || candle.high) - parseFloat(candle.l || candle.low);
    return body / range < 0.1 && range > 0;
  }

  isHammer(candle) {
    const body = Math.abs(parseFloat(candle.c || candle.close) - parseFloat(candle.o || candle.open));
    const lowerShadow = Math.min(parseFloat(candle.c || candle.close), parseFloat(candle.o || candle.open)) - parseFloat(candle.l || candle.low);
    const upperShadow = parseFloat(candle.h || candle.high) - Math.max(parseFloat(candle.c || candle.close), parseFloat(candle.o || candle.open));
    return lowerShadow > body * 2 && upperShadow < body * 0.5;
  }

  isEngulfing(prevCandle, currentCandle) {
    const prevBody = Math.abs(parseFloat(prevCandle.c || prevCandle.close) - parseFloat(prevCandle.o || prevCandle.open));
    const currBody = Math.abs(parseFloat(currentCandle.c || currentCandle.close) - parseFloat(currentCandle.o || currentCandle.open));
    
    const prevBullish = parseFloat(prevCandle.c || prevCandle.close) > parseFloat(prevCandle.o || prevCandle.open);
    const currBullish = parseFloat(currentCandle.c || currentCandle.close) > parseFloat(currentCandle.o || currentCandle.open);
    
    if (currBody > prevBody * 1.5) {
      if (!prevBullish && currBullish) {
        return 'Bullish Engulfing';
      } else if (prevBullish && !currBullish) {
        return 'Bearish Engulfing';
      }
    }
    return null;
  }

  calculateIndicators() {
    const closes = this.ohlcvData.map(c => parseFloat(c.c || c.close));
    const volumes = this.ohlcvData.map(c => parseFloat(c.v || c.volume || 0));

    return {
      rsi: this.calculateRSI(closes),
      macd: this.calculateMACD(closes),
      ichimoku: this.calculateIchimoku(this.ohlcvData),
      sma20: this.calculateSMA(closes, 20),
      sma50: this.calculateSMA(closes, 50),
      volume_avg: volumes.length > 0 ? volumes.reduce((a, b) => a + b, 0) / volumes.length : 0
    };
  }

  calculateRSI(prices, period = 14) {
    if (prices.length < period + 1) return null;
    
    const deltas = [];
    for (let i = 1; i < prices.length; i++) {
      deltas.push(prices[i] - prices[i - 1]);
    }
    
    const gains = deltas.slice(-period).filter(d => d > 0);
    const losses = deltas.slice(-period).filter(d => d < 0).map(d => Math.abs(d));
    
    const avgGain = gains.length > 0 ? gains.reduce((a, b) => a + b, 0) / period : 0;
    const avgLoss = losses.length > 0 ? losses.reduce((a, b) => a + b, 0) / period : 0;
    
    if (avgLoss === 0) return avgGain > 0 ? 100 : 50;
    
    const rs = avgGain / avgLoss;
    return 100 - (100 / (1 + rs));
  }

  calculateMACD(prices, fast = 12, slow = 26, signal = 9) {
    if (prices.length < slow + signal) return null;
    
    const emaFast = this.calculateEMA(prices, fast);
    const emaSlow = this.calculateEMA(prices, slow);
    
    if (!emaFast || !emaSlow) return null;
    
    const macdLine = emaFast - emaSlow;
    const signalLine = this.calculateEMA([macdLine], signal);
    
    return {
      macd: macdLine,
      signal: signalLine,
      histogram: macdLine - signalLine
    };
  }

  calculateEMA(prices, period) {
    if (prices.length < period) return null;
    
    const multiplier = 2 / (period + 1);
    let ema = prices.slice(0, period).reduce((a, b) => a + b, 0) / period;
    
    for (let i = period; i < prices.length; i++) {
      ema = (prices[i] - ema) * multiplier + ema;
    }
    
    return ema;
  }

  calculateSMA(prices, period) {
    if (prices.length < period) return null;
    return prices.slice(-period).reduce((a, b) => a + b, 0) / period;
  }

  calculateIchimoku(ohlcv) {
    if (ohlcv.length < 52) return null;
    
    const closes = ohlcv.map(c => parseFloat(c.c || c.close));
    const highs = ohlcv.map(c => parseFloat(c.h || c.high));
    const lows = ohlcv.map(c => parseFloat(c.l || c.low));
    
    const tenkan = (Math.max(...highs.slice(-9)) + Math.min(...lows.slice(-9))) / 2;
    const kijun = (Math.max(...highs.slice(-26)) + Math.min(...lows.slice(-26))) / 2;
    const senkouA = (tenkan + kijun) / 2;
    const senkouB = (Math.max(...highs.slice(-52)) + Math.min(...lows.slice(-52))) / 2;
    const chikou = closes[closes.length - 26];
    
    return {
      tenkan,
      kijun,
      senkouA,
      senkouB,
      chikou,
      cloud: senkouA > senkouB ? 'bullish' : 'bearish'
    };
  }

  generateSignals() {
    const indicators = this.calculateIndicators();
    const signals = [];
    
    // RSI signals
    if (indicators.rsi) {
      if (indicators.rsi < 30) {
        signals.push({ type: 'BUY', source: 'RSI Oversold', strength: 'Strong' });
      } else if (indicators.rsi > 70) {
        signals.push({ type: 'SELL', source: 'RSI Overbought', strength: 'Strong' });
      }
    }
    
    // MACD signals
    if (indicators.macd) {
      if (indicators.macd.histogram > 0 && indicators.macd.macd > indicators.macd.signal) {
        signals.push({ type: 'BUY', source: 'MACD Bullish Crossover', strength: 'Medium' });
      } else if (indicators.macd.histogram < 0 && indicators.macd.macd < indicators.macd.signal) {
        signals.push({ type: 'SELL', source: 'MACD Bearish Crossover', strength: 'Medium' });
      }
    }
    
    // Support/Resistance signals
    const sr = this.calculateSupportResistance();
    const lastClose = parseFloat(this.ohlcvData[this.ohlcvData.length - 1].c || this.ohlcvData[this.ohlcvData.length - 1].close);
    
    if (sr.support && lastClose <= sr.support * 1.02) {
      signals.push({ type: 'BUY', source: 'Near Support Level', strength: 'Medium' });
    }
    
    if (sr.resistance && lastClose >= sr.resistance * 0.98) {
      signals.push({ type: 'SELL', source: 'Near Resistance Level', strength: 'Medium' });
    }
    
    return signals;
  }

  updateChart() {
    if (!this.chart || !this.candlestickSeries) {
      // Try to reload chart if not initialized
      this.loadChart();
      return;
    }

    if (!this.ohlcvData || this.ohlcvData.length === 0) {
      logger.warn('TechnicalAnalysis', 'No OHLCV data to display');
      return;
    }

    try {
      // Format data for TradingView
      const chartData = this.ohlcvData
        .filter(candle => {
          const close = parseFloat(candle.c || candle.close || 0);
          const open = parseFloat(candle.o || candle.open || 0);
          const high = parseFloat(candle.h || candle.high || 0);
          const low = parseFloat(candle.l || candle.low || 0);
          return close > 0 && open > 0 && high > 0 && low > 0 && high >= low;
        })
        .map(candle => ({
          time: Math.floor(parseInt(candle.t || candle.openTime || Date.now()) / 1000),
          open: parseFloat(candle.o || candle.open),
          high: parseFloat(candle.h || candle.high),
          low: parseFloat(candle.l || candle.low),
          close: parseFloat(candle.c || candle.close)
        }))
        .sort((a, b) => a.time - b.time); // Ensure chronological order

      if (chartData.length === 0) {
        throw new Error('No valid chart data after filtering');
      }

      this.candlestickSeries.setData(chartData);
      this.chart.timeScale().fitContent();
      
      // Draw trend lines with animation
      this.drawTrendLines();
      
      // Draw support/resistance levels
      this.drawSupportResistance();

      // Update volume if enabled
      if (this.indicators.volume && this.volumeSeries) {
        const volumeData = this.ohlcvData.map(candle => ({
          time: Math.floor(parseInt(candle.t || candle.openTime) / 1000),
          value: parseFloat(candle.v || candle.volume || 0),
          color: parseFloat(candle.c || candle.close) >= parseFloat(candle.o || candle.open) 
            ? 'rgba(34, 197, 94, 0.5)' 
            : 'rgba(239, 68, 68, 0.5)'
        }));
        this.volumeSeries.setData(volumeData);
      }

      // Update price display with validation
      const lastCandle = this.ohlcvData[this.ohlcvData.length - 1];
      if (!lastCandle) {
        logger.warn('TechnicalAnalysis', 'No last candle available for price display');
        return;
      }
      
      const lastClose = parseFloat(lastCandle.c || lastCandle.close);
      if (isNaN(lastClose) || lastClose <= 0) {
        logger.warn('TechnicalAnalysis', 'Invalid last close price');
        return;
      }
      
      const prevClose = this.ohlcvData.length > 1 
        ? parseFloat(this.ohlcvData[this.ohlcvData.length - 2].c || this.ohlcvData[this.ohlcvData.length - 2].close)
        : lastClose;
      
      if (isNaN(prevClose) || prevClose <= 0) {
        logger.warn('TechnicalAnalysis', 'Invalid previous close price');
        return;
      }
      
      const change = prevClose !== 0 ? ((lastClose - prevClose) / prevClose) * 100 : 0;
      
      const priceEl = document.getElementById('chart-price');
      if (priceEl) {
        priceEl.textContent = safeFormatNumber(lastClose);
      }
      
      const changeEl = document.getElementById('chart-change');
      if (changeEl) {
        changeEl.textContent = `${change >= 0 ? '+' : ''}${safeFormatNumber(change, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}%`;
        changeEl.className = `change-display ${change >= 0 ? 'positive' : 'negative'}`;
      }
    } catch (error) {
      logger.error('TechnicalAnalysis', 'Chart update error:', error);
      this.showError('Failed to update chart. Please try again.');
    }
  }
  
  drawTrendLines() {
    if (!this.analysisData || !this.chart) return;
    
    try {
      // Draw trend line based on SMA
      const closes = this.ohlcvData.map(c => parseFloat(c.c || c.close)).filter(v => v > 0);
      if (closes.length < 20) return;
      
      const sma20 = this.calculateSMA(closes, 20);
      if (!sma20) return;
      
      // Create trend line series
      if (!this.trendLineSeries) {
        this.trendLineSeries = this.chart.addLineSeries({
          color: '#2dd4bf',
          lineWidth: 2,
          lineStyle: 2, // Dashed
          title: 'SMA 20'
        });
      }
      
      // Calculate SMA20 data points
      const trendData = [];
      for (let i = 19; i < this.ohlcvData.length; i++) {
        const periodCloses = closes.slice(i - 19, i + 1);
        const sma = periodCloses.reduce((a, b) => a + b, 0) / 20;
        trendData.push({
          time: Math.floor(parseInt(this.ohlcvData[i].t || this.ohlcvData[i].openTime) / 1000),
          value: sma
        });
      }
      
      this.trendLineSeries.setData(trendData);
    } catch (error) {
      logger.warn('TechnicalAnalysis', 'Failed to draw trend lines:', error);
    }
  }
  
  drawSupportResistance() {
    if (!this.analysisData || !this.analysisData.support_resistance || !this.chart) return;
    
    try {
      const { support, resistance } = this.analysisData.support_resistance;
      if (!support && !resistance) return;
      
      const lastTime = Math.floor(parseInt(this.ohlcvData[this.ohlcvData.length - 1].t || this.ohlcvData[this.ohlcvData.length - 1].openTime) / 1000);
      const firstTime = Math.floor(parseInt(this.ohlcvData[0].t || this.ohlcvData[0].openTime) / 1000);
      
      // Draw support line
      if (support && !this.supportLineSeries) {
        this.supportLineSeries = this.chart.addLineSeries({
          color: '#ef4444',
          lineWidth: 2,
          lineStyle: 2,
          title: 'Support'
        });
        this.supportLineSeries.setData([
          { time: firstTime, value: support },
          { time: lastTime, value: support }
        ]);
      }
      
      // Draw resistance line
      if (resistance && !this.resistanceLineSeries) {
        this.resistanceLineSeries = this.chart.addLineSeries({
          color: '#22c55e',
          lineWidth: 2,
          lineStyle: 2,
          title: 'Resistance'
        });
        this.resistanceLineSeries.setData([
          { time: firstTime, value: resistance },
          { time: lastTime, value: resistance }
        ]);
      }
    } catch (error) {
      logger.warn('TechnicalAnalysis', 'Failed to draw support/resistance:', error);
    }
  
  renderAnalysis() {
    if (!this.analysisData) return;

    this.renderSupportResistance();
    this.renderSignals();
    this.renderHarmonicPatterns();
    this.renderElliottWave();
    this.renderTradeRecommendations();
  }

  renderSupportResistance() {
    const container = document.getElementById('support-resistance-levels');
    if (!container || !this.analysisData || !this.analysisData.support_resistance) return;

    const { support, resistance, levels } = this.analysisData.support_resistance;
    
    // Validate levels array
    const validLevels = Array.isArray(levels) ? levels.filter(level => 
      level && typeof level === 'object' && 
      typeof level.value === 'number' && !isNaN(level.value) &&
      typeof level.strength === 'number' && !isNaN(level.strength)
    ) : [];
    
    const supportValue = (support && typeof support === 'number' && !isNaN(support)) 
      ? safeFormatNumber(support) 
      : 'β€”';
    const resistanceValue = (resistance && typeof resistance === 'number' && !isNaN(resistance)) 
      ? safeFormatNumber(resistance) 
      : 'β€”';
    
    container.innerHTML = `

      <div class="level-item support">

        <div class="level-icon">↓</div>

        <div class="level-details">

          <span class="level-type">Support</span>

          <strong class="level-price">${escapeHtml(supportValue)}</strong>

        </div>

      </div>

      <div class="level-item resistance">

        <div class="level-icon">↑</div>

        <div class="level-details">

          <span class="level-type">Resistance</span>

          <strong class="level-price">${escapeHtml(resistanceValue)}</strong>

        </div>

      </div>

      ${validLevels.map(level => {

        const levelType = escapeHtml(String(level.type || 'support'));

        const levelValue = safeFormatNumber(level.value);

        const strengthPercent = safeFormatNumber(level.strength * 100, { minimumFractionDigits: 0, maximumFractionDigits: 0 });

        return `

        <div class="level-item ${levelType}" style="opacity: ${Math.max(0, Math.min(1, level.strength))}">

          <div class="level-icon">${levelType === 'support' ? '↓' : '↑'}</div>

          <div class="level-details">

            <span class="level-type">${levelType === 'support' ? 'Support' : 'Resistance'}</span>

            <strong class="level-price">${escapeHtml(levelValue)}</strong>

            <span class="level-strength">Strength: ${escapeHtml(strengthPercent)}%</span>

          </div>

        </div>

      `;

      }).join('')}

    `;
  }

  renderSignals() {
    const container = document.getElementById('trading-signals');
    if (!container || !this.analysisData || !this.analysisData.signals) {
      if (container) {
        container.innerHTML = '<div class="no-signals">No signals detected</div>';
      }
      return;
    }

    const signals = Array.isArray(this.analysisData.signals) ? this.analysisData.signals : [];
    
    if (signals.length === 0) {
      container.innerHTML = '<div class="no-signals">No signals detected</div>';
      return;
    }
    
    container.innerHTML = signals.map(signal => {
      if (!signal || typeof signal !== 'object') return '';
      
      const signalType = String(signal.type || 'HOLD').toUpperCase();
      const signalSource = escapeHtml(String(signal.source || 'Unknown'));
      const signalStrength = escapeHtml(String(signal.strength || 'Medium'));
      const signalClass = escapeHtml(String(signalType).toLowerCase());
      const signalIcon = signalType === 'BUY' ? '🟒' : signalType === 'SELL' ? 'πŸ”΄' : '🟑';
      
      return `

      <div class="signal-item ${signalClass}">

        <div class="signal-icon">${signalIcon}</div>

        <div class="signal-details">

          <span class="signal-type">${escapeHtml(signalType)}</span>

          <span class="signal-source">${signalSource}</span>

          <span class="signal-strength">${signalStrength}</span>

        </div>

      </div>

    `;
    }).filter(html => html.length > 0).join('') || '<div class="no-signals">No signals detected</div>';
  }

  renderHarmonicPatterns() {
    const container = document.getElementById('harmonic-patterns');
    if (!container || !this.analysisData || !this.analysisData.harmonic_patterns) {
      if (container) {
        container.innerHTML = '<div class="no-patterns">No harmonic patterns detected</div>';
      }
      return;
    }

    const patterns = Array.isArray(this.analysisData.harmonic_patterns) 
      ? this.analysisData.harmonic_patterns.filter(p => p && typeof p === 'object')
      : [];
    
    if (patterns.length === 0) {
      container.innerHTML = '<div class="no-patterns">No harmonic patterns detected</div>';
      return;
    }
    
    container.innerHTML = patterns.map(pattern => {
      const patternType = escapeHtml(String(pattern.type || 'Unknown'));
      const patternPattern = escapeHtml(String(pattern.pattern || 'Neutral').toLowerCase());
      const confidence = typeof pattern.confidence === 'number' && !isNaN(pattern.confidence)
        ? safeFormatNumber(pattern.confidence * 100, { minimumFractionDigits: 0, maximumFractionDigits: 0 })
        : '0';
      
      return `

      <div class="pattern-item ${patternPattern}">

        <div class="pattern-header">

          <span class="pattern-type">${patternType}</span>

          <span class="pattern-confidence">${escapeHtml(confidence)}%</span>

        </div>

        <div class="pattern-details">

          <span class="pattern-direction">${escapeHtml(String(pattern.pattern || 'Neutral'))}</span>

        </div>

      </div>

    `;
    }).filter(html => html.length > 0).join('') || '<div class="no-patterns">No harmonic patterns detected</div>';
  }

  renderElliottWave() {
    const container = document.getElementById('elliott-wave');
    if (!container || !this.analysisData || !this.analysisData.elliott_wave) {
      if (container) {
        container.innerHTML = '<div class="no-wave">Elliott Wave analysis not available</div>';
      }
      return;
    }

    const wave = this.analysisData.elliott_wave;
    if (!wave || typeof wave !== 'object') {
      if (container) {
        container.innerHTML = '<div class="no-wave">Elliott Wave analysis not available</div>';
      }
      return;
    }
    
    const pattern = escapeHtml(String(wave.pattern || 'Incomplete'));
    const waveCount = typeof wave.wave_count === 'number' ? wave.wave_count : 0;
    const targetHtml = (wave.target && typeof wave.target === 'object' && 
                       typeof wave.target.price === 'number' && !isNaN(wave.target.price))
      ? `

          <div class="wave-info">

            <span class="wave-label">Target:</span>

            <span class="wave-value">${escapeHtml(safeFormatNumber(wave.target.price))} (${escapeHtml(String(wave.target.type || 'unknown'))})</span>

          </div>

        `
      : '';
    
    container.innerHTML = `

      <div class="wave-analysis-card">

        <div class="wave-info">

          <span class="wave-label">Pattern:</span>

          <span class="wave-value">${pattern}</span>

        </div>

        <div class="wave-info">

          <span class="wave-label">Wave Count:</span>

          <span class="wave-value">${escapeHtml(String(waveCount))}</span>

        </div>

        ${targetHtml}

      </div>

    `;
  }

  renderTradeRecommendations() {
    const container = document.getElementById('trade-recommendations');
    if (!container) return;

    if (!this.analysisData || !this.ohlcvData || this.ohlcvData.length === 0) {
      container.innerHTML = '<div class="no-recommendations">Insufficient data for recommendations</div>';
      return;
    }

    const signals = Array.isArray(this.analysisData.signals) ? this.analysisData.signals : [];
    const sr = (this.analysisData.support_resistance && typeof this.analysisData.support_resistance === 'object') 
      ? this.analysisData.support_resistance 
      : {};
    
    const lastCandle = this.ohlcvData[this.ohlcvData.length - 1];
    const lastClose = (lastCandle && (typeof lastCandle.c === 'number' || typeof lastCandle.close === 'number'))
      ? parseFloat(lastCandle.c || lastCandle.close)
      : 0;
    
    if (lastClose <= 0 || isNaN(lastClose)) {
      container.innerHTML = '<div class="no-recommendations">Invalid price data</div>';
      return;
    }
    
    const buySignals = signals.filter(s => s && s.type === 'BUY');
    const sellSignals = signals.filter(s => s && s.type === 'SELL');
    
    let recommendation = 'HOLD';
    let tp = null;
    let sl = null;
    
    if (buySignals.length > sellSignals.length) {
      recommendation = 'BUY';
      tp = (sr.resistance && typeof sr.resistance === 'number' && !isNaN(sr.resistance))
        ? sr.resistance 
        : lastClose * 1.05;
      sl = (sr.support && typeof sr.support === 'number' && !isNaN(sr.support))
        ? sr.support 
        : lastClose * 0.95;
    } else if (sellSignals.length > buySignals.length) {
      recommendation = 'SELL';
      tp = (sr.support && typeof sr.support === 'number' && !isNaN(sr.support))
        ? sr.support 
        : lastClose * 0.95;
      sl = (sr.resistance && typeof sr.resistance === 'number' && !isNaN(sr.resistance))
        ? sr.resistance 
        : lastClose * 1.05;
    }
    
    const recommendationClass = escapeHtml(recommendation.toLowerCase());
    const confidenceText = signals.length > 0 ? 'High' : 'Low';
    const tpValue = tp && typeof tp === 'number' && !isNaN(tp) ? safeFormatNumber(tp) : 'β€”';
    const slValue = sl && typeof sl === 'number' && !isNaN(sl) ? safeFormatNumber(sl) : 'β€”';
    
    container.innerHTML = `

      <div class="recommendation-card ${recommendationClass}">

        <div class="recommendation-header">

          <span class="recommendation-type">${escapeHtml(recommendation)}</span>

          <span class="recommendation-confidence">${escapeHtml(confidenceText)}</span>

        </div>

        ${recommendation !== 'HOLD' ? `

          <div class="recommendation-levels">

            <div class="level-item">

              <span class="level-label">Take Profit:</span>

              <strong class="level-value">${escapeHtml(tpValue)}</strong>

            </div>

            <div class="level-item">

              <span class="level-label">Stop Loss:</span>

              <strong class="level-value">${escapeHtml(slValue)}</strong>

            </div>

          </div>

        ` : ''}

        <div class="recommendation-signals">

          <span>${escapeHtml(String(buySignals.length))} Buy Signals</span>

          <span>${escapeHtml(String(sellSignals.length))} Sell Signals</span>

        </div>

      </div>

    `;
  }

  showError(message) {
    this.showNotification(message, 'error');
    logger.error('TechnicalAnalysis', message);
  }
  
  showSuccess(message) {
    this.showNotification(message, 'success');
  }
  
  showWarning(message) {
    this.showNotification(message, 'warning');
  }
  
  showInfo(message) {
    this.showNotification(message, 'info');
  }
  
  showNotification(message, type = 'info') {
    const toast = document.createElement('div');
    toast.className = `notification ${type}`;
    toast.textContent = message;
    toast.style.cssText = `

      position: fixed;

      top: 20px;

      right: 20px;

      padding: 16px 24px;

      background: linear-gradient(135deg, rgba(15, 23, 42, 0.95), rgba(30, 41, 59, 0.95));

      backdrop-filter: blur(10px);

      border-radius: 8px;

      border-left: 4px solid;

      color: var(--text-strong);

      z-index: 10000;

      box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);

      min-width: 300px;

      max-width: 500px;

      animation: slideInRight 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);

    `;
    
    if (type === 'success') toast.style.borderLeftColor = '#22c55e';
    else if (type === 'error') toast.style.borderLeftColor = '#ef4444';
    else if (type === 'warning') toast.style.borderLeftColor = '#eab308';
    else toast.style.borderLeftColor = '#3b82f6';
    
    document.body.appendChild(toast);
    
    setTimeout(() => {
      toast.style.animation = 'slideInRight 0.4s ease-out reverse';
      setTimeout(() => toast.remove(), 400);
    }, 5000);
  }
  
  showLoading(message = 'Loading...') {
    const container = document.getElementById(`mode-${this.currentMode}`);
    if (container) {
      container.innerHTML = `

        <div class="loading-state">

          <div class="loading-spinner"></div>

          <p class="loading-message">${message}</p>

        </div>

      `;
    }
  }
  
  hideLoading() {
    // Loading will be replaced by actual content
  }
  
  renderErrorState(mode, error) {
    const container = document.getElementById(`mode-${mode}`);
    if (container) {
      const errorMessage = error && error.message ? escapeHtml(error.message) : 'An unexpected error occurred';
      container.innerHTML = `

        <div class="error-state">

          <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">

            <circle cx="12" cy="12" r="10"></circle>

            <line x1="12" y1="8" x2="12" y2="12"></line>

            <line x1="12" y1="16" x2="12.01" y2="16"></line>

          </svg>

          <h3>Analysis Failed</h3>

          <p>${errorMessage}</p>

          <button class="btn btn-primary" onclick="if(window.technicalAnalysisPage){window.technicalAnalysisPage.runCurrentModeAnalysis();}">

            Retry Analysis

          </button>

        </div>

      `;
    }
  }
  
  runCurrentModeAnalysis() {
    this.analyze();
  }
  
  delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }
  
  async fetchWithRetry(url, options = {}, timeout = 15000, retries = 3) {
    for (let i = 0; i < retries; i++) {
      try {
        const response = await apiClient.fetch(url, options, timeout);
        if (response.ok) {
          return response;
        }
        
        if (i < retries - 1 && response.status >= 500) {
          const delayMs = Math.min(this.retryConfig.baseDelay * Math.pow(2, i), this.retryConfig.maxDelay);
          await this.delay(delayMs);
          continue;
        }
        
        return response;
      } catch (error) {
        if (i < retries - 1) {
          const delayMs = Math.min(this.retryConfig.baseDelay * Math.pow(2, i), this.retryConfig.maxDelay);
          await this.delay(delayMs);
          continue;
        }
        throw error;
      }
    }
    throw new Error('Max retries exceeded');
  }
}

export default TechnicalAnalysisPage;