Spaces:
Sleeping
Sleeping
File size: 254,943 Bytes
61ca2b1 | 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 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 | """
网格交易和定投交易回测工具 - 后端主入口
"""
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
from fastapi import FastAPI, File, UploadFile, Form, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from contextlib import asynccontextmanager
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import threading
import json
import pandas as pd
import io
from datetime import datetime, timedelta, timezone
import traceback
import os
import sys
import asyncio
import time
import difflib
import socket
import hashlib
from typing import Any
try:
import httpx
except ImportError:
httpx = None
from v2.api import router as v2_router
from v2.db import (
init_db as init_db_v2,
DatabaseUnavailableError,
test_postgres_socket,
manual_bootstrap_database,
)
from v2.time_sync import now_utc
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
if hasattr(sys.stderr, "reconfigure"):
sys.stderr.reconfigure(encoding="utf-8", errors="replace")
# 添加app目录到路径
_app_dir = os.path.join(os.path.dirname(__file__), 'app')
if os.path.isdir(_app_dir):
sys.path.insert(0, _app_dir)
# 完整的SQLite数据库实现
def get_db_path():
"""获取数据库绝对路径"""
backend_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(backend_dir, 'data')
os.makedirs(data_dir, exist_ok=True)
return os.path.join(data_dir, 'backtest.db')
def init_database():
"""初始化SQLite数据库"""
return False
def save_backtest_to_database(strategy_type: str, symbol: str, params: dict, results: dict, task_id: str):
"""保存回测结果到数据库"""
return False
def load_backtest_history():
"""从数据库加载历史记录"""
return []
def delete_backtest_from_database(record_id: str):
"""从数据库删除记录"""
return False
# 初始化数据库
HAS_DATABASE = False
def _maybe_interactive_db_fix(exc: Exception) -> None:
"""
当 v2 数据库初始化失败时,在本地开发环境下提供一个简单的交互式修复菜单。
仅在标准输入为 TTY 且 `WGJY_DB_INTERACTIVE_FIX` 未显式设置为 `0` 时生效,
避免在 Docker/守护进程等非交互环境中卡死。
"""
enable = (os.getenv('WGJY_DB_INTERACTIVE_FIX') or '1').strip().lower()
if enable in ('0', 'false', 'no'):
return
# 非交互环境直接跳过
if not sys.stdin or not sys.stdin.isatty():
return
print('\n检测到 v2 数据库初始化失败,可以尝试以下快速诊断/修复操作:')
print(' 1) 显示当前数据库连接配置(隐藏密码)')
print(' 2) 测试数据库端口连通性')
print(' 3) 如果已配置 WGJY_PG_ADMIN_URL,尝试自动初始化数据库/用户')
print(' 4) 如果已配置 WGJY_PG_ADMIN_URL,尝试先删除同名数据库再重建(危险,仅开发/测试环境)')
print(' 0) 跳过,不做任何操作')
try:
choice = input('请输入选项编号并回车(直接回车等同于 0):').strip()
except Exception:
return
if not choice or choice == '0':
return
from sqlalchemy.engine.url import make_url
from v2.config import get_database_url
if choice == '1':
try:
raw = get_database_url()
u = make_url(raw)
# SQLAlchemy URL 对象在打印时会自动隐藏密码,这里直接打印即可
print(f'当前数据库连接:{u}')
except Exception as e:
print(f'读取或解析数据库连接串失败:{type(e).__name__}: {e}')
elif choice == '2':
try:
raw = get_database_url()
test_postgres_socket(raw)
print('✅ PostgreSQL 端口连通性正常(TCP 探测通过)。')
except Exception as e:
print(f'❌ PostgreSQL 端口或主机不可达:{type(e).__name__}: {e}')
elif choice == '3':
msg = manual_bootstrap_database()
print(msg)
elif choice == '4':
# 临时开启"删除同名数据库再重建"的行为,只在本次交互中生效
old = os.getenv('WGJY_PG_BOOTSTRAP_DROP_DB')
os.environ['WGJY_PG_BOOTSTRAP_DROP_DB'] = '1'
try:
msg = manual_bootstrap_database()
print(msg)
finally:
if old is None:
os.environ.pop('WGJY_PG_BOOTSTRAP_DROP_DB', None)
else:
os.environ['WGJY_PG_BOOTSTRAP_DROP_DB'] = old
else:
print('未识别的选项,跳过交互式修复。')
@asynccontextmanager
async def lifespan(app: FastAPI):
# 启动时的操作
print("启动回测系统后端服务...")
db_retry_task = None
try:
init_db_v2()
except Exception as e:
print(f"❌ v2 数据库初始化失败: {e}")
_maybe_interactive_db_fix(e)
async def _db_retry_loop():
while True:
try:
await asyncio.sleep(3)
init_db_v2()
except asyncio.CancelledError:
raise
except Exception:
continue
else:
print('✅ v2 数据库初始化成功(已自动恢复)')
return
db_retry_task = asyncio.create_task(_db_retry_loop())
startup_refresh_task = asyncio.create_task(asyncio.to_thread(_refresh_akshare_spot_cache, True))
spot_task = asyncio.create_task(_spot_auto_refresh_loop())
async def _auto_update_daily_close_at_15_10():
"""每个交易日下午 15:10(北京时间)自动执行持仓日线增量更新。"""
while True:
try:
await asyncio.sleep(60)
bj = now_utc().astimezone(timezone(timedelta(hours=8)))
if bj.weekday() >= 5:
continue
if bj.hour != 15 or bj.minute != 10:
continue
if httpx is None:
continue
base = os.environ.get('WGJY_SERVER_URL', 'http://127.0.0.1:8000').rstrip('/')
secret = os.environ.get('WGJY_AUTO_UPDATE_SECRET', '')
try:
async with httpx.AsyncClient(timeout=120.0) as client:
await client.post(
f'{base}/api/v2/trades/prices/close/auto-update',
headers={'X-Auto-Update-Secret': secret} if secret else {},
)
except Exception:
pass
await asyncio.sleep(60)
except asyncio.CancelledError:
raise
except Exception:
continue
auto_update_task = asyncio.create_task(_auto_update_daily_close_at_15_10())
yield
# 关闭时的操作
try:
if db_retry_task is not None:
db_retry_task.cancel()
await db_retry_task
except Exception:
pass
try:
startup_refresh_task.cancel()
await startup_refresh_task
except Exception:
pass
try:
spot_task.cancel()
await spot_task
except Exception:
pass
try:
auto_update_task.cancel()
await auto_update_task
except Exception:
pass
print("关闭回测系统后端服务...")
# 创建FastAPI应用
app = FastAPI(
title="网格交易和定投交易回测工具",
description="专业的量化交易策略回测平台",
version="1.0.0",
lifespan=lifespan,
)
@app.exception_handler(DatabaseUnavailableError)
async def _db_unavailable_handler(request: Request, exc: DatabaseUnavailableError):
return JSONResponse(
status_code=503,
content={
'detail': str(exc) or '数据库不可用,请稍后重试',
'code': 'DATABASE_UNAVAILABLE',
},
headers={'Retry-After': '3'},
)
@app.middleware('http')
async def _block_v1_api(request: Request, call_next):
path = request.url.path or ''
if path == '/api/v1' or path.startswith('/api/v1/'):
return JSONResponse(status_code=410, content={'detail': 'api v1 已下线,请使用 /api/v2'})
return await call_next(request)
app.include_router(v2_router)
# ==================== 并发执行器(避免长任务阻塞接口) ====================
_backtest_history_lock = threading.RLock()
_download_tasks_lock = threading.RLock()
_baostock_lock = threading.RLock()
_baostock_login_state = {'pid': None, 'logged_in': False}
def _ensure_baostock_login(bs):
pid = os.getpid()
if _baostock_login_state.get('pid') != pid:
_baostock_login_state['pid'] = pid
_baostock_login_state['logged_in'] = False
if not _baostock_login_state.get('logged_in'):
lg = bs.login()
if getattr(lg, 'error_code', None) != '0':
raise RuntimeError(getattr(lg, 'error_msg', 'login failed'))
_baostock_login_state['logged_in'] = True
def _make_backtest_executor():
"""Windows 下进程池要求函数可 picklable(不能是 __main__)。
- 以 `python -m uvicorn server:app` 启动时:模块名是 `server`,可用进程池获得真正并行。
- 以 `python server.py` 启动时:模块名是 `__main__`,进程池可能异常;退化为线程池。
"""
cpu = os.cpu_count() or 2
workers = max(2, min(4, cpu))
if __name__ == "__main__":
return ThreadPoolExecutor(max_workers=workers)
return ProcessPoolExecutor(max_workers=workers)
BACKTEST_EXECUTOR = _make_backtest_executor()
DOWNLOAD_EXECUTOR = ThreadPoolExecutor(max_workers=8)
PARSE_EXECUTOR = ThreadPoolExecutor(max_workers=4)
# 上传文件解析缓存(避免重复读取/解析 Excel 导致每次回测都很慢)
_PARSED_UPLOAD_CACHE_TTL_SECONDS = 7 * 86400
_PARSED_UPLOAD_PARSER_VERSION = 'v2'
_parsed_upload_cache_lock = threading.Lock()
_parsed_upload_cache: dict[str, dict[str, Any]] = {}
def _get_parsed_upload_cache_dir() -> str:
backend_dir = os.path.dirname(__file__)
cache_dir = os.path.join(backend_dir, 'cache', 'parsed_uploads')
os.makedirs(cache_dir, exist_ok=True)
return cache_dir
def _get_parsed_upload_cache_path(key: str) -> str:
return os.path.join(_get_parsed_upload_cache_dir(), f"{key}.json")
BAOSTOCK_SOCKET_TIMEOUT_SECONDS = 25
BAOSTOCK_MAX_RETRIES = 2
BAOSTOCK_SEGMENT_PARALLELISM = 4
BAOSTOCK_SEGMENT_PARALLEL_THRESHOLD = 4
def _make_baostock_segment_executor():
workers = max(1, int(BAOSTOCK_SEGMENT_PARALLELISM or 1))
if __name__ == "__main__":
return ThreadPoolExecutor(max_workers=1)
return ProcessPoolExecutor(max_workers=workers)
BAOSTOCK_SEGMENT_EXECUTOR = _make_baostock_segment_executor()
def _iter_date_ranges(start_date: str, end_date: str, days_per_segment: int):
try:
start_dt = datetime.strptime(start_date, '%Y-%m-%d')
end_dt = datetime.strptime(end_date, '%Y-%m-%d')
except Exception:
yield start_date, end_date
return
if end_dt < start_dt:
yield start_date, end_date
return
cur = start_dt
step = max(1, int(days_per_segment or 1))
while cur <= end_dt:
seg_end = min(end_dt, cur + timedelta(days=step - 1))
yield cur.strftime('%Y-%m-%d'), seg_end.strftime('%Y-%m-%d')
cur = seg_end + timedelta(days=1)
def _baostock_query_history_segment(bs, symbol: str, fields: str, start_date: str, end_date: str, frequency: str, adjustflag: str):
last_timeout = socket.getdefaulttimeout()
socket.setdefaulttimeout(BAOSTOCK_SOCKET_TIMEOUT_SECONDS)
try:
with _baostock_lock:
_ensure_baostock_login(bs)
rs = bs.query_history_k_data_plus(
symbol,
fields,
start_date=start_date,
end_date=end_date,
frequency=frequency,
adjustflag=adjustflag,
)
if rs is None:
raise RuntimeError('Baostock返回None')
if getattr(rs, 'error_code', None) != '0':
raise RuntimeError(getattr(rs, 'error_msg', 'unknown'))
data_list = []
while rs.next():
data_list.append(rs.get_row_data())
return data_list, getattr(rs, 'fields', None)
finally:
socket.setdefaulttimeout(last_timeout)
def _baostock_segment_worker(payload: tuple[str, str, str, str, str, str]):
symbol, fields, start_date, end_date, frequency, adjustflag = payload
import baostock as bs # type: ignore
last_timeout = socket.getdefaulttimeout()
socket.setdefaulttimeout(BAOSTOCK_SOCKET_TIMEOUT_SECONDS)
try:
with _baostock_lock:
_ensure_baostock_login(bs)
rs = bs.query_history_k_data_plus(
symbol,
fields,
start_date=start_date,
end_date=end_date,
frequency=frequency,
adjustflag=adjustflag,
)
if rs is None:
raise RuntimeError('Baostock返回None')
if getattr(rs, 'error_code', None) != '0':
raise RuntimeError(getattr(rs, 'error_msg', 'unknown'))
rows = []
while rs.next():
rows.append(rs.get_row_data())
return rows, list(getattr(rs, 'fields', None) or [])
finally:
socket.setdefaulttimeout(last_timeout)
def _grid_backtest_job(params: dict, market_data: list):
engine = GridBacktestEngine(params)
return engine.run(market_data)
def _dca_backtest_job(params: dict, market_data: list):
engine = DCABacktestEngine(params)
return engine.run(market_data)
def _run_download_coroutine(task_id: int) -> None:
"""Run the async download coroutine in a dedicated thread."""
try:
asyncio.run(download_stock_data(task_id))
except Exception as e:
try:
with _download_tasks_lock:
task = download_tasks.get(task_id)
if task:
task['status'] = 'failed'
task['stage'] = 'failed'
task['progress'] = 100
task['error'] = str(e)
_save_download_history()
except Exception:
pass
# 全局计数器,用于生成唯一ID
backtest_counter = 0
# 启动时从数据库加载历史记录
backtest_history = load_backtest_history() if HAS_DATABASE else []
# 简化的数据库存储
import os
def init_simple_database():
"""初始化简化数据库"""
db_path = "data/backtest_simple.db"
# 确保数据目录存在
os.makedirs("data", exist_ok=True)
return False
def save_to_simple_database(record):
"""保存记录到简化数据库"""
return False
def load_from_simple_database():
"""从简化数据库加载所有记录"""
return []
def delete_from_simple_database(record_id):
"""从简化数据库删除记录"""
return False
# 注意:backtest_history 已在上面从主数据库加载,不需要重复加载
# backtest_history = load_from_simple_database() # 已注释掉,避免覆盖主数据库的数据
def generate_backtest_id():
"""生成基于时间和次数的回测任务ID"""
global backtest_counter
backtest_counter += 1
now = datetime.now()
# 格式:YYYYMMDD_HHMMSS_次数
return f"{now.strftime('%Y%m%d_%H%M%S')}_{backtest_counter:03d}"
def save_to_memory_history(strategy_type: str, symbol: str, params: dict, results: dict, task_id: str, market_data: list):
"""保存回测结果到内存历史记录和数据库"""
global backtest_history
# 创建历史记录
history_record = {
"id": task_id,
"strategy_type": strategy_type,
"symbol": symbol,
"start_date": market_data[0]['date'] if market_data else datetime.now().isoformat(),
"end_date": market_data[-1]['date'] if market_data else datetime.now().isoformat(),
"initial_capital": params.get('initial_capital', 100000),
"parameters": params,
"results": results,
"status": "completed",
"created_at": datetime.now().isoformat(),
"completed_at": datetime.now().isoformat()
}
# 保存到数据库(持久化)
if HAS_DATABASE:
save_success = save_backtest_to_database(
strategy_type, symbol, params, results, task_id
)
if save_success:
print(f"✅ 回测记录 {task_id} 已持久化到数据库")
else:
print(f"⚠️ 回测记录 {task_id} 数据库保存失败,仅保存在内存")
with _backtest_history_lock:
backtest_history.insert(0, history_record) # 插入到列表开头,最新的在前面
if len(backtest_history) > 100:
backtest_history = backtest_history[:100]
print(f"✅ 回测记录 {task_id} 已保存,当前内存中有 {len(backtest_history)} 条记录")
# 这个函数已经在上面定义了,删除重复的版本
# 配置CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000", "http://127.0.0.1:3000", "http://localhost:3001", "http://127.0.0.1:3001"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
return {
"message": "网格交易和定投交易回测工具 API",
"version": "1.0.0",
"docs": "/docs"
}
@app.get("/health")
async def health_check():
return {"status": "healthy"}
class GridBacktestEngine:
"""真实网格交易回测引擎"""
def __init__(self, params):
self.params = params
# 基本参数
self.initial_capital = float(params.get('initial_capital', 100000))
self.initial_shares = int(params.get('initial_shares', 0))
# 日期范围
self.start_date = params.get('start_date', None)
self.end_date = params.get('end_date', None)
# 网格参数
self.trigger_price_min = float(params.get('trigger_price_min', 8.5))
self.trigger_price_max = float(params.get('trigger_price_max', 76.5))
self.rise_trigger = float(params.get('rise_trigger', 1)) / 100 # 转换为小数
self.fall_trigger = float(params.get('fall_trigger', 1)) / 100 # 转换为小数
# 回落反弹参数
self.rise_pullback_enabled = params.get('rise_pullback_enabled', False)
self.rise_pullback = float(params.get('rise_pullback', 0)) / 100 if params.get('rise_pullback_enabled') else 0
self.fall_rebound_enabled = params.get('fall_rebound_enabled', False)
self.fall_rebound = float(params.get('fall_rebound', 0)) / 100 if params.get('fall_rebound_enabled') else 0
# 委托设置
self.order_type = params.get('order_type', 'shares') # shares 或 amount
self.buy_amount = float(params.get('buy_amount', 100))
self.sell_amount = float(params.get('sell_amount', 100))
# 手续费 - 兼容前端字段名
self.commission_rate = float(params.get('fee_rate', params.get('commission_rate', 0.0005))) # 默认万分之50 = 0.5%
self.min_commission = float(params.get('min_fee', params.get('min_commission', 0.01))) # 默认最低0.01元
self.fee_bilateral = params.get('fee_bilateral', True)
# 初始化基准价
self.base_price = float(params.get('custom_base_price', 25.0))
# 基准价更新方式
self.base_price_update_mode = params.get('base_price_update_mode', 'trigger_price') # trigger_price 或 trade_price
# 倍数委托:仅在"成交价更新基准价"模式下生效
# 当实际涨跌幅远大于设定触发比例时,按「实际涨跌幅 / 触发比例」放大单次委托数量/金额。
# 例如:触发比例 1%,实际上涨 3.2%,则按约 3.2 倍基础委托量下单(仍会按 100 股整数倍取整)。
self.multiple_order_enabled = bool(params.get('multiple_order_enabled', False))
# 持仓限制
self.position_limit_enabled = bool(params.get('position_limit_enabled', True))
self.position_limit_type = params.get('position_limit_type', 'shares')
if self.position_limit_enabled:
self.max_position = float(params.get('max_position', 0)) if params.get('max_position') else None
self.min_position = float(params.get('min_position', 0)) if params.get('min_position') else None
else:
self.max_position = None
self.min_position = None
# 回落反弹状态跟踪
self.pending_sell = False # 是否有待执行的卖出(等待回落)
self.pending_buy = False # 是否有待执行的买入(等待反弹)
self.peak_price = self.base_price # 上涨过程中的峰值价格,初始化为基准价
self.valley_price = self.base_price # 下跌过程中的谷值价格,初始化为基准价
print(f"回测引擎初始化完成")
print(f"初始资金: {self.initial_capital}, 初始股数: {self.initial_shares}")
print(f"网格区间: {self.trigger_price_min} - {self.trigger_price_max}")
print(f"触发幅度: 上涨{self.rise_trigger*100:.2f}%, 下跌{self.fall_trigger*100:.2f}%")
print(f"基准价更新方式: {self.base_price_update_mode}")
if self.rise_pullback_enabled:
print(f"上涨回落: {self.rise_pullback*100:.2f}%")
if self.fall_rebound_enabled:
print(f"下跌反弹: {self.fall_rebound*100:.2f}%")
print(f"手续费参数: 费率={self.commission_rate*10000}万分之一, 最低手续费={self.min_commission}元, 双边收费={self.fee_bilateral}")
print(f"委托参数: 类型={self.order_type}, 买入量={self.buy_amount}, 卖出量={self.sell_amount}")
print(f"初始资金: {self.initial_capital}元, 初始股数: {self.initial_shares}股")
print(f"回测日期: {self.start_date} 至 {self.end_date}")
def run(self, market_data):
"""执行真实网格回测"""
if not market_data or len(market_data) == 0:
raise ValueError("市场数据为空")
# 根据日期范围过滤数据
if self.start_date or self.end_date:
original_count = len(market_data)
filtered_data = []
# 调试:显示前几条和最后几条数据的日期格式
print(f"调试信息 - 前3条数据的日期:")
for i, row in enumerate(market_data[:3]):
print(f" 第{i+1}条: {row.get('date', 'NO_DATE')} (原始格式)")
print(f"调试信息 - 最后3条数据的日期:")
for i, row in enumerate(market_data[-3:]):
print(f" 倒数第{3-i}条: {row.get('date', 'NO_DATE')} (原始格式)")
for row in market_data:
row_date = str(row['date']).split(' ')[0] # 只取日期部分
# 检查是否在日期范围内
include_row = True
try:
# 使用字符串比较进行日期过滤
if self.start_date and row_date < self.start_date:
include_row = False
if self.end_date and row_date > self.end_date:
include_row = False
except Exception as e:
print(f"日期比较错误: {row_date}, {e}")
include_row = True # 如果日期比较失败,保留数据
if include_row:
filtered_data.append(row)
market_data = filtered_data
print(f"日期过滤: 原始数据{original_count}条 -> 过滤后{len(market_data)}条")
print(f"查找日期范围: {self.start_date} 至 {self.end_date}")
if not market_data:
raise ValueError(f"指定日期范围 {self.start_date} 至 {self.end_date} 内没有数据")
print(f"开始回测,数据条数: {len(market_data)}")
print(f"价格范围: {market_data[0]['close']} -> {market_data[-1]['close']}")
print(f"日期范围: {market_data[0]['date']} -> {market_data[-1]['date']}")
# 初始化账户
cash = self.initial_capital
shares = self.initial_shares
total_fee = 0
trades = []
equity_curve = []
# 新增:按"交易日"统计的权益序列,用于计算盈利/亏损天数与连续天数
_daily_equity: list[float] = []
_daily_position: list[float] = []
_prev_day: str | None = None
_last_day_equity: float | None = None
_last_day_position: float | None = None
# 设置基准价为第一天收盘价
first_price = float(market_data[0]['close'])
self.base_price = first_price
initial_position_value = shares * first_price
print(f"初始状态: 现金={cash}, 股数={shares}, 基准价={self.base_price}")
# 统计变量
max_position_value = initial_position_value
min_position_value = initial_position_value
max_position_shares = shares
min_position_shares = shares
peak_equity = self.initial_capital + initial_position_value
max_drawdown = 0
# 遍历每个交易日
for i, row in enumerate(market_data):
current_price = float(row['close'])
current_date = str(row['date'])
# 检查价格是否在网格区间内
if current_price < self.trigger_price_min or current_price > self.trigger_price_max:
# 价格超出网格区间,不交易
pass
else:
# 计算相对于基准价的涨跌幅
if self.base_price > 0:
price_change = (current_price - self.base_price) / self.base_price
# 回落反弹交易逻辑
trigger_reason = ""
should_buy = False
should_sell = False
# 保存当前的待执行状态(在状态重置前)
was_pending_buy = self.pending_buy
was_pending_sell = self.pending_sell
current_valley_price = self.valley_price
current_peak_price = self.peak_price
# 检查是否有待执行的买入(下跌反弹)
if self.pending_buy:
# 更新谷值价格
self.valley_price = min(self.valley_price, current_price)
# 检查是否有足够的反弹
rebound_change = (current_price - self.valley_price) / self.valley_price
if rebound_change >= self.fall_rebound:
should_buy = True
original_change = (self.valley_price - self.base_price) / self.base_price
trigger_reason = f"下跌{abs(original_change)*100:.2f}%触发,反弹{rebound_change*100:.2f}%执行"
# 注意:不在这里重置pending_buy,在交易执行后再重置
print(f"执行买入: {current_date} 价格={current_price:.2f} {trigger_reason}")
# 检查是否有待执行的卖出(上涨回落)
elif self.pending_sell:
# 更新峰值价格
self.peak_price = max(self.peak_price, current_price)
# 检查是否有足够的回落
pullback_change = (self.peak_price - current_price) / self.peak_price
if pullback_change >= self.rise_pullback:
should_sell = True
original_change = (self.peak_price - self.base_price) / self.base_price
trigger_reason = f"上涨{original_change*100:.2f}%触发,回落{pullback_change*100:.2f}%执行"
# 注意:不在这里重置pending_sell,在交易执行后再重置
print(f"执行卖出: {current_date} 价格={current_price:.2f} {trigger_reason}")
# 检查新的触发条件
else:
# 检查下跌触发买入条件
if price_change <= -self.fall_trigger: # 下跌触发买入
if self.fall_rebound_enabled:
# 启用下跌反弹:设置待执行状态
self.pending_buy = True
self.valley_price = current_price
print(f"下跌触发买入信号,等待反弹: {current_date} 价格={current_price:.2f} 跌幅={abs(price_change)*100:.2f}%")
else:
# 未启用下跌反弹:直接买入
should_buy = True
trigger_reason = f"下跌{abs(price_change)*100:.2f}%触发"
# 检查上涨触发卖出条件
elif price_change >= self.rise_trigger: # 上涨触发卖出
if self.rise_pullback_enabled:
# 启用上涨回落:设置待执行状态
self.pending_sell = True
self.peak_price = current_price
print(f"上涨触发卖出信号,等待回落: {current_date} 价格={current_price:.2f} 涨幅={price_change*100:.2f}%")
else:
# 未启用上涨回落:直接卖出
should_sell = True
trigger_reason = f"上涨{price_change*100:.2f}%触发"
# 执行买入交易
if should_buy:
# 每笔基础委托股数(不因跨多格而放大)
base_buy_shares = int(self.buy_amount) if self.order_type == 'shares' else int(self.buy_amount / current_price)
# 触发价更新模式下:如果一次性跌破多个网格,就拆成多笔委托
trigger_steps_buy = 1
if (
self.base_price_update_mode == 'trigger_price'
and not was_pending_buy
and self.fall_trigger > 0
):
try:
total_change_abs = abs(price_change)
trigger_steps_buy = max(1, int(total_change_abs / self.fall_trigger))
except Exception:
trigger_steps_buy = 1
executions = (
trigger_steps_buy
if (self.base_price_update_mode == 'trigger_price' and not was_pending_buy)
else 1
)
for _ in range(executions):
buy_shares = base_buy_shares
# 倍数委托:仅在成交价更新基准价模式下生效
if self.multiple_order_enabled and self.base_price_update_mode == 'trade_price':
try:
# 计算本次相对基准价的总涨跌幅(绝对值)
if was_pending_buy:
# 下跌+反弹模式:参考触发时的谷值
total_change = abs((current_valley_price - old_base_price) / old_base_price)
else:
# 普通模式:直接使用当前价相对基准价的跌幅
total_change = abs(price_change)
unit_change = self.fall_trigger # 每"1 格"对应的跌幅(小数)
if unit_change > 0:
multiple = max(1.0, total_change / unit_change)
buy_shares = int(buy_shares * multiple)
except Exception as e:
print(f"倍数委托(买入)计算失败,按基础委托量执行: {e}")
# 股数必须为100的倍数
buy_shares = (buy_shares // 100) * 100
# 检查持仓上限
if self.max_position is not None and buy_shares > 0:
if self.position_limit_type == 'shares':
if shares + buy_shares > self.max_position:
# 调整买入数量以满足上限
original_buy_shares = buy_shares
buy_shares = int(self.max_position - shares)
buy_shares = (buy_shares // 100) * 100 # 再次确保是100的倍数
print(f"持仓上限警告: 计划买入 {original_buy_shares} 股将超过最大持股数 {self.max_position}。调整为买入 {buy_shares} 股。")
elif self.position_limit_type == 'amount':
if (shares + buy_shares) * current_price > self.max_position:
# 调整买入数量以满足上限
original_buy_shares = buy_shares
allowed_value = self.max_position - (shares * current_price)
buy_shares = int(allowed_value / current_price)
buy_shares = (buy_shares // 100) * 100
print(f"持仓上限警告: 计划买入 {original_buy_shares} 股将超过最大持仓金额 {self.max_position}。调整为买入 {buy_shares} 股。")
cost = buy_shares * current_price
fee = max(cost * self.commission_rate, self.min_commission)
if not (cash >= cost + fee and buy_shares >= 100): # 至少100股才交易
break
cash -= (cost + fee)
shares += buy_shares
total_fee += fee
# 根据基准价更新方式更新基准价
old_base_price = self.base_price # 保存旧基准价用于计算触发价
print(f"DEBUG买入: base_price_update_mode = '{self.base_price_update_mode}', was_pending_buy = {was_pending_buy}")
if self.base_price_update_mode == 'trigger_price':
# 触发价更新:使用触发条件对应的价格
if was_pending_buy:
# 回落反弹模式:使用谷值价格(触发点),只更新一次
self.base_price = current_valley_price
print(f"基准价更新为触发价: {current_valley_price:.2f}元(谷值价格)")
break
else:
# 普通模式:每次按一个网格级差更新一次基准价
trigger_price = old_base_price * (1 - self.fall_trigger)
self.base_price = trigger_price
print(f"基准价更新为触发价: {trigger_price:.4f}元(单格理论触发价)")
else:
# 成交价更新:使用成交价格
self.base_price = current_price
print(f"基准价更新为成交价: {current_price:.2f}元")
trade = {
"date": current_date,
"type": "买入",
"price": round(current_price, 2),
"shares": buy_shares,
"amount": round(cost, 2),
"fee": round(fee, 2),
"cash_balance": round(cash, 2),
"position_value": round(shares * current_price, 2),
"current_shares": shares, # 当前持仓股数
"total_value": round(cash + shares * current_price, 2),
"base_price": round(self.base_price, 4),
"trigger_reason": trigger_reason
}
trades.append(trade)
print(f"买入: {current_date} 价格={current_price:.2f} 数量={buy_shares} 触发条件={trigger_reason}")
# 重置待执行状态(如果是回落反弹交易)
if was_pending_buy:
self.pending_buy = False
# 执行卖出交易
elif should_sell and shares > 0:
# 每笔基础委托股数(不因跨多格而放大)
base_sell_shares = min(int(self.sell_amount), shares) if self.order_type == 'shares' else min(int(self.sell_amount / current_price), shares)
# 触发价更新模式下:如果一次性上涨超过多个网格,就拆成多笔委托
trigger_steps_sell = 1
if (
self.base_price_update_mode == 'trigger_price'
and not was_pending_sell
and self.rise_trigger > 0
):
try:
total_change_up = max(0.0, price_change)
trigger_steps_sell = max(1, int(total_change_up / self.rise_trigger))
except Exception:
trigger_steps_sell = 1
executions_sell = (
trigger_steps_sell
if (self.base_price_update_mode == 'trigger_price' and not was_pending_sell)
else 1
)
for _ in range(executions_sell):
if shares <= 0:
break
sell_shares = min(base_sell_shares, shares)
# 倍数委托:仅在成交价更新基准价模式下生效
if self.multiple_order_enabled and self.base_price_update_mode == 'trade_price':
try:
if was_pending_sell:
# 上涨+回落模式:参考触发时的峰值
total_change = (current_peak_price - old_base_price) / old_base_price
else:
total_change = price_change # 上涨为正
unit_change = self.rise_trigger
if unit_change > 0:
multiple = max(1.0, total_change / unit_change)
sell_shares = int(sell_shares * multiple)
except Exception as e:
print(f"倍数委托(卖出)计算失败,按基础委托量执行: {e}")
# 股数必须为100的倍数
sell_shares = (sell_shares // 100) * 100
# 检查持仓下限
if self.min_position is not None and sell_shares > 0:
if self.position_limit_type == 'shares':
if shares - sell_shares < self.min_position:
# 调整卖出数量以满足下限
original_sell_shares = sell_shares
sell_shares = int(shares - self.min_position)
sell_shares = (sell_shares // 100) * 100
print(f"持仓下限警告: 计划卖出 {original_sell_shares} 股将低于最小持股数 {self.min_position}。调整为卖出 {sell_shares} 股。")
elif self.position_limit_type == 'amount':
if (shares - sell_shares) * current_price < self.min_position:
# 调整卖出数量以满足下限
original_sell_shares = sell_shares
allowed_sell_value = (shares * current_price) - self.min_position
sell_shares = int(allowed_sell_value / current_price)
sell_shares = (sell_shares // 100) * 100
print(f"持仓下限警告: 计划卖出 {original_sell_shares} 股将低于最小持仓金额 {self.min_position}。调整为卖出 {sell_shares} 股。")
# 再次确保本次卖出数量不会超过当前持仓(防止出现负持仓)
if sell_shares > shares:
sell_shares = (shares // 100) * 100
if sell_shares < 100: # 至少100股才交易
break
revenue = sell_shares * current_price
fee = max(revenue * self.commission_rate, self.min_commission) if self.fee_bilateral else 0
cash += (revenue - fee)
shares -= sell_shares
total_fee += fee
# 根据基准价更新方式更新基准价
old_base_price = self.base_price # 保存旧基准价用于计算触发价
print(f"DEBUG卖出: base_price_update_mode = '{self.base_price_update_mode}', was_pending_sell = {was_pending_sell}")
if self.base_price_update_mode == 'trigger_price':
# 触发价更新:使用触发条件对应的价格
if was_pending_sell:
# 回落反弹模式:使用峰值价格(触发点),只更新一次
self.base_price = current_peak_price
print(f"基准价更新为触发价: {current_peak_price:.2f}元(峰值价格)")
break
else:
# 普通模式:每次按一个网格级差更新一次基准价
trigger_price = old_base_price * (1 + self.rise_trigger)
self.base_price = trigger_price
print(f"基准价更新为触发价: {trigger_price:.4f}元(单格理论触发价)")
else:
# 成交价更新:使用成交价格
self.base_price = current_price
print(f"基准价更新为成交价: {current_price:.2f}元")
trade = {
"date": current_date,
"type": "卖出",
"price": round(current_price, 2),
"shares": sell_shares,
"amount": round(revenue, 2),
"fee": round(fee, 2),
"cash_balance": round(cash, 2),
"position_value": round(shares * current_price, 2),
"current_shares": shares, # 当前持仓股数
"total_value": round(cash + shares * current_price, 2),
"base_price": round(self.base_price, 4),
"trigger_reason": trigger_reason
}
trades.append(trade)
print(f"卖出: {current_date} 价格={current_price:.2f} 数量={sell_shares} 触发条件={trigger_reason}")
# 重置待执行状态(如果是回落反弹交易)
if was_pending_sell:
self.pending_sell = False
# 更新持仓统计
current_position_value = shares * current_price
max_position_value = max(max_position_value, current_position_value)
min_position_value = min(min_position_value, current_position_value)
max_position_shares = max(max_position_shares, shares)
min_position_shares = min(min_position_shares, shares)
# 计算权益曲线和回撤
current_equity = cash + current_position_value
if current_equity > peak_equity:
peak_equity = current_equity
drawdown = (peak_equity - current_equity) / peak_equity if peak_equity > 0 else 0
max_drawdown = max(max_drawdown, drawdown)
# 新增:按日聚合权益(用于盈利/亏损天数、连续天数、资金利用率)
day_key = str(current_date).split(' ')[0].split('T')[0]
if _prev_day is None:
_prev_day = day_key
if day_key != _prev_day:
if _last_day_equity is not None and _last_day_position is not None:
_daily_equity.append(float(_last_day_equity))
_daily_position.append(float(_last_day_position))
_prev_day = day_key
_last_day_equity = float(current_equity)
_last_day_position = float(current_position_value)
# 记录权益曲线(每10天记录一次以减少数据量)
if i % 10 == 0 or i == len(market_data) - 1:
equity_curve.append({
"date": current_date,
"value": round(current_equity, 2),
"cash": round(cash, 2),
"position": round(current_position_value, 2),
"return_rate": round((current_equity - self.initial_capital - initial_position_value) / self.initial_capital * 100, 2)
})
# 计算最终指标
last_price = float(market_data[-1]['close'])
final_equity = cash + shares * last_price
initial_equity = self.initial_capital + initial_position_value
total_profit = final_equity - initial_equity
total_return = (total_profit / initial_equity) * 100 if initial_equity > 0 else 0
# 新增:补齐最后一个交易日
if _last_day_equity is not None and _last_day_position is not None:
_daily_equity.append(float(_last_day_equity))
_daily_position.append(float(_last_day_position))
profit_days = 0
loss_days = 0
max_profit_streak = 0
max_loss_streak = 0
_cur_profit = 0
_cur_loss = 0
if len(_daily_equity) >= 2:
for i in range(1, len(_daily_equity)):
diff = _daily_equity[i] - _daily_equity[i - 1]
if diff > 0:
profit_days += 1
_cur_profit += 1
_cur_loss = 0
elif diff < 0:
loss_days += 1
_cur_loss += 1
_cur_profit = 0
else:
_cur_profit = 0
_cur_loss = 0
max_profit_streak = max(max_profit_streak, _cur_profit)
max_loss_streak = max(max_loss_streak, _cur_loss)
capital_utilization = 0.0
try:
if initial_equity > 0 and len(_daily_position) > 0:
capital_utilization = (sum(_daily_position) / len(_daily_position)) / initial_equity * 100
except Exception:
capital_utilization = 0.0
# 计算年化收益率
start_dt = None
end_dt = None
try:
start_dt = pd.to_datetime(market_data[0].get('date')).to_pydatetime()
end_dt = pd.to_datetime(market_data[-1].get('date')).to_pydatetime()
except Exception:
start_dt = None
end_dt = None
if start_dt and end_dt:
days = max((end_dt - start_dt).days, 1)
else:
days = max(len(market_data), 1)
years = max(days / 365.25, 1 / 365.25) # 至少1天
annual_return = ((final_equity / initial_equity) ** (1 / years) - 1) * 100 if initial_equity > 0 else 0
# 计算胜率
buy_trades = [t for t in trades if t['type'] == '买入']
sell_trades = [t for t in trades if t['type'] == '卖出']
winning_trades = 0
# 简单计算:卖出价格高于对应买入价格
for i, sell_trade in enumerate(sell_trades):
if i < len(buy_trades) and sell_trade['price'] > buy_trades[i]['price']:
winning_trades += 1
win_rate = (winning_trades / len(sell_trades) * 100) if len(sell_trades) > 0 else 0
# 计算最大收益率
max_return_rate = 0
if len(equity_curve) > 0:
initial_equity = self.initial_capital + initial_position_value
max_equity_in_curve = max([point['value'] for point in equity_curve])
max_return_rate = ((max_equity_in_curve - initial_equity) / initial_equity) * 100
print(f"回测完成: 交易{len(trades)}次, 最终权益={final_equity:.2f}, 收益率={total_return:.2f}%")
return {
"final_equity": round(final_equity, 2),
"total_profit": round(total_profit, 2),
"total_return": round(total_return, 2),
"annual_return": round(annual_return, 2),
"total_trades": len(trades),
"buy_trades": len(buy_trades), # 买入次数
"sell_trades": len(sell_trades), # 卖出次数
"win_rate": round(win_rate, 2),
"max_invested_return": round(max_return_rate, 2), # 最大投入收益率
"max_drawdown": round(max_drawdown * 100, 2),
"sharpe_ratio": round(annual_return / max(max_drawdown * 100, 1), 2),
"total_fee": round(total_fee, 2),
"max_position_value": round(max_position_value, 2),
"min_position_value": round(min_position_value, 2),
"max_position_shares": max_position_shares,
"min_position_shares": min_position_shares,
"profit_days": int(profit_days),
"loss_days": int(loss_days),
"max_profit_streak": int(max_profit_streak),
"max_loss_streak": int(max_loss_streak),
"capital_utilization": round(float(capital_utilization), 2),
"trade_records": trades, # 返回所有交易记录
"equity_curve": equity_curve,
"summary": {
"data_points": len(market_data),
"trading_days": days,
"price_range": f"{market_data[0]['close']:.2f} - {market_data[-1]['close']:.2f}",
"buy_trades": len(buy_trades),
"sell_trades": len(sell_trades),
"final_cash": round(cash, 2),
"final_shares": shares,
"final_position_value": round(shares * last_price, 2)
}
}
def parse_uploaded_file(file_content: bytes, filename: str, keep_intraday: bool = False):
"""解析上传的文件"""
try:
t0 = time.perf_counter()
# 解析缓存:同一个文件重复回测时直接复用(尤其 xlsx 读取很慢)
base_hash = hashlib.sha1(file_content).hexdigest()
cache_key = f"{base_hash}_{'intraday' if keep_intraday else 'daily'}_{_PARSED_UPLOAD_PARSER_VERSION}"
now_ts = time.time()
try:
with _parsed_upload_cache_lock:
hit = _parsed_upload_cache.get(cache_key)
if hit and (now_ts - float(hit.get('ts', 0.0) or 0.0)) < _PARSED_UPLOAD_CACHE_TTL_SECONDS:
data = hit.get('data')
if isinstance(data, list) and data:
print(f"✅ 命中解析缓存: {cache_key}({len(data)}条)")
return data
except Exception:
pass
# 先尝试从磁盘缓存加载(支持重启后复用)
try:
disk_path = _get_parsed_upload_cache_path(cache_key)
if os.path.exists(disk_path):
mtime = os.path.getmtime(disk_path)
if (now_ts - mtime) < _PARSED_UPLOAD_CACHE_TTL_SECONDS:
with open(disk_path, 'r', encoding='utf-8') as f:
data = json.load(f)
if isinstance(data, list) and data:
with _parsed_upload_cache_lock:
_parsed_upload_cache[cache_key] = {'ts': now_ts, 'data': data}
print(f"✅ 命中磁盘解析缓存: {cache_key}({len(data)}条)")
return data
except Exception:
pass
if filename.endswith('.csv'):
# 直接从 bytes 读取,避免大文件 decode 成巨大的字符串
try:
df = pd.read_csv(io.BytesIO(file_content), encoding='utf-8', engine='c', low_memory=False)
except Exception:
df = pd.read_csv(io.BytesIO(file_content), encoding='gbk', engine='c', low_memory=False)
elif filename.endswith('.day'):
# 通达信日线 .day(二进制)
from v2.tdx_day import parse_tdx_day_bytes
# 这里不依赖 code6:回测只需要价格序列
rows = parse_tdx_day_bytes(buf=file_content, code6='000000')
if not isinstance(rows, list) or not rows:
raise ValueError('文件中未解析出有效的 .day 数据')
market_data = []
for r in rows:
try:
market_data.append(
{
'date': str(r.get('trade_date') or r.get('date') or '').strip(),
'open': float(r.get('open') or r.get('close') or 0),
'high': float(r.get('high') or r.get('close') or 0),
'low': float(r.get('low') or r.get('close') or 0),
'close': float(r.get('close') or 0),
'volume': int(float(r.get('volume') or 0)),
}
)
except Exception:
continue
# 写入缓存(内存+磁盘)并直接返回
try:
if isinstance(market_data, list) and market_data:
with _parsed_upload_cache_lock:
_parsed_upload_cache[cache_key] = {'ts': now_ts, 'data': market_data}
disk_path = _get_parsed_upload_cache_path(cache_key)
with open(disk_path, 'w', encoding='utf-8') as f:
json.dump(market_data, f, ensure_ascii=False)
except Exception:
pass
print(f"成功解析 {len(market_data)} 条数据(耗时 {(time.perf_counter() - t0):.3f}s)")
if market_data:
print(f"样例数据: {market_data[0]}")
return market_data
elif filename.endswith(('.xlsx', '.xls')):
try:
wanted = {
'日期', '时间', 'Date', 'Time',
'开盘价', '开盘', 'Open',
'最高价', '最高', 'High',
'最低价', '最低', 'Low',
'收盘价', '收盘', 'Close',
'成交量', 'Volume',
}
wanted_lower = {str(x).strip().lower() for x in wanted} | {
'date', 'time',
'open', 'high', 'low', 'close',
'volume',
}
df = pd.read_excel(
io.BytesIO(file_content),
engine='openpyxl',
usecols=lambda c: str(c).strip().lower() in wanted_lower,
)
except Exception:
df = pd.read_excel(io.BytesIO(file_content))
elif filename.endswith('.json'):
data = json.loads(file_content.decode('utf-8'))
df = pd.DataFrame(data)
else:
raise ValueError("不支持的文件格式")
df.columns = [str(c).strip() for c in df.columns]
print(f"原始列名: {df.columns.tolist()}")
# 标准化列名
# 注意:分钟线常见同时存在"日期"和"时间",不能都映射为 date,否则会出现重复列
column_mapping = {
'日期': 'date', 'Date': 'date', 'DATE': 'date',
'时间': 'time', 'Time': 'time', 'TIME': 'time',
'开盘价': 'open', '开盘': 'open', 'Open': 'open', 'OPEN': 'open',
'最高价': 'high', '最高': 'high', 'High': 'high', 'HIGH': 'high',
'最低价': 'low', '最低': 'low', 'Low': 'low', 'LOW': 'low',
'收盘价': 'close', '收盘': 'close', 'Close': 'close', 'CLOSE': 'close',
'成交量': 'volume', 'Volume': 'volume', 'VOLUME': 'volume'
}
df = df.rename(columns=column_mapping)
print(f"标准化后列名: {df.columns.tolist()}")
# 确保必需的列存在
# date/time 二选一至少要有一个;close 必须有
missing_columns = []
if 'close' not in df.columns:
missing_columns.append('close')
if 'date' not in df.columns and 'time' not in df.columns:
missing_columns.append('date')
if missing_columns:
# 尝试自动检测列
if len(df.columns) >= 5:
df.columns = ['date', 'open', 'high', 'low', 'close', 'volume'][:len(df.columns)]
else:
raise ValueError(f"缺少必需的列: {missing_columns}")
# 统一补齐/清洗字段(向量化,避免 iterrows 带来的 O(N) Python 循环开销)
if 'open' not in df.columns:
df['open'] = df.get('close')
if 'high' not in df.columns:
df['high'] = df.get('close')
if 'low' not in df.columns:
df['low'] = df.get('close')
if 'volume' not in df.columns:
df['volume'] = 0
# 数值列转为 numeric
for c in ['open', 'high', 'low', 'close', 'volume']:
if c in df.columns:
df[c] = pd.to_numeric(df[c], errors='coerce')
# 日期列:尽量保留时间信息(分钟线/日线都兼容)
if 'date' in df.columns and 'time' in df.columns:
date_dt = pd.to_datetime(df['date'], errors='coerce')
date_base = date_dt.dt.floor('D')
time_raw = df['time']
# 1) 优先识别"完整时间戳"格式:YYYYMMDDHHMMSS(fff)
# 常见于分钟线导出:时间列可能是 20190102093500000 这种数字/字符串。
dt_full = None
try:
time_s = time_raw.astype(str).str.strip()
# 去掉 .0 / 科学计数等,保留数字
digits = time_s.str.replace(r"\D", "", regex=True)
# 取前14位作为 YYYYMMDDHHMMSS
d14 = digits.str.slice(0, 14)
dt1 = pd.to_datetime(d14, format='%Y%m%d%H%M%S', errors='coerce')
# 部分数据可能只有到分钟:YYYYMMDDHHMM
d12 = digits.str.slice(0, 12)
dt2 = pd.to_datetime(d12, format='%Y%m%d%H%M', errors='coerce')
# 选择解析成功率更高的
r1 = float(dt1.notna().mean()) if len(dt1) else 0.0
r2 = float(dt2.notna().mean()) if len(dt2) else 0.0
if max(r1, r2) >= 0.8:
dt_full = dt1 if r1 >= r2 else dt2
except Exception:
dt_full = None
if dt_full is not None:
dt = dt_full
else:
# 2) 退化:把 time 当作"当天时间"来解析(Excel 小数 / 0 days 09:35:00 / 09:35:00)
td = None
try:
if pd.api.types.is_numeric_dtype(time_raw):
time_num = pd.to_numeric(time_raw, errors='coerce')
# Excel 时间通常是 [0,1) 的小数(天的小数部分)
if float(time_num.max(skipna=True) or 0) <= 1.5:
td = pd.to_timedelta(time_num.fillna(0) * 86400.0, unit='s')
except Exception:
td = None
if td is None:
time_s2 = time_raw.astype(str).str.strip()
time_s2 = time_s2.replace({'nan': '', 'None': '', 'NaT': ''})
td = pd.to_timedelta(time_s2.replace('', pd.NA), errors='coerce')
td = td.fillna(pd.Timedelta(0))
dt = date_base + td
elif 'date' in df.columns:
dt = pd.to_datetime(df['date'], errors='coerce')
elif 'time' in df.columns:
dt = pd.to_datetime(df['time'], errors='coerce')
else:
dt = pd.Series([pd.NaT] * len(df))
tmp = pd.DataFrame({
'dt': dt,
'open': df['open'].fillna(df['close']).fillna(0.0).astype(float),
'high': df['high'].fillna(df['close']).fillna(0.0).astype(float),
'low': df['low'].fillna(df['close']).fillna(0.0).astype(float),
'close': df['close'].fillna(0.0).astype(float),
'volume': df['volume'].fillna(0).astype(int),
})
tmp = tmp[tmp['dt'].notna()].copy()
# 如果是分钟/多条/天数据:
# - keep_intraday=True(网格需要真实时间)则保留原始时间戳
# - 否则聚合成日线(显著减少数据量,提升速度)
# 聚合逻辑:open=首条、high=max、low=min、close=末条、volume=sum
tmp['date_only'] = tmp['dt'].dt.floor('D')
try:
is_intraday = bool(tmp['date_only'].duplicated().any())
except Exception:
is_intraday = False
if is_intraday and keep_intraday:
out_df = tmp.sort_values('dt').copy()
# 保留分钟线真实时间(前端展示时以空格切分出 HH:MM:SS)
out_df['date'] = out_df['dt'].dt.strftime('%Y-%m-%d %H:%M:%S')
out_df = out_df[['date', 'open', 'high', 'low', 'close', 'volume']]
elif is_intraday:
tmp = tmp.sort_values('dt')
g = tmp.groupby('date_only', sort=True)
out_df = g.agg(
open=('open', 'first'),
high=('high', 'max'),
low=('low', 'min'),
close=('close', 'last'),
volume=('volume', 'sum'),
).reset_index()
out_df['date'] = out_df['date_only'].dt.strftime('%Y-%m-%d') + ' 15:00:00'
out_df = out_df[['date', 'open', 'high', 'low', 'close', 'volume']]
else:
out_df = tmp.copy()
# 日线统一使用收盘时间 15:00:00,避免前端默认显示 09:30
out_df['date'] = out_df['date_only'].dt.strftime('%Y-%m-%d') + ' 15:00:00'
out_df = out_df[['date', 'open', 'high', 'low', 'close', 'volume']]
market_data = out_df.to_dict('records')
# 写入缓存(内存+磁盘)
try:
if isinstance(market_data, list) and market_data:
with _parsed_upload_cache_lock:
_parsed_upload_cache[cache_key] = {'ts': now_ts, 'data': market_data}
disk_path = _get_parsed_upload_cache_path(cache_key)
with open(disk_path, 'w', encoding='utf-8') as f:
json.dump(market_data, f, ensure_ascii=False)
except Exception:
pass
print(f"成功解析 {len(market_data)} 条数据(耗时 {(time.perf_counter() - t0):.3f}s)")
if market_data:
print(f"样例数据: {market_data[0]}")
return market_data
except Exception as e:
print(f"文件解析错误: {str(e)}")
print(f"详细错误: {traceback.format_exc()}")
raise ValueError(f"文件解析失败: {str(e)}")
async def grid_backtest_with_file(
file: UploadFile = File(...),
params: str = Form(...)
):
print("--- 收到网格回测请求 ---")
print(f"原始参数 (JSON): {params}")
"""使用上传文件进行网格交易回测"""
try:
print(f"\n=== 收到回测请求 ===")
print(f"文件名: {file.filename}")
print(f"文件类型: {file.content_type}")
# 解析参数
params_dict = json.loads(params)
print(f"参数: {json.dumps(params_dict, indent=2, ensure_ascii=False)}")
# 读取文件
file_content = await file.read()
print(f"文件大小: {len(file_content)} 字节")
# --- 新增:保存上传的文件 ---
try:
# 1. 定义 uploads 目录路径
backend_dir = os.path.dirname(__file__)
uploads_dir = os.path.join(backend_dir, 'uploads')
# 2. 按当前日期创建子目录
today_str = datetime.now().strftime('%Y-%m-%d')
date_specific_dir = os.path.join(uploads_dir, today_str)
os.makedirs(date_specific_dir, exist_ok=True)
# 3. 构造安全的文件名并保存
timestamp_str = datetime.now().strftime('%H%M%S')
safe_filename = os.path.basename(file.filename) # 移除路径部分,防止路径遍历攻击
file_path = os.path.join(date_specific_dir, f"{timestamp_str}_{safe_filename}")
with open(file_path, 'wb') as f:
f.write(file_content)
print(f"✅ 文件已保存至: {file_path}")
except Exception as e:
print(f"⚠️ 警告:保存上传文件失败: {e}")
# --- 文件保存逻辑结束 ---
loop = asyncio.get_running_loop()
t_parse0 = time.perf_counter()
market_data = await loop.run_in_executor(
PARSE_EXECUTOR,
parse_uploaded_file,
file_content,
file.filename,
True,
)
print(f"解析阶段总耗时: {(time.perf_counter() - t_parse0):.3f}s")
if not market_data:
raise ValueError("解析后的市场数据为空")
print(f"市场数据条数: {len(market_data)}")
t_bt0 = time.perf_counter()
results = await loop.run_in_executor(
BACKTEST_EXECUTOR,
_grid_backtest_job,
params_dict,
market_data,
)
print(f"回测阶段总耗时: {(time.perf_counter() - t_bt0):.3f}s")
print(f"回测完成,交易次数: {results['total_trades']}")
print(f"最终权益: {results['final_equity']}")
# 生成动态任务ID
with _backtest_history_lock:
task_id = generate_backtest_id()
# 保存到内存历史记录
save_to_memory_history("grid", file.filename or 'uploaded_data', params_dict, results, task_id, market_data)
# 数据已经通过 save_to_memory_history 保存到数据库了
return {
"id": task_id,
"status": "completed",
"created_at": str(datetime.now()),
"completed_at": str(datetime.now()),
"results": results
}
except Exception as e:
print(f"\n=== 回测错误 ===")
print(f"错误: {str(e)}")
print(f"详细错误:\n{traceback.format_exc()}")
# 提供更详细的错误信息
error_detail = f"回测失败: {str(e)}"
if "parse" in str(e).lower():
error_detail = f"文件解析失败: {str(e)}"
elif "grid" in str(e).lower():
error_detail = f"网格引擎错误: {str(e)}"
elif "param" in str(e).lower():
error_detail = f"参数错误: {str(e)}"
raise HTTPException(status_code=500, detail=error_detail)
async def grid_backtest_simple(request: dict):
"""网格交易回测(需要提供真实数据)"""
try:
print(f"\n=== 网格回测请求 ===")
print(f"参数: {json.dumps(request, indent=2, ensure_ascii=False)}")
# 检查是否提供了市场数据
if 'market_data' not in request or not request['market_data']:
raise HTTPException(
status_code=400,
detail="缺少市场数据。请使用文件上传接口 /api/v1/grid/backtest/file 上传真实数据文件。"
)
market_data = request['market_data']
print(f"收到市场数据: {len(market_data)} 条记录")
loop = asyncio.get_running_loop()
results = await loop.run_in_executor(
BACKTEST_EXECUTOR,
_grid_backtest_job,
request,
market_data,
)
print(f"回测完成,交易次数: {results['total_trades']}")
# 生成动态任务ID
with _backtest_history_lock:
task_id = generate_backtest_id()
# 保存到内存历史记录
save_to_memory_history("grid", request.get('symbol', 'virtual_data'), request, results, task_id, market_data)
# 数据已经通过 save_to_memory_history 保存到数据库了
return {
"id": task_id,
"status": "completed",
"created_at": str(datetime.now()),
"completed_at": str(datetime.now()),
"results": results
}
except Exception as e:
print(f"\n=== 回测错误 ===")
print(f"错误: {str(e)}")
print(f"详细错误:\n{traceback.format_exc()}")
raise HTTPException(status_code=500, detail=f"回测失败: {str(e)}")
async def dca_backtest_with_file(
file: UploadFile = File(...),
params: str = Form(...)
):
"""使用上传文件进行定投交易回测"""
try:
print(f"\n=== 定投回测请求 ===")
print(f"文件名: {file.filename}")
print(f"文件类型: {file.content_type}")
# 解析参数
params_dict = json.loads(params)
print(f"参数: {json.dumps(params_dict, indent=2, ensure_ascii=False)}")
# 读取文件
file_content = await file.read()
print(f"文件大小: {len(file_content)} 字节")
# --- 新增:保存上传的文件 ---
try:
# 1. 定义 uploads 目录路径
backend_dir = os.path.dirname(__file__)
uploads_dir = os.path.join(backend_dir, 'uploads')
# 2. 按当前日期创建子目录
today_str = datetime.now().strftime('%Y-%m-%d')
date_specific_dir = os.path.join(uploads_dir, today_str)
os.makedirs(date_specific_dir, exist_ok=True)
# 3. 构造安全的文件名并保存
timestamp_str = datetime.now().strftime('%H%M%S')
safe_filename = os.path.basename(file.filename) # 移除路径部分,防止路径遍历攻击
file_path = os.path.join(date_specific_dir, f"{timestamp_str}_{safe_filename}")
with open(file_path, 'wb') as f:
f.write(file_content)
print(f"✅ 文件已保存至: {file_path}")
except Exception as e:
print(f"⚠️ 警告:保存上传文件失败: {e}")
# --- 文件保存逻辑结束 ---
loop = asyncio.get_running_loop()
t_parse0 = time.perf_counter()
market_data = await loop.run_in_executor(
PARSE_EXECUTOR,
parse_uploaded_file,
file_content,
file.filename,
False,
)
print(f"解析阶段总耗时: {(time.perf_counter() - t_parse0):.3f}s")
if not market_data:
raise ValueError("解析后的市场数据为空")
print(f"市场数据条数: {len(market_data)}")
t_bt0 = time.perf_counter()
results = await loop.run_in_executor(
BACKTEST_EXECUTOR,
_dca_backtest_job,
params_dict,
market_data,
)
print(f"回测阶段总耗时: {(time.perf_counter() - t_bt0):.3f}s")
print(f"定投回测完成")
# 生成动态任务ID
with _backtest_history_lock:
task_id = generate_backtest_id()
# 保存到内存历史记录
save_to_memory_history("dca", file.filename or 'uploaded_dca_data', params_dict, results, task_id, market_data)
# 数据已经通过 save_to_memory_history 保存到数据库了
return {
"id": task_id,
"status": "completed",
"created_at": str(datetime.now()),
"completed_at": str(datetime.now()),
"results": results
}
except Exception as e:
print(f"\n=== 定投回测错误 ===")
print(f"错误: {str(e)}")
print(f"详细错误:\n{traceback.format_exc()}")
raise HTTPException(status_code=500, detail=f"定投回测失败: {str(e)}")
class DCABacktestEngine:
"""定投交易回测引擎"""
def __init__(self, params):
self.params = params
# 处理初始资金:None或0表示无上限,使用足够大的默认值
initial_capital_input = params.get('initial_capital', None)
if initial_capital_input is None or initial_capital_input == 0 or initial_capital_input == '':
# 无上限:使用足够大的值(1亿)
self.initial_capital = 100000000.0
self.is_unlimited_capital = True
else:
self.initial_capital = float(initial_capital_input)
self.is_unlimited_capital = False
self.investment_type = params.get('investment_type', 'shares') # 'shares' or 'amount'
self.investment_value = float(params.get('investment_value', 100)) # 金额或股数
self.dca_price_type = params.get('dca_price_type', 'close') # 'open' or 'close'
self.investment_frequency = params.get('investment_frequency', 'monthly') # daily, weekly, monthly
self.day_of_week = int(params.get('day_of_week', 1)) if params.get('day_of_week') is not None else 1 # 默认为周一
self.day_of_month = int(params.get('day_of_month', 1)) if params.get('day_of_month') is not None else 1 # 默认为每月1号
# 日期范围
self.start_date = params.get('start_date', None)
self.end_date = params.get('end_date', None)
# 手续费 - 兼容前端字段名
self.commission_rate = float(params.get('fee_rate', params.get('commission_rate', 0.0005))) # 默认万分之50 = 0.5%
self.min_commission = float(params.get('min_fee', params.get('min_commission', 0.01))) # 默认最低0.01元
capital_desc = "无上限" if self.is_unlimited_capital else f"{self.initial_capital}"
print(f"定投参数: 初始资金={capital_desc}, 投资类型={self.investment_type}, 投资数值={self.investment_value}, 频率={self.investment_frequency}")
print(f"手续费参数: 费率={self.commission_rate*10000}万分之一, 最低手续费={self.min_commission}元")
print(f"回测日期: {self.start_date} 至 {self.end_date}")
def run(self, market_data):
"""执行定投回测"""
if not market_data or len(market_data) == 0:
raise ValueError("市场数据为空")
# 根据日期范围过滤数据
if self.start_date or self.end_date:
original_count = len(market_data)
filtered_data = []
for row in market_data:
row_date = str(row['date']).split(' ')[0] # 只取日期部分
# 检查是否在日期范围内
include_row = True
if self.start_date and row_date < self.start_date:
include_row = False
if self.end_date and row_date > self.end_date:
include_row = False
if include_row:
filtered_data.append(row)
market_data = filtered_data
print(f"日期过滤: 原始数据{original_count}条 -> 过滤后{len(market_data)}条")
if not market_data:
raise ValueError(f"指定日期范围 {self.start_date} 至 {self.end_date} 内没有数据")
print(f"开始定投回测,数据条数: {len(market_data)}")
print(f"价格范围: {market_data[0]['close']} -> {market_data[-1]['close']}")
print(f"日期范围: {market_data[0]['date']} -> {market_data[-1]['date']}")
# 资金初始化:无上限模式不受现金约束,仅记录投入成本
cash = 0 if self.is_unlimited_capital else self.initial_capital
shares = 0
total_fee = 0
total_invested = 0
trades = []
# --- 重构定投策略以支持精确时间点 ---
# 1) 转为 DataFrame 并按"交易日"分组,避免每个日历日都做一次全表过滤(性能瓶颈)
df = pd.DataFrame(market_data)
df['dt'] = pd.to_datetime(df['date'], errors='coerce')
df = df[df['dt'].notna()].copy()
df = df.sort_values('dt')
df['date_only'] = df['dt'].dt.floor('D')
grouped = {d: g for d, g in df.groupby('date_only', sort=True)}
trading_days = list(grouped.keys())
if not trading_days:
raise ValueError('市场数据为空')
# 如果每个交易日只有一条记录,则视为日线;否则视为分钟线
is_minute_data = any(len(grouped[d]) > 1 for d in trading_days)
last_investment_date = None
# 只遍历实际存在的交易日(而不是遍历所有日历日)
for current_date in trading_days:
should_invest_today = False
# 检查今天是否是投资日
if last_investment_date is None:
if self.investment_frequency == 'daily':
should_invest_today = True
elif self.investment_frequency == 'weekly' and current_date.isoweekday() == self.day_of_week:
should_invest_today = True
elif self.investment_frequency == 'monthly' and current_date.day == self.day_of_month:
should_invest_today = True
else:
if current_date.date() > last_investment_date.date():
if self.investment_frequency == 'daily':
should_invest_today = True
elif self.investment_frequency == 'weekly' and current_date.isoweekday() == self.day_of_week and (current_date.date() - last_investment_date.date()).days >= 6:
should_invest_today = True
elif self.investment_frequency == 'monthly' and current_date.day == self.day_of_month and (current_date.month != last_investment_date.month or current_date.year > last_investment_date.year):
should_invest_today = True
if should_invest_today:
day_data = grouped.get(current_date)
if not day_data.empty:
trade_row = None
if is_minute_data:
# 分钟数据:查找特定时间点
if self.dca_price_type == 'open':
# 开盘价:查找最接近 09:30 的数据
target_dt = pd.Timestamp(current_date.date()).replace(hour=9, minute=30)
else: # 'close'
# 收盘价:查找最接近 15:00 的数据
target_dt = pd.Timestamp(current_date.date()).replace(hour=15, minute=0)
# 找到离目标时间点最近的行(向量化)
idx = (day_data['dt'] - target_dt).abs().idxmin()
trade_row = day_data.loc[idx]
trade_timestamp = trade_row['dt'].to_pydatetime()
else:
# 日数据:直接使用当天的记录
trade_row = day_data.iloc[0]
trade_timestamp = None # 日数据时间在后面设置
if trade_row is not None:
if is_minute_data:
# 分钟数据:已经定位到精确时间点,使用该行的收盘价
current_price = float(trade_row['close'])
# trade_timestamp 已在上面设置
else:
# 日数据:根据选择的价格类型获取价格和设置时间
current_price = float(trade_row[self.dca_price_type])
if self.dca_price_type == 'open':
# 开盘价:时间为 09:30:00
from datetime import datetime as dt
trade_timestamp = dt(current_date.year, current_date.month, current_date.day, 9, 30, 0)
else: # 'close'
# 收盘价:时间为 15:00:00
from datetime import datetime as dt
trade_timestamp = dt(current_date.year, current_date.month, current_date.day, 15, 0, 0)
# --- 执行交易逻辑 ---
buy_shares = 0
if self.investment_type == 'amount':
potential_shares = int(self.investment_value / current_price)
buy_shares = (potential_shares // 100) * 100
else: # 'shares'
buy_shares = int(self.investment_value)
if buy_shares >= 100:
cost = buy_shares * current_price
fee = max(cost * self.commission_rate, self.min_commission)
# 现金约束:仅在有限资金模式下检查
if self.is_unlimited_capital or cash >= cost + fee:
if not self.is_unlimited_capital:
cash -= (cost + fee)
shares += buy_shares
total_fee += fee
total_invested += cost
# 格式化时间戳为 ISO 格式字符串,确保包含完整的时间信息
date_str = trade_timestamp.strftime('%Y-%m-%dT%H:%M:%S')
trade = {
"date": date_str, # ISO 格式:2023-01-15T09:30:00
"type": "定投买入",
"price": round(current_price, 2),
"shares": buy_shares,
"amount": round(cost, 2),
"fee": round(fee, 2),
"cash_balance": round(cash, 2) if not self.is_unlimited_capital else None,
"position_value": round(shares * current_price, 2),
"current_shares": shares,
"total_value": round((cash if not self.is_unlimited_capital else 0) + shares * current_price, 2),
"base_price": round(current_price, 4),
"cumulative_invested": round(total_invested, 2)
}
trades.append(trade)
if len(trades) <= 5:
print(f"定投: {date_str} 价格={current_price:.2f} 数量={buy_shares}")
last_investment_date = trade_timestamp
# --- 指标计算 ---
last_price = float(market_data[-1]['close'])
if self.is_unlimited_capital:
# 无上限模式:现金不纳入权益,但手续费应从最终金额中扣除
final_value = shares * last_price - total_fee
else:
final_value = cash + shares * last_price
# 投入资本基准:无上限模式使用累计投入,有限模式使用初始资金
capital_base = total_invested if self.is_unlimited_capital else self.initial_capital
total_profit = final_value - capital_base
total_return = (total_profit / capital_base) * 100 if capital_base > 0 else 0
# --- 详细指标计算:最大回撤、盈亏平衡点时长等 ---
equity_curve = []
peak_equity = capital_base if capital_base > 0 else 0
max_drawdown = 0
breakeven_duration = 0
max_breakeven_duration = 0
underwater_start_date = None
# 为了计算每日权益,我们需要一个每日投资额/持仓股数的映射
investments_by_date = {t['date'].split('T')[0]: t.get('cumulative_invested', 0) for t in trades}
shares_by_date = {t['date'].split('T')[0]: int(t.get('current_shares', 0) or 0) for t in trades}
daily_total_invested = 0
last_known_investment = 0
daily_shares = 0
last_known_shares = 0
# 新增:用于盈利/亏损天数、连续天数、资金利用率
_daily_equity_dca: list[float] = []
_daily_invested_dca: list[float] = []
for row in market_data:
current_date_str = str(row['date']).split(' ')[0]
current_date = datetime.fromisoformat(current_date_str)
price = float(row['close'])
# 更新当日的总投资额
if current_date_str in investments_by_date:
daily_total_invested = investments_by_date[current_date_str]
last_known_investment = daily_total_invested
else:
daily_total_invested = last_known_investment
if current_date_str in shares_by_date:
daily_shares = shares_by_date[current_date_str]
last_known_shares = daily_shares
else:
daily_shares = last_known_shares
if self.is_unlimited_capital:
# 权益=持仓市值-累计投入(现金无限制,不计入权益)
current_equity = daily_shares * price - daily_total_invested
else:
current_equity = self.initial_capital - daily_total_invested + daily_shares * price
equity_curve.append(current_equity)
_daily_equity_dca.append(float(current_equity))
_daily_invested_dca.append(float(daily_total_invested))
# 计算最大回撤
if current_equity > peak_equity:
peak_equity = current_equity
drawdown = (peak_equity - current_equity) / peak_equity if peak_equity > 0 else 0
if drawdown > max_drawdown:
max_drawdown = drawdown
# 计算盈亏平衡点时长
if self.is_unlimited_capital:
is_underwater = current_equity < 0
else:
is_underwater = current_equity < daily_total_invested
if is_underwater and underwater_start_date is None:
# 刚进入水下
underwater_start_date = current_date
elif not is_underwater and underwater_start_date is not None:
# 刚浮出水面
duration = (current_date - underwater_start_date).days
if duration > max_breakeven_duration:
max_breakeven_duration = duration
underwater_start_date = None
# 如果结束时仍在水下,计算最后一段水下时长
if underwater_start_date is not None:
end_date_dt = datetime.fromisoformat(str(market_data[-1]['date']).split(' ')[0])
duration = (end_date_dt - underwater_start_date).days
if duration > max_breakeven_duration:
max_breakeven_duration = duration
import math
def _finite(v: float, default: float = 0.0) -> float:
try:
fv = float(v)
return fv if math.isfinite(fv) else float(default)
except Exception:
return float(default)
# 年化收益率
days = (datetime.fromisoformat(self.end_date) - datetime.fromisoformat(self.start_date)).days if self.start_date and self.end_date else len(market_data)
years = max(days / 365.25, 1/365.25)
annual_return = ((1 + total_return / 100) ** (1 / years) - 1) * 100 if total_return is not None else 0
annual_return = _finite(annual_return, 0.0)
# 波动率 (年化标准差)
returns = pd.Series(equity_curve).pct_change().dropna()
try:
volatility = float(returns.std()) * (252**0.5) if not returns.empty else 0.0 # 假设每年252个交易日
except Exception:
volatility = 0.0
volatility = _finite(volatility, 0.0)
# 夏普比率 (假设无风险利率为0)
try:
sharpe_ratio = float((annual_return / 100) / volatility) if volatility > 0 else 0.0
except Exception:
sharpe_ratio = 0.0
sharpe_ratio = _finite(sharpe_ratio, 0.0)
# 胜率 (定投场景下,胜率定义为最终价格高于平均成本)
win_rate = 100 if shares > 0 and last_price > (total_invested / shares) else 0
avg_cost = total_invested / shares if shares > 0 else 0
profit_days = 0
loss_days = 0
max_profit_streak = 0
max_loss_streak = 0
_cur_profit = 0
_cur_loss = 0
if len(_daily_equity_dca) >= 2:
for i in range(1, len(_daily_equity_dca)):
diff = _daily_equity_dca[i] - _daily_equity_dca[i - 1]
if diff > 0:
profit_days += 1
_cur_profit += 1
_cur_loss = 0
elif diff < 0:
loss_days += 1
_cur_loss += 1
_cur_profit = 0
else:
_cur_profit = 0
_cur_loss = 0
max_profit_streak = max(max_profit_streak, _cur_profit)
max_loss_streak = max(max_loss_streak, _cur_loss)
capital_utilization = 0.0
try:
if self.is_unlimited_capital:
denom = float(total_invested) if float(total_invested) > 0 else 0.0
if denom > 0 and len(_daily_invested_dca) > 0:
capital_utilization = (sum(_daily_invested_dca) / len(_daily_invested_dca)) / denom * 100
else:
denom = float(self.initial_capital) if float(self.initial_capital) > 0 else 0.0
if denom > 0 and len(_daily_invested_dca) > 0:
capital_utilization = (sum(_daily_invested_dca) / len(_daily_invested_dca)) / denom * 100
except Exception:
capital_utilization = 0.0
# --- 超额收益率计算 ---
# 基准:定期定额等权基准(每期投入相同金额,最后一期以末期价格计算)
# 假设每期投入金额为平均投入额
if len(trades) > 0:
# 计算每期平均投入金额
avg_investment_per_period = total_invested / len(trades)
# 基准策略:每期投入相同金额,购买相同数量的股票
# 基准最终权益 = 每期投入金额 * 期数 + (每期投入金额 / 首期价格) * 期数 * (末期价格 - 首期价格)
first_trade_price = float(trades[0]['price']) if trades else last_price
# 简化计算:基准权益 = 总投入 + 总投入 * (末期价格 / 首期价格 - 1)
benchmark_return = ((last_price / first_trade_price) - 1) * 100 if first_trade_price > 0 else 0
benchmark_return = _finite(benchmark_return, 0.0)
# 超额收益率 = 策略收益率 - 基准收益率
excess_return = _finite(total_return - benchmark_return, 0.0)
else:
excess_return = 0
benchmark_return = 0
print(f"定投回测完成: 投资{len(trades)}次, 最终权益={final_value:.2f}, 收益率={total_return:.2f}%, 超额收益率={excess_return:.2f}%")
total_return = _finite(total_return, 0.0)
max_drawdown = _finite(max_drawdown, 0.0)
return {
"final_value": round(_finite(final_value, 0.0), 2), # 统一为final_value
"total_profit": round(_finite(total_profit, 0.0), 2),
"total_return": round(_finite(total_return, 0.0), 2),
"annual_return": round(_finite(annual_return, 0.0), 2),
"max_drawdown": round(_finite(max_drawdown * 100, 0.0), 2),
"volatility": round(_finite(volatility, 0.0), 4),
"sharpe_ratio": round(_finite(sharpe_ratio, 0.0), 4),
"win_rate": round(_finite(win_rate, 0.0), 2),
"profit_days": int(profit_days),
"loss_days": int(loss_days),
"max_profit_streak": int(max_profit_streak),
"max_loss_streak": int(max_loss_streak),
"capital_utilization": round(float(capital_utilization), 2),
"total_periods": len(trades), # 对应总投入期数
"total_invested": round(total_invested, 2),
"average_cost": round(avg_cost, 2),
"final_shares": shares,
"total_fee": round(total_fee, 2),
"investment_records": trades, # 统一为investment_records
# --- 其他前端可能需要的字段 ---
"excess_return": round(_finite(excess_return, 0.0), 2), # 超额收益率 = 策略收益率 - 基准收益率
"benchmark_return": round(_finite(benchmark_return, 0.0), 2), # 基准收益率(首期价格到末期价格)
"breakeven_duration": max_breakeven_duration, # 盈亏平衡点时长
"return_drawdown_ratio": round(total_return / (max_drawdown * 100), 4) if max_drawdown > 0 else 0,
"summary": {
"data_points": len(market_data),
"trading_days": days
}
}
async def get_backtest_history(
page: int = 1,
page_size: int = 20,
strategy_types: str = None
):
"""获取回测历史列表"""
try:
with _backtest_history_lock:
filtered_history = list(backtest_history)
if strategy_types:
# 支持多个策略类型,用逗号分隔
strategy_list = [s.strip() for s in strategy_types.split(',')]
filtered_history = [record for record in filtered_history if record['strategy_type'] in strategy_list]
# 分页
start_idx = (page - 1) * page_size
end_idx = start_idx + page_size
paginated_records = filtered_history[start_idx:end_idx]
print(f"历史记录API被调用,返回 {len(paginated_records)} 条记录")
return paginated_records
except Exception as e:
print(f"获取历史记录失败: {e}")
return []
async def get_statistics_summary():
"""获取统计摘要"""
try:
with _backtest_history_lock:
snapshot = list(backtest_history)
total_count = len(snapshot)
grid_count = len([r for r in snapshot if r['strategy_type'] == 'grid'])
dca_count = len([r for r in snapshot if r['strategy_type'] == 'dca'])
completed_count = len([r for r in snapshot if r['status'] == 'completed'])
# 计算平均收益率
total_return_sum = 0
count = 0
for record in snapshot:
if record['results'] and 'total_return' in record['results']:
total_return_sum += record['results']['total_return']
count += 1
avg_return = total_return_sum / count if count > 0 else 0
print(f"统计API被调用,当前历史记录数: {total_count}")
return {
"total_backtests": total_count,
"grid_backtests": grid_count,
"dca_backtests": dca_count,
"completed_backtests": completed_count,
"average_return": f"{avg_return:.2f}%",
"success_rate": f"{(completed_count/total_count*100 if total_count > 0 else 0):.2f}%"
}
except Exception as e:
print(f"获取统计数据失败: {e}")
return {
"total_backtests": 0,
"grid_backtests": 0,
"dca_backtests": 0,
"completed_backtests": 0,
"average_return": "0.00%",
"success_rate": "0.00%"
}
# 辅助函数:翻译和格式化
def _extract_stock_info_from_data(record):
"""从记录中提取股票信息(名称和代码)"""
# 先尝试从市场数据中读取
results = record.get('results', {})
trade_records = results.get('trade_records', [])
stock_name = None
stock_code = None
# 检查交易记录中是否有标的信息
if trade_records:
first_trade = trade_records[0]
stock_name = first_trade.get('stock_name') or first_trade.get('symbol_name')
stock_code = first_trade.get('stock_code') or first_trade.get('symbol_code')
# 如果交易记录中没有,尝试从参数中读取
if not stock_name or not stock_code:
params = record.get('parameters', {})
market_data = params.get('market_data', [])
if market_data and len(market_data) > 0:
first_data = market_data[0]
stock_name = stock_name or first_data.get('stock_name') or first_data.get('name') or first_data.get('symbol_name')
stock_code = stock_code or first_data.get('stock_code') or first_data.get('code') or first_data.get('symbol_code') or first_data.get('symbol')
# 如果仍然没有,尝试从文件名解析
if not stock_name or not stock_code:
symbol_str = record.get('symbol', '')
if '_' in symbol_str and '.' in symbol_str:
# 文件名格式:sz_000001_5min.json
parts = symbol_str.split('_')
if len(parts) >= 2:
parsed_code = parts[1] # 000001
stock_code = stock_code or parsed_code
stock_name = stock_name or _get_stock_name_by_code(parsed_code)
return stock_name or '未知', stock_code or '未知'
def _extract_stock_name(record):
"""提取股票名称"""
name, code = _extract_stock_info_from_data(record)
return name
def _extract_stock_code(record):
"""提取股票代码"""
name, code = _extract_stock_info_from_data(record)
return code
def _get_stock_name_by_code(code):
"""根据股票代码获取名称(简化版)"""
stock_names = {
'000001': '平安银行',
'000002': '万科A',
'000858': '五粮液',
'600036': '招商银行',
'600519': '贵州茅台',
'000858': '五粮液'
}
return stock_names.get(code, f'股票{code}')
def _translate_base_price_type(price_type):
"""翻译基准价类型"""
translations = {
'open': '开盘价',
'close': '收盘价',
'high': '最高价',
'low': '最低价',
'custom': '自定义价格'
}
return translations.get(price_type, price_type)
def _translate_change_type(change_type):
"""翻译变动类型"""
translations = {
'percentage': '百分比',
'absolute': '绝对值'
}
return translations.get(change_type, change_type)
def _translate_trigger_mode(trigger_mode):
"""翻译触发模式"""
translations = {
'direct': '直接触发',
'pullback': '回落触发',
'rebound': '反弹触发'
}
return translations.get(trigger_mode, trigger_mode)
def _translate_order_type(order_type):
"""翻译委托类型"""
translations = {
'shares': '按股数',
'amount': '按金额'
}
return translations.get(order_type, order_type)
def _translate_price_type(price_type):
"""翻译价格类型"""
translations = {
'market': '市价单',
'limit': '限价单'
}
return translations.get(price_type, price_type)
def _translate_position_limit_type(limit_type):
"""翻译持仓限制类型"""
translations = {
'shares': '按股数',
'amount': '按金额'
}
return translations.get(limit_type, limit_type)
def _translate_fee_type(fee_type):
"""翻译手续费类型"""
translations = {
'percentage': '按比例',
'fixed': '固定金额'
}
return translations.get(fee_type, fee_type)
def _translate_investment_frequency(frequency):
"""翻译定投频率"""
translations = {
'daily': '日度',
'weekly': '周度',
'monthly': '月度'
}
return translations.get(frequency, frequency)
def _format_fee_rate(fee_rate):
"""格式化手续费率为万分比符号"""
rate_in_bp = fee_rate * 10000 # 转为万分比
if rate_in_bp == int(rate_in_bp):
return f"{int(rate_in_bp)}‱" # 万分比符号
else:
return f"{rate_in_bp:.1f}‱"
async def export_backtest_record(
record_id: str,
format: str = "excel"
):
"""导出回测结果 - 支持Excel、PDF、Word格式"""
try:
# 查找记录
record = None
for r in backtest_history:
if r['id'] == record_id:
record = r
break
if not record:
raise HTTPException(status_code=404, detail="记录不存在")
if not record.get('results'):
raise HTTPException(status_code=400, detail="无回测结果可导出")
# 获取交易记录
results = record['results']
trade_records = results.get('trade_records', [])
if not trade_records:
raise HTTPException(status_code=400, detail="无交易记录可导出")
# 导出数据处理
import io
# 根据格式选择导出方式
if format == "pdf":
# PDF导出功能 - 横向布局
from reportlab.lib.pagesizes import letter, A4, landscape
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer, PageBreak
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from reportlab.lib.units import inch
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
# 注册中文字体(使用系统字体)
try:
pdfmetrics.registerFont(TTFont('SimSun', 'C:/Windows/Fonts/simsun.ttc'))
font_name = 'SimSun'
except:
# 如果找不到中文字体,使用默认字体
font_name = 'Helvetica'
pdf_buffer = io.BytesIO()
# 使用横向布局
doc = SimpleDocTemplate(pdf_buffer, pagesize=landscape(A4),
leftMargin=0.5*inch, rightMargin=0.5*inch,
topMargin=0.5*inch, bottomMargin=0.5*inch)
story = []
# 样式设置
styles = getSampleStyleSheet()
title_style = ParagraphStyle('CustomTitle', parent=styles['Heading1'], fontSize=18, fontName=font_name, alignment=1)
section_style = ParagraphStyle('SectionTitle', parent=styles['Heading2'], fontSize=14, fontName=font_name)
normal_style = ParagraphStyle('CustomNormal', parent=styles['Normal'], fontSize=9, fontName=font_name)
# 添加标题
story.append(Paragraph(f'回测报告 - {record_id}', title_style))
story.append(Spacer(1, 16))
# 1. 统计信息部分
story.append(Paragraph('统计信息', section_style))
story.append(Spacer(1, 8))
if record['strategy_type'] == 'grid':
stats_data = [
['最终权益', f"¥{results.get('final_equity', 0):,.2f}"],
['总收益', f"¥{results.get('total_profit', 0):,.2f}"],
['总收益率', f"{results.get('total_return', 0):.2f}%"],
['年化收益率', f"{results.get('annual_return', 0):.2f}%"],
['总交易次数', str(results.get('total_trades', 0))],
['胜率', f"{results.get('win_rate', 0):.2f}%"],
['最大回撤', f"{results.get('max_drawdown', 0):.2f}%"],
['总手续费', f"¥{results.get('total_fee', 0):,.2f}"]
]
else: # dca
stats_data = [
['最终权益', f"¥{results.get('final_equity', 0):,.2f}"],
['总收益', f"¥{results.get('total_profit', 0):,.2f}"],
['总收益率', f"{results.get('total_return', 0):.2f}%"],
['年化收益率', f"{results.get('annual_return', 0):.2f}%"],
['投资次数', str(results.get('total_investments', 0))],
['总投入', f"¥{results.get('total_invested', 0):,.2f}"],
['平均成本', f"¥{results.get('average_cost', 0):.2f}"],
['总手续费', f"¥{results.get('total_fee', 0):,.2f}"]
]
stats_table = Table(stats_data, colWidths=[2*inch, 2*inch])
stats_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (0, -1), colors.lightblue),
('TEXTCOLOR', (0, 0), (-1, -1), colors.black),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('FONTNAME', (0, 0), (-1, -1), font_name),
('FONTSIZE', (0, 0), (-1, -1), 9),
('BOTTOMPADDING', (0, 0), (-1, -1), 4),
('GRID', (0, 0), (-1, -1), 1, colors.black)
]))
story.append(stats_table)
story.append(Spacer(1, 16))
# 2. 回测参数部分
story.append(Paragraph('回测参数', section_style))
story.append(Spacer(1, 8))
params = record['parameters']
if record['strategy_type'] == 'grid':
param_data = [
['标的名称', _extract_stock_name(record)],
['标的代码', _extract_stock_code(record)],
['初始资金', f"¥{params.get('initial_capital', 0):,.2f}"],
['初始股数', f"{params.get('initial_shares', 0)}股"],
['上涨触发', f"{params.get('rise_trigger', 0):.2f}%"],
['下跌触发', f"{params.get('fall_trigger', 0):.2f}%"],
['买入金额', f"{params.get('buy_amount', 0)}股"],
['手续费率', _format_fee_rate(float(params.get('fee_rate', 0)))]
]
else: # dca
param_data = [
['标的名称', _extract_stock_name(record)],
['标的代码', _extract_stock_code(record)],
['初始资金', f"¥{params.get('initial_capital', 0):,.2f}"],
['定投金额', f"¥{params.get('investment_amount', 0):,.2f}"],
['定投频率', _translate_investment_frequency(params.get('investment_frequency', 'monthly'))],
['手续费率', _format_fee_rate(float(params.get('fee_rate', 0)))]
]
param_table = Table(param_data, colWidths=[2*inch, 2*inch])
param_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (0, -1), colors.lightgreen),
('TEXTCOLOR', (0, 0), (-1, -1), colors.black),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('FONTNAME', (0, 0), (-1, -1), font_name),
('FONTSIZE', (0, 0), (-1, -1), 9),
('BOTTOMPADDING', (0, 0), (-1, -1), 4),
('GRID', (0, 0), (-1, -1), 1, colors.black)
]))
story.append(param_table)
story.append(PageBreak())
# 3. 交易记录部分 - 新页面,横向布局优势
story.append(Paragraph('交易记录明细', section_style))
story.append(Spacer(1, 8))
# 完整的交易记录表头(横向布局可容纳更多列)
if record['strategy_type'] == 'grid':
trade_data = [['序号', '日期', '时间', '委托', '价格', '数量', '手续费', '现金余额', '持仓市值', '持仓股数', '总资产', '基准价', '触发条件']]
for index, trade in enumerate(trade_records, 1):
date_str = trade.get('date', '')
date_part = date_str.split(' ')[0] if ' ' in date_str else date_str
time_part = date_str.split(' ')[1] if ' ' in date_str else '15:00:00'
trade_data.append([
str(index),
date_part,
time_part,
trade.get('type', ''),
f"¥{trade.get('price', 0):.2f}",
str(trade.get('shares', 0)),
f"¥{trade.get('fee', 0):.2f}",
f"¥{trade.get('cash_balance', 0):,.0f}",
f"¥{trade.get('position_value', 0):,.0f}",
str(trade.get('current_shares', 0)),
f"¥{trade.get('total_value', 0):,.0f}",
f"¥{trade.get('base_price', 0):.2f}",
trade.get('trigger_reason', '')[:10] # 截断长文本
])
col_widths = [0.4*inch, 0.8*inch, 0.6*inch, 0.5*inch, 0.6*inch, 0.5*inch, 0.6*inch, 0.8*inch, 0.8*inch, 0.6*inch, 0.8*inch, 0.6*inch, 1.2*inch]
else: # dca
trade_data = [['序号', '日期', '时间', '委托', '价格', '数量', '手续费', '现金余额', '持仓市值', '持仓股数', '总资产', '累计投入']]
for index, trade in enumerate(trade_records, 1):
date_str = trade.get('date', '')
date_part = date_str.split(' ')[0] if ' ' in date_str else date_str
time_part = date_str.split(' ')[1] if ' ' in date_str else '15:00:00'
trade_data.append([
str(index),
date_part,
time_part,
trade.get('type', ''),
f"¥{trade.get('price', 0):.2f}",
str(trade.get('shares', 0)),
f"¥{trade.get('fee', 0):.2f}",
f"¥{trade.get('cash_balance', 0):,.0f}",
f"¥{trade.get('position_value', 0):,.0f}",
str(trade.get('current_shares', 0)),
f"¥{trade.get('total_value', 0):,.0f}",
f"¥{trade.get('cumulative_invested', 0):,.0f}"
])
col_widths = [0.4*inch, 0.8*inch, 0.6*inch, 0.5*inch, 0.6*inch, 0.5*inch, 0.6*inch, 0.8*inch, 0.8*inch, 0.6*inch, 0.8*inch, 0.8*inch]
trade_table = Table(trade_data, colWidths=col_widths)
trade_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, -1), font_name),
('FONTSIZE', (0, 0), (-1, -1), 9),
('BOTTOMPADDING', (0, 0), (-1, -1), 6),
('GRID', (0, 0), (-1, -1), 1, colors.black)
]))
story.append(trade_table)
doc.build(story)
pdf_buffer.seek(0)
from fastapi.responses import Response
return Response(
content=pdf_buffer.getvalue(),
media_type='application/pdf',
headers={
"Content-Disposition": f"attachment; filename=backtest_{record_id}.pdf"
}
)
elif format == "word":
# Word导出功能
from docx import Document
from docx.shared import Inches
from docx.enum.text import WD_ALIGN_PARAGRAPH
doc = Document()
# 添加标题
title = doc.add_heading(f'回测报告 - {record_id}', 0)
title.alignment = WD_ALIGN_PARAGRAPH.CENTER
# 添加基本信息
doc.add_heading('基本信息', level=1)
basic_table = doc.add_table(rows=1, cols=2)
basic_table.style = 'Table Grid'
basic_info = [
['标的名称', _extract_stock_name(record)],
['标的代码', _extract_stock_code(record)],
['策略类型', '网格交易' if record['strategy_type'] == 'grid' else '定投交易'],
['初始资金', f"¥{record['initial_capital']:,.2f}"],
['回测日期', f"{record.get('start_date', 'N/A')} 至 {record.get('end_date', 'N/A')}"],
]
for info in basic_info:
row_cells = basic_table.add_row().cells
row_cells[0].text = info[0]
row_cells[1].text = str(info[1])
# 添加交易记录
doc.add_heading('交易记录', level=1)
trade_table = doc.add_table(rows=1, cols=7)
trade_table.style = 'Table Grid'
# 表头
hdr_cells = trade_table.rows[0].cells
hdr_cells[0].text = '序号'
hdr_cells[1].text = '日期'
hdr_cells[2].text = '委托'
hdr_cells[3].text = '价格'
hdr_cells[4].text = '数量'
hdr_cells[5].text = '手续费'
hdr_cells[6].text = '资产'
# 数据行
for index, trade in enumerate(trade_records[:50], 1): # 限制显示50条
row_cells = trade_table.add_row().cells
date_str = trade.get('date', '')
date_part = date_str.split(' ')[0] if ' ' in date_str else date_str
time_part = date_str.split(' ')[1] if ' ' in date_str else '15:00:00'
row_cells[0].text = str(index)
row_cells[1].text = date_part
row_cells[2].text = trade.get('type', '')
row_cells[3].text = f"¥{trade.get('price', 0):.2f}"
row_cells[4].text = str(trade.get('shares', 0))
row_cells[5].text = f"¥{trade.get('fee', 0):.2f}"
row_cells[6].text = f"¥{trade.get('total_value', 0):,.2f}"
# 保存到内存
word_buffer = io.BytesIO()
doc.save(word_buffer)
word_buffer.seek(0)
from fastapi.responses import Response
return Response(
content=word_buffer.getvalue(),
media_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document',
headers={
"Content-Disposition": f"attachment; filename=backtest_{record_id}.docx"
}
)
elif format == "excel":
# 使用pandas创建Excel
import pandas as pd
import io
# 创建 DataFrame
if record['strategy_type'] == 'grid':
data_list = []
for index, trade in enumerate(trade_records, 1):
date_str = trade.get('date', '')
date_part = date_str.split(' ')[0] if ' ' in date_str else date_str
time_part = date_str.split(' ')[1] if ' ' in date_str else '15:00:00'
data_list.append({
'序号': index,
'日期': date_part,
'时间': time_part,
'委托': trade.get('type', ''),
'价格': trade.get('price', 0),
'数量': trade.get('shares', 0),
'手续费': trade.get('fee', 0),
'现金余额': trade.get('cash_balance', 0),
'当前持仓市值': trade.get('position_value', 0),
'当前持仓股数': trade.get('current_shares', 0),
'资产': trade.get('total_value', 0),
'最新基准价': trade.get('base_price', 0),
'触发条件': trade.get('trigger_reason', trade.get('trigger_condition', ''))
})
df = pd.DataFrame(data_list)
else: # dca
data_list = []
for index, trade in enumerate(trade_records, 1):
date_str = trade.get('date', '')
date_part = date_str.split(' ')[0] if ' ' in date_str else date_str
time_part = date_str.split(' ')[1] if ' ' in date_str else '09:30:00'
data_list.append({
'序号': index,
'日期': date_part,
'时间': time_part,
'委托': trade.get('type', ''),
'价格': trade.get('price', 0),
'数量': trade.get('shares', 0),
'手续费': trade.get('fee', 0),
'现金余额': trade.get('cash_balance', 0),
'当前持仓市值': trade.get('position_value', 0),
'当前持仓股数': trade.get('current_shares', 0),
'资产': trade.get('total_value', 0),
'累计投入': trade.get('cumulative_invested', 0)
})
df = pd.DataFrame(data_list)
# 创建Excel文件
excel_buffer = io.BytesIO()
with pd.ExcelWriter(excel_buffer, engine='openpyxl') as writer:
df.to_excel(writer, sheet_name='交\u6613\u8bb0\u5f55', index=False)
# 添加统计信息工作表
if record['strategy_type'] == 'grid':
stats_data = {
'指标': [
'最终权益', '总收益', '总收益率', '年化收益率',
'总交易次数', '买入次数', '卖出次数', '胜率',
'最大回撤', '总手续费', '最大投入收益率',
'最大持仓市值', '最小持仓市值', '最大持仓股数', '最小持仓股数'
],
'数值': [
results.get('final_equity', 0),
results.get('total_profit', 0),
f"{results.get('total_return', 0):.2f}%",
f"{results.get('annual_return', 0):.2f}%",
results.get('total_trades', 0),
results.get('buy_trades', 0),
results.get('sell_trades', 0),
f"{results.get('win_rate', 0):.2f}%",
f"{results.get('max_drawdown', 0):.2f}%",
results.get('total_fee', 0),
f"{results.get('max_invested_return', 0):.2f}%",
results.get('max_position_value', 0),
results.get('min_position_value', 0),
results.get('max_position_shares', 0),
results.get('min_position_shares', 0)
]
}
else: # dca
stats_data = {
'指标': [
'最终权益', '总收益', '总收益率', '年化收益率',
'投资次数', '总投入金额', '平均成本', '最终股数', '总手续费'
],
'数值': [
results.get('final_equity', 0),
results.get('total_profit', 0),
f"{results.get('total_return', 0):.2f}%",
f"{results.get('annual_return', 0):.2f}%",
results.get('total_investments', 0),
results.get('total_invested', 0),
results.get('average_cost', 0),
results.get('final_shares', 0),
results.get('total_fee', 0)
]
}
stats_df = pd.DataFrame(stats_data)
stats_df.to_excel(writer, sheet_name='统计信息', index=False)
# 添加参数信息工作表
params = record['parameters']
if record['strategy_type'] == 'grid':
param_data = {
'参数名称': [
# 基本信息
'标的名称', '标的代码', '回测开始日期', '回测结束日期',
# 资金参数
'初始资金', '初始股数',
# 触发参数
'触发价格最小值', '触发价格最大值', '上涨触发', '下跌触发',
# 基准价参数
'基准价类型', '初始基准价', '基准价更新方式',
# 交易参数
'变动类型', '触发模式', '委托类型', '买入金额', '卖出金额', '委托价格类型',
# 持仓限制
'持仓限制类型', '最大持仓', '最小持仓',
# 手续费参数
'手续费类型', '手续费率', '最低手续费(元)', '双边手续费'
],
'参数值': [
# 基本信息 - 从上传数据中读取标的名称和代码
_extract_stock_name(record),
_extract_stock_code(record),
record.get('start_date', '未设置'),
record.get('end_date', '未设置'),
# 资金参数
f"¥{params.get('initial_capital', 0):,.2f}",
f"{params.get('initial_shares', 0)}股",
# 触发参数
f"¥{params.get('trigger_price_min', 0):.2f}",
f"¥{params.get('trigger_price_max', 0):.2f}",
f"{params.get('rise_trigger', 0):.2f}%",
f"{params.get('fall_trigger', 0):.2f}%",
# 基准价参数
_translate_base_price_type(params.get('base_price_type', 'open')),
f"¥{params.get('custom_base_price', 0):.2f}" if params.get('custom_base_price') else '自动',
'成交价更新', # 默认方式
# 交易参数
_translate_change_type(params.get('change_type', 'percentage')),
_translate_trigger_mode(params.get('trigger_mode', 'direct')),
_translate_order_type(params.get('order_type', 'shares')),
f"{params.get('buy_amount', 0)}股" if params.get('order_type') == 'shares' else f"¥{params.get('buy_amount', 0)}",
f"{params.get('sell_amount', 0)}股" if params.get('order_type') == 'shares' else f"¥{params.get('sell_amount', 0)}",
_translate_price_type(params.get('order_price_type', 'market')),
# 持仓限制
_translate_position_limit_type(params.get('position_limit_type', 'shares')) if params.get('position_limit_enabled', True) else '不限制',
f"{params.get('max_position', 0)}" if params.get('position_limit_enabled', True) and params.get('max_position') else '无限制',
f"{params.get('min_position', 0)}" if params.get('position_limit_enabled', True) and params.get('min_position') else '无限制',
# 手续费参数
_translate_fee_type(params.get('fee_type', 'percentage')),
_format_fee_rate(float(params.get('fee_rate', 0))),
f"¥{params.get('min_fee', 5):.2f}",
'是' if params.get('fee_bilateral', True) else '否'
]
}
else: # dca
param_data = {
'参数名称': [
# 基本信息
'标的名称', '标的代码', '回测开始日期', '回测结束日期',
# 资金参数
'初始资金',
# 定投参数
'定投金额', '定投频率', '定投日(周)', '定投日(月)',
# 手续费参数
'手续费类型', '手续费率', '最低手续费(元)'
],
'参数值': [
# 基本信息 - 从上传数据中读取标的名称和代码
_extract_stock_name(record),
_extract_stock_code(record),
record.get('start_date', '未设置'),
record.get('end_date', '未设置'),
# 资金参数
f"¥{params.get('initial_capital', 0):,.2f}",
# 定投参数
f"¥{params.get('investment_amount', 0):,.2f}",
_translate_investment_frequency(params.get('investment_frequency', 'monthly')),
f"周{params.get('day_of_week', '')}" if params.get('day_of_week') else '未设置',
f"{params.get('day_of_month', '')}日" if params.get('day_of_month') else '未设置',
# 手续费参数
'按比例',
_format_fee_rate(float(params.get('fee_rate', 0))),
f"¥{params.get('min_fee', 5):.2f}"
]
}
param_df = pd.DataFrame(param_data)
param_df.to_excel(writer, sheet_name='回测参数', index=False)
excel_buffer.seek(0)
from fastapi.responses import Response
return Response(
content=excel_buffer.getvalue(),
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
headers={
"Content-Disposition": f"attachment; filename=backtest_{record_id}.xlsx"
}
)
else:
raise HTTPException(status_code=400, detail="不支持的导出格式")
except HTTPException:
raise
except Exception as e:
print(f"导出错误: {e}")
raise HTTPException(status_code=500, detail=f"导出失败: {str(e)}")
async def delete_backtest_record(record_id: str):
"""删除历史记录(从数据库和内存中)"""
global backtest_history
try:
# 从数据库删除(持久化)
db_deleted = False
if HAS_DATABASE:
db_deleted = delete_backtest_from_database(record_id)
if db_deleted:
print(f"✅ 记录 {record_id} 已从数据库删除")
else:
print(f"⚠️ 记录 {record_id} 在数据库中不存在或删除失败")
# 从内存删除
original_count = len(backtest_history)
backtest_history = [record for record in backtest_history if record['id'] != record_id]
memory_deleted = len(backtest_history) < original_count
if memory_deleted:
print(f"✅ 记录 {record_id} 已从内存删除")
if db_deleted or memory_deleted:
return {"message": "删除成功"}
else:
return {"error": "记录不存在"}
except Exception as e:
print(f"❌ 删除记录失败: {e}")
return {"error": f"删除失败: {str(e)}"}
async def batch_delete_backtest_records(request: dict):
"""批量删除历史记录"""
global backtest_history
try:
record_ids = request.get('record_ids', [])
if not record_ids:
return {"error": "请提供要删除的记录ID列表"}
deleted_count = 0
failed_ids = []
for record_id in record_ids:
try:
# 从数据库删除
db_deleted = False
if HAS_DATABASE:
db_deleted = delete_backtest_from_database(record_id)
# 从内存删除
original_count = len(backtest_history)
backtest_history = [record for record in backtest_history if record['id'] != record_id]
memory_deleted = len(backtest_history) < original_count
if db_deleted or memory_deleted:
deleted_count += 1
print(f"✅ 批量删除: 记录 {record_id} 已删除")
else:
failed_ids.append(record_id)
print(f"⚠️ 批量删除: 记录 {record_id} 删除失败或不存在")
except Exception as e:
failed_ids.append(record_id)
print(f"❌ 批量删除: 记录 {record_id} 删除失败: {e}")
result = {
"message": f"批量删除完成",
"deleted_count": deleted_count,
"total_count": len(record_ids),
"success_rate": f"{(deleted_count / len(record_ids) * 100):.1f}%"
}
if failed_ids:
result["failed_ids"] = failed_ids
result["warning"] = f"{len(failed_ids)} 条记录删除失败"
return result
except Exception as e:
print(f"❌ 批量删除失败: {e}")
return {"error": f"批量删除失败: {str(e)}"}
# ==================== 数据下载API ====================
download_tasks = {}
download_task_counter = 0
def _get_download_history_path() -> str:
backend_dir = os.path.dirname(__file__)
cache_dir = os.path.join(backend_dir, 'cache')
os.makedirs(cache_dir, exist_ok=True)
return os.path.join(cache_dir, 'download_tasks.json')
def _serialize_download_task(task: dict) -> dict:
if task is None:
return {}
allowed_keys = [
'id', 'symbol', 'stock_name', 'data_type',
'data_provider',
'security_type',
'start_date', 'end_date', 'effective_start_date', 'effective_end_date',
'adjust_flag', 'save_format',
'limit', 'order', 'columns',
'exchange_code', 'ticker',
'data_range_start', 'data_range_end',
'file_size_bytes',
'status', 'stage', 'progress', 'message',
'created_at', 'started_at', 'completed_at',
'data_count', 'file_path',
'cache_key', 'cached', 'adjustments', 'error',
]
out = {k: task.get(k) for k in allowed_keys}
return out
def _infer_market_label_from_code(code: str) -> str:
c = str(code or '').strip().lower()
if c.startswith('sh.'):
return '沪'
if c.startswith('sz.'):
return '深'
c6 = ''.join(ch for ch in c if ch.isdigit())
if c6.startswith(('5', '6', '9')):
return '沪'
if c6:
return '深'
return ''
def _persist_spot_to_market_lists_xlsx() -> None:
try:
path = _get_market_list_xlsx_path()
if not path or not os.path.exists(path):
return
with _akshare_spot_lock:
stocks_spot = dict(_akshare_spot_cache.get('stocks') or {})
etf_spot = dict(_akshare_spot_cache.get('etf') or {})
lof_spot = dict(_akshare_spot_cache.get('lof') or {})
with _market_list_file_lock:
sheets = pd.read_excel(path, sheet_name=None)
def _update_sheet(sheet_name: str, spot_map: dict[str, Any], label: str) -> None:
df = sheets.get(sheet_name)
if df is None or not isinstance(df, pd.DataFrame) or df.empty:
return
# 确保 code6
if 'code6' not in df.columns:
if 'code' in df.columns:
df['code6'] = df['code'].map(lambda x: _normalize_code6(_safe_str(x).split('.')[-1]))
else:
return
df['code6'] = df['code6'].map(_normalize_code6)
spot_rows = []
for k, v in (spot_map or {}).items():
if not isinstance(v, dict):
continue
code6 = _normalize_code6(k)
if not code6:
continue
amount_wan = None
try:
if v.get('amount') is not None:
amount_wan = float(v.get('amount')) / 10000.0
except Exception:
amount_wan = None
spot_rows.append({
'code6': code6,
'最新价': v.get('latest_price'),
'当日涨跌幅': v.get('pct_change'),
'开盘价': v.get('open'),
'最高价': v.get('high'),
'最低价': v.get('low'),
'成交额(万)': amount_wan,
'成交量(手)': v.get('volume'),
'换手率(%)': v.get('turnover_rate'),
'市盈率': v.get('pe'),
})
spot_df = pd.DataFrame(spot_rows)
if spot_df is None or spot_df.empty:
# 仍然写入"市场类型/市"列,保持结构一致
if '市场类型' not in df.columns:
df['市场类型'] = label
else:
df['市场类型'] = label
if '市' not in df.columns:
code_col = 'code' if 'code' in df.columns else None
if code_col:
df['市'] = df[code_col].map(_infer_market_label_from_code)
else:
df['市'] = ''
sheets[sheet_name] = df
return
merged = df.merge(spot_df, on='code6', how='left', suffixes=('', '_spot'))
for col in ['最新价', '当日涨跌幅', '开盘价', '最高价', '最低价', '成交额(万)', '成交量(手)', '换手率(%)', '市盈率']:
if col not in merged.columns:
merged[col] = None
# 只在 spot 有值时更新,否则保留旧值
if f"{col}_spot" in merged.columns:
try:
mask = merged[f"{col}_spot"].notna()
merged.loc[mask, col] = merged.loc[mask, f"{col}_spot"]
except Exception:
pass
merged['市场类型'] = label
if '市' not in merged.columns:
merged['市'] = ''
try:
if 'code' in merged.columns:
merged['市'] = merged['code'].map(_infer_market_label_from_code)
except Exception:
pass
drop_cols = [c for c in merged.columns if str(c).endswith('_spot')]
if drop_cols:
merged = merged.drop(columns=drop_cols)
sheets[sheet_name] = merged
_update_sheet('stocks', stocks_spot, '股票')
_update_sheet('etf', etf_spot, 'ETF')
_update_sheet('lof', lof_spot, 'LOF')
# 记录当日是否已更新过行情(用于跨重启的"每天只自动更新一次")
try:
meta_df = sheets.get('meta')
if meta_df is None or not isinstance(meta_df, pd.DataFrame) or meta_df.empty:
meta_df = pd.DataFrame([{}])
if 'spot_updated_at' not in meta_df.columns:
meta_df['spot_updated_at'] = ''
meta_df.loc[0, 'spot_updated_at'] = _now_bj_iso()
sheets['meta'] = meta_df
market_list_state['spot_updated_at'] = str(meta_df.loc[0, 'spot_updated_at'])
except Exception:
pass
with pd.ExcelWriter(path, engine='openpyxl') as writer:
for sn, sdf in sheets.items():
try:
if isinstance(sdf, pd.DataFrame):
sdf.to_excel(writer, sheet_name=str(sn), index=False)
except Exception:
continue
market_list_cache['loaded'] = False
except Exception:
return
def _save_download_history() -> None:
try:
with _download_tasks_lock:
path = _get_download_history_path()
tasks = list(download_tasks.values())
tasks.sort(key=lambda t: str(t.get('created_at') or ''), reverse=True)
tasks = tasks[:200]
payload = {
'saved_at': _now_bj_iso(),
'max_id': max([int(t.get('id', 0)) for t in tasks], default=0),
'tasks': [_serialize_download_task(t) for t in tasks],
}
tmp_path = f"{path}.tmp"
with open(tmp_path, 'w', encoding='utf-8') as f:
json.dump(payload, f, ensure_ascii=False, indent=2)
os.replace(tmp_path, path)
except Exception:
pass
def _load_download_history() -> None:
global download_task_counter
path = _get_download_history_path()
if not os.path.exists(path):
return
try:
with open(path, 'r', encoding='utf-8') as f:
payload = json.load(f) or {}
tasks = payload.get('tasks') or []
restored = {}
max_id = 0
for t in tasks:
if not isinstance(t, dict):
continue
tid = int(t.get('id', 0) or 0)
if tid <= 0:
continue
restored[tid] = {
'id': tid,
'symbol': t.get('symbol'),
'stock_name': t.get('stock_name'),
'data_type': t.get('data_type'),
'data_provider': t.get('data_provider') or 'baostock',
'security_type': t.get('security_type'),
'start_date': t.get('start_date'),
'end_date': t.get('end_date'),
'effective_start_date': t.get('effective_start_date'),
'effective_end_date': t.get('effective_end_date'),
'adjust_flag': t.get('adjust_flag'),
'save_format': t.get('save_format'),
'limit': t.get('limit'),
'order': t.get('order'),
'columns': t.get('columns'),
'exchange_code': t.get('exchange_code'),
'ticker': t.get('ticker'),
'data_range_start': t.get('data_range_start'),
'data_range_end': t.get('data_range_end'),
'file_size_bytes': t.get('file_size_bytes'),
'status': t.get('status'),
'stage': t.get('stage'),
'progress': t.get('progress', 0),
'message': t.get('message'),
'created_at': t.get('created_at'),
'started_at': t.get('started_at'),
'completed_at': t.get('completed_at'),
'data': None,
'data_count': t.get('data_count', 0),
'file_path': t.get('file_path'),
'cache_key': t.get('cache_key'),
'cached': t.get('cached', False),
'adjustments': t.get('adjustments', []),
'error': t.get('error'),
}
max_id = max(max_id, tid)
download_tasks.clear()
download_tasks.update(restored)
download_task_counter = max(download_task_counter, max_id)
except Exception:
return
_load_download_history()
market_list_state: dict[str, Any] = {
'status': 'idle',
'progress': 0,
'message': '未更新',
'started_at': None,
'completed_at': None,
'updated_at': None,
'spot_updated_at': None,
'counts': {'stocks': 0, 'etf': 0, 'lof': 0},
'error': None,
}
market_list_cache: dict[str, Any] = {
'loaded': False,
'path': None,
'items': [],
}
def _now_bj_iso() -> str:
return now_utc().astimezone(timezone(timedelta(hours=8))).isoformat()
# AkShare 行情缓存(避免频繁拉取导致接口变慢)
_akshare_spot_cache: dict[str, Any] = {
'ts': 0.0,
'ttl': 1800.0,
'stocks': {},
'etf': {},
'lof': {},
}
_akshare_spot_lock = threading.Lock()
_market_list_file_lock = threading.Lock()
# 连续 akshare 失败次数,>=2 时自动尝试 efinance 兜底
_akshare_consecutive_failures: int = 0
# 行情请求重试次数与间隔(秒)
_SPOT_FETCH_MAX_RETRIES = 3
_SPOT_FETCH_RETRY_DELAY = 2.0
def _is_trading_time_bj() -> bool:
try:
bj = now_utc().astimezone(timezone(timedelta(hours=8)))
if bj.weekday() >= 5:
return False
m = bj.hour * 60 + bj.minute
if (9 * 60 + 30) <= m < (11 * 60 + 30):
return True
if (13 * 60) <= m < (15 * 60):
return True
return False
except Exception:
return False
def _apply_proxy_env() -> None:
"""从环境变量应用 HTTP/HTTPS 代理,供 akshare/efinance 等请求使用。支持 MARKET_HTTP_PROXY / HTTP_PROXY 等。"""
for key_in, key_out in [
('MARKET_HTTP_PROXY', 'HTTP_PROXY'),
('MARKET_HTTPS_PROXY', 'HTTPS_PROXY'),
('HTTP_PROXY', 'HTTP_PROXY'),
('HTTPS_PROXY', 'HTTPS_PROXY'),
]:
val = os.environ.get(key_in, '').strip()
if val and not os.environ.get(key_out):
os.environ[key_out] = val
def _retry_spot_fetch(func, max_retries: int = None) -> Any:
"""对行情拉取函数进行重试;失败时等待 _SPOT_FETCH_RETRY_DELAY 秒后重试。"""
n = max_retries if max_retries is not None else _SPOT_FETCH_MAX_RETRIES
last_exc = None
for attempt in range(n):
try:
return func()
except Exception as e:
last_exc = e
if attempt < n - 1:
time.sleep(_SPOT_FETCH_RETRY_DELAY)
if last_exc is not None:
raise last_exc
return None
async def _spot_auto_refresh_loop():
while True:
try:
await asyncio.sleep(60)
if not _is_trading_time_bj():
continue
await asyncio.to_thread(_refresh_akshare_spot_cache, False)
except asyncio.CancelledError:
raise
except Exception:
continue
def _to_float(x: Any) -> float | None:
try:
if x is None:
return None
s = str(x).strip().replace('%', '')
if s == '' or s.lower() in ('nan', 'none'):
return None
return float(s)
except Exception:
return None
def _normalize_code6(x: Any) -> str:
try:
if x is None:
return ''
s = str(x).strip()
if s == '' or s.lower() in ('nan', 'none'):
return ''
# Excel 里可能把 code6 读成 float(例如 1.0),这里先取小数点前部分
if '.' in s:
s = s.split('.')[0]
digits = ''.join(ch for ch in s if ch.isdigit())
if not digits:
return ''
if len(digits) < 6:
return digits.zfill(6)
if len(digits) > 6:
return digits[-6:]
return digits
except Exception:
return ''
def _first_existing_col(df: 'pd.DataFrame', candidates: list[str]) -> str | None:
try:
cols = set([str(c) for c in df.columns])
for c in candidates:
if c in cols:
return c
except Exception:
pass
return None
def _build_spot_map(
df: 'pd.DataFrame',
code_cols: list[str],
name_cols: list[str],
price_cols: list[str],
pct_cols: list[str],
open_cols: list[str],
high_cols: list[str],
low_cols: list[str],
amount_cols: list[str],
volume_cols: list[str],
turnover_cols: list[str],
pe_cols: list[str],
) -> dict[str, dict[str, Any]]:
if df is None or not isinstance(df, pd.DataFrame) or df.empty:
return {}
code_col = _first_existing_col(df, code_cols)
name_col = _first_existing_col(df, name_cols)
price_col = _first_existing_col(df, price_cols)
pct_col = _first_existing_col(df, pct_cols)
open_col = _first_existing_col(df, open_cols)
high_col = _first_existing_col(df, high_cols)
low_col = _first_existing_col(df, low_cols)
amount_col = _first_existing_col(df, amount_cols)
volume_col = _first_existing_col(df, volume_cols)
turnover_col = _first_existing_col(df, turnover_cols)
pe_col = _first_existing_col(df, pe_cols)
out: dict[str, dict[str, Any]] = {}
for _, row in df.iterrows():
code_raw = _safe_str(row.get(code_col)) if code_col else ''
code6 = _normalize_code6(code_raw)
if not code6:
continue
out[code6] = {
'code6': code6,
'name': _safe_str(row.get(name_col)) if name_col else '',
'latest_price': _to_float(row.get(price_col)) if price_col else None,
'pct_change': _to_float(row.get(pct_col)) if pct_col else None,
'open': _to_float(row.get(open_col)) if open_col else None,
'high': _to_float(row.get(high_col)) if high_col else None,
'low': _to_float(row.get(low_col)) if low_col else None,
'amount': _to_float(row.get(amount_col)) if amount_col else None,
'volume': _to_float(row.get(volume_col)) if volume_col else None,
'turnover_rate': _to_float(row.get(turnover_col)) if turnover_col else None,
'pe': _to_float(row.get(pe_col)) if pe_col else None,
}
return out
def _refresh_akshare_spot_cache(force: bool = False) -> None:
global _akshare_consecutive_failures
now = time.time()
if not force and not _is_trading_time_bj():
return
with _akshare_spot_lock:
ttl = float(_akshare_spot_cache.get('ttl', 60.0) or 60.0)
if not force and (now - float(_akshare_spot_cache.get('ts', 0.0) or 0.0)) < ttl:
return
_apply_proxy_env()
# 不在 lock 内执行网络/第三方调用,避免阻塞其它请求
stocks_map: dict[str, Any] = {}
etf_map: dict[str, Any] = {}
lof_map: dict[str, Any] = {}
akshare_ok = False
try:
import akshare as ak
def _ak_stocks():
df = ak.stock_zh_a_spot_em()
return _build_spot_map(
df,
code_cols=['代码', '股票代码', 'symbol', 'code'],
name_cols=['名称', '股票简称', 'name'],
price_cols=['最新价', '最新', '现价', 'price'],
pct_cols=['涨跌幅', '涨跌幅(%)', 'pct', '涨跌幅%', '涨跌幅(%)'],
open_cols=['今开', '开盘', '开盘价', 'open'],
high_cols=['最高', '最高价', 'high'],
low_cols=['最低', '最低价', 'low'],
amount_cols=['成交额', '成交额(元)', 'amount'],
volume_cols=['成交量', '成交量(手)', 'volume'],
turnover_cols=['换手率', '换手率(%)', 'turnover_rate'],
pe_cols=['市盈率-动态', '市盈率', 'pe', '市盈率(动)'],
)
def _ak_etf():
df = ak.fund_etf_spot_em()
return _build_spot_map(
df,
code_cols=['基金代码', '代码', 'symbol', 'code'],
name_cols=['基金简称', '名称', 'name'],
price_cols=['最新价', '最新', '现价', 'price'],
pct_cols=['涨跌幅', '涨跌幅(%)', 'pct'],
open_cols=['开盘', '开盘价', '今开', 'open'],
high_cols=['最高', '最高价', 'high'],
low_cols=['最低', '最低价', 'low'],
amount_cols=['成交额', '成交额(元)', 'amount'],
volume_cols=['成交量', '成交量(手)', 'volume'],
turnover_cols=['换手率', '换手率(%)', 'turnover_rate'],
pe_cols=['市盈率', 'pe'],
)
def _ak_lof():
df = ak.fund_lof_spot_em()
return _build_spot_map(
df,
code_cols=['基金代码', '代码', 'symbol', 'code'],
name_cols=['基金简称', '名称', 'name'],
price_cols=['最新价', '最新', '现价', 'price'],
pct_cols=['涨跌幅', '涨跌幅(%)', 'pct'],
open_cols=['开盘', '开盘价', '今开', 'open'],
high_cols=['最高', '最高价', 'high'],
low_cols=['最低', '最低价', 'low'],
amount_cols=['成交额', '成交额(元)', 'amount'],
volume_cols=['成交量', '成交量(手)', 'volume'],
turnover_cols=['换手率', '换手率(%)', 'turnover_rate'],
pe_cols=['市盈率', 'pe'],
)
try:
stocks_map = _retry_spot_fetch(_ak_stocks)
akshare_ok = bool(stocks_map)
except Exception:
stocks_map = {}
try:
etf_map = _retry_spot_fetch(_ak_etf)
akshare_ok = akshare_ok or bool(etf_map)
except Exception:
etf_map = {}
try:
lof_map = _retry_spot_fetch(_ak_lof)
akshare_ok = akshare_ok or bool(lof_map)
except Exception:
lof_map = {}
if akshare_ok:
_akshare_consecutive_failures = 0
else:
_akshare_consecutive_failures += 1
except Exception:
stocks_map = {}
etf_map = {}
lof_map = {}
_akshare_consecutive_failures += 1
# 连续失败 >=2 时用 efinance 兜底
fail_count = _akshare_consecutive_failures
if fail_count >= 2:
try:
import efinance as ef
ef_cols = dict(
code_cols=['股票代码', '代码', 'symbol', 'code'],
name_cols=['股票名称', '名称', 'name'],
price_cols=['最新价', 'price'],
pct_cols=['涨跌幅', 'pct'],
open_cols=['今开', 'open'],
high_cols=['最高', 'high'],
low_cols=['最低', 'low'],
amount_cols=['成交额', 'amount'],
volume_cols=['成交量', 'volume'],
turnover_cols=['换手率', 'turnover_rate'],
pe_cols=['动态市盈率', 'pe'],
)
if not stocks_map:
try:
df_ef_stock = _retry_spot_fetch(lambda: ef.stock.get_realtime_quotes())
if df_ef_stock is not None and not df_ef_stock.empty:
stocks_map = _build_spot_map(df_ef_stock, **ef_cols)
except Exception:
pass
if not etf_map:
try:
df_ef_etf = _retry_spot_fetch(lambda: ef.stock.get_realtime_quotes(['ETF']))
if df_ef_etf is not None and not df_ef_etf.empty:
etf_map = _build_spot_map(df_ef_etf, **ef_cols)
except Exception:
pass
if not lof_map:
try:
df_ef_lof = _retry_spot_fetch(lambda: ef.stock.get_realtime_quotes(['LOF']))
if df_ef_lof is not None and not df_ef_lof.empty:
lof_map = _build_spot_map(df_ef_lof, **ef_cols)
except Exception:
pass
except Exception:
pass
with _akshare_spot_lock:
_akshare_spot_cache['stocks'] = stocks_map
_akshare_spot_cache['etf'] = etf_map
_akshare_spot_cache['lof'] = lof_map
_akshare_spot_cache['ts'] = time.time()
_persist_spot_to_market_lists_xlsx()
def _get_market_list_xlsx_path() -> str:
backend_dir = os.path.dirname(__file__)
cache_dir = os.path.join(backend_dir, 'cache')
os.makedirs(cache_dir, exist_ok=True)
return os.path.join(cache_dir, 'market_lists.xlsx')
def _guess_market_prefix(code6: str) -> str:
c = (code6 or '').strip()
if not c:
return ''
if c.lower().startswith('sh.') or c.lower().startswith('sz.'):
return c[:3].lower()
if c[0] in ('6', '9', '5'):
return 'sh.'
return 'sz.'
def _to_prefixed_code(code: str) -> str:
c = (code or '').strip()
if not c:
return ''
if c.lower().startswith('sh.') or c.lower().startswith('sz.'):
return c.lower()
if c.isdigit():
return f"{_guess_market_prefix(c)}{c}"
return c.lower()
def _safe_str(x: Any) -> str:
if x is None:
return ''
return str(x).strip()
def _normalize_q(s: str) -> str:
s = (s or '').strip().lower()
return ''.join(ch for ch in s if ch not in (' ', '\t', '\n', '\r'))
def _get_pinyin_initials(text: str) -> str:
try:
from pypinyin import pinyin, Style
parts = pinyin(text, style=Style.FIRST_LETTER, strict=False)
return ''.join(p[0] for p in parts if p and p[0]).lower()
except Exception:
return ''
def _get_pinyin_full(text: str) -> str:
try:
from pypinyin import pinyin, Style
parts = pinyin(text, style=Style.NORMAL, strict=False)
return ''.join(p[0] for p in parts if p and p[0]).lower()
except Exception:
return ''
def _expand_fuzzy_pinyin(q: str) -> list[str]:
"""对拼音输入做少量模糊音扩展,避免爆炸式组合。"""
base = _normalize_q(q)
if not base:
return []
variants = {base}
replacements = [
('zh', 'z'), ('ch', 'c'), ('sh', 's'),
('z', 'zh'), ('c', 'ch'), ('s', 'sh'),
('l', 'n'), ('n', 'l'),
('ang', 'an'), ('an', 'ang'),
('eng', 'en'), ('en', 'eng'),
('ing', 'in'), ('in', 'ing'),
]
for a, b in replacements:
if a in base:
variants.add(base.replace(a, b))
return list(variants)[:10]
def _score_item(item: dict[str, Any], q_variants: list[str]) -> float:
code = _normalize_q(item.get('code', ''))
code6 = _normalize_q(item.get('code6', ''))
name = _normalize_q(item.get('name', ''))
py_i = _normalize_q(item.get('pinyin_initials', ''))
py_f = _normalize_q(item.get('pinyin_full', ''))
best = 0.0
for q in q_variants:
if not q:
continue
if q == code or q == code6:
best = max(best, 100.0)
continue
if code.startswith(q) or code6.startswith(q):
best = max(best, 92.0)
if q in code or q in code6:
best = max(best, 88.0)
if name and q in name:
best = max(best, 84.0)
if py_i and (py_i.startswith(q) or q in py_i):
best = max(best, 78.0)
if py_f and (py_f.startswith(q) or q in py_f):
best = max(best, 74.0)
if name:
sim = difflib.SequenceMatcher(None, q, name).ratio()
if sim >= 0.60:
best = max(best, 50.0 + sim * 30.0)
if py_f:
sim2 = difflib.SequenceMatcher(None, q, py_f).ratio()
if sim2 >= 0.70:
best = max(best, 45.0 + sim2 * 25.0)
return best
def _load_market_lists_from_disk(force: bool = False) -> bool:
if market_list_cache.get('loaded') and not force:
return True
path = _get_market_list_xlsx_path()
market_list_cache['path'] = path
if not os.path.exists(path):
market_list_cache['loaded'] = False
market_list_cache['items'] = []
return False
try:
sheets = pd.read_excel(path, sheet_name=None)
items: list[dict[str, Any]] = []
counts = {'stocks': 0, 'etf': 0, 'lof': 0}
for sheet_name, df in sheets.items():
if sheet_name not in ('stocks', 'etf', 'lof'):
continue
if df is None or df.empty:
continue
for _, row in df.iterrows():
code = _safe_str(row.get('code', row.get('代码', '')))
name = _safe_str(row.get('name', row.get('名称', '')))
code6 = _normalize_code6(row.get('code6', ''))
if not code:
if code6:
code = _to_prefixed_code(code6)
if not code6 and code:
code6 = _normalize_code6(code.split('.')[-1])
if not code or not name:
continue
it = {
'code': _to_prefixed_code(code),
'code6': code6,
'name': name,
'type': sheet_name,
'ipo_date': _safe_str(row.get('ipo_date', row.get('ipoDate', row.get('上市日期', '')))),
'industry': _safe_str(row.get('industry', row.get('行业', ''))),
# 以下为行情缓存列(可被 AkShare 刷新写入,但不影响行业/上市日期)
'market_type': _safe_str(row.get('市场类型', '')),
'market': _safe_str(row.get('市', '')),
'latest_price': _to_float(row.get('最新价')),
'pct_change': _to_float(row.get('当日涨跌幅')),
'open': _to_float(row.get('开盘价')),
'high': _to_float(row.get('最高价')),
'low': _to_float(row.get('最低价')),
'amount': _to_float(row.get('成交额(万)')),
'volume': _to_float(row.get('成交量(手)')),
'turnover_rate': _to_float(row.get('换手率(%)')),
'pe': _to_float(row.get('市盈率')),
'pinyin_initials': _safe_str(row.get('pinyin_initials', '')),
'pinyin_full': _safe_str(row.get('pinyin_full', '')),
}
items.append(it)
counts[sheet_name] = len(df)
market_list_cache['items'] = items
market_list_cache['loaded'] = True
market_list_state['counts'] = counts
try:
meta_df = sheets.get('meta')
if meta_df is not None and not meta_df.empty:
updated_at = _safe_str(meta_df.iloc[0].get('updated_at', ''))
if updated_at:
market_list_state['updated_at'] = updated_at
spot_updated_at = _safe_str(meta_df.iloc[0].get('spot_updated_at', ''))
if spot_updated_at:
market_list_state['spot_updated_at'] = spot_updated_at
except Exception:
pass
return True
except Exception as e:
market_list_cache['loaded'] = False
market_list_cache['items'] = []
msg = str(e)
if 'openpyxl' in msg and 'Missing optional dependency' in msg:
msg = f"读取本地列表缓存失败: {e}(请安装 openpyxl:pip install openpyxl)"
market_list_state['error'] = f"读取本地列表缓存失败: {msg}"
return False
async def _update_market_lists_job():
market_list_state['status'] = 'updating'
market_list_state['progress'] = 0
market_list_state['message'] = '开始更新列表...'
market_list_state['started_at'] = _now_bj_iso()
market_list_state['completed_at'] = None
market_list_state['error'] = None
path = _get_market_list_xlsx_path()
market_list_cache['path'] = path
try:
market_list_state['message'] = '拉取股票列表...'
market_list_state['progress'] = 10
df_stocks = None
try:
import akshare as ak
df_tmp = ak.stock_info_a_code_name()
cols = {c: str(c) for c in df_tmp.columns}
code_col = None
name_col = None
for c in df_tmp.columns:
if str(c).lower() in ('code', 'symbol', '股票代码') or '代码' in str(c):
code_col = c
break
for c in df_tmp.columns:
if str(c).lower() in ('name', '股票简称', '股票名称') or '名称' in str(c) or '简称' in str(c):
name_col = c
break
if code_col and name_col:
df_stocks = df_tmp[[code_col, name_col]].rename(columns={code_col: 'code6', name_col: 'name'})
except Exception:
df_stocks = None
if df_stocks is None:
try:
import baostock as bs
with _baostock_lock:
lg = bs.login()
if lg.error_code != '0':
raise RuntimeError(lg.error_msg)
rs = bs.query_stock_basic()
data_list = []
while (rs.error_code == '0') and rs.next():
data_list.append(rs.get_row_data())
bs.logout()
df_tmp = pd.DataFrame(data_list, columns=rs.fields)
if 'code' in df_tmp.columns and 'code_name' in df_tmp.columns:
df_stocks = df_tmp[['code', 'code_name']].rename(columns={'code': 'code', 'code_name': 'name'})
df_stocks['code6'] = df_stocks['code'].map(lambda x: _safe_str(x).split('.')[-1])
df_stocks['code'] = df_stocks['code'].map(_to_prefixed_code)
except Exception:
df_stocks = None
if df_stocks is None or df_stocks.empty:
raise RuntimeError('股票列表获取失败(AkShare/Baostock 均失败)')
if 'code' not in df_stocks.columns:
df_stocks['code'] = df_stocks['code6'].map(lambda x: _to_prefixed_code(_safe_str(x)))
market_list_state['message'] = '拉取 ETF 列表...'
market_list_state['progress'] = 35
df_etf = pd.DataFrame(columns=['code6', 'name', 'code'])
try:
import akshare as ak
df_tmp = ak.fund_etf_spot_em()
code_col = None
name_col = None
for c in df_tmp.columns:
if str(c).lower() in ('code', 'symbol') or '代码' in str(c):
code_col = c
break
for c in df_tmp.columns:
if str(c).lower() in ('name',) or '名称' in str(c) or '简称' in str(c):
name_col = c
break
if code_col and name_col:
df_etf = df_tmp[[code_col, name_col]].rename(columns={code_col: 'code6', name_col: 'name'})
except Exception:
df_etf = pd.DataFrame(columns=['code6', 'name'])
if 'code' not in df_etf.columns:
df_etf['code'] = df_etf['code6'].map(lambda x: _to_prefixed_code(_safe_str(x)))
market_list_state['message'] = '拉取 LOF 列表...'
market_list_state['progress'] = 55
df_lof = pd.DataFrame(columns=['code6', 'name', 'code'])
try:
import akshare as ak
df_tmp = ak.fund_lof_spot_em()
code_col = None
name_col = None
for c in df_tmp.columns:
if str(c).lower() in ('code', 'symbol') or '代码' in str(c):
code_col = c
break
for c in df_tmp.columns:
if str(c).lower() in ('name',) or '名称' in str(c) or '简称' in str(c):
name_col = c
break
if code_col and name_col:
df_lof = df_tmp[[code_col, name_col]].rename(columns={code_col: 'code6', name_col: 'name'})
except Exception:
df_lof = pd.DataFrame(columns=['code6', 'name'])
if 'code' not in df_lof.columns:
df_lof['code'] = df_lof['code6'].map(lambda x: _to_prefixed_code(_safe_str(x)))
market_list_state['message'] = '补充上市时间和行业分类...'
market_list_state['progress'] = 65
try:
import baostock as bs
with _baostock_lock:
lg = bs.login()
if lg.error_code != '0':
raise RuntimeError(lg.error_msg)
rs_basic = bs.query_stock_basic()
basic_list = []
while (rs_basic.error_code == '0') and rs_basic.next():
basic_list.append(rs_basic.get_row_data())
df_basic = pd.DataFrame(basic_list, columns=rs_basic.fields)
rs_ind = bs.query_stock_industry()
ind_list = []
while (rs_ind.error_code == '0') and rs_ind.next():
ind_list.append(rs_ind.get_row_data())
df_ind = pd.DataFrame(ind_list, columns=rs_ind.fields)
bs.logout()
if isinstance(df_basic, pd.DataFrame) and not df_basic.empty:
if 'code' in df_basic.columns:
df_basic['code'] = df_basic['code'].map(_to_prefixed_code)
if 'code6' not in df_basic.columns and 'code' in df_basic.columns:
df_basic['code6'] = df_basic['code'].map(lambda x: _safe_str(x).split('.')[-1])
if 'ipoDate' in df_basic.columns and 'ipo_date' not in df_basic.columns:
df_basic['ipo_date'] = df_basic['ipoDate'].map(_safe_str)
keep_cols = [c for c in ['code', 'code6', 'ipo_date'] if c in df_basic.columns]
df_basic2 = df_basic[keep_cols].dropna(subset=['code6']) if keep_cols else None
else:
df_basic2 = None
if isinstance(df_ind, pd.DataFrame) and not df_ind.empty:
if 'code' in df_ind.columns:
df_ind['code'] = df_ind['code'].map(_to_prefixed_code)
if 'code6' not in df_ind.columns and 'code' in df_ind.columns:
df_ind['code6'] = df_ind['code'].map(lambda x: _safe_str(x).split('.')[-1])
industry_col = None
for c in df_ind.columns:
lc = str(c).lower()
if lc in ('industry', 'industryname', 'industry_name'):
industry_col = c
break
if industry_col is None:
for c in df_ind.columns:
if '行业' in str(c):
industry_col = c
break
if industry_col is None:
for c in df_ind.columns:
lc = str(c).lower()
if 'industry' in lc and 'class' not in lc:
industry_col = c
break
if industry_col is not None:
df_ind2 = df_ind[['code6', industry_col]].rename(columns={industry_col: 'industry'})
df_ind2['industry'] = df_ind2['industry'].map(_safe_str)
else:
df_ind2 = None
else:
df_ind2 = None
def _merge_enrichment(df: pd.DataFrame) -> pd.DataFrame:
if df is None or df.empty:
return df
out = df.copy()
if 'code6' not in out.columns:
out['code6'] = out['code'].map(lambda x: _safe_str(x).split('.')[-1])
out['code6'] = out['code6'].map(_safe_str)
if df_basic2 is not None and not df_basic2.empty:
out = out.merge(df_basic2[['code6', 'ipo_date']], on='code6', how='left')
if df_ind2 is not None and not df_ind2.empty:
out = out.merge(df_ind2[['code6', 'industry']], on='code6', how='left')
if 'ipo_date' not in out.columns:
out['ipo_date'] = ''
else:
out['ipo_date'] = out['ipo_date'].map(_safe_str)
if 'industry' not in out.columns:
out['industry'] = ''
else:
out['industry'] = out['industry'].map(_safe_str)
return out
df_stocks = _merge_enrichment(df_stocks)
df_etf = _merge_enrichment(df_etf)
df_lof = _merge_enrichment(df_lof)
except Exception:
# 补充信息失败不影响主流程
pass
market_list_state['message'] = '生成拼音索引...'
market_list_state['progress'] = 70
def _append_pinyin(df: pd.DataFrame) -> pd.DataFrame:
if df is None or df.empty:
return df
df = df.copy()
df['name'] = df['name'].map(_safe_str)
df['code'] = df['code'].map(_to_prefixed_code)
if 'code6' in df.columns:
df['code6'] = df['code6'].map(_safe_str)
else:
df['code6'] = df['code'].map(lambda x: _safe_str(x).split('.')[-1])
if 'ipo_date' not in df.columns:
df['ipo_date'] = ''
else:
df['ipo_date'] = df['ipo_date'].map(_safe_str)
if 'industry' not in df.columns:
df['industry'] = ''
else:
df['industry'] = df['industry'].map(_safe_str)
df['pinyin_initials'] = df['name'].map(_get_pinyin_initials)
df['pinyin_full'] = df['name'].map(_get_pinyin_full)
return df[['code', 'code6', 'name', 'ipo_date', 'industry', 'pinyin_initials', 'pinyin_full']]
df_stocks2 = _append_pinyin(df_stocks)
df_etf2 = _append_pinyin(df_etf)
df_lof2 = _append_pinyin(df_lof)
market_list_state['message'] = '写入本地缓存文件...'
market_list_state['progress'] = 88
meta_df = pd.DataFrame([
{
'updated_at': _now_bj_iso(),
'source': 'akshare/baostock',
'stocks_count': int(len(df_stocks2)) if df_stocks2 is not None else 0,
'etf_count': int(len(df_etf2)) if df_etf2 is not None else 0,
'lof_count': int(len(df_lof2)) if df_lof2 is not None else 0,
}
])
with pd.ExcelWriter(path, engine='openpyxl') as writer:
df_stocks2.to_excel(writer, sheet_name='stocks', index=False)
df_etf2.to_excel(writer, sheet_name='etf', index=False)
df_lof2.to_excel(writer, sheet_name='lof', index=False)
meta_df.to_excel(writer, sheet_name='meta', index=False)
market_list_state['message'] = '加载到内存...'
market_list_state['progress'] = 95
_load_market_lists_from_disk(force=True)
market_list_state['status'] = 'completed'
market_list_state['progress'] = 100
market_list_state['message'] = '更新完成'
market_list_state['completed_at'] = _now_bj_iso()
market_list_state['updated_at'] = market_list_state['completed_at']
market_list_state['counts'] = {
'stocks': int(len(df_stocks2)) if df_stocks2 is not None else 0,
'etf': int(len(df_etf2)) if df_etf2 is not None else 0,
'lof': int(len(df_lof2)) if df_lof2 is not None else 0,
}
except Exception as e:
market_list_state['status'] = 'failed'
market_list_state['progress'] = 100
market_list_state['message'] = '更新失败'
market_list_state['error'] = str(e)
market_list_state['completed_at'] = _now_bj_iso()
async def get_market_lists_status():
path = _get_market_list_xlsx_path()
market_list_state['file_exists'] = os.path.exists(path)
market_list_state['file_path'] = path
if market_list_state.get('updated_at') is None and os.path.exists(path):
_load_market_lists_from_disk(force=False)
return market_list_state
async def update_market_lists(request: Request):
if market_list_state.get('status') == 'updating':
return market_list_state
asyncio.create_task(_update_market_lists_job())
return market_list_state
async def get_market_lists_items(
q: str = '',
market_type: str = 'all',
market: str = 'all',
industry: str = 'all',
sort_by: str = '',
sort_dir: str = 'asc',
page: int = 1,
page_size: int = 50,
):
if not _load_market_lists_from_disk(force=False):
raise HTTPException(status_code=400, detail='本地数据列表尚未缓存,请先点击u201c一键更新列表”')
try:
page = int(page or 1)
except Exception:
page = 1
page = max(1, page)
try:
page_size = int(page_size or 50)
except Exception:
page_size = 50
page_size = max(1, min(200, page_size))
all_items = list(market_list_cache.get('items', []) or [])
industries = sorted({str(it.get('industry') or '').strip() for it in all_items if str(it.get('industry') or '').strip()})
mt = str(market_type or 'all').strip().lower()
mk_raw = str(market or 'all').strip().lower()
ind = str(industry or 'all').strip()
mk = 'all'
if mk_raw in ('sh', '沪'):
mk = '沪'
elif mk_raw in ('sz', '深'):
mk = '深'
items = all_items
if mt in ('stocks', 'etf', 'lof'):
items = [it for it in items if str(it.get('type') or '').lower() == mt]
if mk in ('沪', '深'):
items = [
it for it in items
if _infer_market_label_from_code(it.get('code') or '') == mk
]
if ind and ind.lower() != 'all':
items = [it for it in items if str(it.get('industry') or '').strip() == ind]
q_raw = str(q or '').strip()
sort_key = str(sort_by or '').strip().lower()
sort_direction = str(sort_dir or 'asc').strip().lower()
reverse = sort_direction == 'desc'
if q_raw and not sort_key:
q_norm = _normalize_q(q_raw)
q_variants = {q_norm}
if q_norm.isdigit():
q_variants.add(q_norm)
q_variants.add(_normalize_q(_to_prefixed_code(q_norm)))
if q_norm.startswith('sh.') or q_norm.startswith('sz.'):
q_variants.add(q_norm.split('.')[-1])
q_variants.update(_expand_fuzzy_pinyin(q_norm))
q_list = [qq for qq in q_variants if qq]
scored = []
for it in items:
s = _score_item(it, q_list)
if s > 0:
scored.append((s, it))
scored.sort(key=lambda x: x[0], reverse=True)
items = [it for _, it in scored]
else:
def _to_num(v: Any) -> float | None:
try:
if v is None:
return None
if isinstance(v, (int, float)):
if not (v == v):
return None
return float(v)
s = str(v).strip().replace('%', '')
if s == '' or s.lower() in ('nan', 'none'):
return None
return float(s)
except Exception:
return None
def _sort_val(it: dict[str, Any]) -> tuple[int, Any]:
# None/空值统一排到最后
if sort_key in ('code', '代码'):
v = _normalize_code6(it.get('code6') or it.get('code') or '')
return (0 if v else 1, v)
if sort_key in ('name', '名称'):
v = _safe_str(it.get('name', ''))
return (0 if v else 1, v)
if sort_key in ('type', 'market_type', '市场类型'):
v = _safe_str(it.get('market_type') or it.get('type') or '')
return (0 if v else 1, v)
if sort_key in ('market', '市'):
v = _safe_str(it.get('market') or _infer_market_label_from_code(it.get('code') or ''))
return (0 if v else 1, v)
num_fields = {
'latest_price': 'latest_price',
'pct_change': 'pct_change',
'open': 'open',
'high': 'high',
'low': 'low',
'amount': 'amount',
'volume': 'volume',
'turnover_rate': 'turnover_rate',
'pe': 'pe',
}
if sort_key in num_fields:
n = _to_num(it.get(num_fields[sort_key]))
return (0 if n is not None else 1, n if n is not None else 0.0)
# 默认排序
v = _safe_str(it.get('code', ''))
return (0 if v else 1, v)
if sort_key:
items = sorted(items, key=_sort_val, reverse=reverse)
else:
items = sorted(items, key=lambda it: (str(it.get('type') or ''), str(it.get('code') or '')))
total = len(items)
start = (page - 1) * page_size
end = start + page_size
paged = items[start:end]
def _infer_market_label(code: str) -> str:
c = str(code or '').strip().lower()
if c.startswith('sh.'):
return '沪'
if c.startswith('sz.'):
return '深'
return ''
out_items = []
# 仅在 TTL 到期时刷新 AkShare 行情并写回 market_lists.xlsx;平时优先使用 xlsx 中已缓存的行情列
_refresh_akshare_spot_cache(force=False)
for it in paged:
code = it.get('code')
latest_price = it.get('latest_price')
pct_change = it.get('pct_change')
open_p = it.get('open')
high_p = it.get('high')
low_p = it.get('low')
amount_wan = it.get('amount')
volume = it.get('volume')
turnover_rate = it.get('turnover_rate')
pe = it.get('pe')
out_items.append({
'code': code,
'name': it.get('name'),
'type': it.get('type'),
'ipo_date': it.get('ipo_date', ''),
'industry': it.get('industry', ''),
'market': _infer_market_label(code),
'latest_price': latest_price,
'pct_change': pct_change,
'open': open_p,
'high': high_p,
'low': low_p,
'amount': amount_wan,
'volume': volume,
'turnover_rate': turnover_rate,
'pe': pe,
})
stats = {
'total': int(market_list_state.get('counts', {}).get('stocks', 0) or 0)
+ int(market_list_state.get('counts', {}).get('etf', 0) or 0)
+ int(market_list_state.get('counts', {}).get('lof', 0) or 0),
'stocks': int(market_list_state.get('counts', {}).get('stocks', 0) or 0),
'etf': int(market_list_state.get('counts', {}).get('etf', 0) or 0),
'lof': int(market_list_state.get('counts', {}).get('lof', 0) or 0),
'updated_at': market_list_state.get('updated_at'),
'spot_updated_at': market_list_state.get('spot_updated_at'),
}
return {
'items': out_items,
'total': total,
'page': page,
'page_size': page_size,
'stats': stats,
'industries': industries,
}
async def refresh_market_lists_spot():
try:
await asyncio.to_thread(_refresh_akshare_spot_cache, True)
except Exception as e:
msg = str(e).strip() or '行情刷新异常'
if 'No module named' in msg and 'akshare' in msg:
raise HTTPException(
status_code=503,
detail='行情拉取失败:未安装 akshare,请安装 pip install akshare;若已安装仍失败可尝试安装备用源 pip install efinance',
)
if 'No module named' in msg and 'efinance' in msg:
raise HTTPException(
status_code=503,
detail='行情拉取失败:AKShare 连续失败且未安装 efinance 兜底,请安装 pip install efinance 或检查网络/代理(可设置 HTTP_PROXY、HTTPS_PROXY)',
)
raise HTTPException(
status_code=503,
detail=f'行情刷新失败:{msg}。可检查网络或设置环境变量 HTTP_PROXY/HTTPS_PROXY 后重试。',
)
with _akshare_spot_lock:
ts = float(_akshare_spot_cache.get('ts', 0.0) or 0.0)
stocks = _akshare_spot_cache.get('stocks') or {}
etf = _akshare_spot_cache.get('etf') or {}
lof = _akshare_spot_cache.get('lof') or {}
if not stocks and not etf and not lof:
raise HTTPException(
status_code=503,
detail='行情拉取失败:AKShare 与 efinance 均未获取到数据,请检查网络或配置代理(环境变量 HTTP_PROXY/HTTPS_PROXY)后重试。',
)
return {
'status': 'ok',
'ts': ts,
'refreshed_at': datetime.now().isoformat(),
}
async def search_stocks(keyword: str = ""):
"""搜索股票"""
try:
print(f"🔍 搜索股票: {keyword}")
if not keyword.strip():
print("⚠️ 关键词为空")
return []
if not _load_market_lists_from_disk(force=False):
raise HTTPException(status_code=400, detail='本地股票/ETF/LOF 列表尚未缓存,请先点击“一键更新列表”')
q_raw = keyword.strip()
q_norm = _normalize_q(q_raw)
q_variants = {q_norm}
if q_norm.isdigit():
q_variants.add(q_norm)
q_variants.add(_normalize_q(_to_prefixed_code(q_norm)))
if q_norm.startswith('sh.') or q_norm.startswith('sz.'):
q_variants.add(q_norm.split('.')[-1])
q_variants.update(_expand_fuzzy_pinyin(q_norm))
q_list = [q for q in q_variants if q]
scored = []
for item in market_list_cache.get('items', []):
s = _score_item(item, q_list)
if s > 0:
scored.append((s, item))
scored.sort(key=lambda x: x[0], reverse=True)
top = scored[:20]
results = []
for _, it in top:
results.append({
'code': it.get('code'),
'name': it.get('name'),
'type': it.get('type'),
'ipo_date': it.get('ipo_date', ''),
'industry': it.get('industry', ''),
})
print(f"✅ 搜索完成: 找到{len(results)}只匹配")
return results
except Exception as e:
print(f"❌ 搜索股票失败: {e}")
import traceback
traceback.print_exc()
return []
def _normalize_symbol_for_baostock(symbol: str) -> str:
symbol = (symbol or "").strip()
if not symbol:
return symbol
if symbol.lower().startswith('sh.') or symbol.lower().startswith('sz.'):
return symbol.lower()
if symbol.isdigit():
# Baostock 使用前缀:sh./sz.
# 常见规则:
# - 上交所:6/9 开头股票;50/51/56/58 等 ETF
# - 深交所:0/1/2/3 开头股票;15/16 等 ETF/LOF
if symbol.startswith(('6', '9')):
return f"sh.{symbol}"
if symbol.startswith(('50', '51', '56', '58')):
return f"sh.{symbol}"
if symbol.startswith(('0', '1', '2', '3', '15', '16')):
return f"sz.{symbol}"
return f"sz.{symbol}"
return symbol
def _symbol_to_code6(symbol: str) -> str:
s = (symbol or '').strip().lower()
if s.startswith(('sh.', 'sz.')):
s = s.split('.', 1)[1]
s = ''.join(ch for ch in s if ch.isdigit())
return s
def _map_adjust_flag_to_akshare(adjust_flag: str) -> str:
# Baostock: 1=前复权, 2=后复权, 3=不复权
m = {
'1': 'qfq',
'2': 'hfq',
'3': '',
1: 'qfq',
2: 'hfq',
3: '',
}
return m.get(adjust_flag, '')
def _map_adjust_flag_to_efinance(adjust_flag: Any) -> int:
# Best-effort mapping (efinance may ignore/handle differently).
# 0: 不复权, 1: 前复权, 2: 后复权
m = {
'1': 1,
'2': 2,
'3': 0,
1: 1,
2: 2,
3: 0,
}
return int(m.get(adjust_flag, 0))
def _normalize_symbol_for_efinance(symbol: str) -> str:
s = (symbol or '').strip()
if not s:
return s
low = s.lower()
if low.startswith(('sh.', 'sz.')):
return s.split('.', 1)[1]
if low.startswith('sh') and low[2:].isdigit():
return low[2:]
if low.startswith('sz') and low[2:].isdigit():
return low[2:]
return s
def _efinance_kline(task: dict) -> 'pd.DataFrame':
import efinance as ef
raw_symbol = task.get('symbol')
symbol = _normalize_symbol_for_efinance(raw_symbol)
code6 = _symbol_to_code6(str(raw_symbol or ''))
data_type = str(task.get('data_type') or 'daily').strip().lower()
klt: int | None
if data_type == 'daily':
klt = None
elif data_type.endswith('min'):
klt = int(data_type.replace('min', ''))
else:
raise RuntimeError('efinance 不支持的数据类型')
fqt = _map_adjust_flag_to_efinance(task.get('adjust_flag', '3'))
def _infer_exchange_prefix(sym: str) -> str | None:
s = (sym or '').strip().lower()
if s.startswith('sh.') or s.startswith('sh'):
return 'sh'
if s.startswith('sz.') or s.startswith('sz'):
return 'sz'
c = _symbol_to_code6(s)
if c.startswith(('5', '6', '9')):
return 'sh'
return 'sz'
def _build_symbol_candidates() -> list[str]:
candidates: list[str] = []
if code6:
candidates.append(code6)
exch = _infer_exchange_prefix(str(raw_symbol or ''))
if exch and code6:
candidates.append(f"{exch}{code6}")
# ETF 兜底:同时尝试 sh/sz 前缀(不同数据源/代码可能不一致)
sec_type = _infer_security_type(str(raw_symbol or ''), task.get('security_type'))
if sec_type == 'etf' and code6:
candidates.append(f"sh{code6}")
candidates.append(f"sz{code6}")
# 最后保留一次原 normalize 结果(可能是非 6 位代码)
if symbol and symbol not in candidates:
candidates.append(symbol)
# 去重并保持顺序
seen: set[str] = set()
out: list[str] = []
for c in candidates:
cc = (c or '').strip()
if not cc or cc in seen:
continue
seen.add(cc)
out.append(cc)
return out
def _call_quote_history(sym: str) -> 'pd.DataFrame | None':
try:
if klt is None:
return ef.stock.get_quote_history(sym, fqt=fqt)
return ef.stock.get_quote_history(sym, klt=klt, fqt=fqt)
except TypeError:
# Some versions may not accept fqt
if klt is None:
return ef.stock.get_quote_history(sym)
return ef.stock.get_quote_history(sym, klt=klt)
df = None
last_err: Exception | None = None
for sym in _build_symbol_candidates():
try:
df = _call_quote_history(sym)
if isinstance(df, pd.DataFrame) and not df.empty:
break
except Exception as e:
last_err = e
df = None
continue
if df is None and last_err is not None:
# 统一抛出,便于上层记录错误
raise last_err
if df is None or not isinstance(df, pd.DataFrame) or df.empty:
return pd.DataFrame()
start_date = task.get('effective_start_date') or task.get('start_date')
end_date = task.get('effective_end_date') or task.get('end_date')
if (start_date or end_date) and '日期' in df.columns:
try:
date_only = df['日期'].astype(str).str.slice(0, 10)
mask = pd.Series([True] * len(df))
if start_date:
mask &= date_only >= str(start_date)
if end_date:
mask &= date_only <= str(end_date)
df = df.loc[mask].copy()
except Exception:
pass
return df
def _infer_security_type(symbol: str, declared_type: Any = None) -> str:
t = (declared_type or '').strip().lower() if isinstance(declared_type, str) else ''
if t in ('stocks', 'stock', 'a', 'a股'):
return 'stocks'
if t in ('etf',):
return 'etf'
if t in ('lof',):
return 'lof'
# 经验规则兜底
code6 = _symbol_to_code6(symbol)
if code6.startswith(('15', '51', '56', '58')):
# 常见 ETF: 15/51/56/58
return 'etf'
if code6.startswith('16'):
return 'lof'
# 使用本地市场列表缓存做最终判断
try:
if _load_market_lists_from_disk(force=False):
code_prefixed = _to_prefixed_code(code6)
for it in market_list_cache.get('items', []):
if it.get('code') == code_prefixed:
it_type = (it.get('type') or '').lower()
if it_type in ('stocks', 'etf', 'lof'):
return it_type
except Exception:
pass
return 'stocks'
def _akshare_kline_daily(task: dict) -> 'pd.DataFrame':
import akshare as ak
symbol = task.get('symbol')
code6 = _symbol_to_code6(symbol)
sec_type = _infer_security_type(symbol, task.get('security_type'))
start_date = (task.get('effective_start_date') or task.get('start_date') or '').replace('-', '')
end_date = (task.get('effective_end_date') or task.get('end_date') or '').replace('-', '')
adjust = _map_adjust_flag_to_akshare(task.get('adjust_flag', '3'))
if sec_type == 'etf':
try:
df = ak.fund_etf_hist_em(symbol=code6, period='daily', start_date=start_date, end_date=end_date, adjust=adjust)
except Exception:
# Fallback: 腾讯行情接口 (web.ifzq.gtimg.cn)
import requests
prefix = 'sh' if code6.startswith(('5', '6', '9', '8')) else 'sz'
url = (f'https://web.ifzq.gtimg.cn/appstock/app/fqkline/get'
f'?_var=kline_dayqfq¶m={prefix}{code6},day,{start_date[:4]}-{start_date[4:6]}-{start_date[6:8]},'
f'{end_date[:4]}-{end_date[4:6]}-{end_date[6:8]},2000,qfq&r=0.1')
resp = requests.get(url, timeout=10)
raw = resp.text
import json
raw = raw[raw.index('{'):]
data = json.loads(raw)
klines = data['data'][f'{prefix}{code6}'].get('qfqday') or data['data'][f'{prefix}{code6}'].get('day') or []
rows = []
for k in klines:
rows.append({'日期': k[0], '开盘': k[1], '收盘': k[2], '最高': k[3], '最低': k[4], '成交量': k[5]})
df = pd.DataFrame(rows)
elif sec_type == 'lof':
df = ak.fund_lof_hist_em(symbol=code6, period='daily', start_date=start_date, end_date=end_date, adjust=adjust)
else:
df = ak.stock_zh_a_hist(symbol=code6, period='daily', start_date=start_date, end_date=end_date, adjust=adjust)
# 确保有代码列,便于后续插入"股票名称"列
if isinstance(df, pd.DataFrame) and not df.empty:
if '股票代码' not in df.columns and 'code' not in df.columns:
df.insert(0, '股票代码', code6)
return df
def _derive_canghai_exchange_code(symbol: str) -> str:
s = (symbol or '').strip().lower()
if s.startswith('sh.'):
return 'XSHG'
if s.startswith('sz.'):
return 'XSHE'
code6 = _symbol_to_code6(symbol)
if code6.startswith(('5', '6', '9')):
return 'XSHG'
return 'XSHE'
def _mask_token(token: Any) -> str:
t = '' if token is None else str(token)
if not t:
return ''
if len(t) <= 8:
return '***'
return f"{t[:4]}***{t[-4:]}"
def _canghai_etf_5min(task: dict) -> 'pd.DataFrame':
import requests
token = task.get('canghai_token') or task.get('token')
if not token:
raise RuntimeError('缺少沧海数据 token')
exchange_code = (task.get('exchange_code') or '').strip() or _derive_canghai_exchange_code(task.get('symbol', ''))
ticker = (task.get('ticker') or '').strip() or _symbol_to_code6(task.get('symbol', ''))
if not exchange_code:
raise RuntimeError('缺少沧海数据 exchange_code')
if not ticker:
raise RuntimeError('缺少沧海数据 ticker')
start_date = task.get('effective_start_date') or task.get('start_date')
end_date = task.get('effective_end_date') or task.get('end_date')
limit = task.get('limit')
order = task.get('order')
columns = task.get('columns')
url = f"https://www.tsanghi.com/api/fin/etf/{exchange_code}/5min"
params: dict[str, Any] = {
'token': token,
'ticker': ticker,
}
if start_date:
params['start_date'] = start_date
if end_date:
params['end_date'] = end_date
if limit is not None and str(limit).strip() != '':
params['limit'] = int(limit)
if order is not None and str(order).strip() != '':
params['order'] = int(order)
params['fmt'] = 'json'
resp = requests.get(url, params=params, timeout=30)
resp.raise_for_status()
payload = resp.json() if resp.content else {}
if not isinstance(payload, dict):
raise RuntimeError('沧海数据返回格式异常')
code = payload.get('code')
if code not in (200, '200', 0, '0', None):
raise RuntimeError(payload.get('msg') or f"沧海数据返回错误 code={code}")
rows = payload.get('data')
if rows is None:
rows = payload.get('rows')
if not isinstance(rows, list) or not rows:
raise RuntimeError('沧海数据未返回数据')
df = pd.DataFrame(rows)
if 'ticker' in df.columns and 'code' not in df.columns:
df.rename(columns={'ticker': 'code'}, inplace=True)
if 'date' in df.columns:
df['date'] = df['date'].astype(str)
if 'time' not in df.columns and 'date' in df.columns:
try:
df['time'] = df['date'].str.split(' ').str[-1]
except Exception:
pass
return df
def _canghai_minute_bars(task: dict) -> 'pd.DataFrame':
import requests
token = task.get('canghai_token') or task.get('token')
if not token:
raise RuntimeError('缺少沧海数据 token')
sec_type = _infer_security_type(task.get('symbol', ''), task.get('security_type'))
if sec_type not in ('stocks', 'etf'):
raise RuntimeError('沧海数据仅支持 股票/ETF')
data_type = str(task.get('data_type') or '').strip().lower()
if data_type not in ('5min', '15min', '30min', '60min'):
raise RuntimeError('沧海数据仅支持 5min/15min/30min/60min')
minutes = int(data_type.replace('min', ''))
symbol = task.get('symbol', '')
exchange_code = (task.get('exchange_code') or '').strip() or _derive_canghai_exchange_code(symbol)
ticker = (task.get('ticker') or '').strip() or _symbol_to_code6(symbol)
if not exchange_code:
raise RuntimeError('缺少沧海数据 exchange_code')
if not ticker:
raise RuntimeError('缺少沧海数据 ticker')
start_date = task.get('effective_start_date') or task.get('start_date')
end_date = task.get('effective_end_date') or task.get('end_date')
limit = task.get('limit')
order = task.get('order')
columns = task.get('columns')
product = 'etf' if sec_type == 'etf' else 'stock'
url = f"https://www.tsanghi.com/api/fin/{product}/{exchange_code}/{minutes}min"
params: dict[str, Any] = {
'token': token,
'ticker': ticker,
'fmt': 'json',
}
if start_date:
params['start_date'] = start_date
if end_date:
params['end_date'] = end_date
if limit is not None and str(limit).strip() != '':
params['limit'] = int(limit)
if order is not None and str(order).strip() != '':
params['order'] = int(order)
if columns is None or str(columns).strip() == '':
params['columns'] = 'ticker,date,open,high,low,close,volume,amount,pre_close'
else:
params['columns'] = str(columns)
masked = dict(params)
masked['token'] = _mask_token(masked.get('token'))
started = time.perf_counter()
print(f"🌊 沧海数据请求: GET {url} params={masked}")
resp = requests.get(url, params=params, timeout=30)
elapsed_ms = int((time.perf_counter() - started) * 1000)
print(f"🌊 沧海数据响应: status={resp.status_code} elapsed_ms={elapsed_ms} bytes={len(resp.content or b'')}")
resp.raise_for_status()
payload = resp.json() if resp.content else {}
if not isinstance(payload, dict):
raise RuntimeError('沧海数据返回格式异常')
code = payload.get('code')
if code not in (200, '200', 0, '0', None):
raise RuntimeError(payload.get('msg') or f"沧海数据返回错误 code={code}")
rows = payload.get('data')
if rows is None:
rows = payload.get('rows')
if not isinstance(rows, list) or not rows:
raise RuntimeError('沧海数据未返回数据')
df = pd.DataFrame(rows)
if 'ticker' in df.columns and 'code' not in df.columns:
df.rename(columns={'ticker': 'code'}, inplace=True)
for col in ('open', 'high', 'low', 'close', 'volume', 'amount', 'pre_close'):
if col in df.columns:
df[col] = pd.to_numeric(df[col], errors='coerce')
if 'date' in df.columns:
df['date'] = df['date'].astype(str)
try:
dt = pd.to_datetime(df['date'], errors='coerce')
if dt.notna().any():
df = df.assign(_dt=dt).sort_values('_dt').drop(columns=['_dt'])
except Exception:
pass
return df
def _canghai_daily_bars(task: dict) -> 'pd.DataFrame':
import requests
token = task.get('canghai_token') or task.get('token')
if not token:
raise RuntimeError('缺少沧海数据 token')
sec_type = _infer_security_type(task.get('symbol', ''), task.get('security_type'))
if sec_type not in ('stocks', 'etf'):
raise RuntimeError('沧海数据仅支持 股票/ETF')
symbol = task.get('symbol', '')
exchange_code = (task.get('exchange_code') or '').strip() or _derive_canghai_exchange_code(symbol)
ticker = (task.get('ticker') or '').strip() or _symbol_to_code6(symbol)
if not exchange_code:
raise RuntimeError('缺少沧海数据 exchange_code')
if not ticker:
raise RuntimeError('缺少沧海数据 ticker')
start_date = task.get('effective_start_date') or task.get('start_date')
end_date = task.get('effective_end_date') or task.get('end_date')
limit = task.get('limit')
order = task.get('order')
columns = task.get('columns')
product = 'etf' if sec_type == 'etf' else 'stock'
url = f"https://www.tsanghi.com/api/fin/{product}/{exchange_code}/daily"
params: dict[str, Any] = {
'token': token,
'ticker': ticker,
'fmt': 'json',
}
if start_date:
params['start_date'] = start_date
if end_date:
params['end_date'] = end_date
if limit is not None and str(limit).strip() != '':
params['limit'] = int(limit)
if order is not None and str(order).strip() != '':
params['order'] = int(order)
if columns is None or str(columns).strip() == '':
params['columns'] = 'ticker,date,open,high,low,close,volume,amount,pre_close'
else:
params['columns'] = str(columns)
masked = dict(params)
masked['token'] = _mask_token(masked.get('token'))
started = time.perf_counter()
print(f"🌊 沧海数据请求: GET {url} params={masked}")
resp = requests.get(url, params=params, timeout=30)
elapsed_ms = int((time.perf_counter() - started) * 1000)
print(f"🌊 沧海数据响应: status={resp.status_code} elapsed_ms={elapsed_ms} bytes={len(resp.content or b'')}")
resp.raise_for_status()
payload = resp.json() if resp.content else {}
if not isinstance(payload, dict):
raise RuntimeError('沧海数据返回格式异常')
code = payload.get('code')
if code not in (200, '200', 0, '0', None):
raise RuntimeError(payload.get('msg') or f"沧海数据返回错误 code={code}")
rows = payload.get('data')
if rows is None:
rows = payload.get('rows')
if not isinstance(rows, list) or not rows:
raise RuntimeError('沧海数据未返回数据')
df = pd.DataFrame(rows)
if 'ticker' in df.columns and 'code' not in df.columns:
df.rename(columns={'ticker': 'code'}, inplace=True)
for col in ('open', 'high', 'low', 'close', 'volume', 'amount', 'pre_close'):
if col in df.columns:
df[col] = pd.to_numeric(df[col], errors='coerce')
if 'date' in df.columns:
df['date'] = df['date'].astype(str)
try:
dt = pd.to_datetime(df['date'], errors='coerce')
if dt.notna().any():
df = df.assign(_dt=dt).sort_values('_dt').drop(columns=['_dt'])
except Exception:
pass
return df
def _akshare_kline_min(task: dict) -> 'pd.DataFrame':
"""AKShare 分时接口限制较多:通常只能获取近期分时数据。"""
import akshare as ak
symbol = task.get('symbol')
code6 = _symbol_to_code6(symbol)
sec_type = _infer_security_type(symbol, task.get('security_type'))
adjust = _map_adjust_flag_to_akshare(task.get('adjust_flag', '3'))
start_date = task.get('effective_start_date') or task.get('start_date')
end_date = task.get('effective_end_date') or task.get('end_date')
start_dt = f"{start_date} 09:30:00"
end_dt = f"{end_date} 15:00:00"
data_type = task.get('data_type')
period = '60' if data_type == '60min' else ('30' if data_type == '30min' else '5')
if sec_type == 'etf':
df = ak.fund_etf_hist_min_em(symbol=code6, period=period, adjust=adjust, start_date=start_dt, end_date=end_dt)
elif sec_type == 'lof':
df = ak.fund_lof_hist_min_em(symbol=code6, period=period, adjust=adjust, start_date=start_dt, end_date=end_dt)
else:
df = ak.stock_zh_a_hist_min_em(symbol=code6, start_date=start_dt, end_date=end_dt, period=period, adjust=adjust)
if isinstance(df, pd.DataFrame) and not df.empty:
if '股票代码' not in df.columns and 'code' not in df.columns:
df.insert(0, '股票代码', code6)
return df
def _clamp_date_str(date_str: str, min_date: str | None = None, max_date: str | None = None) -> tuple[str, bool]:
"""基于 YYYY-MM-DD 的字符串比较进行范围裁剪,返回 (裁剪后日期, 是否发生裁剪)。"""
changed = False
if not date_str:
return date_str, False
new_val = date_str
if min_date and new_val < min_date:
new_val = min_date
changed = True
if max_date and new_val > max_date:
new_val = max_date
changed = True
return new_val, changed
def _build_download_cache_key(task: dict) -> str:
return "|".join([
str(task.get('data_provider', 'baostock')),
str(task.get('symbol', '')),
str(task.get('data_type', '')),
str(task.get('effective_start_date', task.get('start_date', ''))),
str(task.get('effective_end_date', task.get('end_date', ''))),
str(task.get('adjust_flag', '')),
str(task.get('save_format', '')),
str(task.get('limit', '')),
str(task.get('order', '')),
str(task.get('columns', '')),
str(task.get('exchange_code', '')),
str(task.get('ticker', '')),
])
def _safe_filename(text: str) -> str:
# 简单文件名清洗,避免非法字符
invalid = '<>:/\\|?*\"'
out = ''.join('_' if ch in invalid else ch for ch in (text or ""))
return out.strip() or 'data'
async def create_download_task(data: dict):
"""创建下载任务"""
global download_task_counter
try:
safe_data = dict(data or {})
if 'canghai_token' in safe_data:
safe_data['canghai_token'] = _mask_token(safe_data.get('canghai_token'))
if 'token' in safe_data:
safe_data['token'] = _mask_token(safe_data.get('token'))
print(f"📥 收到下载请求: {safe_data}")
with _download_tasks_lock:
download_task_counter += 1
task_id = download_task_counter
# 解析请求参数
symbol = data.get('symbol', '')
stock_name = data.get('stock_name', '')
data_type = data.get('data_type', 'daily') # daily, 60min, 30min, 5min, basic_info
start_date = data.get('start_date', '')
end_date = data.get('end_date', '')
adjust_flag = data.get('adjust_flag', '3') # 1=前复权, 2=后复权, 3=不复权
save_format = data.get('save_format', 'excel') # csv, excel, json
security_type = data.get('security_type') or data.get('type')
data_provider = (data.get('data_provider') or 'baostock')
exchange_code = data.get('exchange_code')
ticker = data.get('ticker')
canghai_token = data.get('canghai_token') or data.get('token')
limit = data.get('limit')
order = data.get('order')
columns = data.get('columns')
if not symbol:
raise HTTPException(status_code=400, detail="请选择股票")
if not start_date or not end_date:
raise HTTPException(status_code=400, detail="请选择日期范围")
# 后端统一处理:结束日期默认不超过昨天,开始日期不早于 1990-01-01
yesterday = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d')
effective_start, start_changed = _clamp_date_str(start_date, min_date='1990-01-01')
effective_end, end_changed = _clamp_date_str(end_date, max_date=yesterday)
adjustments = []
if start_changed:
adjustments.append(f"开始日期已调整为 {effective_start}")
if end_changed:
adjustments.append(f"结束日期已调整为 {effective_end}")
if effective_start and effective_end and effective_start > effective_end:
raise HTTPException(status_code=400, detail="日期范围不合法:开始日期不能晚于结束日期")
provider_norm = str(data_provider or 'baostock').strip().lower()
inferred_sec_type = _infer_security_type(symbol, security_type)
if provider_norm == 'canghai':
if not str(canghai_token or '').strip():
raise HTTPException(status_code=400, detail='沧海数据:缺少 token')
if inferred_sec_type not in ('stocks', 'etf'):
raise HTTPException(status_code=400, detail='沧海数据仅支持 股票/ETF')
if data_type not in ('daily', '5min', '15min', '30min', '60min'):
raise HTTPException(status_code=400, detail='沧海数据仅支持 daily/5min/15min/30min/60min')
exchange_code = (exchange_code or '').strip() or _derive_canghai_exchange_code(symbol)
ticker = (ticker or '').strip() or _symbol_to_code6(symbol)
try:
start_dt = datetime.strptime(effective_start, '%Y-%m-%d')
end_dt = datetime.strptime(effective_end, '%Y-%m-%d')
if abs((end_dt - start_dt).days) > 366:
raise HTTPException(status_code=400, detail='沧海数据:start_date 和 end_date 最大跨度为 1 年')
except HTTPException:
raise
except Exception:
raise HTTPException(status_code=400, detail='沧海数据:日期格式错误,应为 YYYY-MM-DD')
if provider_norm == 'efinance':
if data_type not in ('daily', '5min', '15min', '30min', '60min'):
raise HTTPException(status_code=400, detail='efinance 仅支持 daily/5min/15min/30min/60min')
# 创建任务
task = {
'id': task_id,
'data_provider': data_provider,
'symbol': symbol,
'stock_name': stock_name,
'data_type': data_type,
'security_type': inferred_sec_type,
'start_date': start_date,
'end_date': end_date,
'effective_start_date': effective_start,
'effective_end_date': effective_end,
'adjust_flag': adjust_flag,
'save_format': save_format,
'limit': limit,
'order': order,
'columns': columns,
'exchange_code': exchange_code,
'ticker': ticker,
'canghai_token': canghai_token,
'status': 'downloading',
'stage': 'queued',
'progress': 0,
'message': '任务已创建,等待执行',
'created_at': datetime.now().isoformat(),
'started_at': None,
'completed_at': None,
'data': None,
'data_count': 0,
'file_path': None,
'cache_key': None,
'cached': False,
'adjustments': adjustments,
'error': None
}
task['cache_key'] = _build_download_cache_key(task)
# 缓存命中:直接返回已完成任务(无需再次请求数据源)
backend_dir = os.path.dirname(__file__)
cache_dir = os.path.join(backend_dir, 'cache', 'downloads')
os.makedirs(cache_dir, exist_ok=True)
cache_index_path = os.path.join(cache_dir, 'index.json')
cache_index = {}
try:
if os.path.exists(cache_index_path):
with open(cache_index_path, 'r', encoding='utf-8') as f:
cache_index = json.load(f) or {}
except Exception:
cache_index = {}
existing_path = cache_index.get(task['cache_key'])
if existing_path and os.path.exists(existing_path):
task['status'] = 'completed'
task['stage'] = 'completed'
task['progress'] = 100
task['message'] = '缓存命中:已直接返回本地缓存文件'
task['cached'] = True
task['file_path'] = existing_path
task['started_at'] = task.get('created_at')
task['completed_at'] = datetime.now().isoformat()
with _download_tasks_lock:
download_tasks[task_id] = task
_save_download_history()
print(f"✅ 任务 {task_id} 缓存命中: {existing_path}")
return task
with _download_tasks_lock:
download_tasks[task_id] = task
_save_download_history()
print(f"✅ 创建下载任务 {task_id}: {symbol} ({data_type})")
DOWNLOAD_EXECUTOR.submit(_run_download_coroutine, task_id)
return task
except HTTPException:
raise
except Exception as e:
print(f"❌ 创建下载任务失败: {e}")
import traceback
traceback.print_exc()
raise HTTPException(status_code=500, detail=str(e))
async def download_stock_data(task_id: int):
"""异步下载股票数据"""
try:
with _download_tasks_lock:
task = download_tasks.get(task_id)
if not task:
return
print(f"📥 开始下载任务 {task_id}: {task['symbol']} ({task['data_type']})")
if not task.get('started_at'):
task['started_at'] = datetime.now().isoformat()
task['stage'] = 'initializing'
task['progress'] = 5
task['message'] = '初始化下载任务'
data_type = task['data_type']
provider = (task.get('data_provider') or 'baostock').strip().lower()
if provider == 'canghai':
sec_type = _infer_security_type(task.get('symbol', ''), task.get('security_type'))
task['security_type'] = sec_type
if sec_type not in ('stocks', 'etf'):
task['status'] = 'failed'
task['stage'] = 'failed'
task['progress'] = 100
task['error'] = '沧海数据仅支持 股票/ETF'
_save_download_history()
return
if data_type not in ('daily', '5min', '15min', '30min', '60min'):
task['status'] = 'failed'
task['stage'] = 'failed'
task['progress'] = 100
task['error'] = '沧海数据仅支持 daily/5min/15min/30min/60min'
_save_download_history()
return
task['stage'] = 'query'
task['progress'] = 35
task['message'] = f"沧海数据 获取 {('ETF' if sec_type == 'etf' else '股票')} {data_type} 数据"
if data_type == 'daily':
df = _canghai_daily_bars(task)
else:
df = _canghai_minute_bars(task)
task['stage'] = 'saving'
task['progress'] = 85
task['message'] = '正在保存文件'
await _persist_download_result(task_id, df)
return
if provider == 'efinance':
if data_type not in ('daily', '5min', '15min', '30min', '60min'):
task['status'] = 'failed'
task['stage'] = 'failed'
task['progress'] = 100
task['error'] = 'efinance 仅支持 daily/5min/15min/30min/60min'
_save_download_history()
return
task['stage'] = 'query'
task['progress'] = 35
task['message'] = f"efinance 获取 {data_type} 数据"
try:
df = _efinance_kline(task)
if df is None or not isinstance(df, pd.DataFrame) or df.empty:
raise RuntimeError('efinance 未获取到数据')
except Exception as e:
task['status'] = 'failed'
task['stage'] = 'failed'
task['progress'] = 100
msg = str(e)
if 'No module named' in msg and 'efinance' in msg:
msg = '缺少 efinance 模块,请安装:pip install efinance'
task['error'] = f"efinance 获取数据失败: {msg}"
_save_download_history()
return
task['stage'] = 'saving'
task['progress'] = 85
task['message'] = '正在保存文件'
await _persist_download_result(task_id, df)
return
# basic_info 优先使用 AKShare
if data_type == 'basic_info':
task['stage'] = 'fetching_basic_info'
task['progress'] = 30
task['message'] = '正在获取基本信息'
symbol_raw = task.get('symbol', '')
symbol_6 = symbol_raw
if symbol_6.lower().startswith(('sh.', 'sz.')):
symbol_6 = symbol_6.split('.', 1)[1]
symbol_6 = ''.join(ch for ch in symbol_6 if ch.isdigit())
result = None
if provider == 'baostock':
try:
import baostock as bs
with _baostock_lock:
lg = bs.login()
if lg.error_code != '0':
raise Exception(lg.error_msg)
bs_symbol = _normalize_symbol_for_baostock(symbol_raw)
rs = bs.query_stock_basic(code=bs_symbol)
data_list = []
while (rs.error_code == '0') & rs.next():
data_list.append(rs.get_row_data())
result = pd.DataFrame(data_list, columns=rs.fields)
bs.logout()
task['message'] = 'Baostock 获取基本信息成功'
except Exception as e:
try:
with _baostock_lock:
bs.logout()
except Exception:
pass
task['status'] = 'failed'
task['stage'] = 'failed'
task['progress'] = 100
task['error'] = f"Baostock 获取基本信息失败: {e}"
_save_download_history()
return
else:
try:
import akshare as ak
df_info = ak.stock_individual_info_em(symbol=symbol_6)
result = df_info
task['message'] = 'AKShare 获取基本信息成功'
except Exception as e:
if provider == 'akshare':
task['status'] = 'failed'
task['stage'] = 'failed'
task['progress'] = 100
task['error'] = f"AKShare 获取基本信息失败: {e}"
_save_download_history()
return
print(f"⚠️ AKShare 获取基本信息失败,尝试 Baostock 兜底: {e}")
try:
import baostock as bs
with _baostock_lock:
lg = bs.login()
if lg.error_code != '0':
raise Exception(lg.error_msg)
bs_symbol = _normalize_symbol_for_baostock(symbol_raw)
rs = bs.query_stock_basic(code=bs_symbol)
data_list = []
while (rs.error_code == '0') & rs.next():
data_list.append(rs.get_row_data())
result = pd.DataFrame(data_list, columns=rs.fields)
bs.logout()
task['message'] = 'Baostock 获取基本信息成功(兜底)'
except Exception as e2:
task['status'] = 'failed'
task['stage'] = 'failed'
task['progress'] = 100
task['error'] = f"获取基本信息失败: {e2}"
_save_download_history()
return
# 统一落盘
task['progress'] = 80
task['stage'] = 'saving'
task['message'] = '正在保存文件'
await _persist_download_result(task_id, result)
return
# K线类:优先 Baostock,失败/无数据才回退 AKShare
sec_type = _infer_security_type(task.get('symbol', ''), task.get('security_type'))
task['security_type'] = sec_type
data_type = task.get('data_type')
# Baostock 优先,失败再用 AKShare(auto 模式)
bs = None
try:
import baostock as bs # type: ignore
except Exception:
bs = None
if provider == 'baostock' and bs is None:
task['status'] = 'failed'
task['stage'] = 'failed'
task['progress'] = 100
task['error'] = '缺少 baostock 模块,请安装:pip install baostock'
_save_download_history()
return
result = None
used_baostock = False
if provider != 'akshare' and bs is not None and data_type in ('daily', '60min', '30min', '5min'):
try:
symbol_bs = _normalize_symbol_for_baostock(task.get('symbol', ''))
data_type = task.get('data_type', 'daily')
if data_type == 'daily':
frequency = 'd'
fields = 'date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST'
elif data_type == '60min':
frequency = '60'
fields = 'date,time,code,open,high,low,close,volume,amount,adjustflag'
elif data_type == '30min':
frequency = '30'
fields = 'date,time,code,open,high,low,close,volume,amount,adjustflag'
elif data_type == '5min':
frequency = '5'
fields = 'date,time,code,open,high,low,close,volume,amount,adjustflag'
else:
frequency = 'd'
fields = 'date,code,open,high,low,close,volume,amount,adjustflag'
start_date_str = task.get('effective_start_date') or task.get('start_date')
end_date_str = task.get('effective_end_date') or task.get('end_date')
# Baostock 不支持 ETF/LOF 分钟线
if frequency in ('5', '30', '60') and sec_type in ('etf', 'lof'):
if provider == 'baostock':
raise RuntimeError('Baostock 不支持 ETF/LOF 分钟线,请改用其它数据源(如 AKShare)')
raise RuntimeError('skip_baostock_etf_lof_min')
# 分钟线起始时间不早于 2019-01-01
if frequency in ('5', '30', '60'):
try:
_min_floor = datetime(2019, 1, 1)
_sdt = datetime.strptime(str(start_date_str), '%Y-%m-%d')
if _sdt < _min_floor:
start_date_str = '2019-01-01'
except Exception:
# 解析失败则用字符串兜底
if str(start_date_str) < '2019-01-01':
start_date_str = '2019-01-01'
print(f"📅 查询日期范围: {start_date_str} 至 {end_date_str}")
print(f"🔄 查询参数: symbol={symbol_bs}, frequency={frequency}, adjustflag={task.get('adjust_flag')}")
task['stage'] = 'query'
task['progress'] = 35
task['message'] = f"Baostock 查询: {start_date_str} 至 {end_date_str}"
loop = asyncio.get_running_loop()
last_err = None
rows = None
flds = None
payload = (
str(symbol_bs),
str(fields),
str(start_date_str),
str(end_date_str),
str(frequency),
str(task.get('adjust_flag', '3')),
)
for attempt in range(BAOSTOCK_MAX_RETRIES + 1):
try:
rows, flds = await loop.run_in_executor(
BAOSTOCK_SEGMENT_EXECUTOR,
_baostock_segment_worker,
payload,
)
break
except Exception as e:
last_err = e
if attempt >= BAOSTOCK_MAX_RETRIES:
raise
await asyncio.sleep(0.5)
if not rows:
raise RuntimeError('未获取到数据')
result = pd.DataFrame(rows, columns=(flds or fields.split(',')))
try:
if 'date' in result.columns and 'time' in result.columns:
result = result.sort_values(['date', 'time']).reset_index(drop=True)
elif 'date' in result.columns:
result = result.sort_values(['date']).reset_index(drop=True)
except Exception:
pass
used_baostock = True
except Exception as e:
if str(e) == 'skip_baostock_etf_lof_min':
used_baostock = False
result = None
task['message'] = 'Baostock 不支持 ETF/LOF 分钟线,尝试 AKShare 兜底'
else:
used_baostock = False
result = None
if provider == 'baostock':
task['status'] = 'failed'
task['stage'] = 'failed'
task['progress'] = 100
task['error'] = f"Baostock 获取数据失败: {e}"
_save_download_history()
return
task['message'] = f"Baostock 失败,尝试 AKShare 兜底: {e}"
if result is None:
try:
task['stage'] = 'query'
task['progress'] = 35
if data_type == 'daily':
task['message'] = 'AKShare 获取日线数据(兜底)'
result = _akshare_kline_daily(task)
elif data_type in ('60min', '30min', '5min'):
task['message'] = 'AKShare 获取分时数据(兜底,仅支持近期)'
result = _akshare_kline_min(task)
else:
raise RuntimeError('不支持的数据类型')
if result is None or result.empty:
raise RuntimeError('AKShare 未获取到数据')
except Exception as e:
task['status'] = 'failed'
task['stage'] = 'failed'
task['progress'] = 100
msg = str(e)
if 'No module named' in msg and 'akshare' in msg:
if bs is None:
msg = '未安装 baostock 与 akshare,无法下载数据(请优先安装:pip install baostock;可选安装:pip install akshare)'
else:
msg = '未安装 akshare,无法使用 AKShare 兜底(可选安装:pip install akshare)'
task['error'] = f"数据获取失败(Baostock/AKShare): {msg}"
_save_download_history()
return
task['stage'] = 'saving'
task['progress'] = 85
task['message'] = '正在保存文件'
await _persist_download_result(task_id, result)
except Exception as e:
print(f"❌ 下载任务 {task_id} 失败: {e}")
import traceback
traceback.print_exc()
task = download_tasks.get(task_id)
if task:
task['status'] = 'failed'
task['stage'] = 'failed'
task['progress'] = 100
task['error'] = str(e)
_save_download_history()
async def _persist_download_result(task_id: int, df: 'pd.DataFrame'):
task = download_tasks.get(task_id)
if task is None:
return
try:
def _infer_data_range(dataframe: 'pd.DataFrame') -> tuple[str | None, str | None]:
if dataframe is None or not isinstance(dataframe, pd.DataFrame) or dataframe.empty:
return None, None
date_col = None
for c in ('日期', 'date', 'Date', 'datetime', '时间'):
if c in dataframe.columns:
date_col = c
break
if date_col is None:
return None, None
time_col = None
for c in ('时间', 'time'):
if c in dataframe.columns:
time_col = c
break
def _normalize_time_str(val: str) -> str:
t = (val or '').strip()
if not t:
return t
if t.isdigit():
if len(t) == 6:
return f"{t[0:2]}:{t[2:4]}:{t[4:6]}"
if len(t) == 4:
return f"{t[0:2]}:{t[2:4]}:00"
if len(t) == 2:
return f"{t}:00:00"
return t
try:
if time_col and date_col != time_col:
time_series = dataframe[time_col].astype(str).map(_normalize_time_str)
series = pd.to_datetime(
dataframe[date_col].astype(str).str.strip() + ' ' + time_series.astype(str).str.strip(),
errors='coerce'
)
else:
series = pd.to_datetime(dataframe[date_col].astype(str).str.strip(), errors='coerce')
except Exception:
return None, None
series = series.dropna()
if series.empty:
# 若时间列导致解析失败,回退到仅用日期列推断范围
try:
series = pd.to_datetime(dataframe[date_col].astype(str).str.strip(), errors='coerce').dropna()
except Exception:
return None, None
if series.empty:
return None, None
dt_min = series.min()
dt_max = series.max()
if time_col and date_col != time_col:
return dt_min.strftime('%Y-%m-%d %H:%M:%S'), dt_max.strftime('%Y-%m-%d %H:%M:%S')
return dt_min.strftime('%Y-%m-%d'), dt_max.strftime('%Y-%m-%d')
def _standardize_download_dataframe(df: 'pd.DataFrame') -> 'pd.DataFrame':
"""标准化数据下载输出列:
1) 若日期列包含时间信息,拆分为"日期""时间"
2) 列顺序固定为:股票代码、股票名称、日期、时间(如存在)、开盘价、最高价、最低价、收盘价
3) 其它列统一放到收盘价之后
"""
if df is None or df.empty:
return df
df = df.copy()
# 兼容中英文列名(在COLUMN_MAP应用前后都可工作)
rename_candidates = {
'code': '股票代码',
'symbol': '股票代码',
'股票代码': '股票代码',
'code_name': '股票名称',
'name': '股票名称',
'股票名称': '股票名称',
'date': '日期',
'日期': '日期',
'time': '时间',
'时间': '时间',
'open': '开盘价',
'开盘': '开盘价',
'开盘价': '开盘价',
'high': '最高价',
'最高': '最高价',
'最高价': '最高价',
'low': '最低价',
'最低': '最低价',
'最低价': '最低价',
'close': '收盘价',
'收盘': '收盘价',
'收盘价': '收盘价',
}
df.rename(columns={k: v for k, v in rename_candidates.items() if k in df.columns}, inplace=True)
# 拆分"日期/时间"
if '日期' in df.columns:
date_series = df['日期']
# 若已有"时间"列,优先保留;否则判断"日期"是否包含时间信息
if '时间' not in df.columns:
try:
as_str = date_series.astype(str)
has_time = as_str.str.contains(r'\d{1,2}:\d{2}', regex=True, na=False).any()
except Exception:
has_time = False
if has_time:
dt = pd.to_datetime(date_series.astype(str), errors='coerce')
if dt.notna().any():
df['日期'] = dt.dt.strftime('%Y-%m-%d')
df.insert(df.columns.get_loc('日期') + 1, '时间', dt.dt.strftime('%H:%M:%S'))
else:
# 同时存在"日期""时间"时,确保"日期"仅保留日期部分
try:
dt = pd.to_datetime(date_series.astype(str), errors='coerce')
if dt.notna().any():
df['日期'] = dt.dt.strftime('%Y-%m-%d')
except Exception:
pass
base_order = ['股票代码', '股票名称', '日期', '时间', '开盘价', '最高价', '最低价', '收盘价']
if '时间' not in df.columns:
base_order = [c for c in base_order if c != '时间']
ordered = [c for c in base_order if c in df.columns]
extras = [c for c in df.columns if c not in ordered]
return df[ordered + extras]
# 定义列名中英文字典
COLUMN_MAP = {
'date': '日期',
'time': '时间',
'code': '股票代码',
'open': '开盘价',
'high': '最高价',
'low': '最低价',
'close': '收盘价',
'volume': '成交量',
'amount': '成交额',
'pre_close': '昨收价',
'adjustflag': '复权状态',
'turn': '换手率',
'tradestatus': '交易状态',
'pctChg': '涨跌幅',
'peTTM': '市盈率TTM',
'pbMRQ': '市净率',
'psTTM': '市销率TTM',
'pcfNcfTTM': '市现率TTM',
'isST': '是否ST',
'code_name': '股票名称',
'ipoDate': '上市日期',
'outDate': '退市日期',
'type': '股票类型',
'status': '上市状态',
# AKShare a stock_individual_info_em
'item': '项目',
'value': '值',
}
# 应用列名翻译
df.rename(columns=COLUMN_MAP, inplace=True)
data_range_start, data_range_end = _infer_data_range(df)
task['data_range_start'] = data_range_start
task['data_range_end'] = data_range_end
# 插入股票名称列
if '股票代码' in df.columns:
# 获取股票代码列的位置
try:
code_col_index = df.columns.get_loc('股票代码')
stock_name_val = task.get('stock_name', '')
# 若已存在"股票名称"(例如 efinance 返回自带该列),则不要重复 insert
if '股票名称' not in df.columns:
# 插入股票名称
df.insert(code_col_index + 1, '股票名称', stock_name_val)
else:
# 仅在原值为空时用任务里的 stock_name 补齐,避免覆盖数据源自带的名称
if stock_name_val:
try:
s = df['股票名称']
empty_mask = s.isna() | (s.astype(str).str.strip() == '')
if empty_mask.any():
df.loc[empty_mask, '股票名称'] = stock_name_val
except Exception:
pass
except KeyError:
pass # 如果找不到列,则不插入
# 翻译复权状态的值
if '复权状态' in df.columns:
adjust_map = {
'1': '前复权',
'2': '后复权',
'3': '不复权',
}
# 使用 .astype(str) 确保即使是数字也能匹配
df['复权状态'] = df['复权状态'].astype(str).map(adjust_map).fillna(df['复权状态'])
# 统一标准化列顺序(CSV/Excel/JSON)
df = _standardize_download_dataframe(df)
backend_dir = os.path.dirname(__file__)
cache_dir = os.path.join(backend_dir, 'cache', 'downloads')
os.makedirs(cache_dir, exist_ok=True)
save_format = task.get('save_format', 'csv')
stock_name = task.get('stock_name') or task.get('symbol') or 'data'
data_type = task.get('data_type', 'daily')
start_date = task.get('effective_start_date') or task.get('start_date')
end_date = task.get('effective_end_date') or task.get('end_date')
extension = 'xlsx' if save_format == 'excel' else save_format
filename = f"{_safe_filename(stock_name)}_{data_type}_{start_date}_{end_date}.{extension}"
file_path = os.path.join(cache_dir, filename)
if save_format == 'csv':
df.to_csv(file_path, index=False, encoding='utf-8-sig')
elif save_format == 'excel':
df.to_excel(file_path, index=False, engine='openpyxl')
elif save_format == 'json':
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(df.to_dict('records'), f, ensure_ascii=False, indent=2)
else:
raise ValueError('不支持的格式')
try:
task['file_size_bytes'] = int(os.path.getsize(file_path)) if os.path.exists(file_path) else None
except Exception:
task['file_size_bytes'] = None
task['file_path'] = file_path
task['data'] = None
task['data_count'] = int(len(df))
task['status'] = 'completed'
task['stage'] = 'completed'
task['progress'] = 100
task['message'] = '下载完成'
task['completed_at'] = datetime.now().isoformat()
_save_download_history()
cache_index_path = os.path.join(cache_dir, 'index.json')
cache_index = {}
try:
if os.path.exists(cache_index_path):
with open(cache_index_path, 'r', encoding='utf-8') as f:
cache_index = json.load(f) or {}
except Exception:
cache_index = {}
cache_key = task.get('cache_key')
if cache_key:
cache_index[cache_key] = file_path
try:
with open(cache_index_path, 'w', encoding='utf-8') as f:
json.dump(cache_index, f, ensure_ascii=False, indent=2)
except Exception:
pass
print(f"✅ 任务 {task_id} 文件已保存: {file_path}")
except Exception as e:
task['status'] = 'failed'
task['stage'] = 'failed'
task['progress'] = 100
task['error'] = f"保存文件失败: {e}"
print(f"❌ 任务 {task_id} 保存失败: {e}")
_save_download_history()
def _infer_data_range_from_file(file_path: str) -> tuple[str | None, str | None]:
try:
if not file_path or not os.path.exists(file_path):
return None, None
ext = os.path.splitext(file_path)[1].lower().lstrip('.')
def _normalize_time_str(val: str) -> str:
t = (val or '').strip()
if not t:
return t
if t.isdigit():
if len(t) == 6:
return f"{t[0:2]}:{t[2:4]}:{t[4:6]}"
if len(t) == 4:
return f"{t[0:2]}:{t[2:4]}:00"
if len(t) == 2:
return f"{t}:00:00"
return t
def _infer_from_df(df: 'pd.DataFrame') -> tuple[str | None, str | None]:
if df is None or not isinstance(df, pd.DataFrame) or df.empty:
return None, None
date_col = None
for c in ('日期', 'date', 'Date', 'datetime', '时间'):
if c in df.columns:
date_col = c
break
if date_col is None:
return None, None
time_col = None
for c in ('时间', 'time'):
if c in df.columns:
time_col = c
break
try:
if time_col and date_col != time_col:
time_series = df[time_col].astype(str).map(_normalize_time_str)
series = pd.to_datetime(
df[date_col].astype(str).str.strip() + ' ' + time_series.astype(str).str.strip(),
errors='coerce'
)
else:
series = pd.to_datetime(df[date_col].astype(str).str.strip(), errors='coerce')
except Exception:
return None, None
series = series.dropna()
if series.empty:
try:
series = pd.to_datetime(df[date_col].astype(str).str.strip(), errors='coerce').dropna()
except Exception:
return None, None
if series.empty:
return None, None
dt_min = series.min()
dt_max = series.max()
if time_col and date_col != time_col:
return dt_min.strftime('%Y-%m-%d %H:%M:%S'), dt_max.strftime('%Y-%m-%d %H:%M:%S')
return dt_min.strftime('%Y-%m-%d'), dt_max.strftime('%Y-%m-%d')
if ext == 'csv':
head = pd.read_csv(file_path, nrows=0, encoding='utf-8-sig')
cols = list(head.columns)
date_col = next((c for c in ('日期', 'date', 'Date', 'datetime') if c in cols), None)
time_col = next((c for c in ('时间', 'time') if c in cols), None)
usecols = [c for c in (date_col, time_col) if c]
if not usecols:
return None, None
df = pd.read_csv(file_path, usecols=usecols, dtype=str, encoding='utf-8-sig')
return _infer_from_df(df)
if ext in ('xlsx', 'xls'):
head = pd.read_excel(file_path, nrows=0, engine='openpyxl')
cols = list(head.columns)
date_col = next((c for c in ('日期', 'date', 'Date', 'datetime') if c in cols), None)
time_col = next((c for c in ('时间', 'time') if c in cols), None)
usecols = [c for c in (date_col, time_col) if c]
if not usecols:
return None, None
df = pd.read_excel(file_path, usecols=usecols, dtype=str, engine='openpyxl')
return _infer_from_df(df)
if ext == 'json':
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
if not isinstance(data, list) or not data:
return None, None
df = pd.DataFrame(data)
return _infer_from_df(df)
return None, None
except Exception:
return None, None
async def list_download_history():
with _download_tasks_lock:
tasks = list(download_tasks.values())
tasks.sort(key=lambda t: str(t.get('created_at') or ''), reverse=True)
changed = False
for t in tasks:
try:
if (not t.get('data_range_start') or not t.get('data_range_end')) and t.get('file_path') and os.path.exists(t.get('file_path')):
s, e = _infer_data_range_from_file(t.get('file_path'))
if s and e:
with _download_tasks_lock:
# t 是引用,直接更新即可;加锁保证并发安全
t['data_range_start'] = s
t['data_range_end'] = e
changed = True
except Exception:
pass
if changed:
_save_download_history()
def _iso_to_dt(v: str | None) -> datetime | None:
if not v:
return None
try:
return datetime.fromisoformat(str(v))
except Exception:
return None
return [
{
'id': t.get('id'),
'data_provider': t.get('data_provider') or 'baostock',
'symbol': t.get('symbol'),
'stock_name': t.get('stock_name'),
'data_type': t.get('data_type'),
'security_type': t.get('security_type'),
'start_date': t.get('start_date'),
'end_date': t.get('end_date'),
'effective_start_date': t.get('effective_start_date'),
'effective_end_date': t.get('effective_end_date'),
'data_range_start': t.get('data_range_start'),
'data_range_end': t.get('data_range_end'),
'adjust_flag': t.get('adjust_flag'),
'save_format': t.get('save_format'),
'limit': t.get('limit'),
'order': t.get('order'),
'columns': t.get('columns'),
'exchange_code': t.get('exchange_code'),
'ticker': t.get('ticker'),
'file_size_bytes': t.get('file_size_bytes'),
'status': t.get('status'),
'stage': t.get('stage'),
'progress': t.get('progress', 0),
'message': t.get('message'),
'created_at': t.get('created_at'),
'started_at': t.get('started_at'),
'completed_at': t.get('completed_at'),
'download_seconds': (
(lambda s, e: int((e - s).total_seconds()) if s and e else None)(
_iso_to_dt(t.get('started_at')),
_iso_to_dt(t.get('completed_at')),
)
),
'error': t.get('error'),
'data_count': t.get('data_count', 0),
'cached': t.get('cached', False),
'file_path': t.get('file_path'),
'adjustments': t.get('adjustments', []),
}
for t in tasks
]
async def delete_download_history(task_id: int, request: Request, delete_file: bool = False):
with _download_tasks_lock:
if task_id not in download_tasks:
raise HTTPException(status_code=404, detail='任务不存在')
task = download_tasks.pop(task_id)
if delete_file:
try:
file_path = task.get('file_path')
if file_path and os.path.exists(file_path):
os.remove(file_path)
cache_key = task.get('cache_key')
if cache_key:
backend_dir = os.path.dirname(__file__)
cache_dir = os.path.join(backend_dir, 'cache', 'downloads')
cache_index_path = os.path.join(cache_dir, 'index.json')
if os.path.exists(cache_index_path):
with open(cache_index_path, 'r', encoding='utf-8') as f:
cache_index = json.load(f) or {}
if cache_index.get(cache_key) == file_path:
cache_index.pop(cache_key, None)
with open(cache_index_path, 'w', encoding='utf-8') as f:
json.dump(cache_index, f, ensure_ascii=False, indent=2)
except Exception:
pass
_save_download_history()
return {'deleted': task_id}
async def batch_delete_download_history(payload: dict):
ids = payload.get('ids') or []
delete_file = bool(payload.get('delete_file', False))
deleted = []
not_found = []
for raw_id in ids:
try:
tid = int(raw_id)
except Exception:
continue
with _download_tasks_lock:
if tid not in download_tasks:
not_found.append(tid)
continue
task = download_tasks.pop(tid)
deleted.append(tid)
if delete_file:
try:
file_path = task.get('file_path')
if file_path and os.path.exists(file_path):
os.remove(file_path)
cache_key = task.get('cache_key')
if cache_key:
backend_dir = os.path.dirname(__file__)
cache_dir = os.path.join(backend_dir, 'cache', 'downloads')
cache_index_path = os.path.join(cache_dir, 'index.json')
if os.path.exists(cache_index_path):
with open(cache_index_path, 'r', encoding='utf-8') as f:
cache_index = json.load(f) or {}
if cache_index.get(cache_key) == file_path:
cache_index.pop(cache_key, None)
with open(cache_index_path, 'w', encoding='utf-8') as f:
json.dump(cache_index, f, ensure_ascii=False, indent=2)
except Exception:
pass
_save_download_history()
return {'deleted': deleted, 'not_found': not_found}
async def get_download_status(task_id: int):
"""获取下载任务状态"""
try:
with _download_tasks_lock:
if task_id not in download_tasks:
raise HTTPException(status_code=404, detail="任务不存在")
task = download_tasks[task_id]
if (not task.get('data_range_start') or not task.get('data_range_end')) and task.get('file_path') and os.path.exists(task.get('file_path')):
s, e = _infer_data_range_from_file(task.get('file_path'))
if s and e:
with _download_tasks_lock:
task['data_range_start'] = s
task['data_range_end'] = e
_save_download_history()
# 返回任务信息(不包含完整数据)
return {
'id': task['id'],
'data_provider': task.get('data_provider') or 'baostock',
'symbol': task['symbol'],
'stock_name': task['stock_name'],
'data_type': task['data_type'],
'start_date': task['start_date'],
'end_date': task['end_date'],
'effective_start_date': task.get('effective_start_date'),
'effective_end_date': task.get('effective_end_date'),
'data_range_start': task.get('data_range_start'),
'data_range_end': task.get('data_range_end'),
'adjust_flag': task['adjust_flag'],
'save_format': task['save_format'],
'limit': task.get('limit'),
'order': task.get('order'),
'columns': task.get('columns'),
'exchange_code': task.get('exchange_code'),
'ticker': task.get('ticker'),
'file_size_bytes': task.get('file_size_bytes'),
'status': task['status'],
'stage': task.get('stage'),
'progress': task.get('progress', 0),
'message': task.get('message'),
'created_at': task['created_at'],
'completed_at': task.get('completed_at'),
'error': task.get('error'),
'data_count': task.get('data_count', len(task['data']) if task.get('data') else 0),
'cached': task.get('cached', False),
'file_path': task.get('file_path'),
'adjustments': task.get('adjustments', [])
}
except Exception as e:
print(f"获取下载状态失败: {e}")
raise HTTPException(status_code=500, detail=str(e))
async def download_file(task_id: int, format: str = None):
"""下载数据文件"""
try:
if task_id not in download_tasks:
raise HTTPException(status_code=404, detail="任务不存在")
task = download_tasks[task_id]
if task['status'] != 'completed':
raise HTTPException(status_code=400, detail="任务未完成或已失败")
# 优先返回已落盘文件
file_path = task.get('file_path')
if file_path and os.path.exists(file_path):
from fastapi.responses import FileResponse
return FileResponse(
file_path,
media_type='application/octet-stream',
filename=os.path.basename(file_path)
)
if not task.get('data'):
raise HTTPException(status_code=400, detail="没有可下载的数据")
# 使用指定格式或任务中的格式
save_format = format or task['save_format']
# 转换数据为DataFrame
df = pd.DataFrame(task['data'])
# 统一标准化列顺序(与落盘保存一致)
try:
df = _standardize_download_dataframe(df)
except Exception:
pass
# 根据格式导出
if save_format == 'csv':
output = io.StringIO()
df.to_csv(output, index=False, encoding='utf-8')
content = output.getvalue().encode('utf-8')
media_type = 'text/csv'
filename = f"{task['stock_name']}_{task['data_type']}.csv"
elif save_format == 'excel':
output = io.BytesIO()
df.to_excel(output, index=False, engine='openpyxl')
content = output.getvalue()
media_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
filename = f"{task['stock_name']}_{task['data_type']}.xlsx"
elif save_format == 'json':
content = json.dumps(task['data'], ensure_ascii=False, indent=2).encode('utf-8')
media_type = 'application/json'
filename = f"{task['stock_name']}_{task['data_type']}.json"
else:
raise HTTPException(status_code=400, detail="不支持的格式")
from fastapi.responses import StreamingResponse
return StreamingResponse(
iter([content]),
media_type=media_type,
headers={"Content-Disposition": f"attachment; filename={filename}"}
)
except Exception as e:
print(f"下载文件失败: {e}")
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
print("=" * 50)
print("启动回测系统后端服务...")
print("服务地址: http://localhost:8000")
print("API文档: http://localhost:8000/docs")
print("=" * 50)
uvicorn.run(app, host="0.0.0.0", port=8000, reload=False)
|