File size: 58,825 Bytes
bb4c54e
 
fccb755
bb4c54e
 
fccb755
 
 
 
 
 
 
bb4c54e
 
 
fccb755
bb4c54e
 
 
 
fccb755
bb4c54e
 
 
 
 
fccb755
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fccb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fccb755
 
 
 
bb4c54e
fccb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
fccb755
 
 
 
 
 
 
 
 
 
 
bb4c54e
fccb755
 
 
bb4c54e
fccb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fccb755
 
bb4c54e
 
 
 
 
fccb755
bb4c54e
 
fccb755
bb4c54e
 
 
 
 
 
 
fccb755
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
 
 
fccb755
 
 
 
bb4c54e
fccb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
fccb755
 
 
 
 
 
 
 
 
 
 
bb4c54e
fccb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fccb755
 
bb4c54e
 
 
 
 
fccb755
bb4c54e
fccb755
bb4c54e
 
 
 
 
 
 
fccb755
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
 
fccb755
 
 
 
bb4c54e
fccb755
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
fccb755
 
 
 
 
 
 
 
 
 
 
bb4c54e
fccb755
 
 
bb4c54e
fccb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fccb755
 
bb4c54e
 
 
 
 
fccb755
 
 
 
bb4c54e
 
 
 
 
fccb755
bb4c54e
 
 
 
 
 
 
 
fccb755
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
fccb755
 
 
 
bb4c54e
fccb755
bb4c54e
 
fccb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
fccb755
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
fccb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fccb755
 
bb4c54e
 
 
 
 
fccb755
 
 
 
 
bb4c54e
 
 
 
 
 
fccb755
bb4c54e
 
 
 
 
 
 
 
 
 
fccb755
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
fccb755
 
 
 
bb4c54e
fccb755
bb4c54e
 
fccb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
fccb755
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
fccb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fccb755
 
bb4c54e
 
 
 
 
fccb755
 
 
 
 
bb4c54e
 
 
 
 
 
fccb755
bb4c54e
 
 
 
 
 
 
 
fccb755
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
 
 
 
fccb755
 
 
 
bb4c54e
fccb755
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
fccb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
fccb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fccb755
 
bb4c54e
 
 
 
 
fccb755
bb4c54e
fccb755
bb4c54e
 
 
 
 
 
 
 
fccb755
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
 
fccb755
 
 
 
bb4c54e
fccb755
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
fccb755
 
 
 
 
 
 
 
 
 
 
bb4c54e
fccb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fccb755
 
bb4c54e
 
 
 
 
fccb755
bb4c54e
fccb755
bb4c54e
 
 
 
 
 
 
fccb755
 
 
 
 
 
 
 
bb4c54e
 
 
 
 
 
 
 
 
bee039d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c54e
 
bee039d
bb4c54e
bee039d
bb4c54e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bee039d
bb4c54e
bee039d
 
bb4c54e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bee039d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
#!/usr/bin/env python3
"""
Technical Indicators API Router (PRODUCTION SAFE)
Provides API endpoints for calculating technical indicators on cryptocurrency data.
Includes: Bollinger Bands, Stochastic RSI, ATR, SMA, EMA, MACD, RSI

CRITICAL RULES:
- HTTP 400 for insufficient data (NOT HTTP 500)
- Strict minimum candle requirements enforced
- NaN/Infinity values sanitized before response
- Comprehensive logging for all operations
- Never crash - always return valid JSON
"""

from fastapi import APIRouter, HTTPException, Query
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
from typing import List, Dict, Any, Optional
from datetime import datetime
import logging
import math

logger = logging.getLogger(__name__)

router = APIRouter(prefix="/api/indicators", tags=["Technical Indicators"])

# ============================================================================
# MINIMUM CANDLE REQUIREMENTS (MANDATORY)
# ============================================================================
MIN_CANDLES = {
    "SMA": 20,
    "EMA": 20,
    "RSI": 15,
    "ATR": 15,
    "MACD": 35,
    "STOCH_RSI": 50,
    "BOLLINGER_BANDS": 20
}


# ============================================================================
# Pydantic Models
# ============================================================================

class OHLCVData(BaseModel):
    """OHLCV data model"""
    timestamp: int
    open: float
    high: float
    low: float
    close: float
    volume: float


class IndicatorRequest(BaseModel):
    """Request model for indicator calculation"""
    symbol: str = Field(default="BTC", description="Cryptocurrency symbol")
    timeframe: str = Field(default="1h", description="Timeframe (1m, 5m, 15m, 1h, 4h, 1d)")
    ohlcv: Optional[List[OHLCVData]] = Field(default=None, description="OHLCV data array")
    period: int = Field(default=14, description="Indicator period")


class BollingerBandsResponse(BaseModel):
    """Bollinger Bands response model"""
    upper: float
    middle: float
    lower: float
    bandwidth: float
    percent_b: float
    signal: str
    description: str


class StochRSIResponse(BaseModel):
    """Stochastic RSI response model"""
    value: float
    k_line: float
    d_line: float
    signal: str
    description: str


class ATRResponse(BaseModel):
    """Average True Range response model"""
    value: float
    percent: float
    volatility_level: str
    signal: str
    description: str


class SMAResponse(BaseModel):
    """Simple Moving Average response model"""
    sma20: float
    sma50: float
    sma200: Optional[float]
    price_vs_sma20: str
    price_vs_sma50: str
    trend: str
    signal: str
    description: str


class EMAResponse(BaseModel):
    """Exponential Moving Average response model"""
    ema12: float
    ema26: float
    ema50: Optional[float]
    trend: str
    signal: str
    description: str


class MACDResponse(BaseModel):
    """MACD response model"""
    macd_line: float
    signal_line: float
    histogram: float
    trend: str
    signal: str
    description: str


class RSIResponse(BaseModel):
    """RSI response model"""
    value: float
    signal: str
    description: str


class ComprehensiveIndicatorsResponse(BaseModel):
    """All indicators combined response"""
    symbol: str
    timeframe: str
    timestamp: str
    current_price: float
    bollinger_bands: BollingerBandsResponse
    stoch_rsi: StochRSIResponse
    atr: ATRResponse
    sma: SMAResponse
    ema: EMAResponse
    macd: MACDResponse
    rsi: RSIResponse
    overall_signal: str
    recommendation: str


# ============================================================================
# Helper Functions - Data Validation & Sanitization
# ============================================================================

def sanitize_value(value: Any) -> Optional[float]:
    """
    Sanitize a numeric value - remove NaN, Infinity, None
    Returns None if value is invalid, otherwise returns the float value
    """
    if value is None:
        return None
    try:
        val = float(value)
        if math.isnan(val) or math.isinf(val):
            return None
        return val
    except (ValueError, TypeError):
        return None


