Spaces:
Running
on
Zero
Running
on
Zero
File size: 152,205 Bytes
f5e0821 877eedc f5e0821 fe5cebb 877eedc db1cbec 654a165 fd75294 8481b42 f5e0821 fe5cebb f5e0821 fe5cebb f5e0821 8481b42 fd75294 db1cbec a96f134 f5e0821 61754ec f5e0821 db1cbec 248f443 db1cbec 8f98e80 61754ec 2384e68 61754ec db1cbec 2384e68 db1cbec 2384e68 248f443 db1cbec db47567 6b080c6 f5e0821 654a165 fe5cebb f5e0821 e115d61 f5e0821 fe5cebb f5e0821 fe5cebb f5e0821 654a165 bd7c6bf f5e0821 e115d61 bd7c6bf e115d61 bd7c6bf e115d61 bd7c6bf f5e0821 fe5cebb f5e0821 fd75294 f5e0821 fd75294 e115d61 fd75294 f5e0821 bd7c6bf 165a173 fd75294 1c92324 fd75294 1c92324 165a173 e115d61 1c92324 165a173 e115d61 1702ce4 bd7c6bf cee45d4 fe5cebb cee45d4 bd7c6bf 1702ce4 bd7c6bf 1702ce4 bd7c6bf 1702ce4 cee45d4 f5e0821 e115d61 165a173 1c92324 cee45d4 fd75294 cee45d4 fd75294 e115d61 fd75294 cee45d4 1c92324 f5e0821 bd7c6bf 165a173 fe5cebb f5e0821 fe5cebb 165a173 248f443 fe5cebb 165a173 248f443 fe5cebb 165a173 248f443 fe5cebb 3d44e72 8481b42 9dc46d3 ef5069d f5e0821 8481b42 f5e0821 09db56d f5e0821 fe5cebb 8481b42 9dc46d3 73f8121 ef5069d f5e0821 8828171 f5e0821 fe5cebb f5e0821 fe5cebb db1cbec 2665f90 ef5069d 737efc2 ef5069d 9dc46d3 737efc2 bd7c6bf ef5069d 2665f90 bd7c6bf 61754ec db1cbec cd87fec 6616ba7 8f98e80 cf7c042 6616ba7 cf7c042 6616ba7 cf7c042 09db56d ef5069d a65d91d 9dc46d3 a65d91d ef5069d a65d91d 9dc46d3 a65d91d ef5069d a65d91d 9dc46d3 bd7c6bf db1cbec f2f7fa2 579637f b94ef5d 579637f b94ef5d efd66db b94ef5d efd66db b94ef5d efd66db b94ef5d 0025d7f 0bddd1c 0025d7f 0bddd1c 0025d7f b94ef5d 579637f b94ef5d 027a49e b94ef5d 248f443 b94ef5d da0507e b94ef5d f2f7fa2 b94ef5d f2f7fa2 b94ef5d 579637f 9dc46d3 579637f 737efc2 9dc46d3 737efc2 9dc46d3 737efc2 73f8121 737efc2 ef5069d 9dc46d3 17ea549 ef5069d 17ea549 9dc46d3 ef5069d 17ea549 ef5069d 73f8121 ef5069d 73f8121 ef5069d 73f8121 ef5069d 17ea549 a65d91d 9dc46d3 a65d91d 9dc46d3 ef5069d 17ea549 a65d91d 17ea549 ef5069d 17ea549 ef5069d 17ea549 ef5069d 9dc46d3 ef5069d 17ea549 9dc46d3 17ea549 9dc46d3 db1cbec 65bf228 ef5069d 65bf228 ef5069d 65bf228 737efc2 ef5069d 9dc46d3 ef5069d 9dc46d3 65bf228 9dc46d3 65bf228 9dc46d3 65bf228 9dc46d3 ef5069d 9dc46d3 737efc2 73f8121 737efc2 ef5069d 65bf228 9dc46d3 ef5069d 9dc46d3 ef5069d 9dc46d3 ef5069d 73f8121 ef5069d 73f8121 ef5069d 9dc46d3 73f8121 ef5069d 9dc46d3 ef5069d 9dc46d3 ef5069d 9dc46d3 ef5069d 9dc46d3 65bf228 9dc46d3 ef5069d 65bf228 ef5069d 65bf228 ef5069d 65bf228 737efc2 ef5069d 9dc46d3 ef5069d 9dc46d3 65bf228 9dc46d3 65bf228 9dc46d3 65bf228 9dc46d3 65bf228 9dc46d3 ef5069d 9dc46d3 737efc2 ef5069d 65bf228 9dc46d3 ef5069d 9dc46d3 ef5069d 9dc46d3 ef5069d 73f8121 ef5069d 73f8121 ef5069d 9dc46d3 73f8121 ef5069d 9dc46d3 ef5069d 9dc46d3 ef5069d 9dc46d3 ef5069d 9dc46d3 65bf228 9dc46d3 ef5069d 65bf228 ef5069d 65bf228 ef5069d 65bf228 737efc2 ef5069d 9dc46d3 ef5069d 9dc46d3 65bf228 9dc46d3 65bf228 9dc46d3 65bf228 9dc46d3 ef5069d 9dc46d3 737efc2 73f8121 737efc2 ef5069d 9dc46d3 ef5069d 9dc46d3 ef5069d 9dc46d3 ef5069d 73f8121 ef5069d 73f8121 ef5069d 9dc46d3 73f8121 ef5069d 9dc46d3 ef5069d 9dc46d3 ef5069d 9dc46d3 ef5069d 9dc46d3 65bf228 9dc46d3 db1cbec 579637f db1cbec fe5cebb f5e0821 09db56d fe5cebb 09db56d f5e0821 fe5cebb f5e0821 fe5cebb f5e0821 fe5cebb f5e0821 fe5cebb f5e0821 fe5cebb f5e0821 65bf228 fe5cebb 65bf228 09db56d f5e0821 09db56d f5e0821 fe5cebb f5e0821 fe5cebb 8828171 f5e0821 09db56d f5e0821 09db56d db1cbec 8828171 65bf228 7d6fae7 8828171 f5e0821 db1cbec f5e0821 fe5cebb 8481b42 542229d 8481b42 542229d 8481b42 b7e9276 8481b42 7a5abd6 542229d 7a5abd6 542229d 7a5abd6 8481b42 542229d 8481b42 542229d 8481b42 542229d 8481b42 542229d 8481b42 542229d 8481b42 c9bad9c 8481b42 c9bad9c 8481b42 c9bad9c 8481b42 c9bad9c 8481b42 b7e9276 8481b42 b7e9276 8481b42 b7e9276 8481b42 b7e9276 8481b42 b7e9276 8481b42 b7e9276 8481b42 b7e9276 8481b42 b7e9276 8481b42 b7e9276 8481b42 ef5069d f5e0821 09db56d 8481b42 f5e0821 654a165 8481b42 73f8121 ef5069d 8481b42 9dc46d3 8481b42 09db56d ef5069d 09db56d 8481b42 09db56d 8481b42 654a165 7a5abd6 8481b42 f2e16b4 8481b42 f5e0821 09db56d e115d61 09db56d 6655309 e115d61 6655309 09db56d 1702ce4 09db56d 7d6fae7 09db56d 7d6fae7 e115d61 09db56d 7d6fae7 09db56d e115d61 09db56d 6655309 e115d61 6655309 09db56d 1702ce4 09db56d 7d6fae7 09db56d 7d6fae7 e115d61 09db56d 7d6fae7 1702ce4 8481b42 9dc46d3 ef5069d 6655309 8481b42 9dc46d3 73f8121 ef5069d 8481b42 6655309 e115d61 6655309 8481b42 6655309 e115d61 6655309 e115d61 8481b42 7a5abd6 6655309 654a165 6655309 1702ce4 09db56d 8481b42 9dc46d3 ef5069d eb30d21 8481b42 eb30d21 3d44e72 eb30d21 3d44e72 eb30d21 3d44e72 eb30d21 3d44e72 eb30d21 3d44e72 8481b42 3d44e72 8481b42 3d44e72 8481b42 3d44e72 9dc46d3 73f8121 ef5069d eb30d21 3d44e72 ef5069d 3d44e72 73f8121 eb30d21 ef5069d eb30d21 09db56d eb30d21 8481b42 9dc46d3 ef5069d 8481b42 7a5abd6 09db56d 8481b42 9dc46d3 ef5069d eb30d21 8481b42 eb30d21 3d44e72 eb30d21 3d44e72 eb30d21 3d44e72 e115d61 3d44e72 eb30d21 3d44e72 f2e16b4 3d44e72 f2e16b4 3d44e72 f2e16b4 3d44e72 9dc46d3 73f8121 ef5069d eb30d21 3d44e72 ef5069d 3d44e72 73f8121 eb30d21 ef5069d eb30d21 09db56d eb30d21 f2e16b4 9dc46d3 ef5069d 7d6fae7 7a5abd6 09db56d f2e16b4 9dc46d3 ef5069d eb30d21 f2e16b4 eb30d21 3d44e72 eb30d21 3d44e72 eb30d21 3d44e72 e115d61 3d44e72 eb30d21 3d44e72 f2e16b4 3d44e72 f2e16b4 3d44e72 f2e16b4 3d44e72 9dc46d3 73f8121 ef5069d eb30d21 3d44e72 ef5069d 3d44e72 73f8121 eb30d21 ef5069d eb30d21 09db56d eb30d21 f2e16b4 9dc46d3 ef5069d 7d6fae7 7a5abd6 f5e0821 762264d |
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 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 |
import gradio as gr
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import yfinance as yf
import torch
from chronos import ChronosPipeline
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from sklearn.preprocessing import MinMaxScaler
import plotly.express as px
from typing import Dict, List, Tuple, Optional
import json
import spaces
import gc
import pytz
import time
import random
from scipy import stats
from scipy.optimize import minimize
import warnings
warnings.filterwarnings('ignore')
# Additional imports for advanced features
try:
from hmmlearn import hmm
HMM_AVAILABLE = True
except ImportError:
HMM_AVAILABLE = False
print("Warning: hmmlearn not available. Regime detection will use simplified methods.")
try:
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
ENSEMBLE_AVAILABLE = True
except ImportError:
ENSEMBLE_AVAILABLE = False
print("Warning: scikit-learn not available. Ensemble methods will be simplified.")
# Initialize global variables
pipeline = None
scaler = MinMaxScaler(feature_range=(-1, 1))
scaler.fit_transform([[-1, 1]])
# Global market data cache
market_data_cache = {}
cache_expiry = {}
CACHE_DURATION = 3600 # 1 hour cache
def retry_yfinance_request(func, max_retries=3, initial_delay=1):
"""
Retry mechanism for yfinance requests with exponential backoff.
Args:
func: Function to retry
max_retries: Maximum number of retry attempts
initial_delay: Initial delay in seconds before first retry
Returns:
Result of the function call if successful
"""
for attempt in range(max_retries):
try:
return func()
except Exception as e:
if "401" in str(e) and attempt < max_retries - 1:
# Calculate delay with exponential backoff and jitter
delay = initial_delay * (2 ** attempt) + random.uniform(0, 1)
time.sleep(delay)
continue
raise e
def clear_gpu_memory():
"""Clear GPU memory cache"""
if torch.cuda.is_available():
torch.cuda.empty_cache()
gc.collect()
@spaces.GPU()
def load_pipeline():
"""Load the Chronos model without GPU configuration"""
global pipeline
try:
if pipeline is None:
clear_gpu_memory()
print("Loading Chronos model...")
pipeline = ChronosPipeline.from_pretrained(
"amazon/chronos-t5-large",
device_map="cuda", # Force CUDA device mapping
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
trust_remote_code=True,
use_safetensors=True
)
# Set model to evaluation mode
pipeline.model = pipeline.model.eval()
# Disable gradient computation
for param in pipeline.model.parameters():
param.requires_grad = False
print("Chronos model loaded successfully")
return pipeline
except Exception as e:
print(f"Error loading pipeline: {str(e)}")
print(f"Error type: {type(e)}")
print(f"Error details: {str(e)}")
raise RuntimeError(f"Failed to load model: {str(e)}")
def is_market_open() -> bool:
"""Check if the market is currently open"""
now = datetime.now()
# Check if it's a weekday (0 = Monday, 6 = Sunday)
if now.weekday() >= 5: # Saturday or Sunday
return False
# Check if it's during market hours (9:30 AM - 4:00 PM ET)
et_time = now.astimezone(pytz.timezone('US/Eastern'))
market_open = et_time.replace(hour=9, minute=30, second=0, microsecond=0)
market_close = et_time.replace(hour=16, minute=0, second=0, microsecond=0)
return market_open <= et_time <= market_close
def get_next_trading_day() -> datetime:
"""Get the next trading day"""
now = datetime.now()
next_day = now + timedelta(days=1)
# Skip weekends
while next_day.weekday() >= 5: # Saturday or Sunday
next_day += timedelta(days=1)
return next_day
def get_historical_data(symbol: str, timeframe: str = "1d", lookback_days: int = 365) -> pd.DataFrame:
"""
Fetch historical data using yfinance with enhanced support for intraday data.
Args:
symbol (str): The stock symbol (e.g., 'AAPL')
timeframe (str): The timeframe for data ('1d', '1h', '15m')
lookback_days (int): Number of days to look back
Returns:
pd.DataFrame: Historical data with OHLCV and technical indicators
"""
try:
# Check if market is open for intraday data
if timeframe in ["1h", "15m"] and not is_market_open():
next_trading_day = get_next_trading_day()
raise Exception(f"Market is currently closed. Next trading day is {next_trading_day.strftime('%Y-%m-%d')}")
# Map timeframe to yfinance interval and adjust lookback period
tf_map = {
"1d": "1d",
"1h": "1h",
"15m": "15m"
}
interval = tf_map.get(timeframe, "1d")
# Adjust lookback period based on timeframe and yfinance limits
if timeframe == "1h":
lookback_days = min(lookback_days, 60) # Yahoo allows up to 60 days for hourly data
elif timeframe == "15m":
lookback_days = min(lookback_days, 7) # Yahoo allows up to 7 days for 15m data
# Calculate date range
end_date = datetime.now()
start_date = end_date - timedelta(days=lookback_days)
# Fetch data using yfinance with retry mechanism
ticker = yf.Ticker(symbol)
def fetch_history():
return ticker.history(
start=start_date,
end=end_date,
interval=interval,
prepost=True, # Include pre/post market data for intraday
actions=True, # Include dividends and splits
auto_adjust=True, # Automatically adjust for splits
back_adjust=True, # Back-adjust data for splits
repair=True # Repair missing data points
)
df = retry_yfinance_request(fetch_history)
if df.empty:
raise Exception(f"No data available for {symbol} in {timeframe} timeframe")
# Ensure all required columns are present and numeric
required_columns = ['Open', 'High', 'Low', 'Close', 'Volume']
for col in required_columns:
if col not in df.columns:
raise Exception(f"Missing required column: {col}")
df[col] = pd.to_numeric(df[col], errors='coerce')
# Get additional info for structured products with retry mechanism
def fetch_info():
info = ticker.info
if info is None:
raise Exception(f"Could not fetch company info for {symbol}")
return info
try:
info = retry_yfinance_request(fetch_info)
df['Market_Cap'] = float(info.get('marketCap', 0))
df['Sector'] = info.get('sector', 'Unknown')
df['Industry'] = info.get('industry', 'Unknown')
df['Dividend_Yield'] = float(info.get('dividendYield', 0))
# Add additional company metrics
df['Enterprise_Value'] = float(info.get('enterpriseValue', 0))
df['P/E_Ratio'] = float(info.get('trailingPE', 0))
df['Forward_P/E'] = float(info.get('forwardPE', 0))
df['PEG_Ratio'] = float(info.get('pegRatio', 0))
df['Price_to_Book'] = float(info.get('priceToBook', 0))
df['Price_to_Sales'] = float(info.get('priceToSalesTrailing12Months', 0))
df['Return_on_Equity'] = float(info.get('returnOnEquity', 0))
df['Return_on_Assets'] = float(info.get('returnOnAssets', 0))
df['Debt_to_Equity'] = float(info.get('debtToEquity', 0))
df['Current_Ratio'] = float(info.get('currentRatio', 0))
df['Quick_Ratio'] = float(info.get('quickRatio', 0))
df['Gross_Margin'] = float(info.get('grossMargins', 0))
df['Operating_Margin'] = float(info.get('operatingMargins', 0))
df['Net_Margin'] = float(info.get('netIncomeToCommon', 0))
except Exception as e:
print(f"Warning: Could not fetch company info for {symbol}: {str(e)}")
# Set default values for missing info
df['Market_Cap'] = 0.0
df['Sector'] = 'Unknown'
df['Industry'] = 'Unknown'
df['Dividend_Yield'] = 0.0
df['Enterprise_Value'] = 0.0
df['P/E_Ratio'] = 0.0
df['Forward_P/E'] = 0.0
df['PEG_Ratio'] = 0.0
df['Price_to_Book'] = 0.0
df['Price_to_Sales'] = 0.0
df['Return_on_Equity'] = 0.0
df['Return_on_Assets'] = 0.0
df['Debt_to_Equity'] = 0.0
df['Current_Ratio'] = 0.0
df['Quick_Ratio'] = 0.0
df['Gross_Margin'] = 0.0
df['Operating_Margin'] = 0.0
df['Net_Margin'] = 0.0
# Calculate technical indicators with adjusted windows based on timeframe
if timeframe == "1d":
sma_window_20 = 20
sma_window_50 = 50
sma_window_200 = 200
vol_window = 20
elif timeframe == "1h":
sma_window_20 = 20 * 6 # 5 trading days
sma_window_50 = 50 * 6 # ~10 trading days
sma_window_200 = 200 * 6 # ~40 trading days
vol_window = 20 * 6
else: # 15m
sma_window_20 = 20 * 24 # 5 trading days
sma_window_50 = 50 * 24 # ~10 trading days
sma_window_200 = 200 * 24 # ~40 trading days
vol_window = 20 * 24
# Calculate technical indicators
df['SMA_20'] = df['Close'].rolling(window=sma_window_20, min_periods=1).mean()
df['SMA_50'] = df['Close'].rolling(window=sma_window_50, min_periods=1).mean()
df['SMA_200'] = df['Close'].rolling(window=sma_window_200, min_periods=1).mean()
df['RSI'] = calculate_rsi(df['Close'])
df['MACD'], df['MACD_Signal'] = calculate_macd(df['Close'])
df['BB_Upper'], df['BB_Middle'], df['BB_Lower'] = calculate_bollinger_bands(df['Close'])
# Calculate returns and volatility
df['Returns'] = df['Close'].pct_change()
df['Volatility'] = df['Returns'].rolling(window=vol_window, min_periods=1).std()
df['Annualized_Vol'] = df['Volatility'] * np.sqrt(252)
# Calculate drawdown metrics
df['Rolling_Max'] = df['Close'].rolling(window=len(df), min_periods=1).max()
df['Drawdown'] = (df['Close'] - df['Rolling_Max']) / df['Rolling_Max']
df['Max_Drawdown'] = df['Drawdown'].rolling(window=len(df), min_periods=1).min()
# Calculate liquidity metrics
df['Avg_Daily_Volume'] = df['Volume'].rolling(window=vol_window, min_periods=1).mean()
df['Volume_Volatility'] = df['Volume'].rolling(window=vol_window, min_periods=1).std()
# Calculate additional intraday metrics for shorter timeframes
if timeframe in ["1h", "15m"]:
# Intraday volatility
df['Intraday_High_Low'] = (df['High'] - df['Low']) / df['Close']
df['Intraday_Volatility'] = df['Intraday_High_Low'].rolling(window=vol_window, min_periods=1).mean()
# Volume analysis
df['Volume_Price_Trend'] = (df['Volume'] * df['Returns']).rolling(window=vol_window, min_periods=1).sum()
df['Volume_SMA'] = df['Volume'].rolling(window=vol_window, min_periods=1).mean()
df['Volume_Ratio'] = df['Volume'] / df['Volume_SMA']
# Price momentum
df['Price_Momentum'] = df['Close'].pct_change(periods=5)
df['Volume_Momentum'] = df['Volume'].pct_change(periods=5)
# Fill NaN values using forward fill then backward fill
df = df.ffill().bfill()
# Ensure we have enough data points
min_required_points = 64 # Minimum required for Chronos
if len(df) < min_required_points:
# Try to fetch more historical data with retry mechanism
extended_start_date = start_date - timedelta(days=min_required_points - len(df))
def fetch_extended_history():
return ticker.history(
start=extended_start_date,
end=start_date,
interval=interval,
prepost=True,
actions=True,
auto_adjust=True,
back_adjust=True,
repair=True
)
extended_df = retry_yfinance_request(fetch_extended_history)
if not extended_df.empty:
df = pd.concat([extended_df, df])
df = df.ffill().bfill()
if len(df) < 2:
raise Exception(f"Insufficient data points for {symbol} in {timeframe} timeframe")
# Final check for any remaining None values
df = df.fillna(0)
return df
except Exception as e:
raise Exception(f"Error fetching historical data for {symbol}: {str(e)}")
def calculate_rsi(prices: pd.Series, period: int = 14) -> pd.Series:
"""Calculate Relative Strength Index"""
# Handle None values by forward filling
prices = prices.ffill().bfill()
delta = prices.diff()
gain = (delta.where(delta > 0, 0)).rolling(window=period).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean()
rs = gain / loss
return 100 - (100 / (1 + rs))
def calculate_macd(prices: pd.Series, fast: int = 12, slow: int = 26, signal: int = 9) -> Tuple[pd.Series, pd.Series]:
"""Calculate MACD and Signal line"""
# Handle None values by forward filling
prices = prices.ffill().bfill()
exp1 = prices.ewm(span=fast, adjust=False).mean()
exp2 = prices.ewm(span=slow, adjust=False).mean()
macd = exp1 - exp2
signal_line = macd.ewm(span=signal, adjust=False).mean()
return macd, signal_line
def calculate_bollinger_bands(prices: pd.Series, period: int = 20, std_dev: int = 2) -> Tuple[pd.Series, pd.Series, pd.Series]:
"""Calculate Bollinger Bands"""
# Handle None values by forward filling
prices = prices.ffill().bfill()
middle_band = prices.rolling(window=period).mean()
std = prices.rolling(window=period).std()
upper_band = middle_band + (std * std_dev)
lower_band = middle_band - (std * std_dev)
return upper_band, middle_band, lower_band
@spaces.GPU()
def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5, strategy: str = "chronos",
use_ensemble: bool = True, use_regime_detection: bool = True, use_stress_testing: bool = True,
risk_free_rate: float = 0.02, ensemble_weights: Dict = None,
market_index: str = "^GSPC",
random_real_points: int = 4, use_smoothing: bool = True,
smoothing_type: str = "exponential", smoothing_window: int = 5,
smoothing_alpha: float = 0.3) -> Tuple[Dict, go.Figure]:
"""
Make prediction using selected strategy with advanced features.
Args:
symbol (str): Stock symbol
timeframe (str): Data timeframe ('1d', '1h', '15m')
prediction_days (int): Number of days to predict
strategy (str): Prediction strategy to use
use_ensemble (bool): Whether to use ensemble methods
use_regime_detection (bool): Whether to use regime detection
use_stress_testing (bool): Whether to perform stress testing
risk_free_rate (float): Risk-free rate for calculations
ensemble_weights (Dict): Weights for ensemble models
market_index (str): Market index for correlation analysis
random_real_points (int): Number of random real points to include in long-horizon context
use_smoothing (bool): Whether to apply smoothing to predictions
smoothing_type (str): Type of smoothing to apply ('exponential', 'moving_average', 'kalman', 'savitzky_golay', 'none')
Returns:
Tuple[Dict, go.Figure]: Trading signals and visualization plot
"""
try:
# Get historical data
df = get_historical_data(symbol, timeframe)
if strategy == "chronos":
try:
# Prepare data for Chronos
prices = df['Close'].values
chronos_context_size = 64 # Chronos model's context window size (fixed at 64)
input_context_size = len(prices) # Available input data can be much larger
# Use a larger range for scaler fitting to get better normalization
scaler_range = min(input_context_size, chronos_context_size * 2) # Use up to 128 points for scaler
# Select the most recent chronos_context_size points for the model input
context_window = prices[-chronos_context_size:]
scaler = MinMaxScaler(feature_range=(-1, 1))
# Fit scaler on a larger range for better normalization
scaler.fit(prices[-scaler_range:].reshape(-1, 1))
normalized_prices = scaler.transform(context_window.reshape(-1, 1)).flatten()
# Ensure we have enough data points for Chronos
min_data_points = chronos_context_size
if len(normalized_prices) < min_data_points:
padding = np.full(min_data_points - len(normalized_prices), normalized_prices[-1])
normalized_prices = np.concatenate([padding, normalized_prices])
elif len(normalized_prices) > min_data_points:
normalized_prices = normalized_prices[-min_data_points:]
# Load pipeline and move to GPU
pipe = load_pipeline()
# Get the model's device and dtype
device = torch.device("cuda:0") # Force CUDA device
dtype = torch.float16 # Force float16
print(f"Model device: {device}")
print(f"Model dtype: {dtype}")
# Convert to tensor and ensure proper shape and device
context = torch.tensor(normalized_prices, dtype=dtype, device=device)
# Adjust prediction length based on timeframe
if timeframe == "1d":
max_prediction_length = chronos_context_size # 64 days
actual_prediction_length = min(prediction_days, max_prediction_length)
trim_length = prediction_days
elif timeframe == "1h":
max_prediction_length = chronos_context_size # 64 hours
actual_prediction_length = min(prediction_days * 24, max_prediction_length)
trim_length = prediction_days * 24
else: # 15m
max_prediction_length = chronos_context_size # 64 intervals
actual_prediction_length = min(prediction_days * 96, max_prediction_length)
trim_length = prediction_days * 96
actual_prediction_length = max(1, actual_prediction_length)
# Use predict_quantiles with proper formatting
with torch.amp.autocast('cuda'):
# Ensure all inputs are on GPU
context = context.to(device)
# Move quantile levels to GPU
quantile_levels = torch.tensor([0.1, 0.5, 0.9], device=device, dtype=dtype)
# Ensure prediction length is on GPU
prediction_length = torch.tensor(actual_prediction_length, device=device, dtype=torch.long)
# Force all model components to GPU
pipe.model = pipe.model.to(device)
# Move model to evaluation mode
pipe.model.eval()
# Ensure context is properly shaped and on GPU
if len(context.shape) == 1:
context = context.unsqueeze(0)
context = context.to(device)
# Move all model parameters and buffers to GPU
for param in pipe.model.parameters():
param.data = param.data.to(device)
for buffer in pipe.model.buffers():
buffer.data = buffer.data.to(device)
# Move all model submodules to GPU
for module in pipe.model.modules():
if hasattr(module, 'to'):
module.to(device)
# Move all model attributes to GPU
for name, value in pipe.model.__dict__.items():
if isinstance(value, torch.Tensor):
pipe.model.__dict__[name] = value.to(device)
# Move all model config tensors to GPU
if hasattr(pipe.model, 'config'):
for key, value in pipe.model.config.__dict__.items():
if isinstance(value, torch.Tensor):
setattr(pipe.model.config, key, value.to(device))
# Move all pipeline tensors to GPU
for name, value in pipe.__dict__.items():
if isinstance(value, torch.Tensor):
setattr(pipe, name, value.to(device))
# Ensure all model states are on GPU
if hasattr(pipe.model, 'state_dict'):
state_dict = pipe.model.state_dict()
for key in state_dict:
if isinstance(state_dict[key], torch.Tensor):
state_dict[key] = state_dict[key].to(device)
pipe.model.load_state_dict(state_dict)
# Move any additional components to GPU
if hasattr(pipe, 'tokenizer'):
# Move tokenizer to GPU if it supports it
if hasattr(pipe.tokenizer, 'to'):
pipe.tokenizer = pipe.tokenizer.to(device)
# Move all tokenizer tensors to GPU
for name, value in pipe.tokenizer.__dict__.items():
if isinstance(value, torch.Tensor):
setattr(pipe.tokenizer, name, value.to(device))
# Handle MeanScaleUniformBins specific attributes
if hasattr(pipe.tokenizer, 'bins'):
if isinstance(pipe.tokenizer.bins, torch.Tensor):
pipe.tokenizer.bins = pipe.tokenizer.bins.to(device)
if hasattr(pipe.tokenizer, 'scale'):
if isinstance(pipe.tokenizer.scale, torch.Tensor):
pipe.tokenizer.scale = pipe.tokenizer.scale.to(device)
if hasattr(pipe.tokenizer, 'mean'):
if isinstance(pipe.tokenizer.mean, torch.Tensor):
pipe.tokenizer.mean = pipe.tokenizer.mean.to(device)
# Move any additional tensors in the tokenizer's attributes to GPU
for name, value in pipe.tokenizer.__dict__.items():
if isinstance(value, torch.Tensor):
pipe.tokenizer.__dict__[name] = value.to(device)
# Remove the EOS token handling since MeanScaleUniformBins doesn't use it
if hasattr(pipe.tokenizer, '_append_eos_token'):
# Create a wrapper that just returns the input tensors
def wrapped_append_eos(token_ids, attention_mask):
return token_ids, attention_mask
pipe.tokenizer._append_eos_token = wrapped_append_eos
# Force synchronization again to ensure all tensors are on GPU
torch.cuda.synchronize()
# Ensure all model components are in eval mode
pipe.model.eval()
# Move any additional tensors in the model's config to GPU
if hasattr(pipe.model, 'config'):
for key, value in pipe.model.config.__dict__.items():
if isinstance(value, torch.Tensor):
setattr(pipe.model.config, key, value.to(device))
# Move any additional tensors in the model's state dict to GPU
if hasattr(pipe.model, 'state_dict'):
state_dict = pipe.model.state_dict()
for key in state_dict:
if isinstance(state_dict[key], torch.Tensor):
state_dict[key] = state_dict[key].to(device)
pipe.model.load_state_dict(state_dict)
# Move any additional tensors in the model's buffers to GPU
for name, buffer in pipe.model.named_buffers():
if buffer is not None:
pipe.model.register_buffer(name, buffer.to(device))
# Move any additional tensors in the model's parameters to GPU
for name, param in pipe.model.named_parameters():
if param is not None:
param.data = param.data.to(device)
# Move any additional tensors in the model's attributes to GPU
for name, value in pipe.model.__dict__.items():
if isinstance(value, torch.Tensor):
pipe.model.__dict__[name] = value.to(device)
# Move any additional tensors in the model's modules to GPU
for name, module in pipe.model.named_modules():
if hasattr(module, 'to'):
module.to(device)
# Move any tensors in the module's __dict__
for key, value in module.__dict__.items():
if isinstance(value, torch.Tensor):
setattr(module, key, value.to(device))
# Force synchronization again to ensure all tensors are on GPU
torch.cuda.synchronize()
# Ensure tokenizer is on GPU and all its tensors are on GPU
if hasattr(pipe, 'tokenizer'):
# Move tokenizer to GPU if it supports it
if hasattr(pipe.tokenizer, 'to'):
pipe.tokenizer = pipe.tokenizer.to(device)
# Move all tokenizer tensors to GPU
for name, value in pipe.tokenizer.__dict__.items():
if isinstance(value, torch.Tensor):
setattr(pipe.tokenizer, name, value.to(device))
# Handle MeanScaleUniformBins specific attributes
if hasattr(pipe.tokenizer, 'bins'):
if isinstance(pipe.tokenizer.bins, torch.Tensor):
pipe.tokenizer.bins = pipe.tokenizer.bins.to(device)
if hasattr(pipe.tokenizer, 'scale'):
if isinstance(pipe.tokenizer.scale, torch.Tensor):
pipe.tokenizer.scale = pipe.tokenizer.scale.to(device)
if hasattr(pipe.tokenizer, 'mean'):
if isinstance(pipe.tokenizer.mean, torch.Tensor):
pipe.tokenizer.mean = pipe.tokenizer.mean.to(device)
# Move any additional tensors in the tokenizer's attributes to GPU
for name, value in pipe.tokenizer.__dict__.items():
if isinstance(value, torch.Tensor):
pipe.tokenizer.__dict__[name] = value.to(device)
# Force synchronization again to ensure all tensors are on GPU
torch.cuda.synchronize()
# Make prediction
quantiles, mean = pipe.predict_quantiles(
context=context,
prediction_length=actual_prediction_length,
quantile_levels=[0.1, 0.5, 0.9]
)
if quantiles is None or mean is None:
raise ValueError("Chronos returned empty prediction")
print(f"Quantiles shape: {quantiles.shape}, Mean shape: {mean.shape}")
# Convert to numpy arrays
quantiles = quantiles.detach().cpu().numpy()
mean = mean.detach().cpu().numpy()
# Denormalize predictions using the same scaler as context
mean_pred = scaler.inverse_transform(mean.reshape(-1, 1)).flatten()
lower_bound = scaler.inverse_transform(quantiles[0, :, 0].reshape(-1, 1)).flatten()
upper_bound = scaler.inverse_transform(quantiles[0, :, 2].reshape(-1, 1)).flatten()
# Calculate standard deviation from quantiles
std_pred = (upper_bound - lower_bound) / (2 * 1.645)
# Check for discontinuity and apply continuity correction
last_actual = prices[-1]
first_pred = mean_pred[0]
if abs(first_pred - last_actual) > max(1e-6, 0.005 * abs(last_actual)): # Further reduced threshold
print(f"Warning: Discontinuity detected between last actual ({last_actual}) and first prediction ({first_pred})")
# Apply continuity correction to first prediction
mean_pred[0] = last_actual
# Adjust subsequent predictions to maintain trend with optional smoothing
if len(mean_pred) > 1:
# Calculate the trend from the original prediction
original_trend = mean_pred[1] - first_pred
# Apply the same trend but starting from the last actual value
for i in range(1, len(mean_pred)):
mean_pred[i] = last_actual + original_trend * i
# Apply financial smoothing if enabled
if use_smoothing:
mean_pred = apply_financial_smoothing(mean_pred, smoothing_type, smoothing_window, smoothing_alpha, 3, use_smoothing)
# If we had to limit the prediction length, extend the prediction recursively
if actual_prediction_length < trim_length:
extended_mean_pred = mean_pred.copy()
extended_std_pred = std_pred.copy()
# Store the original scaler for consistency
original_scaler = scaler
# Calculate the number of extension steps needed
remaining_steps = trim_length - actual_prediction_length
steps_needed = (remaining_steps + actual_prediction_length - 1) // actual_prediction_length
for step in range(steps_needed):
# Use all available datapoints for context, including predictions
# This allows the model to build upon its own predictions for better long-horizon forecasting
all_available_data = np.concatenate([prices, extended_mean_pred])
# If we have more data than chronos_context_size, use the most recent chronos_context_size points
# Otherwise, use all available data (this allows for longer context when available)
if len(all_available_data) > chronos_context_size:
context_window = all_available_data[-chronos_context_size:]
else:
context_window = all_available_data
# Use the original scaler to maintain consistency - fit on historical data only
# but transform the combined context window
normalized_context = original_scaler.transform(context_window.reshape(-1, 1)).flatten()
context = torch.tensor(normalized_context, dtype=dtype, device=device)
if len(context.shape) == 1:
context = context.unsqueeze(0)
# Calculate next prediction length based on timeframe
if timeframe == "1d":
next_length = min(max_prediction_length, remaining_steps)
elif timeframe == "1h":
next_length = min(max_prediction_length, remaining_steps)
else:
next_length = min(max_prediction_length, remaining_steps)
with torch.amp.autocast('cuda'):
next_quantiles, next_mean = pipe.predict_quantiles(
context=context,
prediction_length=next_length,
quantile_levels=[0.1, 0.5, 0.9]
)
# Convert predictions to numpy and denormalize using original scaler
next_mean = next_mean.detach().cpu().numpy()
next_quantiles = next_quantiles.detach().cpu().numpy()
# Denormalize predictions using the original scaler
next_mean_pred = original_scaler.inverse_transform(next_mean.reshape(-1, 1)).flatten()
next_lower = original_scaler.inverse_transform(next_quantiles[0, :, 0].reshape(-1, 1)).flatten()
next_upper = original_scaler.inverse_transform(next_quantiles[0, :, 2].reshape(-1, 1)).flatten()
# Calculate standard deviation
next_std_pred = (next_upper - next_lower) / (2 * 1.645)
# Check for discontinuity and apply continuity correction
if abs(next_mean_pred[0] - extended_mean_pred[-1]) > max(1e-6, 0.05 * abs(extended_mean_pred[-1])):
print(f"Warning: Discontinuity detected between last prediction ({extended_mean_pred[-1]}) and next prediction ({next_mean_pred[0]})")
# Apply continuity correction to first prediction
next_mean_pred[0] = extended_mean_pred[-1]
# Adjust subsequent predictions to maintain trend
if len(next_mean_pred) > 1:
original_trend = next_mean_pred[1] - next_mean_pred[0]
for i in range(1, len(next_mean_pred)):
next_mean_pred[i] = extended_mean_pred[-1] + original_trend * i
# Apply financial smoothing if enabled
if use_smoothing and len(next_mean_pred) > 1:
next_mean_pred = apply_financial_smoothing(next_mean_pred, smoothing_type, smoothing_window, smoothing_alpha, 3, use_smoothing)
# Append predictions
extended_mean_pred = np.concatenate([extended_mean_pred, next_mean_pred])
extended_std_pred = np.concatenate([extended_std_pred, next_std_pred])
remaining_steps -= len(next_mean_pred)
if remaining_steps <= 0:
break
# Trim to exact prediction length if needed
mean_pred = extended_mean_pred[:trim_length]
std_pred = extended_std_pred[:trim_length]
# Extend Chronos forecasting to volume and technical indicators
volume_pred = None
rsi_pred = None
macd_pred = None
try:
# Prepare volume data for Chronos
volume_data = df['Volume'].values
if len(volume_data) >= chronos_context_size:
# Normalize volume data
scaler_range = min(len(volume_data), chronos_context_size * 2)
context_window = volume_data[-chronos_context_size:]
volume_scaler = MinMaxScaler(feature_range=(-1, 1))
# Fit scaler on a larger range for better normalization
volume_scaler.fit(volume_data[-scaler_range:].reshape(-1, 1))
normalized_volume = volume_scaler.transform(context_window.reshape(-1, 1)).flatten()
if len(normalized_volume) < chronos_context_size:
padding = np.full(chronos_context_size - len(normalized_volume), normalized_volume[-1])
normalized_volume = np.concatenate([padding, normalized_volume])
elif len(normalized_volume) > chronos_context_size:
normalized_volume = normalized_volume[-chronos_context_size:]
volume_context = torch.tensor(normalized_volume, dtype=dtype, device=device)
if len(volume_context.shape) == 1:
volume_context = volume_context.unsqueeze(0)
with torch.amp.autocast('cuda'):
volume_quantiles, volume_mean = pipe.predict_quantiles(
context=volume_context,
prediction_length=actual_prediction_length,
quantile_levels=[0.1, 0.5, 0.9]
)
volume_quantiles = volume_quantiles.detach().cpu().numpy()
volume_mean = volume_mean.detach().cpu().numpy()
volume_pred = volume_scaler.inverse_transform(volume_mean.reshape(-1, 1)).flatten()
lower_bound = volume_scaler.inverse_transform(volume_quantiles[0, :, 0].reshape(-1, 1)).flatten()
upper_bound = volume_scaler.inverse_transform(volume_quantiles[0, :, 2].reshape(-1, 1)).flatten()
std_pred_vol = (upper_bound - lower_bound) / (2 * 1.645)
last_actual = volume_data[-1]
first_pred = volume_pred[0]
if abs(first_pred - last_actual) > max(1e-6, 0.005 * abs(last_actual)):
print(f"Warning: Discontinuity detected between last actual volume ({last_actual}) and first prediction ({first_pred})")
# Apply continuity correction
volume_pred[0] = last_actual
# Adjust subsequent predictions to maintain trend with optional smoothing
if len(volume_pred) > 1:
# Calculate the trend from the original prediction
original_trend = volume_pred[1] - first_pred
# Apply the same trend but starting from the last actual value
for i in range(1, len(volume_pred)):
volume_pred[i] = last_actual + original_trend * i
# Apply financial smoothing if enabled
if use_smoothing:
volume_pred = apply_financial_smoothing(volume_pred, smoothing_type, smoothing_window, smoothing_alpha, 3, use_smoothing)
# Extend volume predictions if needed
if actual_prediction_length < trim_length:
extended_volume_pred = volume_pred.copy()
extended_volume_std = std_pred_vol.copy()
remaining_steps = trim_length - actual_prediction_length
steps_needed = (remaining_steps + actual_prediction_length - 1) // actual_prediction_length
for step in range(steps_needed):
# Use all available datapoints for context, including predictions
# This allows the model to build upon its own predictions for better long-horizon forecasting
all_available_data = np.concatenate([volume_data, extended_volume_pred])
# If we have more data than chronos_context_size, use the most recent chronos_context_size points
# Otherwise, use all available data (this allows for longer context when available)
if len(all_available_data) > chronos_context_size:
context_window = all_available_data[-chronos_context_size:]
else:
context_window = all_available_data
# Use the original volume scaler to maintain consistency - fit on historical data only
# but transform the combined context window
normalized_context = volume_scaler.transform(context_window.reshape(-1, 1)).flatten()
context = torch.tensor(normalized_context, dtype=dtype, device=device)
if len(context.shape) == 1:
context = context.unsqueeze(0)
next_length = min(chronos_context_size, remaining_steps)
with torch.amp.autocast('cuda'):
next_quantiles, next_mean = pipe.predict_quantiles(
context=context,
prediction_length=next_length,
quantile_levels=[0.1, 0.5, 0.9]
)
next_mean = next_mean.detach().cpu().numpy()
next_quantiles = next_quantiles.detach().cpu().numpy()
next_mean_pred = volume_scaler.inverse_transform(next_mean.reshape(-1, 1)).flatten()
next_lower = volume_scaler.inverse_transform(next_quantiles[0, :, 0].reshape(-1, 1)).flatten()
next_upper = volume_scaler.inverse_transform(next_quantiles[0, :, 2].reshape(-1, 1)).flatten()
next_std_pred = (next_upper - next_lower) / (2 * 1.645)
# Check for discontinuity and apply continuity correction
if abs(next_mean_pred[0] - extended_volume_pred[-1]) > max(1e-6, 0.05 * abs(extended_volume_pred[-1])):
print(f"Warning: Discontinuity detected between last volume prediction ({extended_volume_pred[-1]}) and next prediction ({next_mean_pred[0]})")
next_mean_pred[0] = extended_volume_pred[-1]
if len(next_mean_pred) > 1:
original_trend = next_mean_pred[1] - next_mean_pred[0]
for i in range(1, len(next_mean_pred)):
next_mean_pred[i] = extended_volume_pred[-1] + original_trend * i
# Apply financial smoothing if enabled
if use_smoothing and len(next_mean_pred) > 1:
next_mean_pred = apply_financial_smoothing(next_mean_pred, smoothing_type, smoothing_window, smoothing_alpha, 3, use_smoothing)
extended_volume_pred = np.concatenate([extended_volume_pred, next_mean_pred])
extended_volume_std = np.concatenate([extended_volume_std, next_std_pred])
remaining_steps -= len(next_mean_pred)
if remaining_steps <= 0:
break
volume_pred = extended_volume_pred[:trim_length]
else:
avg_volume = df['Volume'].mean()
volume_pred = np.full(trim_length, avg_volume)
except Exception as e:
print(f"Volume prediction error: {str(e)}")
# Fallback: use historical average
avg_volume = df['Volume'].mean()
volume_pred = np.full(trim_length, avg_volume)
try:
# Prepare RSI data for Chronos
rsi_data = df['RSI'].values
if len(rsi_data) >= chronos_context_size and not np.any(np.isnan(rsi_data)):
# RSI is already normalized (0-100), but we'll scale it to (-1, 1)
scaler_range = min(len(rsi_data), chronos_context_size * 2)
context_window = rsi_data[-chronos_context_size:]
rsi_scaler = MinMaxScaler(feature_range=(-1, 1))
# Fit scaler on a larger range for better normalization
rsi_scaler.fit(rsi_data[-scaler_range:].reshape(-1, 1))
normalized_rsi = rsi_scaler.transform(context_window.reshape(-1, 1)).flatten()
if len(normalized_rsi) < chronos_context_size:
padding = np.full(chronos_context_size - len(normalized_rsi), normalized_rsi[-1])
normalized_rsi = np.concatenate([padding, normalized_rsi])
elif len(normalized_rsi) > chronos_context_size:
normalized_rsi = normalized_rsi[-chronos_context_size:]
rsi_context = torch.tensor(normalized_rsi, dtype=dtype, device=device)
if len(rsi_context.shape) == 1:
rsi_context = rsi_context.unsqueeze(0)
with torch.amp.autocast('cuda'):
rsi_quantiles, rsi_mean = pipe.predict_quantiles(
context=rsi_context,
prediction_length=actual_prediction_length,
quantile_levels=[0.1, 0.5, 0.9]
)
# Convert and denormalize RSI predictions
rsi_quantiles = rsi_quantiles.detach().cpu().numpy()
rsi_mean = rsi_mean.detach().cpu().numpy()
rsi_pred = rsi_scaler.inverse_transform(rsi_mean.reshape(-1, 1)).flatten()
# Clamp RSI to valid range (0-100)
lower_bound = rsi_scaler.inverse_transform(rsi_quantiles[0, :, 0].reshape(-1, 1)).flatten()
upper_bound = rsi_scaler.inverse_transform(rsi_quantiles[0, :, 2].reshape(-1, 1)).flatten()
std_pred_rsi = (upper_bound - lower_bound) / (2 * 1.645)
rsi_pred = np.clip(rsi_pred, 0, 100)
last_actual = rsi_data[-1]
first_pred = rsi_pred[0]
if abs(first_pred - last_actual) > max(1e-6, 0.005 * abs(last_actual)):
print(f"Warning: Discontinuity detected between last actual RSI ({last_actual}) and first prediction ({first_pred})")
# Apply continuity correction
rsi_pred[0] = last_actual
if len(rsi_pred) > 1:
trend = rsi_pred[1] - first_pred
rsi_pred[1:] = rsi_pred[1:] - first_pred + last_actual
rsi_pred = np.clip(rsi_pred, 0, 100) # Re-clip after adjustment
# Extend RSI predictions if needed
if actual_prediction_length < trim_length:
extended_rsi_pred = rsi_pred.copy()
extended_rsi_std = std_pred_rsi.copy()
remaining_steps = trim_length - actual_prediction_length
steps_needed = (remaining_steps + actual_prediction_length - 1) // actual_prediction_length
for step in range(steps_needed):
# Use all available datapoints for context, including predictions
# This allows the model to build upon its own predictions for better long-horizon forecasting
all_available_data = np.concatenate([rsi_data, extended_rsi_pred])
# If we have more data than chronos_context_size, use the most recent chronos_context_size points
# Otherwise, use all available data (this allows for longer context when available)
if len(all_available_data) > chronos_context_size:
context_window = all_available_data[-chronos_context_size:]
else:
context_window = all_available_data
# Use the original RSI scaler to maintain consistency - fit on historical data only
# but transform the combined context window
normalized_context = rsi_scaler.transform(context_window.reshape(-1, 1)).flatten()
context = torch.tensor(normalized_context, dtype=dtype, device=device)
if len(context.shape) == 1:
context = context.unsqueeze(0)
next_length = min(chronos_context_size, remaining_steps)
with torch.amp.autocast('cuda'):
next_quantiles, next_mean = pipe.predict_quantiles(
context=context,
prediction_length=next_length,
quantile_levels=[0.1, 0.5, 0.9]
)
next_mean = next_mean.detach().cpu().numpy()
next_quantiles = next_quantiles.detach().cpu().numpy()
next_mean_pred = rsi_scaler.inverse_transform(next_mean.reshape(-1, 1)).flatten()
next_lower = rsi_scaler.inverse_transform(next_quantiles[0, :, 0].reshape(-1, 1)).flatten()
next_upper = rsi_scaler.inverse_transform(next_quantiles[0, :, 2].reshape(-1, 1)).flatten()
next_std_pred = (next_upper - next_lower) / (2 * 1.645)
next_mean_pred = np.clip(next_mean_pred, 0, 100)
# Check for discontinuity and apply continuity correction
if abs(next_mean_pred[0] - extended_rsi_pred[-1]) > max(1e-6, 0.005 * abs(extended_rsi_pred[-1])):
print(f"Warning: Discontinuity detected between last RSI prediction ({extended_rsi_pred[-1]}) and next prediction ({next_mean_pred[0]})")
next_mean_pred[0] = extended_rsi_pred[-1]
if len(next_mean_pred) > 1:
original_trend = next_mean_pred[1] - next_mean_pred[0]
for i in range(1, len(next_mean_pred)):
next_mean_pred[i] = extended_rsi_pred[-1] + original_trend * i
next_mean_pred = np.clip(next_mean_pred, 0, 100)
# Apply financial smoothing if enabled
if use_smoothing and len(next_mean_pred) > 1:
next_mean_pred = apply_financial_smoothing(next_mean_pred, smoothing_type, smoothing_window, smoothing_alpha, 3, use_smoothing)
next_mean_pred = np.clip(next_mean_pred, 0, 100)
extended_rsi_pred = np.concatenate([extended_rsi_pred, next_mean_pred])
extended_rsi_std = np.concatenate([extended_rsi_std, next_std_pred])
remaining_steps -= len(next_mean_pred)
if remaining_steps <= 0:
break
rsi_pred = extended_rsi_pred[:trim_length]
else:
last_rsi = df['RSI'].iloc[-1]
rsi_pred = np.full(trim_length, last_rsi)
except Exception as e:
print(f"RSI prediction error: {str(e)}")
# Fallback: use last known RSI value
last_rsi = df['RSI'].iloc[-1]
rsi_pred = np.full(trim_length, last_rsi)
try:
# Prepare MACD data for Chronos
macd_data = df['MACD'].values
if len(macd_data) >= chronos_context_size and not np.any(np.isnan(macd_data)):
# Normalize MACD data
scaler_range = min(len(macd_data), chronos_context_size * 2)
context_window = macd_data[-chronos_context_size:]
macd_scaler = MinMaxScaler(feature_range=(-1, 1))
# Fit scaler on a larger range for better normalization
macd_scaler.fit(macd_data[-scaler_range:].reshape(-1, 1))
normalized_macd = macd_scaler.transform(context_window.reshape(-1, 1)).flatten()
if len(normalized_macd) < chronos_context_size:
padding = np.full(chronos_context_size - len(normalized_macd), normalized_macd[-1])
normalized_macd = np.concatenate([padding, normalized_macd])
elif len(normalized_macd) > chronos_context_size:
normalized_macd = normalized_macd[-chronos_context_size:]
macd_context = torch.tensor(normalized_macd, dtype=dtype, device=device)
if len(macd_context.shape) == 1:
macd_context = macd_context.unsqueeze(0)
with torch.amp.autocast('cuda'):
macd_quantiles, macd_mean = pipe.predict_quantiles(
context=macd_context,
prediction_length=actual_prediction_length,
quantile_levels=[0.1, 0.5, 0.9]
)
# Convert and denormalize MACD predictions
macd_quantiles = macd_quantiles.detach().cpu().numpy()
macd_mean = macd_mean.detach().cpu().numpy()
macd_pred = macd_scaler.inverse_transform(macd_mean.reshape(-1, 1)).flatten()
lower_bound = macd_scaler.inverse_transform(macd_quantiles[0, :, 0].reshape(-1, 1)).flatten()
upper_bound = macd_scaler.inverse_transform(macd_quantiles[0, :, 2].reshape(-1, 1)).flatten()
std_pred_macd = (upper_bound - lower_bound) / (2 * 1.645)
last_actual = macd_data[-1]
first_pred = macd_pred[0]
# Check for discontinuity and apply continuity correction
if abs(first_pred - last_actual) > max(1e-6, 0.005 * abs(last_actual)):
print(f"Warning: Discontinuity detected between last actual MACD ({last_actual}) and first prediction ({first_pred})")
# Apply continuity correction
macd_pred[0] = last_actual
# Adjust subsequent predictions to maintain trend with optional smoothing
if len(macd_pred) > 1:
# Calculate the trend from the original prediction
original_trend = macd_pred[1] - first_pred
# Apply the same trend but starting from the last actual value
for i in range(1, len(macd_pred)):
macd_pred[i] = last_actual + original_trend * i
# Apply financial smoothing if enabled
if use_smoothing:
macd_pred = apply_financial_smoothing(macd_pred, smoothing_type, smoothing_window, smoothing_alpha, 3, use_smoothing)
# Extend MACD predictions if needed
if actual_prediction_length < trim_length:
extended_macd_pred = macd_pred.copy()
extended_macd_std = std_pred_macd.copy()
remaining_steps = trim_length - actual_prediction_length
steps_needed = (remaining_steps + actual_prediction_length - 1) // actual_prediction_length
for step in range(steps_needed):
# Use all available datapoints for context, including predictions
# This allows the model to build upon its own predictions for better long-horizon forecasting
all_available_data = np.concatenate([macd_data, extended_macd_pred])
# If we have more data than chronos_context_size, use the most recent chronos_context_size points
# Otherwise, use all available data (this allows for longer context when available)
if len(all_available_data) > chronos_context_size:
context_window = all_available_data[-chronos_context_size:]
else:
context_window = all_available_data
# Use the original MACD scaler to maintain consistency - fit on historical data only
# but transform the combined context window
normalized_context = macd_scaler.transform(context_window.reshape(-1, 1)).flatten()
context = torch.tensor(normalized_context, dtype=dtype, device=device)
if len(context.shape) == 1:
context = context.unsqueeze(0)
next_length = min(chronos_context_size, remaining_steps)
with torch.amp.autocast('cuda'):
next_quantiles, next_mean = pipe.predict_quantiles(
context=context,
prediction_length=next_length,
quantile_levels=[0.1, 0.5, 0.9]
)
next_mean = next_mean.detach().cpu().numpy()
next_quantiles = next_quantiles.detach().cpu().numpy()
next_mean_pred = macd_scaler.inverse_transform(next_mean.reshape(-1, 1)).flatten()
next_lower = macd_scaler.inverse_transform(next_quantiles[0, :, 0].reshape(-1, 1)).flatten()
next_upper = macd_scaler.inverse_transform(next_quantiles[0, :, 2].reshape(-1, 1)).flatten()
next_std_pred = (next_upper - next_lower) / (2 * 1.645)
# Check for discontinuity and apply continuity correction
if abs(next_mean_pred[0] - extended_macd_pred[-1]) > max(1e-6, 0.05 * abs(extended_macd_pred[-1])):
print(f"Warning: Discontinuity detected between last MACD prediction ({extended_macd_pred[-1]}) and next prediction ({next_mean_pred[0]})")
next_mean_pred[0] = extended_macd_pred[-1]
if len(next_mean_pred) > 1:
original_trend = next_mean_pred[1] - next_mean_pred[0]
for i in range(1, len(next_mean_pred)):
next_mean_pred[i] = extended_macd_pred[-1] + original_trend * i
# Apply financial smoothing if enabled
if use_smoothing and len(next_mean_pred) > 1:
next_mean_pred = apply_financial_smoothing(next_mean_pred, smoothing_type, smoothing_window, smoothing_alpha, 3, use_smoothing)
extended_macd_pred = np.concatenate([extended_macd_pred, next_mean_pred])
extended_macd_std = np.concatenate([extended_macd_std, next_std_pred])
remaining_steps -= len(next_mean_pred)
if remaining_steps <= 0:
break
macd_pred = extended_macd_pred[:trim_length]
else:
last_macd = df['MACD'].iloc[-1]
macd_pred = np.full(trim_length, last_macd)
except Exception as e:
print(f"MACD prediction error: {str(e)}")
# Fallback: use last known MACD value
last_macd = df['MACD'].iloc[-1]
macd_pred = np.full(trim_length, last_macd)
except Exception as e:
print(f"Chronos prediction error: {str(e)}")
print(f"Error type: {type(e)}")
print(f"Error details: {str(e)}")
raise
if strategy == "technical":
# Technical analysis based prediction
last_price = df['Close'].iloc[-1]
rsi = df['RSI'].iloc[-1]
macd = df['MACD'].iloc[-1]
macd_signal = df['MACD_Signal'].iloc[-1]
# Simple prediction based on technical indicators
trend = 1 if (rsi > 50 and macd > macd_signal) else -1
volatility = df['Volatility'].iloc[-1]
# Generate predictions
mean_pred = np.array([last_price * (1 + trend * volatility * i) for i in range(1, prediction_days + 1)])
std_pred = np.array([volatility * last_price * i for i in range(1, prediction_days + 1)])
# Create prediction dates based on timeframe
last_date = df.index[-1]
if timeframe == "1d":
pred_dates = pd.date_range(start=last_date + timedelta(days=1), periods=prediction_days)
elif timeframe == "1h":
pred_dates = pd.date_range(start=last_date + timedelta(hours=1), periods=prediction_days * 24)
else: # 15m
pred_dates = pd.date_range(start=last_date + timedelta(minutes=15), periods=prediction_days * 96)
# Create visualization
fig = make_subplots(rows=3, cols=1,
shared_xaxes=True,
vertical_spacing=0.05,
subplot_titles=('Price Prediction', 'Technical Indicators', 'Volume'))
# Add historical price
fig.add_trace(
go.Scatter(x=df.index, y=df['Close'], name='Historical Price',
line=dict(color='blue')),
row=1, col=1
)
# Add prediction mean
fig.add_trace(
go.Scatter(x=pred_dates, y=mean_pred, name='Predicted Price',
line=dict(color='red')),
row=1, col=1
)
# Add confidence intervals
fig.add_trace(
go.Scatter(x=pred_dates, y=mean_pred + 1.96 * std_pred,
fill=None, mode='lines', line_color='rgba(255,0,0,0.2)',
name='Upper Bound'),
row=1, col=1
)
fig.add_trace(
go.Scatter(x=pred_dates, y=mean_pred - 1.96 * std_pred,
fill='tonexty', mode='lines', line_color='rgba(255,0,0,0.2)',
name='Lower Bound'),
row=1, col=1
)
# Add technical indicators
fig.add_trace(
go.Scatter(x=df.index, y=df['RSI'], name='RSI',
line=dict(color='purple')),
row=2, col=1
)
fig.add_trace(
go.Scatter(x=df.index, y=df['MACD'], name='MACD',
line=dict(color='orange')),
row=2, col=1
)
fig.add_trace(
go.Scatter(x=df.index, y=df['MACD_Signal'], name='MACD Signal',
line=dict(color='green')),
row=2, col=1
)
# Add predicted technical indicators if available
if rsi_pred is not None:
fig.add_trace(
go.Scatter(x=pred_dates, y=rsi_pred, name='Predicted RSI',
line=dict(color='purple', dash='dash')),
row=2, col=1
)
if macd_pred is not None:
fig.add_trace(
go.Scatter(x=pred_dates, y=macd_pred, name='Predicted MACD',
line=dict(color='orange', dash='dash')),
row=2, col=1
)
# Add volume
fig.add_trace(
go.Bar(x=df.index, y=df['Volume'], name='Volume',
marker_color='gray'),
row=3, col=1
)
# Add predicted volume if available
if volume_pred is not None:
fig.add_trace(
go.Bar(x=pred_dates, y=volume_pred, name='Predicted Volume',
marker_color='red', opacity=0.7),
row=3, col=1
)
# Update layout with timeframe-specific settings
fig.update_layout(
title=f'{symbol} {timeframe} Analysis and Prediction',
xaxis_title='Date',
yaxis_title='Price',
height=1000,
showlegend=True
)
# Calculate trading signals
signals = calculate_trading_signals(df)
# Add prediction information to signals
signals.update({
"symbol": symbol,
"timeframe": timeframe,
"prediction": mean_pred.tolist(),
"confidence": std_pred.tolist(),
"dates": pred_dates.strftime('%Y-%m-%d %H:%M:%S').tolist(),
"strategy_used": strategy
})
# Add predicted indicators to signals if available
if volume_pred is not None:
signals["predicted_volume"] = volume_pred.tolist()
if rsi_pred is not None:
signals["predicted_rsi"] = rsi_pred.tolist()
if macd_pred is not None:
signals["predicted_macd"] = macd_pred.tolist()
# Implement advanced features
# 1. Market Regime Detection
if use_regime_detection:
try:
returns = df['Returns'].dropna()
regime_info = detect_market_regime(returns)
signals["regime_info"] = regime_info
except Exception as e:
print(f"Regime detection error: {str(e)}")
signals["regime_info"] = {"error": str(e)}
# 2. Advanced Trading Signals with Regime Awareness
try:
regime_info = signals.get("regime_info", {})
advanced_signals = advanced_trading_signals(df, regime_info)
signals["advanced_signals"] = advanced_signals
except Exception as e:
print(f"Advanced trading signals error: {str(e)}")
signals["advanced_signals"] = {"error": str(e)}
# 3. Stress Testing
if use_stress_testing:
try:
stress_results = stress_test_scenarios(df, mean_pred)
signals["stress_test_results"] = stress_results
except Exception as e:
print(f"Stress testing error: {str(e)}")
signals["stress_test_results"] = {"error": str(e)}
# 4. Ensemble Methods
if use_ensemble and ensemble_weights:
try:
ensemble_mean, ensemble_uncertainty = create_ensemble_prediction(
df, prediction_days, ensemble_weights
)
if len(ensemble_mean) > 0:
signals["ensemble_used"] = True
signals["ensemble_prediction"] = ensemble_mean.tolist()
signals["ensemble_uncertainty"] = ensemble_uncertainty.tolist()
# Update the main prediction with ensemble if available
if len(ensemble_mean) == len(mean_pred):
mean_pred = ensemble_mean
std_pred = ensemble_uncertainty
else:
signals["ensemble_used"] = False
except Exception as e:
print(f"Ensemble prediction error: {str(e)}")
signals["ensemble_used"] = False
signals["ensemble_error"] = str(e)
# 5. Enhanced Uncertainty Quantification
try:
if 'quantiles' in locals():
skewed_uncertainty = calculate_skewed_uncertainty(quantiles)
signals["skewed_uncertainty"] = skewed_uncertainty.tolist()
except Exception as e:
print(f"Skewed uncertainty calculation error: {str(e)}")
return signals, fig
except Exception as e:
raise Exception(f"Prediction error: {str(e)}")
finally:
clear_gpu_memory()
def calculate_trading_signals(df: pd.DataFrame) -> Dict:
"""Calculate trading signals based on technical indicators"""
signals = {
"RSI": "Oversold" if df['RSI'].iloc[-1] < 30 else "Overbought" if df['RSI'].iloc[-1] > 70 else "Neutral",
"MACD": "Buy" if df['MACD'].iloc[-1] > df['MACD_Signal'].iloc[-1] else "Sell",
"Bollinger": "Buy" if df['Close'].iloc[-1] < df['BB_Lower'].iloc[-1] else "Sell" if df['Close'].iloc[-1] > df['BB_Upper'].iloc[-1] else "Hold",
"SMA": "Buy" if df['SMA_20'].iloc[-1] > df['SMA_50'].iloc[-1] else "Sell"
}
# Calculate overall signal
buy_signals = sum(1 for signal in signals.values() if signal == "Buy")
sell_signals = sum(1 for signal in signals.values() if signal == "Sell")
if buy_signals > sell_signals:
signals["Overall"] = "Buy"
elif sell_signals > buy_signals:
signals["Overall"] = "Sell"
else:
signals["Overall"] = "Hold"
return signals
def get_market_data(symbol: str = "^GSPC", lookback_days: int = 365) -> pd.DataFrame:
"""
Fetch market data (S&P 500 by default) for correlation analysis and regime detection.
Args:
symbol (str): Market index symbol (default: ^GSPC for S&P 500)
lookback_days (int): Number of days to look back
Returns:
pd.DataFrame: Market data with returns
"""
cache_key = f"{symbol}_{lookback_days}"
current_time = time.time()
# Check cache
if cache_key in market_data_cache and current_time < cache_expiry.get(cache_key, 0):
return market_data_cache[cache_key]
try:
ticker = yf.Ticker(symbol)
end_date = datetime.now()
start_date = end_date - timedelta(days=lookback_days)
def fetch_market_history():
return ticker.history(
start=start_date,
end=end_date,
interval="1d",
prepost=False,
actions=False,
auto_adjust=True
)
df = retry_yfinance_request(fetch_market_history)
if not df.empty:
df['Returns'] = df['Close'].pct_change()
df['Volatility'] = df['Returns'].rolling(window=20).std()
# Cache the data
market_data_cache[cache_key] = df
cache_expiry[cache_key] = current_time + CACHE_DURATION
return df
except Exception as e:
print(f"Warning: Could not fetch market data for {symbol}: {str(e)}")
return pd.DataFrame()
def detect_market_regime(returns: pd.Series, n_regimes: int = 3) -> Dict:
"""
Detect market regime using Hidden Markov Model or simplified methods.
Args:
returns (pd.Series): Price returns
n_regimes (int): Number of regimes to detect
Returns:
Dict: Regime information including probabilities and characteristics
"""
def get_regime_name(regime_idx: int, means: List[float], volatilities: List[float]) -> str:
"""
Convert regime index to descriptive name based on characteristics.
Args:
regime_idx (int): Regime index (0, 1, 2)
means (List[float]): List of regime means
volatilities (List[float]): List of regime volatilities
Returns:
str: Descriptive regime name
"""
if len(means) != 3 or len(volatilities) != 3:
return f"Regime {regime_idx}"
# Sort regimes by volatility (low to high)
vol_sorted = sorted(range(len(volatilities)), key=lambda i: volatilities[i])
# Sort regimes by mean return (low to high)
mean_sorted = sorted(range(len(means)), key=lambda i: means[i])
# Determine regime characteristics
if regime_idx == vol_sorted[0]: # Lowest volatility
if means[regime_idx] > 0:
return "Low Volatility Bull"
else:
return "Low Volatility Bear"
elif regime_idx == vol_sorted[2]: # Highest volatility
if means[regime_idx] > 0:
return "High Volatility Bull"
else:
return "High Volatility Bear"
else: # Medium volatility
if means[regime_idx] > 0:
return "Moderate Bull"
else:
return "Moderate Bear"
if len(returns) < 50:
return {"regime": "Normal Market", "probabilities": [1.0], "volatility": returns.std()}
try:
if HMM_AVAILABLE:
# Use HMM for regime detection
# Convert pandas Series to numpy array for reshape
returns_array = returns.dropna().values
# Try different HMM configurations if convergence fails
for attempt in range(3):
try:
if attempt == 0:
model = hmm.GaussianHMM(n_components=n_regimes, random_state=42, covariance_type="full", n_iter=100)
elif attempt == 1:
model = hmm.GaussianHMM(n_components=n_regimes, random_state=42, covariance_type="diag", n_iter=200)
else:
model = hmm.GaussianHMM(n_components=n_regimes, random_state=42, covariance_type="spherical", n_iter=300)
model.fit(returns_array.reshape(-1, 1))
# Get regime probabilities for the last observation
regime_probs = model.predict_proba(returns_array.reshape(-1, 1))
current_regime = model.predict(returns_array.reshape(-1, 1))[-1]
# Calculate regime characteristics
regime_means = model.means_.flatten()
regime_vols = np.sqrt(model.covars_.diagonal(axis1=1, axis2=2)) if model.covariance_type == "full" else np.sqrt(model.covars_)
# Convert regime index to descriptive name
regime_name = get_regime_name(int(current_regime), regime_means.tolist(), regime_vols.tolist())
return {
"regime": regime_name,
"regime_index": int(current_regime),
"probabilities": regime_probs[-1].tolist(),
"means": regime_means.tolist(),
"volatilities": regime_vols.tolist(),
"method": f"HMM-{model.covariance_type}"
}
except Exception as e:
if attempt == 2: # Last attempt failed
print(f"HMM failed after {attempt + 1} attempts: {str(e)}")
break
continue
else:
# Simplified regime detection using volatility clustering
volatility = returns.rolling(window=20).std().dropna()
vol_percentile = volatility.iloc[-1] / volatility.quantile(0.8)
if vol_percentile > 1.2:
regime_name = "High Volatility Market"
regime = 2 # High volatility regime
elif vol_percentile < 0.8:
regime_name = "Low Volatility Market"
regime = 0 # Low volatility regime
else:
regime_name = "Normal Market"
regime = 1 # Normal regime
return {
"regime": regime_name,
"regime_index": regime,
"probabilities": [0.1, 0.8, 0.1] if regime == 1 else [0.8, 0.1, 0.1] if regime == 0 else [0.1, 0.1, 0.8],
"volatility": volatility.iloc[-1],
"method": "Volatility-based"
}
except Exception as e:
print(f"Warning: Regime detection failed: {str(e)}")
return {"regime": "Normal Market", "regime_index": 1, "probabilities": [1.0], "volatility": returns.std(), "method": "Fallback"}
def calculate_advanced_risk_metrics(df: pd.DataFrame, market_returns: pd.Series = None,
risk_free_rate: float = 0.02) -> Dict:
"""
Calculate advanced risk metrics including tail risk and market correlation.
Args:
df (pd.DataFrame): Stock data
market_returns (pd.Series): Market returns for correlation analysis
risk_free_rate (float): Annual risk-free rate
Returns:
Dict: Advanced risk metrics
"""
try:
returns = df['Returns'].dropna()
if len(returns) < 30:
return {"error": "Insufficient data for risk calculation"}
# Basic metrics
annual_return = returns.mean() * 252
annual_vol = returns.std() * np.sqrt(252)
# Market-adjusted metrics
beta = 1.0
alpha = 0.0
correlation = 0.0
aligned_returns = None
aligned_market = None
if market_returns is not None and len(market_returns) > 0:
try:
# Align dates
aligned_returns = returns.reindex(market_returns.index).dropna()
aligned_market = market_returns.reindex(aligned_returns.index).dropna()
# Ensure both arrays have the same length
if len(aligned_returns) > 10 and len(aligned_market) > 10:
# Find the common length
min_length = min(len(aligned_returns), len(aligned_market))
aligned_returns = aligned_returns.iloc[-min_length:]
aligned_market = aligned_market.iloc[-min_length:]
# Ensure they have the same length
if len(aligned_returns) == len(aligned_market) and len(aligned_returns) > 10:
try:
beta = np.cov(aligned_returns, aligned_market)[0,1] / np.var(aligned_market)
alpha = aligned_returns.mean() - beta * aligned_market.mean()
correlation = np.corrcoef(aligned_returns, aligned_market)[0,1]
except Exception as e:
print(f"Market correlation calculation error: {str(e)}")
beta = 1.0
alpha = 0.0
correlation = 0.0
else:
beta = 1.0
alpha = 0.0
correlation = 0.0
else:
beta = 1.0
alpha = 0.0
correlation = 0.0
except Exception as e:
print(f"Market data alignment error: {str(e)}")
beta = 1.0
alpha = 0.0
correlation = 0.0
aligned_returns = None
aligned_market = None
# Tail risk metrics
var_95 = np.percentile(returns, 5)
var_99 = np.percentile(returns, 1)
cvar_95 = returns[returns <= var_95].mean()
cvar_99 = returns[returns <= var_99].mean()
# Maximum drawdown
cumulative_returns = (1 + returns).cumprod()
rolling_max = cumulative_returns.expanding().max()
drawdown = (cumulative_returns - rolling_max) / rolling_max
max_drawdown = drawdown.min()
# Skewness and kurtosis
skewness = stats.skew(returns)
kurtosis = stats.kurtosis(returns)
# Risk-adjusted returns
sharpe_ratio = (annual_return - risk_free_rate) / annual_vol if annual_vol > 0 else 0
sortino_ratio = (annual_return - risk_free_rate) / (returns[returns < 0].std() * np.sqrt(252)) if returns[returns < 0].std() > 0 else 0
calmar_ratio = annual_return / abs(max_drawdown) if max_drawdown != 0 else 0
# Information ratio (if market data available)
information_ratio = 0
if aligned_returns is not None and aligned_market is not None:
try:
if len(aligned_returns) > 10 and len(aligned_market) > 10:
min_length = min(len(aligned_returns), len(aligned_market))
aligned_returns_for_ir = aligned_returns.iloc[-min_length:]
aligned_market_for_ir = aligned_market.iloc[-min_length:]
if len(aligned_returns_for_ir) == len(aligned_market_for_ir):
excess_returns = aligned_returns_for_ir - aligned_market_for_ir
information_ratio = excess_returns.mean() / excess_returns.std() if excess_returns.std() > 0 else 0
else:
information_ratio = 0
else:
information_ratio = 0
except Exception as e:
print(f"Information ratio calculation error: {str(e)}")
information_ratio = 0
return {
"Annual_Return": annual_return,
"Annual_Volatility": annual_vol,
"Sharpe_Ratio": sharpe_ratio,
"Sortino_Ratio": sortino_ratio,
"Calmar_Ratio": calmar_ratio,
"Information_Ratio": information_ratio,
"Beta": beta,
"Alpha": alpha * 252,
"Correlation_with_Market": correlation,
"VaR_95": var_95,
"VaR_99": var_99,
"CVaR_95": cvar_95,
"CVaR_99": cvar_99,
"Max_Drawdown": max_drawdown,
"Skewness": skewness,
"Kurtosis": kurtosis,
"Risk_Free_Rate": risk_free_rate
}
except Exception as e:
print(f"Advanced risk metrics calculation error: {str(e)}")
return {"error": f"Risk calculation failed: {str(e)}"}
def create_ensemble_prediction(df: pd.DataFrame, prediction_days: int,
ensemble_weights: Dict = None) -> Tuple[np.ndarray, np.ndarray]:
"""
Create ensemble prediction combining multiple models.
Args:
df (pd.DataFrame): Historical data
prediction_days (int): Number of days to predict
ensemble_weights (Dict): Weights for different models
Returns:
Tuple[np.ndarray, np.ndarray]: Mean and uncertainty predictions
"""
if ensemble_weights is None:
ensemble_weights = {"chronos": 0.6, "technical": 0.2, "statistical": 0.2}
predictions = {}
uncertainties = {}
# Chronos prediction (placeholder - will be filled by main prediction function)
predictions["chronos"] = np.array([])
uncertainties["chronos"] = np.array([])
# Technical prediction
if ensemble_weights.get("technical", 0) > 0:
try:
last_price = df['Close'].iloc[-1]
rsi = df['RSI'].iloc[-1]
macd = df['MACD'].iloc[-1]
macd_signal = df['MACD_Signal'].iloc[-1]
volatility = df['Volatility'].iloc[-1]
# Enhanced technical prediction
trend = 1 if (rsi > 50 and macd > macd_signal) else -1
mean_reversion = (df['SMA_200'].iloc[-1] - last_price) / last_price if 'SMA_200' in df.columns else 0
tech_pred = []
for i in range(1, prediction_days + 1):
# Combine trend and mean reversion
prediction = last_price * (1 + trend * volatility * 0.3 + mean_reversion * 0.1 * i)
tech_pred.append(prediction)
predictions["technical"] = np.array(tech_pred)
uncertainties["technical"] = np.array([volatility * last_price * i for i in range(1, prediction_days + 1)])
except Exception as e:
print(f"Technical prediction error: {str(e)}")
predictions["technical"] = np.array([])
uncertainties["technical"] = np.array([])
# Statistical prediction (ARIMA-like)
if ensemble_weights.get("statistical", 0) > 0:
try:
returns = df['Returns'].dropna()
if len(returns) > 10:
# Simple moving average with momentum
ma_short = df['Close'].rolling(window=10).mean().iloc[-1]
ma_long = df['Close'].rolling(window=30).mean().iloc[-1]
momentum = (ma_short - ma_long) / ma_long
last_price = df['Close'].iloc[-1]
stat_pred = []
for i in range(1, prediction_days + 1):
# Mean reversion with momentum
prediction = last_price * (1 + momentum * 0.5 - 0.001 * i) # Decay factor
stat_pred.append(prediction)
predictions["statistical"] = np.array(stat_pred)
uncertainties["statistical"] = np.array([returns.std() * last_price * np.sqrt(i) for i in range(1, prediction_days + 1)])
else:
predictions["statistical"] = np.array([])
uncertainties["statistical"] = np.array([])
except Exception as e:
print(f"Statistical prediction error: {str(e)}")
predictions["statistical"] = np.array([])
uncertainties["statistical"] = np.array([])
# Combine predictions
valid_predictions = {k: v for k, v in predictions.items() if len(v) > 0}
valid_uncertainties = {k: v for k, v in uncertainties.items() if len(v) > 0}
if not valid_predictions:
return np.array([]), np.array([])
# Weighted ensemble
total_weight = sum(ensemble_weights.get(k, 0) for k in valid_predictions.keys())
if total_weight == 0:
return np.array([]), np.array([])
# Normalize weights
normalized_weights = {k: ensemble_weights.get(k, 0) / total_weight for k in valid_predictions.keys()}
# Calculate weighted mean and uncertainty
max_length = max(len(v) for v in valid_predictions.values())
ensemble_mean = np.zeros(max_length)
ensemble_uncertainty = np.zeros(max_length)
for model, pred in valid_predictions.items():
weight = normalized_weights[model]
if len(pred) < max_length:
# Extend prediction using last value
extended_pred = np.concatenate([pred, np.full(max_length - len(pred), pred[-1])])
extended_unc = np.concatenate([valid_uncertainties[model], np.full(max_length - len(pred), valid_uncertainties[model][-1])])
else:
extended_pred = pred[:max_length]
extended_unc = valid_uncertainties[model][:max_length]
ensemble_mean += weight * extended_pred
ensemble_uncertainty += weight * extended_unc
return ensemble_mean, ensemble_uncertainty
def stress_test_scenarios(df: pd.DataFrame, prediction: np.ndarray,
scenarios: Dict = None) -> Dict:
"""
Perform stress testing under various market scenarios.
Args:
df (pd.DataFrame): Historical data
prediction (np.ndarray): Base prediction
scenarios (Dict): Stress test scenarios
Returns:
Dict: Stress test results
"""
if scenarios is None:
scenarios = {
"market_crash": {"volatility_multiplier": 3.0, "return_shock": -0.15},
"high_volatility": {"volatility_multiplier": 2.0, "return_shock": -0.05},
"low_volatility": {"volatility_multiplier": 0.5, "return_shock": 0.02},
"bull_market": {"volatility_multiplier": 1.2, "return_shock": 0.10},
"interest_rate_shock": {"volatility_multiplier": 1.5, "return_shock": -0.08}
}
base_volatility = df['Volatility'].iloc[-1]
base_return = df['Returns'].mean()
last_price = df['Close'].iloc[-1]
stress_results = {}
for scenario_name, params in scenarios.items():
try:
# Calculate stressed parameters
stressed_vol = base_volatility * params["volatility_multiplier"]
stressed_return = base_return + params["return_shock"]
# Generate stressed prediction
stressed_pred = []
for i, pred in enumerate(prediction):
# Apply stress factors
stress_factor = 1 + stressed_return * (i + 1) / 252
volatility_impact = np.random.normal(0, stressed_vol * np.sqrt((i + 1) / 252))
stressed_price = pred * stress_factor * (1 + volatility_impact)
stressed_pred.append(stressed_price)
# Calculate stress metrics
stress_results[scenario_name] = {
"prediction": np.array(stressed_pred),
"max_loss": min(stressed_pred) / last_price - 1,
"volatility": stressed_vol,
"expected_return": stressed_return,
"var_95": np.percentile([p / last_price - 1 for p in stressed_pred], 5)
}
except Exception as e:
print(f"Stress test error for {scenario_name}: {str(e)}")
stress_results[scenario_name] = {"error": str(e)}
return stress_results
def calculate_skewed_uncertainty(quantiles: np.ndarray, confidence_level: float = 0.9) -> np.ndarray:
"""
Calculate uncertainty accounting for skewness in return distributions.
Args:
quantiles (np.ndarray): Quantile predictions from Chronos
confidence_level (float): Confidence level for uncertainty calculation
Returns:
np.ndarray: Uncertainty estimates
"""
try:
lower = quantiles[0, :, 0]
median = quantiles[0, :, 1]
upper = quantiles[0, :, 2]
# Calculate skewness for each prediction point
uncertainties = []
for i in range(len(lower)):
# Calculate skewness
if upper[i] != median[i] and median[i] != lower[i]:
skewness = (median[i] - lower[i]) / (upper[i] - median[i])
else:
skewness = 1.0
# Adjust z-score based on skewness
if skewness > 1.2: # Right-skewed
z_score = stats.norm.ppf(confidence_level) * (1 + 0.1 * skewness)
elif skewness < 0.8: # Left-skewed
z_score = stats.norm.ppf(confidence_level) * (1 - 0.1 * abs(skewness))
else:
z_score = stats.norm.ppf(confidence_level)
# Calculate uncertainty
uncertainty = (upper[i] - lower[i]) / (2 * z_score)
uncertainties.append(uncertainty)
return np.array(uncertainties)
except Exception as e:
print(f"Skewed uncertainty calculation error: {str(e)}")
# Fallback to simple calculation
return (quantiles[0, :, 2] - quantiles[0, :, 0]) / (2 * 1.645)
def adaptive_smoothing(new_pred: np.ndarray, historical_pred: np.ndarray,
prediction_uncertainty: np.ndarray) -> np.ndarray:
"""
Apply adaptive smoothing based on prediction uncertainty.
Args:
new_pred (np.ndarray): New predictions
historical_pred (np.ndarray): Historical predictions
prediction_uncertainty (np.ndarray): Prediction uncertainty
Returns:
np.ndarray: Smoothed predictions
"""
try:
if len(historical_pred) == 0:
return new_pred
# Calculate adaptive alpha based on uncertainty
uncertainty_ratio = prediction_uncertainty / np.mean(np.abs(historical_pred))
if uncertainty_ratio > 0.1: # High uncertainty
alpha = 0.1 # More smoothing
elif uncertainty_ratio < 0.05: # Low uncertainty
alpha = 0.5 # Less smoothing
else:
alpha = 0.3 # Default
# Apply weighted smoothing
smoothed = alpha * new_pred + (1 - alpha) * historical_pred[-len(new_pred):]
return smoothed
except Exception as e:
print(f"Adaptive smoothing error: {str(e)}")
return new_pred
def advanced_trading_signals(df: pd.DataFrame, regime_info: Dict = None) -> Dict:
"""
Generate advanced trading signals with confidence levels and regime awareness.
Args:
df (pd.DataFrame): Stock data
regime_info (Dict): Market regime information
Returns:
Dict: Advanced trading signals
"""
try:
# Calculate signal strength and confidence
rsi = df['RSI'].iloc[-1]
macd = df['MACD'].iloc[-1]
macd_signal = df['MACD_Signal'].iloc[-1]
rsi_strength = abs(rsi - 50) / 50 # 0-1 scale
macd_strength = abs(macd - macd_signal) / df['Close'].iloc[-1]
# Regime-adjusted thresholds
if regime_info and "volatilities" in regime_info:
volatility_regime = df['Volatility'].iloc[-1] / np.mean(regime_info["volatilities"])
else:
volatility_regime = 1.0
# Adjust RSI thresholds based on volatility
rsi_oversold = 30 + (volatility_regime - 1) * 10
rsi_overbought = 70 - (volatility_regime - 1) * 10
# Calculate signals with confidence
signals = {}
# RSI signal
if rsi < rsi_oversold:
rsi_signal = "Oversold"
rsi_confidence = min(0.9, 0.5 + rsi_strength * 0.4)
elif rsi > rsi_overbought:
rsi_signal = "Overbought"
rsi_confidence = min(0.9, 0.5 + rsi_strength * 0.4)
else:
rsi_signal = "Neutral"
rsi_confidence = 0.3
signals["RSI"] = {
"signal": rsi_signal,
"strength": rsi_strength,
"confidence": rsi_confidence,
"value": rsi
}
# MACD signal
if macd > macd_signal:
macd_signal = "Buy"
macd_confidence = min(0.8, 0.4 + macd_strength * 40)
else:
macd_signal = "Sell"
macd_confidence = min(0.8, 0.4 + macd_strength * 40)
signals["MACD"] = {
"signal": macd_signal,
"strength": macd_strength,
"confidence": macd_confidence,
"value": macd
}
# Bollinger Bands signal
if 'BB_Upper' in df.columns and 'BB_Lower' in df.columns:
current_price = df['Close'].iloc[-1]
bb_upper = df['BB_Upper'].iloc[-1]
bb_lower = df['BB_Lower'].iloc[-1]
# Calculate position within Bollinger Bands (0-1 scale)
bb_position = (current_price - bb_lower) / (bb_upper - bb_lower) if bb_upper != bb_lower else 0.5
bb_strength = abs(bb_position - 0.5) * 2 # 0-1 scale, strongest at edges
if current_price < bb_lower:
bb_signal = "Buy"
bb_confidence = 0.7
elif current_price > bb_upper:
bb_signal = "Sell"
bb_confidence = 0.7
else:
bb_signal = "Hold"
bb_confidence = 0.5
signals["Bollinger"] = {
"signal": bb_signal,
"strength": bb_strength,
"confidence": bb_confidence,
"position": bb_position
}
# SMA signal
if 'SMA_20' in df.columns and 'SMA_50' in df.columns:
sma_20 = df['SMA_20'].iloc[-1]
sma_50 = df['SMA_50'].iloc[-1]
# Calculate SMA strength based on ratio
sma_ratio = sma_20 / sma_50 if sma_50 != 0 else 1.0
sma_strength = abs(sma_ratio - 1.0) # 0-1 scale, strongest when ratio differs most from 1
if sma_20 > sma_50:
sma_signal = "Buy"
sma_confidence = 0.6
else:
sma_signal = "Sell"
sma_confidence = 0.6
signals["SMA"] = {
"signal": sma_signal,
"strength": sma_strength,
"confidence": sma_confidence,
"ratio": sma_ratio
}
# Calculate weighted overall signal
buy_signals = []
sell_signals = []
for signal_name, signal_data in signals.items():
# Get strength with default value if not present
strength = signal_data.get("strength", 0.5) # Default strength of 0.5
confidence = signal_data.get("confidence", 0.5) # Default confidence of 0.5
if signal_data["signal"] == "Buy":
buy_signals.append(strength * confidence)
elif signal_data["signal"] == "Sell":
sell_signals.append(strength * confidence)
weighted_buy = sum(buy_signals) if buy_signals else 0
weighted_sell = sum(sell_signals) if sell_signals else 0
if weighted_buy > weighted_sell:
overall_signal = "Buy"
overall_confidence = weighted_buy / (weighted_buy + weighted_sell) if (weighted_buy + weighted_sell) > 0 else 0
elif weighted_sell > weighted_buy:
overall_signal = "Sell"
overall_confidence = weighted_sell / (weighted_buy + weighted_sell) if (weighted_buy + weighted_sell) > 0 else 0
else:
overall_signal = "Hold"
overall_confidence = 0.5
return {
"signals": signals,
"overall_signal": overall_signal,
"confidence": overall_confidence,
"regime_adjusted": regime_info is not None
}
except Exception as e:
print(f"Advanced trading signals error: {str(e)}")
return {"error": str(e)}
def apply_financial_smoothing(data: np.ndarray, smoothing_type: str = "exponential",
window_size: int = 5, alpha: float = 0.3,
poly_order: int = 3, use_smoothing: bool = True) -> np.ndarray:
"""
Apply financial smoothing algorithms to time series data.
Args:
data (np.ndarray): Input time series data
smoothing_type (str): Type of smoothing to apply
- 'exponential': Exponential moving average (good for trend following)
- 'moving_average': Simple moving average (good for noise reduction)
- 'kalman': Kalman filter (good for adaptive smoothing)
- 'savitzky_golay': Savitzky-Golay filter (good for preserving peaks/valleys)
- 'double_exponential': Double exponential smoothing (good for trend + seasonality)
- 'triple_exponential': Triple exponential smoothing (Holt-Winters, good for complex patterns)
- 'adaptive': Adaptive smoothing based on volatility
- 'none': No smoothing applied
window_size (int): Window size for moving average and Savitzky-Golay
alpha (float): Smoothing factor for exponential methods (0-1)
poly_order (int): Polynomial order for Savitzky-Golay filter
use_smoothing (bool): Whether to apply smoothing
Returns:
np.ndarray: Smoothed data
"""
if not use_smoothing or smoothing_type == "none" or len(data) < 3:
return data
try:
if smoothing_type == "exponential":
# Exponential Moving Average - good for trend following
smoothed = np.zeros_like(data)
smoothed[0] = data[0]
for i in range(1, len(data)):
smoothed[i] = alpha * data[i] + (1 - alpha) * smoothed[i-1]
return smoothed
elif smoothing_type == "moving_average":
# Simple Moving Average - good for noise reduction
if len(data) < window_size:
return data
smoothed = np.zeros_like(data)
# Handle the beginning of the series
for i in range(min(window_size - 1, len(data))):
smoothed[i] = np.mean(data[:i+1])
# Apply moving average for the rest
for i in range(window_size - 1, len(data)):
smoothed[i] = np.mean(data[i-window_size+1:i+1])
return smoothed
elif smoothing_type == "kalman":
# Kalman Filter - adaptive smoothing
if len(data) < 2:
return data
# Initialize Kalman filter parameters
Q = 0.01 # Process noise
R = 0.1 # Measurement noise
P = 1.0 # Initial estimate error
x = data[0] # Initial state estimate
smoothed = np.zeros_like(data)
smoothed[0] = x
for i in range(1, len(data)):
# Prediction step
x_pred = x
P_pred = P + Q
# Update step
K = P_pred / (P_pred + R) # Kalman gain
x = x_pred + K * (data[i] - x_pred)
P = (1 - K) * P_pred
smoothed[i] = x
return smoothed
elif smoothing_type == "savitzky_golay":
# Savitzky-Golay filter - preserves peaks and valleys
if len(data) < window_size:
return data
# Ensure window_size is odd
if window_size % 2 == 0:
window_size += 1
# Ensure polynomial order is less than window_size
if poly_order >= window_size:
poly_order = window_size - 1
try:
from scipy.signal import savgol_filter
return savgol_filter(data, window_size, poly_order)
except ImportError:
# Fallback to simple moving average if scipy not available
return apply_financial_smoothing(data, "moving_average", window_size)
elif smoothing_type == "double_exponential":
# Double Exponential Smoothing (Holt's method) - trend + level
if len(data) < 3:
return data
smoothed = np.zeros_like(data)
trend = np.zeros_like(data)
# Initialize
smoothed[0] = data[0]
trend[0] = data[1] - data[0] if len(data) > 1 else 0
# Apply double exponential smoothing
for i in range(1, len(data)):
prev_smoothed = smoothed[i-1]
prev_trend = trend[i-1]
smoothed[i] = alpha * data[i] + (1 - alpha) * (prev_smoothed + prev_trend)
trend[i] = alpha * (smoothed[i] - prev_smoothed) + (1 - alpha) * prev_trend
return smoothed
elif smoothing_type == "triple_exponential":
# Triple Exponential Smoothing (Holt-Winters) - trend + level + seasonality
if len(data) < 6:
return apply_financial_smoothing(data, "double_exponential", window_size, alpha)
# For simplicity, we'll use a seasonal period of 5 (common for financial data)
season_period = min(5, len(data) // 2)
smoothed = np.zeros_like(data)
trend = np.zeros_like(data)
season = np.zeros_like(data)
# Initialize
smoothed[0] = data[0]
trend[0] = (data[season_period] - data[0]) / season_period if len(data) > season_period else 0
# Initialize seasonal components
for i in range(season_period):
season[i] = data[i] - smoothed[0]
# Apply triple exponential smoothing
for i in range(1, len(data)):
prev_smoothed = smoothed[i-1]
prev_trend = trend[i-1]
prev_season = season[(i-1) % season_period]
smoothed[i] = alpha * (data[i] - prev_season) + (1 - alpha) * (prev_smoothed + prev_trend)
trend[i] = alpha * (smoothed[i] - prev_smoothed) + (1 - alpha) * prev_trend
season[i % season_period] = alpha * (data[i] - smoothed[i]) + (1 - alpha) * prev_season
return smoothed
elif smoothing_type == "adaptive":
# Adaptive smoothing based on volatility
if len(data) < 5:
return data
# Calculate rolling volatility
returns = np.diff(data) / data[:-1]
volatility = np.zeros_like(data)
volatility[0] = np.std(returns) if len(returns) > 0 else 0.01
for i in range(1, len(data)):
if i < 5:
volatility[i] = np.std(returns[:i]) if i > 0 else 0.01
else:
volatility[i] = np.std(returns[i-5:i])
# Normalize volatility to smoothing factor
vol_factor = np.clip(volatility / np.mean(volatility), 0.1, 0.9)
adaptive_alpha = 1 - vol_factor # Higher volatility = less smoothing
# Apply adaptive exponential smoothing
smoothed = np.zeros_like(data)
smoothed[0] = data[0]
for i in range(1, len(data)):
current_alpha = adaptive_alpha[i]
smoothed[i] = current_alpha * data[i] + (1 - current_alpha) * smoothed[i-1]
return smoothed
else:
# Default to exponential smoothing
return apply_financial_smoothing(data, "exponential", window_size, alpha)
except Exception as e:
print(f"Smoothing error: {str(e)}")
return data
def create_interface():
"""Create the Gradio interface with separate tabs for different timeframes"""
with gr.Blocks(title="Advanced Stock Prediction Analysis") as demo:
gr.Markdown("# Advanced Stock Prediction Analysis")
gr.Markdown("Analyze stocks with advanced features including regime detection, ensemble methods, and stress testing.")
# Add market status message
market_status = "Market is currently closed" if not is_market_open() else "Market is currently open"
next_trading_day = get_next_trading_day()
gr.Markdown(f"""
### Market Status: {market_status}
Next trading day: {next_trading_day.strftime('%Y-%m-%d')}
""")
# Advanced Settings Accordion
with gr.Accordion("Advanced Settings", open=False):
with gr.Row():
with gr.Column():
use_ensemble = gr.Checkbox(label="Use Ensemble Methods", value=True)
use_regime_detection = gr.Checkbox(label="Use Regime Detection", value=True)
use_stress_testing = gr.Checkbox(label="Use Stress Testing", value=True)
use_smoothing = gr.Checkbox(label="Use Smoothing", value=True)
smoothing_type = gr.Dropdown(
choices=["exponential", "moving_average", "kalman", "savitzky_golay",
"double_exponential", "triple_exponential", "adaptive", "none"],
label="Smoothing Type",
value="exponential",
info="""Smoothing algorithms:
• Exponential: Trend following (default)
• Moving Average: Noise reduction
• Kalman: Adaptive smoothing
• Savitzky-Golay: Preserves peaks/valleys
• Double Exponential: Trend + level
• Triple Exponential: Complex patterns
• Adaptive: Volatility-based
• None: No smoothing"""
)
smoothing_window = gr.Slider(
minimum=3,
maximum=21,
value=5,
step=1,
label="Smoothing Window Size",
info="Window size for moving average and Savitzky-Golay filters"
)
smoothing_alpha = gr.Slider(
minimum=0.1,
maximum=0.9,
value=0.3,
step=0.05,
label="Smoothing Alpha",
info="Smoothing factor for exponential methods (0.1-0.9)"
)
risk_free_rate = gr.Slider(
minimum=0.0,
maximum=0.1,
value=0.02,
step=0.001,
label="Risk-Free Rate (Annual)"
)
market_index = gr.Dropdown(
choices=["^GSPC", "^DJI", "^IXIC", "^RUT"],
label="Market Index for Correlation",
value="^GSPC"
)
random_real_points = gr.Slider(
minimum=0,
maximum=16,
value=4,
step=1,
label="Random Real Points in Long-Horizon Context"
)
with gr.Column():
gr.Markdown("### Ensemble Weights")
chronos_weight = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.6,
step=0.1,
label="Chronos Weight"
)
technical_weight = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.2,
step=0.1,
label="Technical Weight"
)
statistical_weight = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.2,
step=0.1,
label="Statistical Weight"
)
with gr.Tabs() as tabs:
# Daily Analysis Tab
with gr.TabItem("Daily Analysis"):
with gr.Row():
with gr.Column():
daily_symbol = gr.Textbox(label="Stock Symbol (e.g., AAPL)", value="AAPL")
daily_prediction_days = gr.Slider(
minimum=1,
maximum=365,
value=30,
step=1,
label="Days to Predict"
)
daily_lookback_days = gr.Slider(
minimum=1,
maximum=3650,
value=365,
step=1,
label="Historical Lookback (Days)"
)
daily_strategy = gr.Dropdown(
choices=["chronos", "technical"],
label="Prediction Strategy",
value="chronos"
)
daily_predict_btn = gr.Button("Analyze Stock")
gr.Markdown("""
**Daily Analysis Features:**
- **Extended Data Range**: Up to 10 years of historical data (3650 days)
- **24/7 Availability**: Available regardless of market hours
- **Auto-Adjusted Data**: Automatically adjusted for splits and dividends
- **Comprehensive Financial Ratios**: P/E, PEG, Price-to-Book, Price-to-Sales, and more
- **Advanced Risk Metrics**: Sharpe ratio, VaR, drawdown analysis, market correlation
- **Market Regime Detection**: Identifies bull/bear/sideways market conditions
- **Stress Testing**: Scenario analysis under various market conditions
- **Ensemble Methods**: Combines multiple prediction models for improved accuracy
- **Maximum prediction period**: 365 days
- **Ideal for**: Medium to long-term investment analysis, portfolio management, and strategic planning
- **Technical Indicators**: RSI, MACD, Bollinger Bands, moving averages optimized for daily data
- **Volume Analysis**: Average daily volume, volume volatility, and liquidity metrics
- **Sector Analysis**: Industry classification, market cap ranking, and sector-specific metrics
""")
with gr.Column():
daily_plot = gr.Plot(label="Analysis and Prediction")
with gr.Row():
with gr.Column():
gr.Markdown("### Structured Product Metrics")
daily_metrics = gr.JSON(label="Product Metrics")
gr.Markdown("### Advanced Risk Analysis")
daily_risk_metrics = gr.JSON(label="Risk Metrics")
gr.Markdown("### Market Regime Analysis")
daily_regime_metrics = gr.JSON(label="Regime Metrics")
gr.Markdown("### Trading Signals")
daily_signals = gr.JSON(label="Trading Signals")
gr.Markdown("### Advanced Trading Signals")
daily_signals_advanced = gr.JSON(label="Advanced Trading Signals")
with gr.Column():
gr.Markdown("### Sector & Financial Analysis")
daily_sector_metrics = gr.JSON(label="Sector Metrics")
gr.Markdown("### Stress Test Results")
daily_stress_results = gr.JSON(label="Stress Test Results")
gr.Markdown("### Ensemble Analysis")
daily_ensemble_metrics = gr.JSON(label="Ensemble Metrics")
# Hourly Analysis Tab
with gr.TabItem("Hourly Analysis"):
with gr.Row():
with gr.Column():
hourly_symbol = gr.Textbox(label="Stock Symbol (e.g., AAPL)", value="AAPL")
hourly_prediction_days = gr.Slider(
minimum=1,
maximum=7, # Limited to 7 days for hourly predictions
value=3,
step=1,
label="Days to Predict"
)
hourly_lookback_days = gr.Slider(
minimum=1,
maximum=60, # Enhanced to 60 days for hourly data
value=14,
step=1,
label="Historical Lookback (Days)"
)
hourly_strategy = gr.Dropdown(
choices=["chronos", "technical"],
label="Prediction Strategy",
value="chronos"
)
hourly_predict_btn = gr.Button("Analyze Stock")
gr.Markdown("""
**Hourly Analysis Features:**
- **Extended Data Range**: Up to 60 days of historical data
- **Pre/Post Market Data**: Includes extended hours trading data
- **Auto-Adjusted Data**: Automatically adjusted for splits and dividends
- **Metrics**: Intraday volatility, volume analysis, and momentum indicators
- **Comprehensive Financial Ratios**: P/E, PEG, Price-to-Book, and more
- **Maximum prediction period**: 7 days
- **Data available during market hours only**
""")
with gr.Column():
hourly_plot = gr.Plot(label="Analysis and Prediction")
hourly_signals = gr.JSON(label="Trading Signals")
with gr.Row():
with gr.Column():
gr.Markdown("### Structured Product Metrics")
hourly_metrics = gr.JSON(label="Product Metrics")
gr.Markdown("### Advanced Risk Analysis")
hourly_risk_metrics = gr.JSON(label="Risk Metrics")
gr.Markdown("### Market Regime Analysis")
hourly_regime_metrics = gr.JSON(label="Regime Metrics")
gr.Markdown("### Trading Signals")
hourly_signals_advanced = gr.JSON(label="Advanced Trading Signals")
with gr.Column():
gr.Markdown("### Sector & Financial Analysis")
hourly_sector_metrics = gr.JSON(label="Sector Metrics")
gr.Markdown("### Stress Test Results")
hourly_stress_results = gr.JSON(label="Stress Test Results")
gr.Markdown("### Ensemble Analysis")
hourly_ensemble_metrics = gr.JSON(label="Ensemble Metrics")
# 15-Minute Analysis Tab
with gr.TabItem("15-Minute Analysis"):
with gr.Row():
with gr.Column():
min15_symbol = gr.Textbox(label="Stock Symbol (e.g., AAPL)", value="AAPL")
min15_prediction_days = gr.Slider(
minimum=1,
maximum=2, # Limited to 2 days for 15-minute predictions
value=1,
step=1,
label="Days to Predict"
)
min15_lookback_days = gr.Slider(
minimum=1,
maximum=7, # 7 days for 15-minute data
value=3,
step=1,
label="Historical Lookback (Days)"
)
min15_strategy = gr.Dropdown(
choices=["chronos", "technical"],
label="Prediction Strategy",
value="chronos"
)
min15_predict_btn = gr.Button("Analyze Stock")
gr.Markdown("""
**15-Minute Analysis Features:**
- **Data Range**: Up to 7 days of historical data (vs 5 days previously)
- **High-Frequency Metrics**: Intraday volatility, volume-price trends, momentum analysis
- **Pre/Post Market Data**: Includes extended hours trading data
- **Auto-Adjusted Data**: Automatically adjusted for splits and dividends
- **Enhanced Technical Indicators**: Optimized for short-term trading
- **Maximum prediction period**: 2 days
- **Requires at least 64 data points for Chronos predictions**
- **Data available during market hours only**
""")
with gr.Column():
min15_plot = gr.Plot(label="Analysis and Prediction")
min15_signals = gr.JSON(label="Trading Signals")
with gr.Row():
with gr.Column():
gr.Markdown("### Structured Product Metrics")
min15_metrics = gr.JSON(label="Product Metrics")
gr.Markdown("### Advanced Risk Analysis")
min15_risk_metrics = gr.JSON(label="Risk Metrics")
gr.Markdown("### Market Regime Analysis")
min15_regime_metrics = gr.JSON(label="Regime Metrics")
gr.Markdown("### Trading Signals")
min15_signals_advanced = gr.JSON(label="Advanced Trading Signals")
with gr.Column():
gr.Markdown("### Sector & Financial Analysis")
min15_sector_metrics = gr.JSON(label="Sector Metrics")
gr.Markdown("### Stress Test Results")
min15_stress_results = gr.JSON(label="Stress Test Results")
gr.Markdown("### Ensemble Analysis")
min15_ensemble_metrics = gr.JSON(label="Ensemble Metrics")
def analyze_stock(symbol, timeframe, prediction_days, lookback_days, strategy,
use_ensemble, use_regime_detection, use_stress_testing,
risk_free_rate, market_index, chronos_weight, technical_weight, statistical_weight,
random_real_points, use_smoothing, smoothing_type, smoothing_window, smoothing_alpha):
try:
# Create ensemble weights
ensemble_weights = {
"chronos": chronos_weight,
"technical": technical_weight,
"statistical": statistical_weight
}
# Get market data for correlation analysis
market_df = get_market_data(market_index, lookback_days)
market_returns = market_df['Returns'] if not market_df.empty else None
# Make prediction with advanced features
signals, fig = make_prediction(
symbol=symbol,
timeframe=timeframe,
prediction_days=prediction_days,
strategy=strategy,
use_ensemble=use_ensemble,
use_regime_detection=use_regime_detection,
use_stress_testing=use_stress_testing,
risk_free_rate=risk_free_rate,
ensemble_weights=ensemble_weights,
market_index=market_index,
random_real_points=random_real_points,
use_smoothing=use_smoothing,
smoothing_type=smoothing_type,
smoothing_window=smoothing_window,
smoothing_alpha=smoothing_alpha
)
# Get historical data for additional metrics
df = get_historical_data(symbol, timeframe, lookback_days)
# Calculate structured product metrics
product_metrics = {
"Market_Cap": df['Market_Cap'].iloc[-1],
"Sector": df['Sector'].iloc[-1],
"Industry": df['Industry'].iloc[-1],
"Dividend_Yield": df['Dividend_Yield'].iloc[-1],
"Avg_Daily_Volume": df['Avg_Daily_Volume'].iloc[-1],
"Volume_Volatility": df['Volume_Volatility'].iloc[-1],
"Enterprise_Value": df['Enterprise_Value'].iloc[-1],
"P/E_Ratio": df['P/E_Ratio'].iloc[-1],
"Forward_P/E": df['Forward_P/E'].iloc[-1],
"PEG_Ratio": df['PEG_Ratio'].iloc[-1],
"Price_to_Book": df['Price_to_Book'].iloc[-1],
"Price_to_Sales": df['Price_to_Sales'].iloc[-1]
}
# Calculate advanced risk metrics
risk_metrics = calculate_advanced_risk_metrics(df, market_returns, risk_free_rate)
# Calculate sector metrics
sector_metrics = {
"Sector": df['Sector'].iloc[-1],
"Industry": df['Industry'].iloc[-1],
"Market_Cap_Rank": "Large" if df['Market_Cap'].iloc[-1] > 1e10 else "Mid" if df['Market_Cap'].iloc[-1] > 1e9 else "Small",
"Liquidity_Score": "High" if df['Avg_Daily_Volume'].iloc[-1] > 1e6 else "Medium" if df['Avg_Daily_Volume'].iloc[-1] > 1e5 else "Low",
"Gross_Margin": df['Gross_Margin'].iloc[-1],
"Operating_Margin": df['Operating_Margin'].iloc[-1],
"Net_Margin": df['Net_Margin'].iloc[-1]
}
# Add intraday-specific metrics for shorter timeframes
if timeframe in ["1h", "15m"]:
intraday_metrics = {
"Intraday_Volatility": df['Intraday_Volatility'].iloc[-1] if 'Intraday_Volatility' in df.columns else 0,
"Volume_Ratio": df['Volume_Ratio'].iloc[-1] if 'Volume_Ratio' in df.columns else 0,
"Price_Momentum": df['Price_Momentum'].iloc[-1] if 'Price_Momentum' in df.columns else 0,
"Volume_Momentum": df['Volume_Momentum'].iloc[-1] if 'Volume_Momentum' in df.columns else 0,
"Volume_Price_Trend": df['Volume_Price_Trend'].iloc[-1] if 'Volume_Price_Trend' in df.columns else 0
}
product_metrics.update(intraday_metrics)
# Extract regime and stress test information
regime_metrics = signals.get("regime_info", {})
stress_results = signals.get("stress_test_results", {})
ensemble_metrics = {
"ensemble_used": signals.get("ensemble_used", False),
"ensemble_weights": ensemble_weights
}
# Separate basic and advanced signals
basic_signals = {
"RSI": signals.get("RSI", "Neutral"),
"MACD": signals.get("MACD", "Hold"),
"Bollinger": signals.get("Bollinger", "Hold"),
"SMA": signals.get("SMA", "Hold"),
"Overall": signals.get("Overall", "Hold"),
"symbol": signals.get("symbol", symbol),
"timeframe": signals.get("timeframe", timeframe),
"strategy_used": signals.get("strategy_used", strategy)
}
advanced_signals = signals.get("advanced_signals", {})
return basic_signals, fig, product_metrics, risk_metrics, sector_metrics, regime_metrics, stress_results, ensemble_metrics, advanced_signals
except Exception as e:
error_message = str(e)
if "Market is currently closed" in error_message:
error_message = f"{error_message}. Please try again during market hours or use daily timeframe."
elif "Insufficient data points" in error_message:
error_message = f"Not enough data available for {symbol} in {timeframe} timeframe. Please try a different timeframe or symbol."
elif "no price data found" in error_message:
error_message = f"No data available for {symbol} in {timeframe} timeframe. Please try a different timeframe or symbol."
raise gr.Error(error_message)
# Daily analysis button click
def daily_analysis(s: str, pd: int, ld: int, st: str, ue: bool, urd: bool, ust: bool,
rfr: float, mi: str, cw: float, tw: float, sw: float,
rrp: int, usm: bool, smt: str, sww: float, sa: float) -> Tuple[Dict, go.Figure, Dict, Dict, Dict, Dict, Dict, Dict, Dict]:
"""
Process daily timeframe stock analysis with advanced features.
This function performs comprehensive stock analysis using daily data with support for
multiple prediction strategies, ensemble methods, regime detection, and stress testing.
It's designed for medium to long-term investment analysis with up to 365 days of prediction.
Args:
s (str): Stock symbol (e.g., "AAPL", "MSFT", "GOOGL", "TSLA")
Must be a valid stock symbol available on Yahoo Finance
pd (int): Number of days to predict (1-365)
The forecast horizon for the analysis. Longer periods may have higher uncertainty
ld (int): Historical lookback period in days (1-3650)
Amount of historical data to use for analysis. More data generally improves accuracy
st (str): Prediction strategy to use ("chronos" or "technical")
- "chronos": Uses Amazon's Chronos T5 model for time series forecasting
- "technical": Uses traditional technical analysis indicators
ue (bool): Use ensemble methods
When True, combines multiple prediction models for improved accuracy
urd (bool): Use regime detection
When True, detects market regimes (bull/bear/sideways) to adjust predictions
ust (bool): Use stress testing
When True, performs scenario analysis under various market conditions
rfr (float): Risk-free rate (0.0-0.1)
Annual risk-free rate used for risk-adjusted return calculations
mi (str): Market index for correlation analysis
Options: "^GSPC" (S&P 500), "^DJI" (Dow Jones), "^IXIC" (NASDAQ), "^RUT" (Russell 2000)
cw (float): Chronos weight in ensemble (0.0-1.0)
Weight given to Chronos model predictions in ensemble methods
tw (float): Technical weight in ensemble (0.0-1.0)
Weight given to technical analysis predictions in ensemble methods
sw (float): Statistical weight in ensemble (0.0-1.0)
Weight given to statistical model predictions in ensemble methods
rrp (int): Number of random real points to include in long-horizon context
usm (bool): Use smoothing
When True, applies smoothing to predictions to reduce noise and improve continuity
smt (str): Smoothing type to use
Options: "exponential", "moving_average", "kalman", "savitzky_golay", "double_exponential", "triple_exponential", "adaptive", "none"
sww (float): Smoothing window size for moving average and Savitzky-Golay
sa (float): Smoothing alpha for exponential methods (0.1-0.9)
Returns:
Tuple[Dict, go.Figure, Dict, Dict, Dict, Dict, Dict, Dict, Dict]: Analysis results containing:
- Dict: Basic trading signals (RSI, MACD, Bollinger Bands, SMA, Overall)
- go.Figure: Interactive plot with historical data, predictions, and confidence intervals
- Dict: Structured product metrics (Market Cap, P/E ratios, financial ratios)
- Dict: Advanced risk metrics (Sharpe ratio, VaR, drawdown, correlation)
- Dict: Sector and industry analysis metrics
- Dict: Market regime detection results
- Dict: Stress testing scenario results
- Dict: Ensemble method configuration and results
- Dict: Advanced trading signals with confidence levels
Raises:
gr.Error: If data cannot be fetched, insufficient data points, or other analysis errors
Common errors include invalid symbols, market closure, or insufficient historical data
Example:
>>> signals, plot, metrics, risk, sector, regime, stress, ensemble, advanced = daily_analysis(
... "AAPL", 30, 365, "chronos", True, True, True, 0.02, "^GSPC", 0.6, 0.2, 0.2, 4, True, "exponential", 5, 0.3
... )
Notes:
- Daily analysis is available 24/7 regardless of market hours
- Maximum prediction period is 365 days
- Historical data can go back up to 10 years (3650 days)
- Ensemble weights should sum to 1.0 for optimal results
- Risk-free rate is typically between 0.02-0.05 (2-5% annually)
- Smoothing helps reduce prediction noise but may reduce responsiveness to sudden changes
"""
return analyze_stock(s, "1d", pd, ld, st, ue, urd, ust, rfr, mi, cw, tw, sw, rrp, usm, smt, sww, sa)
daily_predict_btn.click(
fn=daily_analysis,
inputs=[daily_symbol, daily_prediction_days, daily_lookback_days, daily_strategy,
use_ensemble, use_regime_detection, use_stress_testing, risk_free_rate, market_index,
chronos_weight, technical_weight, statistical_weight,
random_real_points, use_smoothing, smoothing_type, smoothing_window, smoothing_alpha],
outputs=[daily_signals, daily_plot, daily_metrics, daily_risk_metrics, daily_sector_metrics,
daily_regime_metrics, daily_stress_results, daily_ensemble_metrics, daily_signals_advanced]
)
# Hourly analysis button click
def hourly_analysis(s: str, pd: int, ld: int, st: str, ue: bool, urd: bool, ust: bool,
rfr: float, mi: str, cw: float, tw: float, sw: float,
rrp: int, usm: bool, smt: str, sww: float, sa: float) -> Tuple[Dict, go.Figure, Dict, Dict, Dict, Dict, Dict, Dict, Dict]:
"""
Process hourly timeframe stock analysis with advanced features.
This function performs high-frequency stock analysis using hourly data, ideal for
short to medium-term trading strategies. It includes intraday volatility analysis,
volume-price trends, and momentum indicators optimized for hourly timeframes.
Args:
s (str): Stock symbol (e.g., "AAPL", "MSFT", "GOOGL", "TSLA")
Must be a valid stock symbol with sufficient liquidity for hourly analysis
pd (int): Number of days to predict (1-7)
Limited to 7 days due to Yahoo Finance hourly data constraints
ld (int): Historical lookback period in days (1-60)
Enhanced to 60 days for hourly data (vs standard 30 days)
st (str): Prediction strategy to use ("chronos" or "technical")
- "chronos": Uses Amazon's Chronos T5 model optimized for hourly data
- "technical": Uses technical indicators adjusted for hourly timeframes
ue (bool): Use ensemble methods
Combines multiple models for improved short-term prediction accuracy
urd (bool): Use regime detection
Detects intraday market regimes and volatility patterns
ust (bool): Use stress testing
Performs scenario analysis for short-term market shocks
rfr (float): Risk-free rate (0.0-0.1)
Annual risk-free rate for risk-adjusted calculations
mi (str): Market index for correlation analysis
Options: "^GSPC" (S&P 500), "^DJI" (Dow Jones), "^IXIC" (NASDAQ), "^RUT" (Russell 2000)
cw (float): Chronos weight in ensemble (0.0-1.0)
Weight for Chronos model in ensemble predictions
tw (float): Technical weight in ensemble (0.0-1.0)
Weight for technical analysis in ensemble predictions
sw (float): Statistical weight in ensemble (0.0-1.0)
Weight for statistical models in ensemble predictions
rrp (int): Number of random real points to include in long-horizon context
usm (bool): Use smoothing
When True, applies smoothing to predictions to reduce noise and improve continuity
smt (str): Smoothing type to use
Options: "exponential", "moving_average", "kalman", "savitzky_golay", "double_exponential", "triple_exponential", "adaptive", "none"
sww (float): Smoothing window size for moving average and Savitzky-Golay
sa (float): Smoothing alpha for exponential methods (0.1-0.9)
Returns:
Tuple[Dict, go.Figure, Dict, Dict, Dict, Dict, Dict, Dict, Dict]: Analysis results containing:
- Dict: Basic trading signals optimized for hourly timeframes
- go.Figure: Interactive plot with hourly data, predictions, and intraday patterns
- Dict: Product metrics including intraday volatility and volume analysis
- Dict: Risk metrics adjusted for hourly data frequency
- Dict: Sector analysis with intraday-specific metrics
- Dict: Market regime detection for hourly patterns
- Dict: Stress testing results for short-term scenarios
- Dict: Ensemble analysis configuration and results
- Dict: Advanced signals with intraday-specific indicators
Raises:
gr.Error: If market is closed, insufficient data, or analysis errors
Hourly data is only available during market hours (9:30 AM - 4:00 PM ET)
Example:
>>> signals, plot, metrics, risk, sector, regime, stress, ensemble, advanced = hourly_analysis(
... "AAPL", 3, 14, "chronos", True, True, True, 0.02, "^GSPC", 0.6, 0.2, 0.2, 4, True, "exponential", 5, 0.3
... )
Notes:
- Only available during market hours (9:30 AM - 4:00 PM ET, weekdays)
- Maximum prediction period is 7 days (168 hours)
- Historical data limited to 60 days due to Yahoo Finance constraints
- Includes pre/post market data for extended hours analysis
- Optimized for day trading and swing trading strategies
- Requires high-liquidity stocks for reliable hourly analysis
- Smoothing helps reduce prediction noise but may reduce responsiveness to sudden changes
"""
return analyze_stock(s, "1h", pd, ld, st, ue, urd, ust, rfr, mi, cw, tw, sw, rrp, usm, smt, sww, sa)
hourly_predict_btn.click(
fn=hourly_analysis,
inputs=[hourly_symbol, hourly_prediction_days, hourly_lookback_days, hourly_strategy,
use_ensemble, use_regime_detection, use_stress_testing, risk_free_rate, market_index,
chronos_weight, technical_weight, statistical_weight,
random_real_points, use_smoothing, smoothing_type, smoothing_window, smoothing_alpha],
outputs=[hourly_signals, hourly_plot, hourly_metrics, hourly_risk_metrics, hourly_sector_metrics,
hourly_regime_metrics, hourly_stress_results, hourly_ensemble_metrics, hourly_signals_advanced]
)
# 15-minute analysis button click
def min15_analysis(s: str, pd: int, ld: int, st: str, ue: bool, urd: bool, ust: bool,
rfr: float, mi: str, cw: float, tw: float, sw: float,
rrp: int, usm: bool, smt: str, sww: float, sa: float) -> Tuple[Dict, go.Figure, Dict, Dict, Dict, Dict, Dict, Dict, Dict]:
"""
Process 15-minute timeframe stock analysis with advanced features.
This function performs ultra-high-frequency stock analysis using 15-minute data,
designed for scalping and very short-term trading strategies. It includes specialized
indicators for intraday patterns, volume analysis, and momentum detection.
Args:
s (str): Stock symbol (e.g., "AAPL", "MSFT", "GOOGL", "TSLA")
Must be a highly liquid stock symbol suitable for high-frequency analysis
pd (int): Number of days to predict (1-2)
Limited to 2 days due to 15-minute data granularity and model constraints
ld (int): Historical lookback period in days (1-7)
Enhanced to 7 days for 15-minute data (vs standard 5 days)
st (str): Prediction strategy to use ("chronos" or "technical")
- "chronos": Uses Amazon's Chronos T5 model optimized for 15-minute intervals
- "technical": Uses technical indicators specifically tuned for 15-minute timeframes
ue (bool): Use ensemble methods
Combines multiple models for improved ultra-short-term prediction accuracy
urd (bool): Use regime detection
Detects micro-market regimes and volatility clustering patterns
ust (bool): Use stress testing
Performs scenario analysis for intraday market shocks and volatility spikes
rfr (float): Risk-free rate (0.0-0.1)
Annual risk-free rate for risk-adjusted calculations (less relevant for 15m analysis)
mi (str): Market index for correlation analysis
Options: "^GSPC" (S&P 500), "^DJI" (Dow Jones), "^IXIC" (NASDAQ), "^RUT" (Russell 2000)
cw (float): Chronos weight in ensemble (0.0-1.0)
Weight for Chronos model in ensemble predictions
tw (float): Technical weight in ensemble (0.0-1.0)
Weight for technical analysis in ensemble predictions
sw (float): Statistical weight in ensemble (0.0-1.0)
Weight for statistical models in ensemble predictions
rrp (int): Number of random real points to include in long-horizon context
usm (bool): Use smoothing
When True, applies smoothing to predictions to reduce noise and improve continuity
smt (str): Smoothing type to use
Options: "exponential", "moving_average", "kalman", "savitzky_golay", "double_exponential", "triple_exponential", "adaptive", "none"
sww (float): Smoothing window size for moving average and Savitzky-Golay
sa (float): Smoothing alpha for exponential methods (0.1-0.9)
Returns:
Tuple[Dict, go.Figure, Dict, Dict, Dict, Dict, Dict, Dict, Dict]: Analysis results containing:
- Dict: Basic trading signals optimized for 15-minute timeframes
- go.Figure: Interactive plot with 15-minute data, predictions, and micro-patterns
- Dict: Product metrics including high-frequency volatility and volume analysis
- Dict: Risk metrics adjusted for 15-minute data frequency
- Dict: Sector analysis with ultra-short-term metrics
- Dict: Market regime detection for 15-minute patterns
- Dict: Stress testing results for intraday scenarios
- Dict: Ensemble analysis configuration and results
- Dict: Advanced signals with 15-minute-specific indicators
Raises:
gr.Error: If market is closed, insufficient data points, or analysis errors
15-minute data requires at least 64 data points and is only available during market hours
Example:
>>> signals, plot, metrics, risk, sector, regime, stress, ensemble, advanced = min15_analysis(
... "AAPL", 1, 3, "chronos", True, True, True, 0.02, "^GSPC", 0.6, 0.2, 0.2, 4, True, "exponential", 5, 0.3
... )
Notes:
- Only available during market hours (9:30 AM - 4:00 PM ET, weekdays)
- Maximum prediction period is 2 days (192 15-minute intervals)
- Historical data limited to 7 days due to Yahoo Finance constraints
- Requires minimum 64 data points for reliable Chronos predictions
- Optimized for scalping and very short-term trading strategies
- Includes specialized indicators for intraday momentum and volume analysis
- Higher transaction costs and slippage considerations for 15-minute strategies
- Best suited for highly liquid large-cap stocks with tight bid-ask spreads
- Smoothing helps reduce prediction noise but may reduce responsiveness to sudden changes
"""
return analyze_stock(s, "15m", pd, ld, st, ue, urd, ust, rfr, mi, cw, tw, sw, rrp, usm, smt, sww, sa)
min15_predict_btn.click(
fn=min15_analysis,
inputs=[min15_symbol, min15_prediction_days, min15_lookback_days, min15_strategy,
use_ensemble, use_regime_detection, use_stress_testing, risk_free_rate, market_index,
chronos_weight, technical_weight, statistical_weight,
random_real_points, use_smoothing, smoothing_type, smoothing_window, smoothing_alpha],
outputs=[min15_signals, min15_plot, min15_metrics, min15_risk_metrics, min15_sector_metrics,
min15_regime_metrics, min15_stress_results, min15_ensemble_metrics, min15_signals_advanced]
)
return demo
if __name__ == "__main__":
demo = create_interface()
demo.launch(ssr_mode=False, mcp_server=True) |