def sanitize_dict(data: Dict[str, Any]) -> Dict[str, Any]:
    """
    Sanitize all numeric values in a dictionary
    Replace NaN/Infinity with None or 0 depending on context
    """
    sanitized = {}
    for key, value in data.items():
        if isinstance(value, dict):
            sanitized[key] = sanitize_dict(value)
        elif isinstance(value, (int, float)):
            clean_val = sanitize_value(value)
            sanitized[key] = clean_val if clean_val is not None else 0
        else:
            sanitized[key] = value
    return sanitized


def validate_ohlcv_data(ohlcv: Optional[Dict[str, Any]], min_candles: int, symbol: str, indicator: str) -> tuple[bool, Optional[List[float]], Optional[str]]:
    """
    Validate OHLCV data and extract prices
    
    Returns:
        (is_valid, prices, error_message)
    """
    if not ohlcv:
        logger.warning(f"❌ {indicator} - {symbol}: No OHLCV data received")
        return False, None, "No market data available"
    
    if "prices" not in ohlcv:
        logger.warning(f"❌ {indicator} - {symbol}: OHLCV missing 'prices' key")
        return False, None, "Invalid market data format"
    
    prices = [p[1] for p in ohlcv["prices"] if len(p) >= 2]
    
    if not prices:
        logger.warning(f"❌ {indicator} - {symbol}: Empty price array")
        return False, None, "No price data available"
    
    if len(prices) < min_candles:
        logger.warning(f"❌ {indicator} - {symbol}: Insufficient candles ({len(prices)} < {min_candles} required)")
        return False, None, f"Insufficient market data: need at least {min_candles} candles, got {len(prices)}"
    
    logger.info(f"βœ… {indicator} - {symbol}: Validated {len(prices)} candles (required: {min_candles})")
    return True, prices, None


# ============================================================================
# Helper Functions for Calculations
# ============================================================================

def calculate_sma(prices: List[float], period: int) -> float:
    """Calculate Simple Moving Average"""
    if len(prices) < period:
        return prices[-1] if prices else 0
    return sum(prices[-period:]) / period


def calculate_ema(prices: List[float], period: int) -> float:
    """Calculate Exponential Moving Average"""
    if len(prices) < period:
        return prices[-1] if prices else 0
    
    multiplier = 2 / (period + 1)
    ema = sum(prices[:period]) / period  # SMA for first period
    
    for price in prices[period:]:
        ema = (price * multiplier) + (ema * (1 - multiplier))
    
    return ema


def calculate_rsi(prices: List[float], period: int = 14) -> float:
    """Calculate Relative Strength Index"""
    if len(prices) < period + 1:
        return 50.0
    
    deltas = [prices[i] - prices[i-1] for i in range(1, len(prices))]
    gains = [d if d > 0 else 0 for d in deltas[-period:]]
    losses = [-d if d < 0 else 0 for d in deltas[-period:]]
    
    avg_gain = sum(gains) / period
    avg_loss = sum(losses) / period
    
    if avg_loss == 0:
        return 100.0 if avg_gain > 0 else 50.0
    
    rs = avg_gain / avg_loss
    return 100 - (100 / (1 + rs))


def calculate_bollinger_bands(prices: List[float], period: int = 20, std_dev: float = 2) -> Dict[str, float]:
    """Calculate Bollinger Bands"""
    if len(prices) < period:
        current = prices[-1] if prices else 0
        return {
            "upper": current,
            "middle": current,
            "lower": current,
            "bandwidth": 0,
            "percent_b": 50
        }
    
    recent_prices = prices[-period:]
    middle = sum(recent_prices) / period
    
    # Calculate standard deviation
    variance = sum((p - middle) ** 2 for p in recent_prices) / period
    std = variance ** 0.5
    
    upper = middle + (std_dev * std)
    lower = middle - (std_dev * std)
    
    # Bandwidth as percentage
    bandwidth = ((upper - lower) / middle) * 100 if middle > 0 else 0
    
    # Percent B (position within bands)
    current_price = prices[-1]
    if upper != lower:
        percent_b = ((current_price - lower) / (upper - lower)) * 100
    else:
        percent_b = 50
    
    return {
        "upper": round(upper, 8),
        "middle": round(middle, 8),
        "lower": round(lower, 8),
        "bandwidth": round(bandwidth, 2),
        "percent_b": round(percent_b, 2)
    }


def calculate_stoch_rsi(prices: List[float], rsi_period: int = 14, stoch_period: int = 14) -> Dict[str, float]:
    """Calculate Stochastic RSI"""
    if len(prices) < rsi_period + stoch_period:
        return {"value": 50, "k_line": 50, "d_line": 50}
    
    # Calculate RSI values for the stoch period
    rsi_values = []
    for i in range(stoch_period + 3):  # Extra for smoothing
        end_idx = len(prices) - stoch_period + i + 1
        if end_idx > rsi_period:
            slice_prices = prices[:end_idx]
            rsi_values.append(calculate_rsi(slice_prices, rsi_period))
    
    if len(rsi_values) < stoch_period:
        return {"value": 50, "k_line": 50, "d_line": 50}
    
    recent_rsi = rsi_values[-stoch_period:]
    rsi_high = max(recent_rsi)
    rsi_low = min(recent_rsi)
    
    current_rsi = rsi_values[-1]
    
    if rsi_high == rsi_low:
        stoch_rsi = 50
    else:
        stoch_rsi = ((current_rsi - rsi_low) / (rsi_high - rsi_low)) * 100
    
    # K line is the raw Stoch RSI
    k_line = stoch_rsi
    
    # D line is 3-period SMA of K
    if len(rsi_values) >= 3:
        k_values = []
        for i in range(3):
            idx = -3 + i
            r_high = max(rsi_values[idx-stoch_period+1:idx+1]) if idx+1 <= 0 else rsi_high
            r_low = min(rsi_values[idx-stoch_period+1:idx+1]) if idx+1 <= 0 else rsi_low
            curr = rsi_values[idx]
            if r_high != r_low:
                k_values.append(((curr - r_low) / (r_high - r_low)) * 100)
            else:
                k_values.append(50)
        d_line = sum(k_values) / 3
    else:
        d_line = k_line
    
    return {
        "value": round(stoch_rsi, 2),
        "k_line": round(k_line, 2),
        "d_line": round(d_line, 2)
    }


def calculate_atr(highs: List[float], lows: List[float], closes: List[float], period: int = 14) -> float:
    """Calculate Average True Range"""
    if len(closes) < period + 1:
        if len(highs) > 0 and len(lows) > 0:
            return highs[-1] - lows[-1]
        return 0
    
    true_ranges = []
    for i in range(1, len(closes)):
        high = highs[i]
        low = lows[i]
        prev_close = closes[i-1]
        
        tr = max(
            high - low,
            abs(high - prev_close),
            abs(low - prev_close)
        )
        true_ranges.append(tr)
    
    # ATR is the average of the last 'period' true ranges
    if len(true_ranges) < period:
        return sum(true_ranges) / len(true_ranges) if true_ranges else 0
    
    return sum(true_ranges[-period:]) / period


def calculate_macd(prices: List[float], fast: int = 12, slow: int = 26, signal: int = 9) -> Dict[str, float]:
    """Calculate MACD"""
    if len(prices) < slow + signal:
        return {"macd_line": 0, "signal_line": 0, "histogram": 0}
    
    ema_fast = calculate_ema(prices, fast)
    ema_slow = calculate_ema(prices, slow)
    macd_line = ema_fast - ema_slow
    
    # Calculate signal line (EMA of MACD)
    # We need MACD values history for signal line
    macd_values = []
    for i in range(signal + 5):
        idx = len(prices) - signal - 5 + i
        if idx > slow:
            slice_prices = prices[:idx+1]
            ef = calculate_ema(slice_prices, fast)
            es = calculate_ema(slice_prices, slow)
            macd_values.append(ef - es)
    
    if len(macd_values) >= signal:
        signal_line = calculate_ema(macd_values, signal)
    else:
        signal_line = macd_line
    
    histogram = macd_line - signal_line
    
    return {
        "macd_line": round(macd_line, 8),
        "signal_line": round(signal_line, 8),
        "histogram": round(histogram, 8)
    }


# ============================================================================
# API Endpoints
# ============================================================================

@router.get("/services")
async def list_indicator_services():
    """List all available technical indicator services"""
    return {
        "success": True,
        "services": [
            {
                "id": "bollinger_bands",
                "name": "Bollinger Bands",
                "description": "Volatility bands placed above and below a moving average",
                "endpoint": "/api/indicators/bollinger-bands",
                "parameters": ["symbol", "timeframe", "period", "std_dev"],
                "icon": "πŸ“Š",
                "category": "volatility"
            },
            {
                "id": "stoch_rsi",
                "name": "Stochastic RSI",
                "description": "Combines Stochastic oscillator with RSI for momentum",
                "endpoint": "/api/indicators/stoch-rsi",
                "parameters": ["symbol", "timeframe", "rsi_period", "stoch_period"],
                "icon": "πŸ“ˆ",
                "category": "momentum"
            },
            {
                "id": "atr",
                "name": "Average True Range (ATR)",
                "description": "Measures market volatility and price movement",
                "endpoint": "/api/indicators/atr",
                "parameters": ["symbol", "timeframe", "period"],
                "icon": "πŸ“‰",
                "category": "volatility"
            },
            {
                "id": "sma",
                "name": "Simple Moving Average (SMA)",
                "description": "Average price over specified periods (20, 50, 200)",
                "endpoint": "/api/indicators/sma",
                "parameters": ["symbol", "timeframe"],
                "icon": "〰️",
                "category": "trend"
            },
            {
                "id": "ema",
                "name": "Exponential Moving Average (EMA)",
                "description": "Weighted moving average giving more weight to recent prices",
                "endpoint": "/api/indicators/ema",
                "parameters": ["symbol", "timeframe"],
                "icon": "πŸ“",
                "category": "trend"
            },
            {
                "id": "macd",
                "name": "MACD",
                "description": "Moving Average Convergence Divergence - trend following momentum",
                "endpoint": "/api/indicators/macd",
                "parameters": ["symbol", "timeframe", "fast", "slow", "signal"],
                "icon": "πŸ”€",
                "category": "momentum"
            },
            {
                "id": "rsi",
                "name": "RSI",
                "description": "Relative Strength Index - momentum oscillator (0-100)",
                "endpoint": "/api/indicators/rsi",
                "parameters": ["symbol", "timeframe", "period"],
                "icon": "πŸ’ͺ",
                "category": "momentum"
            },
            {
                "id": "comprehensive",
                "name": "Comprehensive Analysis",
                "description": "All indicators combined with trading signals",
                "endpoint": "/api/indicators/comprehensive",
                "parameters": ["symbol", "timeframe"],
                "icon": "🎯",
                "category": "analysis"
            }
        ],
        "categories": {
            "volatility": "Measure price volatility and potential breakouts",
            "momentum": "Identify overbought/oversold conditions",
            "trend": "Determine market direction and strength",
            "analysis": "Complete multi-indicator analysis"
        },
        "timestamp": datetime.utcnow().isoformat() + "Z"
    }


@router.get("/bollinger-bands")
async def get_bollinger_bands(
    symbol: str = Query(default="BTC", description="Cryptocurrency symbol"),
    timeframe: str = Query(default="1h", description="Timeframe"),
    period: int = Query(default=20, description="Period for calculation"),
    std_dev: float = Query(default=2.0, description="Standard deviation multiplier")
):
    """Calculate Bollinger Bands for a symbol - PRODUCTION SAFE"""
    indicator_name = "BOLLINGER_BANDS"
    logger.info(f"πŸ“Š {indicator_name} - Endpoint called: symbol={symbol}, timeframe={timeframe}, period={period}, std_dev={std_dev}")
    
    try:
        # Validate parameters
        if period < 1 or period > 100:
            logger.warning(f"❌ {indicator_name} - Invalid period: {period}")
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": f"Invalid period: must be between 1 and 100, got {period}"
                }
            )
        if std_dev <= 0 or std_dev > 5:
            logger.warning(f"❌ {indicator_name} - Invalid std_dev: {std_dev}")
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": f"Invalid std_dev: must be between 0 and 5, got {std_dev}"
                }
            )
        
        # Get OHLCV data from market API
        from backend.services.coingecko_client import coingecko_client
        
        # Map timeframe to days
        timeframe_days = {"1m": 1, "5m": 1, "15m": 1, "1h": 7, "4h": 30, "1d": 90}
        days = timeframe_days.get(timeframe, 7)
        
        try:
            ohlcv = await coingecko_client.get_ohlcv(symbol, days=days)
        except Exception as e:
            logger.error(f"❌ {indicator_name} - Failed to fetch OHLCV: {e}")
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": "Unable to fetch market data for this symbol"
                }
            )
        
        # Validate OHLCV data
        min_required = MIN_CANDLES["BOLLINGER_BANDS"]
        is_valid, prices, error_msg = validate_ohlcv_data(ohlcv, min_required, symbol, indicator_name)
        
        if not is_valid:
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": error_msg,
                    "symbol": symbol.upper(),
                    "timeframe": timeframe,
                    "indicator": "bollinger_bands",
                    "data_points": 0
                }
            )
        
        # Calculate Bollinger Bands
        try:
            bb = calculate_bollinger_bands(prices, period, std_dev)
            current_price = prices[-1] if prices else 0
            
            # Sanitize output
            bb = sanitize_dict(bb)
            current_price = sanitize_value(current_price) or 0
            
        except Exception as e:
            logger.error(f"❌ {indicator_name} - Calculation failed: {e}", exc_info=True)
            return JSONResponse(
                status_code=500,
                content={
                    "error": True,
                    "message": "Internal indicator calculation error"
                }
            )
        
        # Determine signal
        if bb["percent_b"] > 95:
            signal = "overbought"
            description = "Price at upper band - potential reversal or breakout"
        elif bb["percent_b"] < 5:
            signal = "oversold"
            description = "Price at lower band - potential bounce or breakdown"
        elif bb["percent_b"] > 70:
            signal = "bullish_caution"
            description = "Price approaching upper band - watch for resistance"
        elif bb["percent_b"] < 30:
            signal = "bearish_caution"
            description = "Price approaching lower band - watch for support"
        else:
            signal = "neutral"
            description = "Price within normal range - no extreme conditions"
        
        logger.info(f"βœ… {indicator_name} - Success: symbol={symbol}, percent_b={bb['percent_b']:.2f}, signal={signal}")
        
        return {
            "success": True,
            "symbol": symbol.upper(),
            "timeframe": timeframe,
            "indicator": "bollinger_bands",
            "value": bb,
            "data": bb,
            "current_price": round(current_price, 8),
            "data_points": len(prices),
            "signal": signal,
            "description": description,
            "timestamp": datetime.utcnow().isoformat() + "Z",
            "source": "coingecko"
        }
        
    except Exception as e:
        logger.error(f"❌ {indicator_name} - Unexpected error: {e}", exc_info=True)
        return JSONResponse(
            status_code=500,
            content={
                "error": True,
                "message": "Internal server error"
            }
        )


@router.get("/stoch-rsi")
async def get_stoch_rsi(
    symbol: str = Query(default="BTC", description="Cryptocurrency symbol"),
    timeframe: str = Query(default="1h", description="Timeframe"),
    rsi_period: int = Query(default=14, description="RSI period"),
    stoch_period: int = Query(default=14, description="Stochastic period")
):
    """Calculate Stochastic RSI for a symbol - PRODUCTION SAFE"""
    indicator_name = "STOCH_RSI"
    logger.info(f"πŸ“Š {indicator_name} - Endpoint called: symbol={symbol}, timeframe={timeframe}, rsi_period={rsi_period}, stoch_period={stoch_period}")
    
    try:
        # Validate parameters
        if rsi_period < 1 or rsi_period > 100:
            logger.warning(f"❌ {indicator_name} - Invalid rsi_period: {rsi_period}")
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": f"Invalid rsi_period: must be between 1 and 100, got {rsi_period}"
                }
            )
        if stoch_period < 1 or stoch_period > 100:
            logger.warning(f"❌ {indicator_name} - Invalid stoch_period: {stoch_period}")
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": f"Invalid stoch_period: must be between 1 and 100, got {stoch_period}"
                }
            )
        
        # Fetch OHLCV data
        from backend.services.coingecko_client import coingecko_client
        
        timeframe_days = {"1m": 1, "5m": 1, "15m": 1, "1h": 7, "4h": 30, "1d": 90}
        days = timeframe_days.get(timeframe, 7)
        
        try:
            ohlcv = await coingecko_client.get_ohlcv(symbol, days=days)
        except Exception as e:
            logger.error(f"❌ {indicator_name} - Failed to fetch OHLCV: {e}")
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": "Unable to fetch market data for this symbol"
                }
            )
        
        # Validate OHLCV data
        min_required = MIN_CANDLES["STOCH_RSI"]
        is_valid, prices, error_msg = validate_ohlcv_data(ohlcv, min_required, symbol, indicator_name)
        
        if not is_valid:
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": error_msg,
                    "symbol": symbol.upper(),
                    "timeframe": timeframe,
                    "indicator": "stoch_rsi",
                    "data_points": 0
                }
            )
        
        # Calculate Stochastic RSI
        try:
            stoch = calculate_stoch_rsi(prices, rsi_period, stoch_period)
            
            # Sanitize output
            stoch = sanitize_dict(stoch)
            
        except Exception as e:
            logger.error(f"❌ {indicator_name} - Calculation failed: {e}", exc_info=True)
            return JSONResponse(
                status_code=500,
                content={
                    "error": True,
                    "message": "Internal indicator calculation error"
                }
            )
        
        # Determine signal
        if stoch["value"] > 80:
            signal = "overbought"
            description = "Extreme overbought - high probability of pullback"
        elif stoch["value"] < 20:
            signal = "oversold"
            description = "Extreme oversold - high probability of bounce"
        elif stoch["k_line"] > stoch["d_line"] and stoch["value"] < 50:
            signal = "bullish_crossover"
            description = "K crossed above D in oversold territory - bullish signal"
        elif stoch["k_line"] < stoch["d_line"] and stoch["value"] > 50:
            signal = "bearish_crossover"
            description = "K crossed below D in overbought territory - bearish signal"
        else:
            signal = "neutral"
            description = "Normal momentum range - no extreme conditions"
        
        logger.info(f"βœ… {indicator_name} - Success: symbol={symbol}, value={stoch['value']:.2f}, signal={signal}")
        
        return {
            "success": True,
            "symbol": symbol.upper(),
            "timeframe": timeframe,
            "indicator": "stoch_rsi",
            "value": stoch,
            "data": stoch,
            "data_points": len(prices),
            "signal": signal,
            "description": description,
            "timestamp": datetime.utcnow().isoformat() + "Z",
            "source": "coingecko"
        }
        
    except Exception as e:
        logger.error(f"❌ {indicator_name} - Unexpected error: {e}", exc_info=True)
        return JSONResponse(
            status_code=500,
            content={
                "error": True,
                "message": "Internal server error"
            }
        )


@router.get("/atr")
async def get_atr(
    symbol: str = Query(default="BTC", description="Cryptocurrency symbol"),
    timeframe: str = Query(default="1h", description="Timeframe"),
    period: int = Query(default=14, description="ATR period")
):
    """Calculate Average True Range for a symbol - PRODUCTION SAFE"""
    indicator_name = "ATR"
    logger.info(f"πŸ“Š {indicator_name} - Endpoint called: symbol={symbol}, timeframe={timeframe}, period={period}")
    
    try:
        # Validate parameters
        if period < 1 or period > 100:
            logger.warning(f"❌ {indicator_name} - Invalid period: {period}")
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": f"Invalid period: must be between 1 and 100, got {period}"
                }
            )
        
        # Fetch OHLCV data
        from backend.services.coingecko_client import coingecko_client
        
        timeframe_days = {"1m": 1, "5m": 1, "15m": 1, "1h": 7, "4h": 30, "1d": 90}
        days = timeframe_days.get(timeframe, 7)
        
        try:
            ohlcv = await coingecko_client.get_ohlcv(symbol, days=days)
        except Exception as e:
            logger.error(f"❌ {indicator_name} - Failed to fetch OHLCV: {e}")
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": "Unable to fetch market data for this symbol"
                }
            )
        
        # Validate OHLCV data
        min_required = MIN_CANDLES["ATR"]
        is_valid, prices, error_msg = validate_ohlcv_data(ohlcv, min_required, symbol, indicator_name)
        
        if not is_valid:
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": error_msg,
                    "symbol": symbol.upper(),
                    "timeframe": timeframe,
                    "indicator": "atr",
                    "data_points": 0
                }
            )
        
        # Calculate ATR
        try:
            # For ATR we need H/L/C - use price approximation
            highs = [p * 1.005 for p in prices]  # Approximate
            lows = [p * 0.995 for p in prices]
            
            atr_value = calculate_atr(highs, lows, prices, period)
            current_price = prices[-1] if prices else 1
            atr_percent = (atr_value / current_price) * 100 if current_price > 0 else 0
            
            # Sanitize
            atr_value = sanitize_value(atr_value) or 0
            atr_percent = sanitize_value(atr_percent) or 0
            current_price = sanitize_value(current_price) or 0
            
        except Exception as e:
            logger.error(f"❌ {indicator_name} - Calculation failed: {e}", exc_info=True)
            return JSONResponse(
                status_code=500,
                content={
                    "error": True,
                    "message": "Internal indicator calculation error"
                }
            )
        
        # Determine volatility level
        if atr_percent > 5:
            volatility_level = "very_high"
            signal = "high_risk"
            description = "Very high volatility - increase position sizing caution"
        elif atr_percent > 3:
            volatility_level = "high"
            signal = "caution"
            description = "High volatility - wider stop losses recommended"
        elif atr_percent > 1.5:
            volatility_level = "medium"
            signal = "neutral"
            description = "Normal volatility - standard position sizing"
        else:
            volatility_level = "low"
            signal = "breakout_watch"
            description = "Low volatility - potential breakout forming"
        
        logger.info(f"βœ… {indicator_name} - Success: symbol={symbol}, value={atr_value:.2f}, volatility={volatility_level}")
        
        return {
            "success": True,
            "symbol": symbol.upper(),
            "timeframe": timeframe,
            "indicator": "atr",
            "value": {
                "value": round(atr_value, 8),
                "percent": round(atr_percent, 2)
            },
            "data": {
                "value": round(atr_value, 8),
                "percent": round(atr_percent, 2)
            },
            "current_price": round(current_price, 8),
            "data_points": len(prices),
            "volatility_level": volatility_level,
            "signal": signal,
            "description": description,
            "timestamp": datetime.utcnow().isoformat() + "Z",
            "source": "coingecko"
        }
        
    except Exception as e:
        logger.error(f"❌ {indicator_name} - Unexpected error: {e}", exc_info=True)
        return JSONResponse(
            status_code=500,
            content={
                "error": True,
                "message": "Internal server error"
            }
        )


@router.get("/sma")
async def get_sma(
    symbol: str = Query(default="BTC", description="Cryptocurrency symbol"),
    timeframe: str = Query(default="1h", description="Timeframe")
):
    """Calculate Simple Moving Averages (20, 50, 200) for a symbol - PRODUCTION SAFE"""
    indicator_name = "SMA"
    logger.info(f"πŸ“Š {indicator_name} - Endpoint called: symbol={symbol}, timeframe={timeframe}")
    
    try:
        # Fetch OHLCV data
        from backend.services.coingecko_client import coingecko_client
        
        try:
            # Need more data for SMA 200
            ohlcv = await coingecko_client.get_ohlcv(symbol, days=365)
        except Exception as e:
            logger.error(f"❌ {indicator_name} - Failed to fetch OHLCV: {e}")
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": "Unable to fetch market data for this symbol"
                }
            )
        
        # Validate OHLCV data
        min_required = MIN_CANDLES["SMA"]
        is_valid, prices, error_msg = validate_ohlcv_data(ohlcv, min_required, symbol, indicator_name)
        
        if not is_valid:
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": error_msg,
                    "symbol": symbol.upper(),
                    "timeframe": timeframe,
                    "indicator": "sma",
                    "data_points": 0
                }
            )
        
        # Calculate SMAs
        try:
            current_price = prices[-1] if prices else 0
            
            sma20 = calculate_sma(prices, 20)
            sma50 = calculate_sma(prices, 50)
            sma200 = calculate_sma(prices, 200) if len(prices) >= 200 else None
            
            # Sanitize
            current_price = sanitize_value(current_price) or 0
            sma20 = sanitize_value(sma20) or 0
            sma50 = sanitize_value(sma50) or 0
            sma200 = sanitize_value(sma200) if sma200 is not None else None
            
        except Exception as e:
            logger.error(f"❌ {indicator_name} - Calculation failed: {e}", exc_info=True)
            return JSONResponse(
                status_code=500,
                content={
                    "error": True,
                    "message": "Internal indicator calculation error"
                }
            )
        
        price_vs_sma20 = "above" if current_price > sma20 else "below"
        price_vs_sma50 = "above" if current_price > sma50 else "below"
        
        # Determine trend
        if current_price > sma20 > sma50:
            trend = "strong_bullish"
            signal = "buy"
            description = "Strong uptrend - price above rising SMAs"
        elif current_price > sma20 and current_price > sma50:
            trend = "bullish"
            signal = "buy"
            description = "Bullish trend - price above major SMAs"
        elif current_price < sma20 < sma50:
            trend = "strong_bearish"
            signal = "sell"
            description = "Strong downtrend - price below falling SMAs"
        elif current_price < sma20 and current_price < sma50:
            trend = "bearish"
            signal = "sell"
            description = "Bearish trend - price below major SMAs"
        else:
            trend = "neutral"
            signal = "hold"
            description = "Mixed signals - waiting for clearer direction"
        
        logger.info(f"βœ… {indicator_name} - Success: symbol={symbol}, trend={trend}")
        
        return {
            "success": True,
            "symbol": symbol.upper(),
            "timeframe": timeframe,
            "indicator": "sma",
            "value": {
                "sma20": round(sma20, 8),
                "sma50": round(sma50, 8),
                "sma200": round(sma200, 8) if sma200 else None
            },
            "data": {
                "sma20": round(sma20, 8),
                "sma50": round(sma50, 8),
                "sma200": round(sma200, 8) if sma200 else None
            },
            "current_price": round(current_price, 8),
            "data_points": len(prices),
            "price_vs_sma20": price_vs_sma20,
            "price_vs_sma50": price_vs_sma50,
            "trend": trend,
            "signal": signal,
            "description": description,
            "timestamp": datetime.utcnow().isoformat() + "Z",
            "source": "coingecko"
        }
        
    except Exception as e:
        logger.error(f"❌ {indicator_name} - Unexpected error: {e}", exc_info=True)
        return JSONResponse(
            status_code=500,
            content={
                "error": True,
                "message": "Internal server error"
            }
        )


@router.get("/ema")
async def get_ema(
    symbol: str = Query(default="BTC", description="Cryptocurrency symbol"),
    timeframe: str = Query(default="1h", description="Timeframe")
):
    """Calculate Exponential Moving Averages for a symbol - PRODUCTION SAFE"""
    indicator_name = "EMA"
    logger.info(f"πŸ“Š {indicator_name} - Endpoint called: symbol={symbol}, timeframe={timeframe}")
    
    try:
        # Fetch OHLCV data
        from backend.services.coingecko_client import coingecko_client
        
        try:
            ohlcv = await coingecko_client.get_ohlcv(symbol, days=90)
        except Exception as e:
            logger.error(f"❌ {indicator_name} - Failed to fetch OHLCV: {e}")
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": "Unable to fetch market data for this symbol"
                }
            )
        
        # Validate OHLCV data
        min_required = MIN_CANDLES["EMA"]
        is_valid, prices, error_msg = validate_ohlcv_data(ohlcv, min_required, symbol, indicator_name)
        
        if not is_valid:
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": error_msg,
                    "symbol": symbol.upper(),
                    "timeframe": timeframe,
                    "indicator": "ema",
                    "data_points": 0
                }
            )
        
        # Calculate EMAs
        try:
            current_price = prices[-1] if prices else 0
            
            ema12 = calculate_ema(prices, 12)
            ema26 = calculate_ema(prices, 26)
            ema50 = calculate_ema(prices, 50) if len(prices) >= 50 else None
            
            # Sanitize
            current_price = sanitize_value(current_price) or 0
            ema12 = sanitize_value(ema12) or 0
            ema26 = sanitize_value(ema26) or 0
            ema50 = sanitize_value(ema50) if ema50 is not None else None
            
        except Exception as e:
            logger.error(f"❌ {indicator_name} - Calculation failed: {e}", exc_info=True)
            return JSONResponse(
                status_code=500,
                content={
                    "error": True,
                    "message": "Internal indicator calculation error"
                }
            )
        
        # Determine trend
        if ema12 > ema26:
            if current_price > ema12:
                trend = "strong_bullish"
                signal = "buy"
                description = "Strong bullish - price above rising EMAs"
            else:
                trend = "bullish"
                signal = "buy"
                description = "Bullish EMAs - EMA12 above EMA26"
        else:
            if current_price < ema12:
                trend = "strong_bearish"
                signal = "sell"
                description = "Strong bearish - price below falling EMAs"
            else:
                trend = "bearish"
                signal = "sell"
                description = "Bearish EMAs - EMA12 below EMA26"
        
        logger.info(f"βœ… {indicator_name} - Success: symbol={symbol}, trend={trend}")
        
        return {
            "success": True,
            "symbol": symbol.upper(),
            "timeframe": timeframe,
            "indicator": "ema",
            "value": {
                "ema12": round(ema12, 8),
                "ema26": round(ema26, 8),
                "ema50": round(ema50, 8) if ema50 else None
            },
            "data": {
                "ema12": round(ema12, 8),
                "ema26": round(ema26, 8),
                "ema50": round(ema50, 8) if ema50 else None
            },
            "current_price": round(current_price, 8),
            "data_points": len(prices),
            "trend": trend,
            "signal": signal,
            "description": description,
            "timestamp": datetime.utcnow().isoformat() + "Z",
            "source": "coingecko"
        }
        
    except Exception as e:
        logger.error(f"❌ {indicator_name} - Unexpected error: {e}", exc_info=True)
        return JSONResponse(
            status_code=500,
            content={
                "error": True,
                "message": "Internal server error"
            }
        )


@router.get("/macd")
async def get_macd(
    symbol: str = Query(default="BTC", description="Cryptocurrency symbol"),
    timeframe: str = Query(default="1h", description="Timeframe"),
    fast: int = Query(default=12, description="Fast EMA period"),
    slow: int = Query(default=26, description="Slow EMA period"),
    signal_period: int = Query(default=9, description="Signal line period")
):
    """Calculate MACD for a symbol - PRODUCTION SAFE"""
    indicator_name = "MACD"
    logger.info(f"πŸ“Š {indicator_name} - Endpoint called: symbol={symbol}, timeframe={timeframe}, fast={fast}, slow={slow}, signal={signal_period}")
    
    try:
        # Validate parameters
        if fast >= slow:
            logger.warning(f"❌ {indicator_name} - Invalid parameters: fast={fast} must be < slow={slow}")
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": f"Invalid parameters: fast period ({fast}) must be less than slow period ({slow})"
                }
            )
        
        # Fetch OHLCV data
        from backend.services.coingecko_client import coingecko_client
        
        try:
            ohlcv = await coingecko_client.get_ohlcv(symbol, days=90)
        except Exception as e:
            logger.error(f"❌ {indicator_name} - Failed to fetch OHLCV: {e}")
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": "Unable to fetch market data for this symbol"
                }
            )
        
        # Validate OHLCV data
        min_required = MIN_CANDLES["MACD"]
        is_valid, prices, error_msg = validate_ohlcv_data(ohlcv, min_required, symbol, indicator_name)
        
        if not is_valid:
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": error_msg,
                    "symbol": symbol.upper(),
                    "timeframe": timeframe,
                    "indicator": "macd",
                    "data_points": 0
                }
            )
        
        # Calculate MACD
        try:
            macd = calculate_macd(prices, fast, slow, signal_period)
            
            # Sanitize output
            macd = sanitize_dict(macd)
            
        except Exception as e:
            logger.error(f"❌ {indicator_name} - Calculation failed: {e}", exc_info=True)
            return JSONResponse(
                status_code=500,
                content={
                    "error": True,
                    "message": "Internal indicator calculation error"
                }
            )
        
        # Determine signal
        if macd["histogram"] > 0:
            if macd["macd_line"] > 0:
                trend = "strong_bullish"
                signal = "buy"
                description = "Strong bullish - MACD and histogram positive"
            else:
                trend = "bullish"
                signal = "buy"
                description = "Bullish crossover - MACD above signal"
        else:
            if macd["macd_line"] < 0:
                trend = "strong_bearish"
                signal = "sell"
                description = "Strong bearish - MACD and histogram negative"
            else:
                trend = "bearish"
                signal = "sell"
                description = "Bearish crossover - MACD below signal"
        
        logger.info(f"βœ… {indicator_name} - Success: symbol={symbol}, trend={trend}, signal={signal}")
        
        return {
            "success": True,
            "symbol": symbol.upper(),
            "timeframe": timeframe,
            "indicator": "macd",
            "value": macd,
            "data": macd,
            "data_points": len(prices),
            "trend": trend,
            "signal": signal,
            "description": description,
            "timestamp": datetime.utcnow().isoformat() + "Z",
            "source": "coingecko"
        }
        
    except Exception as e:
        logger.error(f"❌ {indicator_name} - Unexpected error: {e}", exc_info=True)
        return JSONResponse(
            status_code=500,
            content={
                "error": True,
                "message": "Internal server error"
            }
        )


@router.get("/rsi")
async def get_rsi(
    symbol: str = Query(default="BTC", description="Cryptocurrency symbol"),
    timeframe: str = Query(default="1h", description="Timeframe"),
    period: int = Query(default=14, description="RSI period")
):
    """Calculate RSI for a symbol - PRODUCTION SAFE"""
    indicator_name = "RSI"
    logger.info(f"πŸ“Š {indicator_name} - Endpoint called: symbol={symbol}, timeframe={timeframe}, period={period}")
    
    try:
        # Validate parameters
        if period < 1 or period > 100:
            logger.warning(f"❌ {indicator_name} - Invalid period: {period}")
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": f"Invalid period: must be between 1 and 100, got {period}"
                }
            )
        
        # Fetch OHLCV data
        from backend.services.coingecko_client import coingecko_client
        
        timeframe_days = {"1m": 1, "5m": 1, "15m": 1, "1h": 7, "4h": 30, "1d": 90}
        days = timeframe_days.get(timeframe, 7)
        
        try:
            ohlcv = await coingecko_client.get_ohlcv(symbol, days=days)
        except Exception as e:
            logger.error(f"❌ {indicator_name} - Failed to fetch OHLCV: {e}")
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": "Unable to fetch market data for this symbol"
                }
            )
        
        # Validate OHLCV data with minimum candle requirement
        min_required = MIN_CANDLES["RSI"]
        is_valid, prices, error_msg = validate_ohlcv_data(ohlcv, min_required, symbol, indicator_name)
        
        if not is_valid:
            return JSONResponse(
                status_code=400,
                content={
                    "error": True,
                    "message": error_msg,
                    "symbol": symbol.upper(),
                    "timeframe": timeframe,
                    "indicator": "rsi",
                    "data_points": 0
                }
            )
        
        # Calculate RSI
        try:
            rsi = calculate_rsi(prices, period)
            
            # Sanitize output
            rsi = sanitize_value(rsi)
            if rsi is None:
                raise ValueError("RSI calculation returned invalid value")
            
        except Exception as e:
            logger.error(f"❌ {indicator_name} - Calculation failed: {e}", exc_info=True)
            return JSONResponse(
                status_code=500,
                content={
                    "error": True,
                    "message": "Internal indicator calculation error"
                }
            )
        
        # Determine signal
        if rsi > 70:
            signal = "overbought"
            description = f"RSI at {rsi:.1f} - overbought conditions, potential pullback"
        elif rsi < 30:
            signal = "oversold"
            description = f"RSI at {rsi:.1f} - oversold conditions, potential bounce"
        elif rsi > 60:
            signal = "bullish"
            description = f"RSI at {rsi:.1f} - bullish momentum"
        elif rsi < 40:
            signal = "bearish"
            description = f"RSI at {rsi:.1f} - bearish momentum"
        else:
            signal = "neutral"
            description = f"RSI at {rsi:.1f} - neutral zone"
        
        logger.info(f"βœ… {indicator_name} - Success: symbol={symbol}, value={rsi:.2f}, signal={signal}")
        
        return {
            "success": True,
            "symbol": symbol.upper(),
            "timeframe": timeframe,
            "indicator": "rsi",
            "value": round(rsi, 2),
            "data": {"value": round(rsi, 2)},
            "data_points": len(prices),
            "signal": signal,
            "description": description,
            "timestamp": datetime.utcnow().isoformat() + "Z",
            "source": "coingecko"
        }
        
    except Exception as e:
        logger.error(f"❌ {indicator_name} - Unexpected error: {e}", exc_info=True)
        return JSONResponse(
            status_code=500,
            content={
                "error": True,
                "message": "Internal server error"
            }
        )


@router.get("/comprehensive")
async def get_comprehensive_analysis(
    symbol: str = Query(default="BTC", description="Cryptocurrency symbol"),
    timeframe: str = Query(default="1h", description="Timeframe")
):
    """Get comprehensive analysis with all indicators"""
    try:
        # Try to import coingecko client
        try:
            from backend.services.coingecko_client import coingecko_client
            client_available = True
        except ImportError as import_err:
            logger.error(f"CoinGecko client import failed: {import_err}")
            client_available = False
        
        # Try to get historical data if client is available
        ohlcv = None
        if client_available:
            try:
                ohlcv = await coingecko_client.get_ohlcv(symbol, days=365)
            except Exception as fetch_err:
                logger.error(f"Failed to fetch OHLCV data: {fetch_err}")
                ohlcv = None
        
        if not ohlcv or "prices" not in ohlcv:
            # Return comprehensive fallback with real structure
            current_price = 67500 if symbol.upper() == "BTC" else 3400 if symbol.upper() == "ETH" else 100
            logger.warning(f"Using fallback data for {symbol} - API unavailable")
            return {
                "success": True,
                "symbol": symbol.upper(),
                "timeframe": timeframe,
                "current_price": current_price,
                "indicators": {
                    "bollinger_bands": {"upper": current_price * 1.05, "middle": current_price, "lower": current_price * 0.95, "bandwidth": 10, "percent_b": 50},
                    "stoch_rsi": {"value": 50, "k_line": 50, "d_line": 50},
                    "atr": {"value": current_price * 0.02, "percent": 2.0},
                    "sma": {"sma20": current_price, "sma50": current_price * 0.98, "sma200": current_price * 0.95},
                    "ema": {"ema12": current_price, "ema26": current_price * 0.99},
                    "macd": {"macd_line": 50, "signal_line": 45, "histogram": 5},
                    "rsi": {"value": 55}
                },
                "signals": {
                    "bollinger_bands": "neutral",
                    "stoch_rsi": "neutral",
                    "atr": "medium_volatility",
                    "sma": "bullish",
                    "ema": "bullish",
                    "macd": "bullish",
                    "rsi": "neutral"
                },
                "overall_signal": "HOLD",
                "confidence": 60,
                "recommendation": "Mixed signals - wait for clearer direction. Note: Using fallback data as API is temporarily unavailable.",
                "timestamp": datetime.utcnow().isoformat() + "Z",
                "source": "fallback",
                "warning": "API temporarily unavailable - using fallback data"
            }
        
        prices = [p[1] for p in ohlcv["prices"]]
        current_price = prices[-1] if prices else 0
        
        # Calculate all indicators
        bb = calculate_bollinger_bands(prices, 20, 2)
        stoch = calculate_stoch_rsi(prices, 14, 14)
        
        # Approximate H/L for ATR
        highs = [p * 1.005 for p in prices]
        lows = [p * 0.995 for p in prices]
        atr_value = calculate_atr(highs, lows, prices, 14)
        atr_percent = (atr_value / current_price) * 100 if current_price > 0 else 0
        
        sma20 = calculate_sma(prices, 20)
        sma50 = calculate_sma(prices, 50)
        sma200 = calculate_sma(prices, 200) if len(prices) >= 200 else None
        
        ema12 = calculate_ema(prices, 12)
        ema26 = calculate_ema(prices, 26)
        
        macd = calculate_macd(prices, 12, 26, 9)
        rsi = calculate_rsi(prices, 14)
        
        # Determine individual signals
        signals = {}
        
        # BB signal
        if bb["percent_b"] > 80:
            signals["bollinger_bands"] = "overbought"
        elif bb["percent_b"] < 20:
            signals["bollinger_bands"] = "oversold"
        else:
            signals["bollinger_bands"] = "neutral"
        
        # Stoch RSI signal
        if stoch["value"] > 80:
            signals["stoch_rsi"] = "overbought"
        elif stoch["value"] < 20:
            signals["stoch_rsi"] = "oversold"
        else:
            signals["stoch_rsi"] = "neutral"
        
        # ATR signal
        if atr_percent > 5:
            signals["atr"] = "high_volatility"
        elif atr_percent < 1:
            signals["atr"] = "low_volatility"
        else:
            signals["atr"] = "medium_volatility"
        
        # SMA signal
        if current_price > sma20 and current_price > sma50:
            signals["sma"] = "bullish"
        elif current_price < sma20 and current_price < sma50:
            signals["sma"] = "bearish"
        else:
            signals["sma"] = "neutral"
        
        # EMA signal
        if ema12 > ema26:
            signals["ema"] = "bullish"
        else:
            signals["ema"] = "bearish"
        
        # MACD signal
        if macd["histogram"] > 0:
            signals["macd"] = "bullish"
        else:
            signals["macd"] = "bearish"
        
        # RSI signal
        if rsi > 70:
            signals["rsi"] = "overbought"
        elif rsi < 30:
            signals["rsi"] = "oversold"
        elif rsi > 50:
            signals["rsi"] = "bullish"
        else:
            signals["rsi"] = "bearish"
        
        # Calculate overall signal
        bullish_count = sum(1 for s in signals.values() if s in ["bullish", "oversold"])
        bearish_count = sum(1 for s in signals.values() if s in ["bearish", "overbought"])
        
        if bullish_count >= 5:
            overall_signal = "STRONG_BUY"
            confidence = 85
            recommendation = "Strong bullish signals across multiple indicators - consider buying"
        elif bullish_count >= 4:
            overall_signal = "BUY"
            confidence = 70
            recommendation = "Majority bullish signals - favorable conditions for entry"
        elif bearish_count >= 5:
            overall_signal = "STRONG_SELL"
            confidence = 85
            recommendation = "Strong bearish signals across multiple indicators - consider selling"
        elif bearish_count >= 4:
            overall_signal = "SELL"
            confidence = 70
            recommendation = "Majority bearish signals - unfavorable conditions"
        else:
            overall_signal = "HOLD"
            confidence = 50
            recommendation = "Mixed signals - wait for clearer direction before taking action"
        
        return {
            "success": True,
            "symbol": symbol.upper(),
            "timeframe": timeframe,
            "current_price": round(current_price, 8),
            "indicators": {
                "bollinger_bands": bb,
                "stoch_rsi": stoch,
                "atr": {"value": round(atr_value, 8), "percent": round(atr_percent, 2)},
                "sma": {"sma20": round(sma20, 8), "sma50": round(sma50, 8), "sma200": round(sma200, 8) if sma200 else None},
                "ema": {"ema12": round(ema12, 8), "ema26": round(ema26, 8)},
                "macd": macd,
                "rsi": {"value": round(rsi, 2)}
            },
            "signals": signals,
            "overall_signal": overall_signal,
            "confidence": confidence,
            "recommendation": recommendation,
            "timestamp": datetime.utcnow().isoformat() + "Z",
            "source": "coingecko"
        }
        
    except Exception as e:
        logger.error(f"Comprehensive analysis error: {e}")
        # Instead of raising 500, return a proper error response with structure
        current_price = 67500 if symbol.upper() == "BTC" else 3400 if symbol.upper() == "ETH" else 100
        return {
            "success": False,
            "error": "Analysis failed - using fallback data",
            "error_detail": str(e),
            "symbol": symbol.upper(),
            "timeframe": timeframe,
            "current_price": current_price,
            "indicators": {
                "bollinger_bands": {"upper": current_price * 1.05, "middle": current_price, "lower": current_price * 0.95, "bandwidth": 10, "percent_b": 50},
                "stoch_rsi": {"value": 50, "k_line": 50, "d_line": 50},
                "atr": {"value": current_price * 0.02, "percent": 2.0},
                "sma": {"sma20": current_price, "sma50": current_price * 0.98, "sma200": current_price * 0.95},
                "ema": {"ema12": current_price, "ema26": current_price * 0.99},
                "macd": {"macd_line": 50, "signal_line": 45, "histogram": 5},
                "rsi": {"value": 55}
            },
            "signals": {
                "bollinger_bands": "neutral",
                "stoch_rsi": "neutral",
                "atr": "medium_volatility",
                "sma": "bullish",
                "ema": "bullish",
                "macd": "bullish",
                "rsi": "neutral"
            },
            "overall_signal": "HOLD",
            "confidence": 0,
            "recommendation": "Unable to perform analysis due to technical error. Please try again later.",
            "timestamp": datetime.utcnow().isoformat() + "Z",
            "source": "error_fallback"
        }