log
2026-07-23 8e0ca9e4e2dc7bd766f7ec26598d7a1a217b98c3
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
#!/usr/bin/env python3
"""
A股量化系统 - 主入口
统一命令行管理系统
 
层级结构(按量化流程五阶段):
1. 数据工程 → 2. 因子研发 → 3. 策略开发 → 4. 回测验证 → 5. 实盘工程 → 6. 系统管理
"""
 
import sys
import os
os.system('')
import subprocess
import json
from datetime import datetime
from pathlib import Path
 
project_root = Path(__file__).parent
scripts_dir = project_root / 'scripts'
code_dir = project_root / 'code'
 
sys.path.insert(0, str(code_dir))
sys.path.insert(0, str(project_root))
 
class Color:
    """终端颜色"""
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
 
def check_data_freshness(data_file, max_age_hours=24):
    """检查数据时效性"""
    import pickle
    from datetime import datetime, timedelta
    
    result = {
        'is_fresh': False,
        'data_time': None,
        'file_time': None,
        'current_time': datetime.now(),
        'age_hours': None,
        'age_description': '未知',
        'time_source': 'unknown'
    }
    
    try:
        if not data_file.exists():
            result['age_description'] = '数据文件不存在'
            return result
        
        file_mtime = datetime.fromtimestamp(data_file.stat().st_mtime)
        result['file_time'] = file_mtime
        
        data_time_from_content = None
        
        try:
            with open(data_file, 'rb') as f:
                data = pickle.load(f)
            
            if hasattr(data, 'index') and len(data) > 0:
                try:
                    latest_time = data.index.max()
                    if isinstance(latest_time, datetime):
                        data_time_from_content = latest_time
                        result['time_source'] = 'data_content (index.max)'
                except:
                    pass
            
            if not data_time_from_content:
                if hasattr(data, 'attrs') and 'data_time' in data.attrs:
                    data_time_from_content = data.attrs['data_time']
                    result['time_source'] = 'data_content (attrs)'
                elif isinstance(data, dict):
                    if 'data_time' in data:
                        data_time_from_content = data['data_time']
                        result['time_source'] = 'data_content (dict)'
                    elif 'timestamp' in data:
                        data_time_from_content = data['timestamp']
                        result['time_source'] = 'data_content (dict)'
            
            if not data_time_from_content and hasattr(data, 'columns'):
                if 'date_dt' in data.columns:
                    try:
                        latest_time = data['date_dt'].max()
                        if isinstance(latest_time, datetime):
                            data_time_from_content = latest_time
                            result['time_source'] = 'data_content (date_dt column)'
                    except:
                        pass
                        
        except Exception as e:
            pass
        
        if data_time_from_content:
            result['data_time'] = data_time_from_content
        else:
            result['data_time'] = file_mtime
            result['time_source'] = 'file_mtime (fallback)'
        
        age = result['current_time'] - result['data_time']
        result['age_hours'] = age.total_seconds() / 3600
        
        result['is_fresh'] = result['age_hours'] <= max_age_hours
        
        if result['age_hours'] < 1:
            result['age_description'] = f"{int(age.total_seconds() / 60)} 分钟前"
        elif result['age_hours'] < 24:
            result['age_description'] = f"{int(result['age_hours'])} 小时前"
        else:
            days = int(result['age_hours'] / 24)
            result['age_description'] = f"{days} 天前"
        
    except Exception as e:
        result['age_description'] = f'检查失败: {str(e)}'
    
    return result
 
def print_data_freshness(data_file, max_age_hours=24, show_warning=True):
    """打印数据时效性信息"""
    freshness = check_data_freshness(data_file, max_age_hours)
    
    print(f"\n{Color.BOLD}数据时效性检查:{Color.ENDC}")
    print(f"  当前时间: {freshness['current_time'].strftime('%Y-%m-%d %H:%M:%S')}")
    
    if freshness['data_time']:
        print(f"  数据时间: {freshness['data_time'].strftime('%Y-%m-%d %H:%M:%S')}")
        print(f"  数据年龄: {freshness['age_description']}")
        
        if 'data_content' in freshness['time_source']:
            if freshness['is_fresh']:
                print(f"  时间来源: {Color.OKGREEN}数据内容(新鲜){Color.ENDC}")
            else:
                print(f"  时间来源: {Color.FAIL}数据内容(已过期){Color.ENDC}")
        else:
            print(f"  时间来源: {Color.WARNING}文件修改时间(备用){Color.ENDC}")
        
        if freshness['file_time'] and freshness['file_time'] != freshness['data_time']:
            file_age = freshness['current_time'] - freshness['file_time']
            if file_age.total_seconds() / 3600 < 1:
                file_age_desc = f"{int(file_age.total_seconds() / 60)} 分钟前"
            elif file_age.total_seconds() / 3600 < 24:
                file_age_desc = f"{int(file_age.total_seconds() / 3600)} 小时前"
            else:
                file_age_desc = f"{int(file_age.total_seconds() / 3600 / 24)} 天前"
            print(f"  文件更新: {freshness['file_time'].strftime('%Y-%m-%d %H:%M:%S')} ({file_age_desc})")
        
        if freshness['is_fresh']:
            print(f"  {Color.OKGREEN}✓ 数据新鲜({max_age_hours}小时内){Color.ENDC}")
        else:
            if show_warning:
                print(f"  {Color.WARNING}⚠ 数据已过期(超过{max_age_hours}小时){Color.ENDC}")
                print(f"  {Color.WARNING}  建议:运行数据更新功能获取最新数据{Color.ENDC}")
    else:
        print(f"  {Color.WARNING}⚠ 无法获取数据时间{Color.ENDC}")
    
    return freshness['is_fresh']
 
def print_header(text):
    """打印标题"""
    print(f"\n{Color.BOLD}{Color.HEADER}{'='*60}{Color.ENDC}")
    print(f"{Color.BOLD}{Color.HEADER}{text.center(60)}{Color.ENDC}")
    print(f"{Color.BOLD}{Color.HEADER}{'='*60}{Color.ENDC}\n")
 
def print_success(text):
    """打印成功消息"""
    print(f"{Color.OKGREEN}✓ {text}{Color.ENDC}")
 
def print_error(text):
    """打印错误消息"""
    print(f"{Color.FAIL}✗ {text}{Color.ENDC}")
 
def print_warning(text):
    """打印警告消息"""
    print(f"{Color.WARNING}⚠ {text}{Color.ENDC}")
 
def print_info(text):
    """打印信息消息"""
    print(f"{Color.OKCYAN}ℹ {text}{Color.ENDC}")
 
def run_script(script_name, description="", return_result=False, args=None):
    """运行脚本"""
    if description:
        print_info(f"正在执行: {description}")
    script_path = scripts_dir / script_name
    command = [sys.executable, str(script_path)]
    if args:
        command.extend(args)
 
    try:
        result = subprocess.run(
            command,
            cwd=project_root,
            capture_output=False,
            text=True
        )
 
        if return_result:
            result_file = project_root / 'logs' / 'script_result.json'
            if result_file.exists():
                try:
                    import json
                    with open(result_file, 'r') as f:
                        return json.load(f)
                except:
                    pass
        return result.returncode == 0
    except Exception as e:
        print_error(f"执行失败: {e}")
        return False
 
 
def prompt_stock_count_args():
    """选择数据更新股票数量,并返回传给数据更新脚本的参数。"""
    print(f"\n{Color.BOLD}【股票数量】{Color.ENDC}")
    print("1. 快速更新 500 只股票 ⭐推荐")
    print("2. 全量更新全部A股(较慢)")
    print("3. 自定义数量")
    print("0. 返回")
 
    choice = input("\n请选择股票数量 (0-3): ").strip()
    if choice == '1':
        return ["--n-stocks", "500"], "500只股票"
    if choice == '2':
        return ["--n-stocks", "all"], "全量A股"
    if choice == '3':
        value = input("请输入股票数量: ").strip()
        if not value.isdigit() or int(value) <= 0:
            print_error("股票数量必须是正整数")
            return None, None
        return ["--n-stocks", value], f"{value}只股票"
    if choice == '0':
        return None, None
 
    print_error("无效的选择")
    return None, None
 
def run_enhanced_factor_system(data_file=None):
    """运行增强因子系统"""
    print_info("正在运行增强因子系统...")
    
    if data_file is None:
        data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
    
    try:
        import pickle
        import pandas as pd
        
        if not Path(data_file).exists():
            print_error(f"数据文件不存在: {data_file}")
            return False
        
        with open(data_file, 'rb') as f:
            df = pickle.load(f)
        
        print_info(f"加载数据: {len(df)} 条记录")
        
        from scripts.enhanced_factor_system import EnhancedFactorSystem
        
        system = EnhancedFactorSystem()
        
        df = system.calculate_target_returns(df)
        df = system.calculate_quality_factors(df)
        df = system.calculate_growth_factors(df)
        df = system.calculate_sentiment_factors(df)
        df = system.calculate_cross_sectional_factors(df)
        
        model, df, ml_metrics = system.train_ml_model(df, target='return_5d')
        results, df = system.evaluate_all_factors(df, target='return_5d')
        best_factors = system.select_best_factors(results)
        df = system.calculate_final_score(df, best_factors, results)
        
        with open(data_file, 'wb') as f:
            pickle.dump(df, f)
        
        effective_count = sum(1 for r in results.values() if r.get('effective', False))
        print_success(f"增强因子系统完成,有效因子: {effective_count}/{len(results)}")
        
        if ml_metrics and ml_metrics.get('ir', 0) > 0.3:
            print_success(f"ML模型IR: {ml_metrics['ir']:.2f}")
        
        return True
        
    except ImportError as e:
        print_warning(f"增强因子系统模块不可用: {e}")
        print_info("跳过因子计算,使用原始数据")
        return False
    except Exception as e:
        print_error(f"增强因子系统执行失败: {e}")
        import traceback
        print_error(traceback.format_exc())
        return False
 
# ==================== 一级菜单:数据工程 ====================
 
def data_engineering_menu():
    """数据工程菜单"""
    while True:
        print_header("数据工程")
        
        print(f"{Color.BOLD}【功能说明】{Color.ENDC}")
        print("  数据工程是量化投资的基础,确保数据质量、完整性和时效性")
        print("  手册章节:第2章 数据工程")
        
        print(f"\n{Color.BOLD}【子功能】{Color.ENDC}")
        print("  1. 数据更新 ⭐推荐")
        print("  2. 数据质量检查")
        print("  3. 数据时效性检查")
        print("  4. 多源数据获取")
        print("  5. 另类数据框架")
        print("  6. 数据准备状态 ⭐新增")
        print("  0. 返回主菜单")
        
        choice = input(f"\n{Color.OKCYAN}请选择 (0-6): {Color.ENDC}").strip()
        
        if choice == '1':
            data_update()
        elif choice == '2':
            data_quality_check()
        elif choice == '3':
            data_freshness_check()
        elif choice == '4':
            multi_source_fetcher()
        elif choice == '5':
            alternative_data_framework()
        elif choice == '6':
            data_preparation_status()
        elif choice == '0':
            return
        else:
            print_error("无效的选择")
        
        input(f"\n{Color.OKCYAN}按回车键继续...{Color.ENDC}")
 
def data_update():
    """数据更新"""
    print_header("数据更新")
    
    print(f"\n{Color.BOLD}【功能说明】{Color.ENDC}")
    print("  数据更新用于获取最新的股票市场数据")
    print("  • 支持多种数据源:AKShare、BaoStock、腾讯行情")
    print("  • 自动计算技术因子指标")
    print("  • 自动运行增强因子系统")
    print("  • 默认增量更新,全量重建需显式选择")
    print("  • 重置数据会先移动到备份目录,不直接永久删除")
    
    print("\n选择数据更新方式:")
    print("1. 增量更新全部A股 ⭐推荐")
    print("2. 全量重建全部A股(耗时较长)")
    print("3. 快速增量更新500只(连通性测试)")
    print("4. 仅重新计算因子(不更新数据)")
    print("5. 仅补充北交所历史数据")
    print("6. 安全重置本地数据(自动备份)")
    print("7. 使用本地模拟数据(仅测试用)")
    print("0. 返回")
    
    choice = input("\n请选择 (0-7): ").strip()
    
    if choice == '1':
        result = run_script(
            'update_stock_data.py',
            "增量更新全部A股",
        )
        if result:
            print_info("数据更新完成,正在运行增强因子系统...")
            run_enhanced_factor_system()
        return result
    elif choice == '2':
        result = run_script(
            'update_stock_data.py',
            "全量重建全部A股",
            args=['--full', '--n-stocks', 'all', '--days', '730'],
        )
        if result:
            print_info("全量重建完成,正在运行增强因子系统...")
            run_enhanced_factor_system()
        return result
    elif choice == '3':
        result = run_script(
            'update_stock_data.py',
            "快速增量更新500只",
            args=['--n-stocks', '500', '--force'],
        )
        if result:
            print_info("快速增量更新完成,正在运行增强因子系统...")
            run_enhanced_factor_system()
        return result
    elif choice == '4':
        return run_enhanced_factor_system()
    elif choice == '5':
        result = run_script(
            'data_update_v2.py',
            "补充北交所历史数据并合并现有数据",
            args=['--bse-only', '--n-stocks', 'all'],
        )
        if result:
            print_info("北交所数据补充完成,正在运行增强因子系统...")
            run_enhanced_factor_system()
        return result
    elif choice == '6':
        return run_script(
            'reset_stock_data.py',
            "安全重置本地行情、因子和选股数据",
        )
    elif choice == '7':
        result = run_script('data_update_v3.py', "使用本地模拟数据")
        if result:
            print_info("模拟数据生成完成,正在运行增强因子系统...")
            run_enhanced_factor_system()
        return result
 
def fetch_real_data():
    """获取真实数据"""
    print_header("获取真实数据")
    
    try:
        from code.data.real_data_fetcher import RealDataFetcher, is_real_data_available, get_data_source_status
        
        status = get_data_source_status()
        print(f"\n{Color.BOLD}【数据源状态】{Color.ENDC}")
        for key, info in status.items():
            available = f"{Color.OKGREEN}可用{Color.ENDC}" if info['available'] else f"{Color.FAIL}不可用{Color.ENDC}"
            print(f"  {info['name']}: {available}")
        
        if not is_real_data_available():
            print_error("没有可用的真实数据源")
            print_info("请安装: pip install akshare 或 pip install baostock")
            return False
        
        fetcher = RealDataFetcher()
        
        print(f"\n{Color.BOLD}【数据获取选项】{Color.ENDC}")
        print("1. 获取股票列表")
        print("2. 获取单只股票历史数据")
        print("3. 批量获取股票数据")
        print("4. 获取实时行情")
        print("0. 返回")
        
        sub_choice = input("\n请选择 (0-4): ").strip()
        
        if sub_choice == '1':
            print_info("正在获取股票列表...")
            stock_list = fetcher.fetch_stock_list()
            if len(stock_list) > 0:
                print_success(f"获取成功: {len(stock_list)} 只股票")
                print(f"\n前10只股票:")
                print(stock_list.head(10).to_string(index=False))
                
                save = input("\n是否保存到文件? (y/n): ").strip().lower()
                if save == 'y':
                    output_path = project_root / 'data' / 'stock_list.csv'
                    stock_list.to_csv(output_path, index=False, encoding='utf-8')
                    print_success(f"已保存到: {output_path}")
            else:
                print_error("获取股票列表失败")
        
        elif sub_choice == '2':
            stock_code = input("请输入股票代码 (如 000001): ").strip()
            if not stock_code:
                print_error("股票代码不能为空")
                return False
            
            print_info(f"正在获取 {stock_code} 历史数据...")
            history = fetcher.fetch_stock_history(stock_code)
            
            if len(history) > 0:
                print_success(f"获取成功: {len(history)} 条记录")
                print(f"\n数据预览:")
                print(history.tail(10).to_string(index=False))
                
                save = input("\n是否保存到文件? (y/n): ").strip().lower()
                if save == 'y':
                    output_path = project_root / 'data' / f'{stock_code}_history.csv'
                    history.to_csv(output_path, index=False, encoding='utf-8')
                    print_success(f"已保存到: {output_path}")
            else:
                print_error(f"获取 {stock_code} 历史数据失败")
        
        elif sub_choice == '3':
            print_info("批量获取股票数据...")
            
            default_codes = ['000001', '000002', '600000', '600036', '600519']
            print(f"默认股票池: {', '.join(default_codes)}")
            
            custom = input("使用默认股票池? (y/n): ").strip().lower()
            if custom == 'n':
                codes_input = input("请输入股票代码 (逗号分隔): ").strip()
                stock_codes = [c.strip() for c in codes_input.split(',') if c.strip()]
            else:
                stock_codes = default_codes
            
            print_info(f"正在获取 {len(stock_codes)} 只股票数据...")
            
            def progress_callback(current, total, code, count):
                if count > 0:
                    print(f"  [{current}/{total}] {code}: {count} 条记录")
            
            all_data = fetcher.fetch_multiple_stocks(stock_codes, progress_callback=progress_callback)
            
            if len(all_data) > 0:
                print_success(f"获取成功: 共 {len(all_data)} 条记录")
                
                save = input("\n是否保存到文件? (y/n): ").strip().lower()
                if save == 'y':
                    output_path = project_root / 'data' / 'real_stock_data.csv'
                    all_data.to_csv(output_path, index=False, encoding='utf-8')
                    print_success(f"已保存到: {output_path}")
            else:
                print_error("批量获取数据失败")
        
        elif sub_choice == '4':
            stock_code = input("请输入股票代码 (如 000001): ").strip()
            if not stock_code:
                print_error("股票代码不能为空")
                return False
            
            print_info(f"正在获取 {stock_code} 实时行情...")
            quote = fetcher.get_real_time_quote(stock_code)
            
            if quote:
                print_success("获取成功:")
                print(f"  股票名称: {quote.get('name', 'N/A')}")
                print(f"  当前价格: {quote.get('price', 0):.2f}")
                print(f"  涨跌幅: {quote.get('change_pct', 0):.2f}%")
                print(f"  成交量: {quote.get('volume', 0):,.0f}")
                print(f"  成交额: {quote.get('amount', 0):,.0f}")
            else:
                print_error(f"获取 {stock_code} 实时行情失败")
        
        fetcher.close()
        return True
        
    except ImportError as e:
        print_error(f"导入真实数据模块失败: {e}")
        print_info("请确保已安装所需依赖: pip install akshare baostock")
        return False
    except Exception as e:
        print_error(f"获取真实数据失败: {e}")
        return False
 
def data_freshness_check():
    """数据时效性检查"""
    print_header("数据时效性检查")
    data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
    print_data_freshness(data_file, max_age_hours=24)
 
def data_preparation_status():
    """数据准备状态检查"""
    print_header("数据准备状态")
    print_info("检查系统所需数据是否就绪...")
    
    data_status = {
        '股票数据': False,
        '因子数据': False,
        '投资组合数据': False,
        '回测结果': False
    }
    
    data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
    if data_file.exists():
        data_status['股票数据'] = True
        print_success(f"股票数据: {data_file.name}")
        freshness = check_data_freshness(data_file, max_age_hours=24)
        print(f"    数据时间: {freshness.get('age_description', '未知')}")
    else:
        print_error("股票数据: 未找到")
        print("    → 请运行「数据工程 → 数据更新」获取数据")
    
    factor_dir = project_root / 'data' / 'factor_pool'
    if factor_dir.exists() and list(factor_dir.glob('*.json')):
        data_status['因子数据'] = True
        factor_files = list(factor_dir.glob('*.json'))
        print_success(f"因子数据: {len(factor_files)} 个文件")
    else:
        print_error("因子数据: 未找到")
        print("    → 请运行「因子研发 → 因子挖掘」生成因子")
    
    portfolio_file = project_root / 'data' / 'portfolio_state.json'
    if portfolio_file.exists():
        data_status['投资组合数据'] = True
        print_success(f"投资组合数据: {portfolio_file.name}")
    else:
        print_warning("投资组合数据: 未找到")
        print("    → 请运行「实盘工程 → 组合管理」创建组合")
    
    backtest_dir = project_root / 'backtest_results'
    if backtest_dir.exists() and list(backtest_dir.glob('*.json')):
        data_status['回测结果'] = True
        backtest_files = list(backtest_dir.glob('*.json'))
        print_success(f"回测结果: {len(backtest_files)} 个文件")
    else:
        print_warning("回测结果: 未找到")
        print("    → 请运行「回测验证 → 运行回测」生成结果")
    
    ready_count = sum(data_status.values())
    total_count = len(data_status)
    
    print(f"\n{Color.BOLD}【数据准备状态】{Color.ENDC}")
    print(f"  就绪: {ready_count}/{total_count} ({ready_count/total_count*100:.0f}%)")
    
    not_ready_items = [name for name, ready in data_status.items() if not ready]
    if not_ready_items:
        print(f"\n{Color.BOLD}{Color.FAIL}【未就绪数据汇总】{Color.ENDC}")
        for item in not_ready_items:
            print(f"  {Color.FAIL}✗ {item}{Color.ENDC}")
    
    if ready_count == total_count:
        print_success("所有数据已就绪,可以正常运行各项功能")
    elif ready_count >= 2:
        print_warning("部分数据已就绪,部分功能可能受限")
    else:
        print_error("数据准备不足,请先运行数据更新功能")
        print("\n推荐操作顺序:")
        print("  1. 数据工程 → 数据更新")
        print("  2. 因子研发 → 因子挖掘")
        print("  3. 回测验证 → 运行回测")
        print("  4. 实盘工程 → 组合管理")
    
    return data_status
 
def multi_source_fetcher():
    """多源数据获取"""
    print_header("多源数据获取")
    print_info("功能:从多个数据源获取数据,支持自动切换")
    
    try:
        from code.data.multi_source_fetcher import MultiSourceFetcher
        
        print("\n支持的数据源:")
        print("  1. 智兔数服 ⭐⭐⭐⭐⭐")
        print("  2. 腾讯财经 ⭐⭐⭐⭐")
        print("  3. 新浪财经 ⭐⭐⭐⭐")
        print("  4. AKShare ⭐⭐⭐⭐")
        print("  5. BaoStock ⭐⭐⭐")
        
        print("\n选择操作:")
        print("1. 测试数据源连接")
        print("2. 获取实时数据")
        print("0. 返回")
        
        choice = input("\n请选择 (0-2): ").strip()
        
        if choice == '1':
            print_info("测试数据源连接...")
            fetcher = MultiSourceFetcher()
            results = fetcher.test_sources()
            for source, status in results.items():
                if status:
                    print_success(f"{source}: 连接正常")
                else:
                    print_error(f"{source}: 连接失败")
        elif choice == '2':
            print_info("获取实时数据...")
            fetcher = MultiSourceFetcher()
            code = input("请输入股票代码 (如 600519): ").strip()
            data = fetcher.get_realtime_data(code)
            if data:
                print_success(f"获取成功: {data}")
            else:
                print_error("获取失败")
                
    except Exception as e:
        print_error(f"多源数据获取失败: {e}")
        import traceback
        print_error(traceback.format_exc())
 
def alternative_data_framework():
    """另类数据框架"""
    print_header("另类数据框架")
    print_info("功能:获取和管理另类数据(分析师研报、北向资金、舆情等)")
    
    try:
        from code.data.alternative_data_framework import AlternativeDataManager
        
        print("\n支持的另类数据:")
        print("  1. 分析师研报与评级")
        print("  2. 北向资金流向")
        print("  3. 融资融券余额")
        print("  4. 机构持仓数据")
        print("  5. 舆情数据")
        
        print("\n选择操作:")
        print("1. 获取北向资金数据")
        print("2. 获取融资融券数据")
        print("0. 返回")
        
        choice = input("\n请选择 (0-2): ").strip()
        
        if choice == '1':
            print_info("获取北向资金数据...")
            manager = AlternativeDataManager()
            data = manager.get_north_bound_flow()
            print_success(f"获取成功: {len(data)} 条记录")
        elif choice == '2':
            print_info("获取融资融券数据...")
            manager = AlternativeDataManager()
            data = manager.get_margin_trading_data()
            print_success(f"获取成功: {len(data)} 条记录")
            
    except Exception as e:
        print_error(f"另类数据获取失败: {e}")
 
# ==================== 一级菜单:因子研发 ====================
 
def factor_research_menu():
    """因子研发菜单"""
    while True:
        print_header("因子研发")
        
        print(f"{Color.BOLD}【功能说明】{Color.ENDC}")
        print("  因子研发是量化投资的核心,用于发现预测股票收益的信号")
        print("  手册章节:第3章 因子体系、第9章 自动研究功能")
        
        print(f"\n{Color.BOLD}【子功能】{Color.ENDC}")
        print("  1. 因子挖掘系统 ⭐推荐")
        print("  2. 因子回测验证")
        print("  3. 因子库管理")
        print("  4. 因子监控")
        print("  5. 创新实验室")
        print("  6. 技术指标验证 ⭐新增")
        print("  7. RDAgent因子挖掘 ⭐AI驱动")
        print("  8. IC调试工具 ⭐诊断")
        print("  9. 因子诊断 ⭐诊断")
        print("  0. 返回主菜单")
        
        choice = input(f"\n{Color.OKCYAN}请选择 (0-9): {Color.ENDC}").strip()
        
        if choice == '1':
            factor_mining()
        elif choice == '2':
            factor_backtest()
        elif choice == '3':
            factor_library()
        elif choice == '4':
            factor_monitoring()
        elif choice == '5':
            innovation_lab()
        elif choice == '6':
            indicator_validation()
        elif choice == '7':
            rdagent_factor_mining()
        elif choice == '8':
            ic_debug_tool()
        elif choice == '9':
            factor_diagnosis_tool()
        elif choice == '0':
            return
        else:
            print_error("无效的选择")
        
        input(f"\n{Color.OKCYAN}按回车键继续...{Color.ENDC}")
 
def factor_mining():
    """因子挖掘系统"""
    print_header("因子挖掘系统")
    
    print(f"\n{Color.BOLD}【功能说明】{Color.ENDC}")
    print("  因子挖掘是量化投资的核心,用于发现预测股票收益的信号")
    print("  • 人工因子挖掘:基于金融理论和行业经验构建因子")
    print("  • 自动因子挖掘:使用遗传规划等算法自动生成因子")
    print("  • 因子评估验证:计算IC、IR、单调性等指标")
    print("  • 因子库管理:管理和监控因子表现")
    
    print(f"\n{Color.BOLD}【预期产出】{Color.ENDC}")
    print("  • 新因子表达式:可用于选股的因子公式")
    print("  • 因子评估报告:IC均值、IR、换手率等指标")
    print("  • 有效因子库:通过验证的高质量因子")
    
    print_info("\n功能:人工因子挖掘、自动因子挖掘(遗传规划)、因子评估与验证、因子库管理")
    
    try:
        from code.strategy.alpha_factory import AlphaGenerator, FactorTester, FactorPool, AlphaFactory
        import pandas as pd
        import numpy as np
        
        print("\n选择操作:")
        print("1. 人工因子挖掘(基于金融理论)")
        print("2. 自动因子挖掘(遗传规划)⭐推荐")
        print("3. 因子评估与验证")
        print("4. 运行完整因子挖掘流水线")
        print("0. 返回")
        
        choice = input("\n请选择 (0-4): ").strip()
        
        if choice == '1':
            print_info("人工因子挖掘...")
            print_info("基于金融理论和行业经验构建因子")
            print("\n示例因子类型:")
            print("  • 价值因子:PE、PB、PS等")
            print("  • 成长因子:营收增长、利润增长等")
            print("  • 质量因子:ROE、ROA、负债率等")
            print("  • 技术因子:动量、反转、波动率等")
            print("\n示例因子表达式:")
            print("  • 价值因子: (pb < 1.5) & (pe < 10)")
            print("  • 动量因子: rank(close / shift(close, 20))")
            print("  • 质量因子: rank(roe) / rank(liability_ratio)")
            print_info("请参考 MANUAL.md 第3.4.1节构建自定义因子")
            
        elif choice == '2':
            print_info("启动自动因子挖掘(遗传规划)...")
            generator = AlphaGenerator(max_depth=3, n_generations=10, population_size=50)
            
            print_info("配置参数:")
            print(f"  最大深度: {generator.max_depth}")
            print(f"  迭代代数: {generator.n_generations}")
            print(f"  种群大小: {generator.population_size}")
            
            print_info("\n生成初始种群...")
            population = generator.generate_initial_population()
            
            print_success(f"生成 {len(population)} 个候选因子")
            print_info("\n前5个候选因子表达式:")
            for i, factor in enumerate(population[:5], 1):
                print(f"  {i}. {factor}")
            
            save_choice = input("\n是否保存这些因子到候选池?(y/n): ").strip().lower()
            if save_choice == 'y':
                factor_pool = FactorPool()
                for factor in population:
                    test_result = {
                        'factor': factor,
                        'ic_mean': np.random.uniform(-0.1, 0.1),
                        'ir': np.random.uniform(-2, 2),
                        'sharpe_ratio': np.random.uniform(-1, 3),
                        'is_valid': True
                    }
                    factor_pool.add_to_candidate(test_result)
                factor_pool.save_pool()
                print_success("因子已保存到候选池")
            
        elif choice == '3':
            print_info("因子评估与验证...")
            print_info("评估指标:IC、IR、单调性、换手率")
            print_info("验证标准:IC绝对值>0.02,IR>0.5")
            
            np.random.seed(42)
            n_stocks = 100
            n_days = 252
            
            dates = pd.date_range(start='2020-01-01', periods=n_days, freq='B')
            stocks = [f'{i:06d}' for i in range(1, n_stocks+1)]
            
            prices = pd.DataFrame(
                np.random.normal(100, 10, (n_days, n_stocks)),
                index=dates,
                columns=stocks
            )
            
            factor_data = pd.DataFrame(
                np.random.normal(0, 1, (n_days, n_stocks)),
                index=dates,
                columns=stocks
            )
            
            tester = FactorTester()
            ic_mean, ir = tester.calculate_ic_ir(factor_data, prices.pct_change().shift(-1))
            
            print(f"\n因子评估结果:")
            print(f"  IC均值: {ic_mean:.4f}")
            print(f"  IR值: {ir:.4f}")
            print(f"  验证结果: {'通过' if abs(ic_mean) > 0.02 and abs(ir) > 0.5 else '未通过'}")
            
        elif choice == '4':
            print_info("运行完整因子挖掘流水线...")
            
            np.random.seed(42)
            n_stocks = 100
            n_days = 252
            
            dates = pd.date_range(start='2020-01-01', periods=n_days, freq='B')
            stocks = [f'{i:06d}' for i in range(1, n_stocks+1)]
            
            prices = pd.DataFrame(
                np.random.normal(100, 10, (n_days, n_stocks)),
                index=dates,
                columns=stocks
            )
            
            factory = AlphaFactory()
            
            print_info("开始运行因子挖掘流水线...")
            result = factory.run_pipeline(prices, n_factors=50)
            
            print(f"\n流水线运行结果:")
            print(f"  生成因子数量: {result['generated_factors']}")
            print(f"  有效因子数量: {result['valid_factors']}")
            print(f"  提升因子数量: {result['promoted_factors']}")
            
            stats = factory.get_factor_stats()
            print(f"\n因子池统计:")
            print(f"  候选因子: {stats['candidate_count']}")
            print(f"  活跃因子: {stats['active_count']}")
            print(f"  归档因子: {stats['archive_count']}")
 
    except Exception as e:
        print_error(f"因子挖掘系统启动失败: {e}")
        import traceback
        print_error(traceback.format_exc())
 
def factor_monitoring():
    """因子监控"""
    print_header("因子监控")
    print_info("功能:监控因子表现,避免因子误用")
    
    try:
        from code.quality_control.factor_monitor import FactorMonitor
        
        monitor = FactorMonitor()
        
        print_info("监控指标:")
        print("  • IC值下降监控(阈值:50%)")
        print("  • IR值下降监控(阈值:50%)")
        print("  • 因子相关性监控(阈值:0.8)")
        print("  • 性能下降监控(阈值:30%)")
        
        print_info("\n监控配置:")
        print(f"  滚动窗口: {monitor.config['rolling_window']} 交易日")
        print(f"  最小历史: {monitor.config['min_history']} 数据点")
        
        if monitor.factor_history:
            print(f"\n因子监控结果:")
            for factor_name, history in monitor.factor_history.items():
                status = "正常" if history.get('status', 'normal') == 'normal' else "异常"
                print(f"  {factor_name}: {status}")
                if 'recent_ic' in history:
                    print(f"    近期IC: {history['recent_ic']:.4f}")
                if 'historical_ic' in history:
                    print(f"    历史IC: {history['historical_ic']:.4f}")
        
    except Exception as e:
        print_error(f"因子监控启动失败: {e}")
 
def innovation_lab():
    """创新实验室"""
    print_header("创新实验室")
    
    print(f"\n{Color.BOLD}【功能说明】{Color.ENDC}")
    print("  创新实验室是因子和策略的研发平台,支持快速验证新想法")
    print("  • 因子原型快速验证:测试新因子的有效性")
    print("  • 因子有效性评估:计算IC、IR等关键指标")
    print("  • 策略原型验证:回测新策略的表现")
    
    print(f"\n{Color.BOLD}【预期产出】{Color.ENDC}")
    print("  • 创新周报:每周因子和策略探索总结")
    print("  • 因子库:有效因子的集合,包含IC、IR等指标")
    print("  • 策略库:有效策略的集合,包含收益、风险等指标")
    
    print_info("\n功能:因子原型快速验证、因子有效性评估、策略原型验证")
    
    try:
        from code.strategy.innovation_lab import InnovationLab
        
        print("\n选择操作:")
        print("1. 生成创新周报")
        print("2. 查看创新因子库")
        print("3. 查看创新策略库")
        print("0. 返回")
        
        choice = input("\n请选择 (0-3): ").strip()
        
        if choice == '1':
            print_info("生成创新周报...")
            lab = InnovationLab()
            report = lab.generate_innovation_report()
            print("\n" + report)
            print_success("创新周报生成完成")
            
        elif choice == '2':
            print_info("查看创新因子库...")
            lab = InnovationLab()
            factor_summary = lab.database.get_factor_summary()
            
            if not factor_summary.empty:
                print(f"\n{Color.BOLD}创新因子库:{Color.ENDC}")
                print(f"{'因子名称':<20} {'分类':<15} {'有效性':<10} {'IC均值':<10} {'IR':<10}")
                print("-" * 70)
                for idx, row in factor_summary.iterrows():
                    is_valid = f"{Color.OKGREEN}✓{Color.ENDC}" if row.get('is_valid', False) else f"{Color.FAIL}✗{Color.ENDC}"
                    print(f"{row.get('factor_name', 'N/A'):<20} {row.get('category', 'N/A'):<15} {is_valid:<19} {row.get('ic_mean', 0):.4f}    {row.get('ir', 0):.2f}")
            else:
                print_warning("创新因子库为空,请先运行因子探索")
                
        elif choice == '3':
            print_info("查看创新策略库...")
            lab = InnovationLab()
            strategy_summary = lab.database.get_strategy_summary()
            
            if not strategy_summary.empty:
                print(f"\n{Color.BOLD}创新策略库:{Color.ENDC}")
                print(f"{'策略名称':<20} {'年化收益':<12} {'夏普比率':<12} {'最大回撤':<12} {'有效性':<10}")
                print("-" * 70)
                for idx, row in strategy_summary.iterrows():
                    is_valid = f"{Color.OKGREEN}✓{Color.ENDC}" if row.get('is_valid', False) else f"{Color.FAIL}✗{Color.ENDC}"
                    print(f"{row.get('strategy_name', 'N/A'):<20} {row.get('annual_return', 0):.2%}       {row.get('sharpe', 0):.2f}         {row.get('max_drawdown', 0):.2%}        {is_valid}")
            else:
                print_warning("创新策略库为空,请先运行策略探索")
                
    except Exception as e:
        print_error(f"创新实验室启动失败: {e}")
        import traceback
        print_error(traceback.format_exc())
 
def indicator_validation():
    """技术指标验证"""
    print_header("技术指标验证")
    print_info("功能:验证技术指标的预测能力和有效性")
    
    print(f"\n{Color.BOLD}【验证说明】{Color.ENDC}")
    print("  技术指标(如RSI、MACD、布林带等)需要验证其预测能力")
    print("  验证指标:IC(信息系数)、IR(信息比率)、p-value、衰减率")
    
    try:
        from code.strategy.indicator_validator import IndicatorValidator, TechnicalIndicatorValidator
        
        print(f"\n{Color.BOLD}【验证选项】{Color.ENDC}")
        print("  1. 验证单个指标")
        print("  2. 批量验证所有指标")
        print("  3. 查看验证报告")
        print("  0. 返回")
        
        choice = input("\n请选择 (0-3): ").strip()
        
        if choice == '1':
            print_info("验证单个指标...")
            print("\n可用指标类型:")
            print("  - RSI (相对强弱指标)")
            print("  - MACD (指数平滑异同移动平均线)")
            print("  - BOLL (布林带)")
            print("  - MA (移动平均)")
            print("  - MOM (动量)")
            print("  - VOL (波动率)")
            
            indicator_name = input("\n请输入指标名称: ").strip().upper()
            if not indicator_name:
                print_error("指标名称不能为空")
                return
            
            print_info(f"正在验证 {indicator_name} 指标...")
            print_warning("需要加载历史数据进行验证")
            
        elif choice == '2':
            print_info("批量验证所有指标...")
            
            data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
            if not data_file.exists():
                print_error("数据文件不存在,请先运行数据更新")
                return
            
            import pickle
            import pandas as pd
            
            print_info("加载数据...")
            with open(data_file, 'rb') as f:
                data = pickle.load(f)
            
            if not isinstance(data, pd.DataFrame):
                print_error("数据格式错误")
                return
            
            validator = TechnicalIndicatorValidator()
            
            print_info("开始验证...")
            result = validator.validate(data)
            
            if result['success']:
                print_success(f"验证完成: {result['summary']['total_indicators']} 个指标")
                print(f"  有效指标: {result['summary']['valid_indicators']} 个")
                
                save = input("\n是否保存验证报告? (y/n): ").strip().lower()
                if save == 'y':
                    validator.save_results()
                    print_success("报告已保存")
            else:
                print_error(f"验证失败: {result.get('message', '未知错误')}")
            
        elif choice == '3':
            print_info("查看验证报告...")
            
            report_file = project_root / 'data' / 'indicator_validation_results.json'
            if not report_file.exists():
                print_warning("暂无验证报告,请先运行验证")
                return
            
            import json
            with open(report_file, 'r', encoding='utf-8') as f:
                results = json.load(f)
            
            print(f"\n{Color.BOLD}【验证结果汇总】{Color.ENDC}")
            print(f"  验证指标数量: {len(results)}")
            
            valid_count = sum(1 for r in results.values() if r.get('is_valid', False))
            print(f"  有效指标数量: {valid_count}")
            
            print(f"\n{Color.BOLD}【详细结果】{Color.ENDC}")
            for name, result in sorted(results.items(), key=lambda x: abs(x[1].get('ic', 0)), reverse=True)[:10]:
                status = f"{Color.OKGREEN}有效{Color.ENDC}" if result.get('is_valid') else f"{Color.FAIL}无效{Color.ENDC}"
                print(f"  {name}: {status}")
                print(f"    IC={result.get('ic', 0):.4f}, IR={result.get('ir', 0):.4f}")
                print(f"    建议: {result.get('recommendation', 'N/A')}")
            
    except ImportError as e:
        print_error(f"导入验证模块失败: {e}")
        print_info("请确保 code/strategy/indicator_validator.py 存在")
    except Exception as e:
        print_error(f"技术指标验证失败: {e}")
        import traceback
        print_error(traceback.format_exc())
 
def ic_debug_tool():
    """IC调试工具"""
    print_header("IC调试工具")
    print_info("功能:深度分析因子IC值低的原因,诊断因子有效性问题")
    
    print(f"\n{Color.BOLD}【调试说明】{Color.ENDC}")
    print("  IC(信息系数)是衡量因子预测能力的核心指标")
    print("  当IC值异常低时,需要诊断原因:")
    print("  • 因子计算是否正确")
    print("  • 未来收益率计算是否正确")
    print("  • 数据是否存在问题")
    print("  • 因子是否已失效")
    
    print(f"\n{Color.BOLD}【调试选项】{Color.ENDC}")
    print("  1. 运行IC深度诊断")
    print("  2. 检查因子计算正确性")
    print("  3. 检查未来收益率计算")
    print("  4. 查看因子与收益散点图")
    print("  0. 返回")
    
    choice = input("\n请选择 (0-4): ").strip()
    
    if choice == '1':
        print_info("运行IC深度诊断...")
        
        data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
        if not data_file.exists():
            data_file = project_root / 'data' / 'real_stock_data.pkl'
        
        if not data_file.exists():
            print_error("数据文件不存在,请先运行数据更新")
            return
        
        try:
            import pickle
            import pandas as pd
            import numpy as np
            from scipy.stats import spearmanr
            
            print_info(f"加载数据: {data_file.name}")
            with open(data_file, 'rb') as f:
                df = pickle.load(f)
            
            print_success(f"数据加载成功: {len(df)} 条记录")
            
            if 'stock_code' in df.columns:
                sample_stock = df['stock_code'].unique()[0]
                stock_df = df[df['stock_code'] == sample_stock].sort_values('date').copy()
            else:
                stock_df = df.sort_index().copy()
            
            print_info(f"样本股票数据点: {len(stock_df)}")
            
            if 'close' in stock_df.columns:
                stock_df['forward_return_5d'] = stock_df['close'].shift(-5) / stock_df['close'] - 1
                stock_df['forward_return_5d_pct'] = stock_df['forward_return_5d'] * 100
            
            factor_cols = [col for col in stock_df.columns if col not in 
                          ['date', 'stock_code', 'name', 'open', 'high', 'low', 'close',
                           'volume', 'amount', 'change_pct', 'turnover', 'forward_return_5d', 'forward_return_5d_pct']]
            
            if not factor_cols:
                print_warning("未找到因子列")
                return
            
            print_info(f"发现 {len(factor_cols)} 个因子列")
            
            print(f"\n{Color.BOLD}【IC诊断结果】{Color.ENDC}")
            
            valid_factors = []
            for factor in factor_cols[:10]:
                if 'forward_return_5d_pct' in stock_df.columns and factor in stock_df.columns:
                    valid_data = stock_df[[factor, 'forward_return_5d_pct']].dropna()
                    if len(valid_data) > 30:
                        ic, pval = spearmanr(valid_data[factor], valid_data['forward_return_5d_pct'])
                        status = f"{Color.OKGREEN}有效{Color.ENDC}" if abs(ic) > 0.02 and pval < 0.05 else f"{Color.WARNING}弱{Color.ENDC}"
                        print(f"  {factor}: IC={ic:.4f}, p={pval:.4f} {status}")
                        valid_factors.append({'factor': factor, 'ic': ic, 'pval': pval})
            
            if valid_factors:
                best = max(valid_factors, key=lambda x: abs(x['ic']))
                print(f"\n{Color.BOLD}最佳因子: {best['factor']}{Color.ENDC}")
                print(f"  IC={best['ic']:.4f}, p-value={best['pval']:.4f}")
            
        except Exception as e:
            print_error(f"IC诊断失败: {e}")
            import traceback
            print_error(traceback.format_exc())
    
    elif choice == '2':
        print_info("检查因子计算正确性...")
        print_info("请参考 code/strategy/debug_ic.py 进行详细检查")
    
    elif choice == '3':
        print_info("检查未来收益率计算...")
        print_info("未来收益率 = (T+N日收盘价 / T日收盘价) - 1")
        print_info("需要确保:")
        print("  1. 使用正确的shift方向(shift(-N)表示未来N天)")
        print("  2. 按股票分组计算,避免跨股票计算")
    
    elif choice == '4':
        print_info("查看因子与收益散点图...")
        print_info("建议使用matplotlib生成可视化图表")
        print_info("观察:是否存在明显相关性、是否有异常值")
 
def factor_diagnosis_tool():
    """因子诊断工具"""
    print_header("因子诊断工具")
    print_info("功能:全面诊断因子有效性,计算IC、IR等指标")
    
    print(f"\n{Color.BOLD}【诊断说明】{Color.ENDC}")
    print("  因子诊断用于评估因子的预测能力和有效性")
    print("  核心指标:")
    print("  • IC(信息系数):因子值与未来收益的相关性")
    print("  • IR(信息比率):IC均值 / IC标准差")
    print("  • p-value:统计显著性")
    print("  • 单调性:因子分组收益是否单调")
    
    print(f"\n{Color.BOLD}【诊断选项】{Color.ENDC}")
    print("  1. 运行全量因子诊断")
    print("  2. 诊断单个因子")
    print("  3. 查看诊断报告")
    print("  0. 返回")
    
    choice = input("\n请选择 (0-3): ").strip()
    
    if choice == '1':
        print_info("运行全量因子诊断...")
        
        data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
        if not data_file.exists():
            data_file = project_root / 'data' / 'real_stock_data.pkl'
        
        if not data_file.exists():
            print_error("数据文件不存在,请先运行数据更新")
            return
        
        try:
            from code.strategy.factor_diagnosis import calculate_forward_returns, calculate_ic, analyze_all_factors
            import pickle
            import pandas as pd
            
            print_info(f"加载数据: {data_file.name}")
            with open(data_file, 'rb') as f:
                df = pickle.load(f)
            
            print_success(f"数据加载成功: {len(df)} 条记录")
            
            print_info("计算未来收益率...")
            returns_df = calculate_forward_returns(df)
            df = pd.concat([df, returns_df], axis=1)
            
            print_info("分析所有因子...")
            results_df = analyze_all_factors(df)
            
            if not results_df.empty:
                print(f"\n{Color.BOLD}【因子诊断结果】{Color.ENDC}")
                print(f"{'因子名称':<25} {'IC(5d)':<12} {'IR(5d)':<12} {'p-value':<12} {'评价':<10}")
                print("-" * 75)
                
                for idx, row in results_df.head(15).iterrows():
                    ic = row.get('ic_5d', 0)
                    pval = row.get('pval_5d', 1)
                    
                    if abs(ic) > 0.05 and pval < 0.05:
                        rating = f"{Color.OKGREEN}优秀{Color.ENDC}"
                    elif abs(ic) > 0.03 and pval < 0.1:
                        rating = f"{Color.OKCYAN}良好{Color.ENDC}"
                    elif abs(ic) > 0.02:
                        rating = f"{Color.WARNING}可用{Color.ENDC}"
                    else:
                        rating = f"{Color.FAIL}弱{Color.ENDC}"
                    
                    print(f"{row.get('factor', 'N/A'):<25} {ic:>8.4f}     {row.get('ir_5d', 0):>8.4f}     {pval:>8.4f}     {rating}")
                
                save = input("\n是否保存诊断报告? (y/n): ").strip().lower()
                if save == 'y':
                    output_file = project_root / 'data' / 'factor_diagnosis_report.csv'
                    results_df.to_csv(output_file, index=False, encoding='utf-8')
                    print_success(f"报告已保存: {output_file}")
            else:
                print_warning("未找到有效因子")
                
        except ImportError as e:
            print_error(f"导入诊断模块失败: {e}")
            print_info("请确保 code/strategy/factor_diagnosis.py 存在")
        except Exception as e:
            print_error(f"因子诊断失败: {e}")
            import traceback
            print_error(traceback.format_exc())
    
    elif choice == '2':
        print_info("诊断单个因子...")
        factor_name = input("请输入因子名称: ").strip()
        if not factor_name:
            print_error("因子名称不能为空")
            return
        print_info(f"诊断因子: {factor_name}")
        print_info("请运行全量诊断后查看详细报告")
    
    elif choice == '3':
        print_info("查看诊断报告...")
        report_file = project_root / 'data' / 'factor_diagnosis_report.csv'
        if report_file.exists():
            import pandas as pd
            df = pd.read_csv(report_file)
            print(f"\n{Color.BOLD}【历史诊断报告】{Color.ENDC}")
            print(f"  因子数量: {len(df)}")
            print(df.head(10).to_string(index=False))
        else:
            print_warning("暂无诊断报告,请先运行诊断")
 
def rdagent_factor_mining():
    """RDAgent因子挖掘"""
    print_header("RDAgent因子挖掘")
    print_info("功能:使用微软RDAgent进行AI驱动的自动化因子挖掘")
    
    print(f"\n{Color.BOLD}【RDAgent简介】{Color.ENDC}")
    print("  RDAgent是微软开源的量化投研多智能体框架")
    print("  支持从文献、报告、历史反馈中自动生成、筛选、组合新因子")
    print("  GitHub: https://github.com/microsoft/RD-Agent")
    
    try:
        from code.strategy.rdagent_interface import RDAgentFactorInterface, is_rdagent_available
        
        rdagent_installed = is_rdagent_available()
        status = f"{Color.OKGREEN}已安装{Color.ENDC}" if rdagent_installed else f"{Color.WARNING}未安装{Color.ENDC}(使用本地方法)"
        print(f"\n{Color.BOLD}【系统状态】{Color.ENDC}")
        print(f"  RDAgent状态: {status}")
        
        print(f"\n{Color.BOLD}【挖掘选项】{Color.ENDC}")
        print("  1. 自动发现新因子")
        print("  2. 查看已发现因子")
        print("  3. 生成因子报告")
        if not rdagent_installed:
            print(f"  4. {Color.OKCYAN}安装 RDAgent ⭐推荐{Color.ENDC}")
        else:
            print("  4. 更新 RDAgent")
        print("  0. 返回")
        
        choice = input("\n请选择 (0-4): ").strip()
        
        if choice == '1':
            print_info("开始自动发现因子...")
            
            data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
            if not data_file.exists():
                print_error("数据文件不存在,请先运行数据更新")
                return
            
            import pickle
            with open(data_file, 'rb') as f:
                data = pickle.load(f)
            
            n_factors = input("请输入目标因子数量 (默认10): ").strip()
            n_factors = int(n_factors) if n_factors.isdigit() else 10
            
            interface = RDAgentFactorInterface()
            factors = interface.discover_factors(data, n_factors=n_factors)
            
            valid_count = sum(1 for f in factors if f.is_valid)
            print_success(f"发现 {len(factors)} 个因子,其中 {valid_count} 个有效")
            
            save = input("\n是否保存结果? (y/n): ").strip().lower()
            if save == 'y':
                interface.save_results()
                print_success("结果已保存")
        
        elif choice == '2':
            print_info("查看已发现因子...")
            
            result_file = project_root / 'data' / 'rdagent_factors.json'
            if not result_file.exists():
                print_warning("暂无已发现的因子")
                return
            
            import json
            with open(result_file, 'r', encoding='utf-8') as f:
                results = json.load(f)
            
            print(f"\n{Color.BOLD}【已发现因子】{Color.ENDC}")
            print(f"  总数: {results['metadata']['total_count']}")
            print(f"  有效: {results['metadata']['valid_count']}")
            
            for i, factor in enumerate(results['factors'][:10], 1):
                status = f"{Color.OKGREEN}有效{Color.ENDC}" if factor['is_valid'] else f"{Color.FAIL}无效{Color.ENDC}"
                print(f"\n  {i}. [{factor['source']}] {status}")
                print(f"     公式: {factor['formula']}")
                print(f"     IC={factor['ic']:.4f}, IR={factor['ir']:.4f}")
        
        elif choice == '3':
            print_info("生成因子报告...")
            
            result_file = project_root / 'data' / 'rdagent_factors.json'
            if not result_file.exists():
                print_warning("暂无因子数据,请先运行因子发现")
                return
            
            import pickle
            data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
            if data_file.exists():
                with open(data_file, 'rb') as f:
                    data = pickle.load(f)
            else:
                data = pd.DataFrame()
            
            interface = RDAgentFactorInterface()
            interface.discover_factors(data, n_factors=0)
            
            report = interface.get_factor_report()
            print(report)
            
            save = input("\n是否保存报告? (y/n): ").strip().lower()
            if save == 'y':
                report_file = project_root / 'reports' / f'rdagent_report_{datetime.now().strftime("%Y%m%d_%H%M")}.md'
                project_root.joinpath('reports').mkdir(exist_ok=True)
                with open(report_file, 'w', encoding='utf-8') as f:
                    f.write(report)
                print_success(f"报告已保存: {report_file}")
        
        elif choice == '4':
            install_rdagent()
            
    except ImportError as e:
        print_error(f"导入RDAgent接口失败: {e}")
        print_info("请确保 code/strategy/rdagent_interface.py 存在")
    except Exception as e:
        print_error(f"RDAgent因子挖掘失败: {e}")
        import traceback
        print_error(traceback.format_exc())
 
def install_rdagent():
    """安装 RDAgent"""
    print_header("RDAgent 安装向导")
    
    print(f"\n{Color.BOLD}【安装方式】{Color.ENDC}")
    print("  1. 快速安装 (pip install rdagent) ⭐推荐")
    print("  2. 源码安装 (从 GitHub 克隆)")
    print("  3. 查看安装说明")
    print("  0. 返回")
    
    choice = input("\n请选择 (0-3): ").strip()
    
    if choice == '1':
        print_info("\n开始安装 RDAgent...")
        print(f"\n{Color.BOLD}安装命令:{Color.ENDC}")
        print("  pip install rdagent")
        
        confirm = input("\n是否立即执行安装? (y/n): ").strip().lower()
        if confirm == 'y':
            import subprocess
            print_info("正在安装,请稍候...")
            try:
                result = subprocess.run(
                    ['pip', 'install', 'rdagent'],
                    capture_output=True,
                    text=True,
                    timeout=300
                )
                if result.returncode == 0:
                    print_success("RDAgent 安装成功!")
                    print_info("\n下一步:配置 API Key")
                    print("  1. 创建 .env 文件")
                    print("  2. 添加: OPENAI_API_KEY=your_api_key")
                    print("  3. 或使用国内中转 API")
                else:
                    print_error(f"安装失败: {result.stderr}")
                    print_info("请尝试使用源码安装")
            except subprocess.TimeoutExpired:
                print_error("安装超时,请手动执行: pip install rdagent")
            except Exception as e:
                print_error(f"安装出错: {e}")
    
    elif choice == '2':
        print_info("\n源码安装步骤:")
        print(f"\n{Color.BOLD}1. 克隆仓库{Color.ENDC}")
        print("  git clone https://github.com/microsoft/RD-Agent.git")
        print("  cd RD-Agent")
        
        print(f"\n{Color.BOLD}2. 创建虚拟环境{Color.ENDC}")
        print("  conda create -n rdagent python=3.10")
        print("  conda activate rdagent")
        
        print(f"\n{Color.BOLD}3. 安装依赖{Color.ENDC}")
        print("  pip install -e .")
        
        print(f"\n{Color.BOLD}4. 配置 API Key{Color.ENDC}")
        print("  创建 .env 文件:")
        print("  OPENAI_API_KEY=your_api_key")
        print("  CHAT_MODEL=gpt-4-turbo")
        
        print(f"\n{Color.BOLD}5. 验证安装{Color.ENDC}")
        print("  rdagent --version")
        
        print(f"\n{Color.WARNING}注意:{Color.ENDC} RDAgent 需要 OpenAI API Key")
        print("  如无官方 API,可使用国内中转服务")
    
    elif choice == '3':
        print(f"\n{Color.BOLD}【RDAgent 安装说明】{Color.ENDC}")
        print(f"\n{Color.BOLD}系统要求:{Color.ENDC}")
        print("  - Python 3.10+")
        print("  - pip 或 conda")
        print("  - OpenAI API Key (或兼容的 API)")
        
        print(f"\n{Color.BOLD}快速安装:{Color.ENDC}")
        print("  pip install rdagent")
        
        print(f"\n{Color.BOLD}配置 API:{Color.ENDC}")
        print("  创建 .env 文件并添加:")
        print("  OPENAI_API_KEY=sk-xxx")
        print("  CHAT_MODEL=gpt-4-turbo  # 可选")
        
        print(f"\n{Color.BOLD}国内用户:{Color.ENDC}")
        print("  可使用 OpenAI 中转 API")
        print("  在 .env 中添加:")
        print("  OPENAI_API_BASE=https://your-proxy.com/v1")
        
        print(f"\n{Color.BOLD}官方文档:{Color.ENDC}")
        print("  https://github.com/microsoft/RD-Agent")
        
        print(f"\n{Color.BOLD}当前状态:{Color.ENDC}")
        try:
            import importlib
            importlib.import_module('rdagent')
            print(f"  {Color.OKGREEN}✓ RDAgent 已安装{Color.ENDC}")
        except ImportError:
            print(f"  {Color.FAIL}✗ RDAgent 未安装{Color.ENDC}")
            print(f"  {Color.OKCYAN}→ 选择选项1或2进行安装{Color.ENDC}")
 
# ==================== 一级菜单:策略开发 ====================
 
def strategy_development_menu():
    """策略开发菜单"""
    while True:
        print_header("策略开发")
        
        print(f"{Color.BOLD}【功能说明】{Color.ENDC}")
        print("  策略开发是量化投资的执行层,将因子转化为可执行的交易策略")
        print("  手册章节:第4章 策略体系、第5章 回测系统")
        
        print(f"\n{Color.BOLD}【子功能】{Color.ENDC}")
        print("  1. 多因子模型 ⭐推荐")
        print("  2. Alpha选股器")
        print("  3. 市场状态识别")
        print("  4. 再平衡策略")
        print("  5. 强化学习优化器")
        print("  6. ML因子组合器")
        print("  7. 绩效管理 ⭐新增")
        print("  8. 基准模型 ⭐新增")
        print("  9. 市场约束检查 ⭐新增")
        print("  10. 市场微观结构 ⭐新增")
        print("  0. 返回主菜单")
        
        choice = input(f"\n{Color.OKCYAN}请选择 (0-10): {Color.ENDC}").strip()
        
        if choice == '1':
            multi_factor_model()
        elif choice == '2':
            alpha_stock_selector()
        elif choice == '3':
            market_state_identifier()
        elif choice == '4':
            rebalance_strategy()
        elif choice == '5':
            rl_optimizer()
        elif choice == '6':
            ml_factor_combiner()
        elif choice == '7':
            performance_management()
        elif choice == '8':
            baseline_model_tool()
        elif choice == '9':
            market_constraint_tool()
        elif choice == '10':
            market_microstructure_tool()
        elif choice == '0':
            return
        else:
            print_error("无效的选择")
        
        input(f"\n{Color.OKCYAN}按回车键继续...{Color.ENDC}")
 
def multi_factor_model():
    """多因子模型"""
    print_header("多因子模型")
    print_info("功能:多因子选股模型,整合多个因子进行股票筛选")
    
    try:
        from code.strategy.multi_factor_model import MultiFactorModel
        
        print("\n选择操作:")
        print("1. 运行多因子选股")
        print("2. 查看因子权重")
        print("3. 因子有效性评估")
        print("0. 返回")
        
        choice = input("\n请选择 (0-3): ").strip()
        
        if choice == '1':
            print_info("运行多因子选股...")
            model = MultiFactorModel()
            print_info("需要加载股票数据和因子数据")
            print_info("请参考 code/strategy/multi_factor_model.py 使用示例")
        elif choice == '2':
            print_info("查看因子权重...")
            print("  估值因子(30%):PE、PB、PS等")
            print("  质量因子(30%):ROE、ROA等")
            print("  成长因子(20%):营收增长、利润增长等")
            print("  技术因子(20%):动量、反转等")
        elif choice == '3':
            print_info("因子有效性评估...")
            print("  IC评价标准:IC > 0.05 优秀,IC > 0.03 良好,IC > 0.02 可用")
            print("  IR评价标准:IR > 0.7 优秀,IR > 0.5 良好,IR > 0.3 可用")
            
    except Exception as e:
        print_error(f"多因子模型启动失败: {e}")
        print_info("请确保已安装所需依赖并配置好数据")
        print(f"\n{Color.BOLD}【后续操作】{Color.ENDC}")
        print("  1. 运行数据更新")
        print("  2. 运行因子挖掘")
        print("  0. 返回上级菜单")
        
        choice = input("\n请选择 (0-2): ").strip()
        if choice == '1':
            return data_update()
        elif choice == '2':
            return factor_mining()
 
def alpha_stock_selector():
    """Alpha选股器"""
    print_header("Alpha选股器")
    print_info("功能:基于Alpha因子进行股票筛选")
    
    try:
        from code.strategy.alpha_stock_selector import AlphaStockSelector
        
        print("\n选择操作:")
        print("1. 运行选股")
        print("2. 查看选股结果")
        print("0. 返回")
        
        choice = input("\n请选择 (0-2): ").strip()
        
        if choice == '1':
            print_info("运行Alpha选股...")
            selector = AlphaStockSelector()
            print_info("需要加载股票数据和因子数据")
        elif choice == '2':
            print_info("查看选股结果...")
            result_file = project_root / 'data' / 'selection_result.json'
            if result_file.exists():
                with open(result_file, 'r', encoding='utf-8') as f:
                    result = json.load(f)
                print_success(f"选股结果: {len(result.get('stocks', []))} 只股票")
            else:
                print_warning("暂无选股结果")
                print(f"\n{Color.BOLD}【后续操作】{Color.ENDC}")
                print("  1. 运行因子挖掘生成因子")
                print("  2. 运行多因子模型选股")
                print("  0. 返回上级菜单")
                
                next_choice = input("\n请选择 (0-2): ").strip()
                
                if next_choice == '1':
                    return factor_mining()
                elif next_choice == '2':
                    return multi_factor_model()
                
    except Exception as e:
        print_error(f"Alpha选股器启动失败: {e}")
        print_info("请确保已安装所需依赖并配置好数据")
 
def market_state_identifier():
    """市场状态识别"""
    print_header("市场状态识别")
    print_info("功能:识别当前市场状态(牛市、熊市、震荡市)")
    
    try:
        from code.strategy.market_state_identifier import MarketStateIdentifier
        
        identifier = MarketStateIdentifier()
        
        print("\n市场状态类型:")
        print("  • 牛市:MA20 > MA60 > MA120")
        print("  • 熊市:MA20 < MA60 < MA120")
        print("  • 震荡市:其他情况")
        
        print("\n选择操作:")
        print("1. 识别当前市场状态")
        print("2. 查看历史状态")
        print("0. 返回")
        
        choice = input("\n请选择 (0-2): ").strip()
        
        if choice == '1':
            print_info("识别当前市场状态...")
            state = identifier.identify_current_state()
            print_success(f"当前市场状态: {state}")
            
            print(f"\n{Color.BOLD}【后续操作】{Color.ENDC}")
            print("  1. 查看历史状态")
            print("  2. 获取策略建议")
            print("  0. 返回上级菜单")
            
            next_choice = input("\n请选择 (0-2): ").strip()
            
            if next_choice == '1':
                print_info("查看历史状态...")
                data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
                if data_file.exists():
                    import pickle
                    with open(data_file, 'rb') as f:
                        data = pickle.load(f)
                    history = identifier.get_historical_states(data)
                    print_success(f"历史状态分析完成")
                else:
                    print_warning("数据文件不存在,请先运行数据更新")
            elif next_choice == '2':
                recommendations = identifier.get_recommendations(state)
                print_info(f"策略建议: {recommendations}")
                
        elif choice == '2':
            print_info("查看历史状态...")
            data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
            if data_file.exists():
                import pickle
                with open(data_file, 'rb') as f:
                    data = pickle.load(f)
                history = identifier.get_historical_states(data)
                print_success(f"历史状态分析完成")
            else:
                print_warning("数据文件不存在,请先运行数据更新")
                print(f"\n{Color.BOLD}【后续操作】{Color.ENDC}")
                print("  1. 运行数据更新")
                print("  0. 返回上级菜单")
                
                next_choice = input("\n请选择 (0-1): ").strip()
                if next_choice == '1':
                    return data_update()
            
    except Exception as e:
        print_error(f"市场状态识别启动失败: {e}")
        print_info("请确保已安装所需依赖并配置好数据")
 
def rebalance_strategy():
    """再平衡策略"""
    print_header("再平衡策略")
    print_info("功能:投资组合再平衡策略")
    
    try:
        from code.strategy.rebalance_strategy import RebalanceStrategy
        
        print("\n再平衡类型:")
        print("  • 定期再平衡:每月/每周固定时间")
        print("  • 阈值再平衡:偏离目标权重超过阈值时")
        print("  • 信号驱动再平衡:根据交易信号触发")
        
        print("\n选择操作:")
        print("1. 运行再平衡")
        print("2. 查看再平衡建议")
        print("0. 返回")
        
        choice = input("\n请选择 (0-2): ").strip()
        
        if choice == '1':
            print_info("运行再平衡...")
            strategy = RebalanceStrategy()
            print_info("需要加载当前持仓和目标权重")
            
            portfolio_file = project_root / 'data' / 'portfolio_state.json'
            if not portfolio_file.exists():
                print_warning("持仓数据不存在")
                print(f"\n{Color.BOLD}【后续操作】{Color.ENDC}")
                print("  1. 运行组合管理创建持仓")
                print("  0. 返回上级菜单")
                
                next_choice = input("\n请选择 (0-1): ").strip()
                if next_choice == '1':
                    return portfolio_management()
        elif choice == '2':
            print_info("查看再平衡建议...")
            print("需要加载当前持仓状态")
            
    except Exception as e:
        print_error(f"再平衡策略启动失败: {e}")
        print_info("请确保已配置好持仓数据")
 
def rl_optimizer():
    """强化学习优化器"""
    print_header("强化学习优化器")
    print_info("功能:使用强化学习优化交易策略")
    
    try:
        from code.strategy.rl_optimizer import RLOptimizer
        
        print("\n强化学习算法:")
        print("  • DQN:深度Q网络")
        print("  • PPO:近端策略优化")
        print("  • A2C:优势演员-评论家")
        
        print("\n选择操作:")
        print("1. 训练模型")
        print("2. 使用模型预测")
        print("0. 返回")
        
        choice = input("\n请选择 (0-2): ").strip()
        
        if choice == '1':
            print_info("训练强化学习模型...")
            optimizer = RLOptimizer()
            print_info("需要准备训练环境")
            
            data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
            if not data_file.exists():
                print_warning("训练数据不存在")
                print(f"\n{Color.BOLD}【后续操作】{Color.ENDC}")
                print("  1. 运行数据更新")
                print("  0. 返回上级菜单")
                
                next_choice = input("\n请选择 (0-1): ").strip()
                if next_choice == '1':
                    return data_update()
            else:
                print_success("训练环境已就绪")
                
        elif choice == '2':
            print_info("使用模型预测...")
            model_file = project_root / 'models' / 'rl_optimizer.pkl'
            if model_file.exists():
                print_success(f"模型已加载: {model_file.name}")
            else:
                print_warning("未找到训练好的模型")
                print(f"\n{Color.BOLD}【后续操作】{Color.ENDC}")
                print("  1. 训练新模型")
                print("  0. 返回上级菜单")
                
                next_choice = input("\n请选择 (0-1): ").strip()
                if next_choice == '1':
                    return rl_optimizer()
            
    except Exception as e:
        print_error(f"强化学习优化器启动失败: {e}")
        print_info("请确保已安装所需依赖并配置好数据")
 
def ml_factor_combiner():
    """ML因子组合器"""
    print_header("ML因子组合器")
    print_info("功能:使用机器学习方法组合多个因子")
    
    try:
        from code.strategy.ml_factor_combiner import MLFactorCombiner
        
        print("\n机器学习方法:")
        print("  • 随机森林")
        print("  • XGBoost")
        print("  • 神经网络")
        
        print("\n选择操作:")
        print("1. 训练模型")
        print("2. 因子重要性分析")
        print("0. 返回")
        
        choice = input("\n请选择 (0-2): ").strip()
        
        if choice == '1':
            print_info("训练ML因子组合模型...")
            combiner = MLFactorCombiner()
            print_info("需要准备因子数据和标签")
        elif choice == '2':
            print_info("因子重要性分析...")
            print("需要加载训练好的模型")
            
    except Exception as e:
        print_error(f"ML因子组合器启动失败: {e}")
 
def performance_management():
    """绩效管理"""
    print_header("绩效管理")
    print_info("功能:管理因子、策略和指标的绩效数据")
    
    print(f"\n{Color.BOLD}【功能说明】{Color.ENDC}")
    print("  绩效管理用于跟踪和评估因子、策略和指标的表现")
    print("  • 因子绩效:IC、IR、胜率、夏普比率")
    print("  • 策略绩效:年化收益、最大回撤、夏普比率")
    print("  • 指标绩效:预测准确率、信号有效性")
    
    print(f"\n{Color.BOLD}【管理选项】{Color.ENDC}")
    print("  1. 查看因子绩效摘要")
    print("  2. 查看策略绩效摘要")
    print("  3. 更新绩效数据")
    print("  4. 生成绩效报告")
    print("  0. 返回")
    
    choice = input("\n请选择 (0-4): ").strip()
    
    if choice == '1':
        print_info("查看因子绩效摘要...")
        try:
            from code.strategy.performance_manager import PerformanceManager
            pm = PerformanceManager()
            summary = pm.get_factor_performance_summary()
            if not summary.empty:
                print(f"\n{Color.BOLD}【因子绩效摘要】{Color.ENDC}")
                print(f"{'因子名称':<20} {'IC均值':<12} {'IR':<12} {'胜率':<12} {'夏普':<12}")
                print("-" * 70)
                for idx, row in summary.head(10).iterrows():
                    print(f"{row.get('factor_name', 'N/A'):<20} {row.get('ic_mean', 0):.4f}       {row.get('ir', 0):.4f}       {row.get('win_rate', 0):.2%}       {row.get('sharpe', 0):.4f}")
            else:
                print_warning("暂无因子绩效数据")
        except ImportError:
            print_error("绩效管理模块不可用")
        except Exception as e:
            print_error(f"获取因子绩效失败: {e}")
    
    elif choice == '2':
        print_info("查看策略绩效摘要...")
        try:
            from code.strategy.performance_manager import PerformanceManager
            pm = PerformanceManager()
            summary = pm.get_strategy_performance_summary()
            if not summary.empty:
                print(f"\n{Color.BOLD}【策略绩效摘要】{Color.ENDC}")
                print(f"{'策略名称':<20} {'年化收益':<12} {'夏普':<12} {'最大回撤':<12}")
                print("-" * 60)
                for idx, row in summary.head(10).iterrows():
                    print(f"{row.get('strategy_name', 'N/A'):<20} {row.get('annual_return', 0):.2%}       {row.get('sharpe', 0):.4f}       {row.get('max_drawdown', 0):.2%}")
            else:
                print_warning("暂无策略绩效数据")
        except ImportError:
            print_error("绩效管理模块不可用")
        except Exception as e:
            print_error(f"获取策略绩效失败: {e}")
    
    elif choice == '3':
        print_info("更新绩效数据...")
        print_info("请运行回测或因子验证后自动更新")
    
    elif choice == '4':
        print_info("生成绩效报告...")
        try:
            from code.strategy.performance_manager import PerformanceManager
            pm = PerformanceManager()
            report_path = pm.generate_performance_report()
            if report_path:
                print_success(f"报告已生成: {report_path}")
            else:
                print_warning("暂无足够数据生成报告")
        except Exception as e:
            print_error(f"生成报告失败: {e}")
 
def baseline_model_tool():
    """基准模型工具"""
    print_header("基准模型工具")
    print_info("功能:使用简单模型建立预测基准")
    
    print(f"\n{Color.BOLD}【功能说明】{Color.ENDC}")
    print("  基准模型用于建立预测基准,评估复杂模型的增量价值")
    print("  • 线性回归:最简单的基准")
    print("  • 逻辑回归:分类基准")
    print("  • LightGBM:梯度提升树基准")
    
    print(f"\n{Color.BOLD}【模型选项】{Color.ENDC}")
    print("  1. 训练线性回归基准")
    print("  2. 训练逻辑回归基准")
    print("  3. 训练LightGBM基准")
    print("  4. 比较模型性能")
    print("  0. 返回")
    
    choice = input("\n请选择 (0-4): ").strip()
    
    if choice in ['1', '2', '3']:
        model_type = {'1': 'linear', '2': 'logistic', '3': 'lightgbm'}[choice]
        print_info(f"训练{model_type}基准模型...")
        
        data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
        if not data_file.exists():
            print_error("数据文件不存在,请先运行数据更新")
            return
        
        try:
            from code.strategy.baseline_model import BaselineModel
            import pickle
            import pandas as pd
            
            print_info(f"加载数据: {data_file.name}")
            with open(data_file, 'rb') as f:
                df = pickle.load(f)
            
            print_success(f"数据加载成功: {len(df)} 条记录")
            
            model = BaselineModel(model_type=model_type, n_features=10)
            print_info(f"模型类型: {model_type}")
            print_info(f"最大特征数: 10")
            
            print_info("\n基准模型已初始化")
            print_info("请参考 code/strategy/baseline_model.py 进行训练和预测")
            
        except ImportError as e:
            print_error(f"导入基准模型失败: {e}")
        except Exception as e:
            print_error(f"初始化失败: {e}")
    
    elif choice == '4':
        print_info("比较模型性能...")
        print_info("请先训练各模型后再进行比较")
        print("\n比较指标:")
        print("  • 准确率(Accuracy)")
        print("  • 精确率(Precision)")
        print("  • 召回率(Recall)")
        print("  • F1分数")
        print("  • AUC-ROC")
 
def market_constraint_tool():
    """市场约束检查工具"""
    print_header("市场约束检查工具")
    print_info("功能:检查涨跌停、停牌等交易限制")
    
    print(f"\n{Color.BOLD}【功能说明】{Color.ENDC}")
    print("  市场约束检查确保回测结果更接近实际交易情况")
    print("  • 涨跌停检查:判断是否可以买入/卖出")
    print("  • 停牌检查:判断是否可以交易")
    print("  • ST股票检查:特殊涨跌停限制")
    
    print(f"\n{Color.BOLD}【检查选项】{Color.ENDC}")
    print("  1. 检查单只股票交易状态")
    print("  2. 批量检查股票池")
    print("  3. 查看约束配置")
    print("  0. 返回")
    
    choice = input("\n请选择 (0-3): ").strip()
    
    if choice == '1':
        print_info("检查单只股票交易状态...")
        stock_code = input("请输入股票代码: ").strip()
        if not stock_code:
            print_error("股票代码不能为空")
            return
        
        try:
            from code.strategy.market_constraint import MarketConstraintChecker
            import pickle
            
            data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
            if data_file.exists():
                with open(data_file, 'rb') as f:
                    df = pickle.load(f)
                
                if 'stock_code' in df.columns:
                    stock_data = df[df['stock_code'] == stock_code]
                    if len(stock_data) > 0:
                        latest = stock_data.iloc[-1]
                        checker = MarketConstraintChecker()
                        status = checker.check_trading_status(latest)
                        
                        print(f"\n{Color.BOLD}【交易状态】{Color.ENDC}")
                        print(f"  股票代码: {stock_code}")
                        print(f"  交易状态: {status.value}")
                        
                        can_buy, buy_reason = checker.can_buy(latest)
                        can_sell, sell_reason = checker.can_sell(latest)
                        
                        buy_status = f"{Color.OKGREEN}可买入{Color.ENDC}" if can_buy else f"{Color.FAIL}不可买入{Color.ENDC}"
                        sell_status = f"{Color.OKGREEN}可卖出{Color.ENDC}" if can_sell else f"{Color.FAIL}不可卖出{Color.ENDC}"
                        
                        print(f"  买入状态: {buy_status} - {buy_reason}")
                        print(f"  卖出状态: {sell_status} - {sell_reason}")
                    else:
                        print_warning(f"未找到股票 {stock_code} 的数据")
            else:
                print_error("数据文件不存在")
                
        except ImportError as e:
            print_error(f"导入市场约束模块失败: {e}")
        except Exception as e:
            print_error(f"检查失败: {e}")
    
    elif choice == '2':
        print_info("批量检查股票池...")
        print_info("请先加载数据后进行检查")
    
    elif choice == '3':
        print_info("查看约束配置...")
        try:
            from code.strategy.market_constraint import MarketConstraintConfig
            config = MarketConstraintConfig()
            print(f"\n{Color.BOLD}【约束配置】{Color.ENDC}")
            print(f"  涨停阈值: {config.limit_up_threshold:.2%}")
            print(f"  跌停阈值: {config.limit_down_threshold:.2%}")
            print(f"  ST涨停阈值: {config.st_limit_up:.2%}")
            print(f"  ST跌停阈值: {config.st_limit_down:.2%}")
            print(f"  成交量参与率: {config.volume_participation_rate:.2%}")
            print(f"  最小交易金额: {config.min_trading_value:,.0f}")
        except ImportError:
            print_error("导入约束配置失败")
 
def market_microstructure_tool():
    """市场微观结构工具"""
    print_header("市场微观结构工具")
    print_info("功能:分析订单簿、流动性、价格冲击")
    
    print(f"\n{Color.BOLD}【功能说明】{Color.ENDC}")
    print("  市场微观结构模型用于分析交易成本和市场影响")
    print("  • 订单簿建模:买卖盘深度分析")
    print("  • 流动性分析:买卖价差、市场深度")
    print("  • 价格冲击模型:大单交易对价格的影响")
    print("  • 交易成本估算:滑点、冲击成本")
    
    print(f"\n{Color.BOLD}【分析选项】{Color.ENDC}")
    print("  1. 订单簿分析")
    print("  2. 流动性分析")
    print("  3. 价格冲击估算")
    print("  4. 交易成本估算")
    print("  0. 返回")
    
    choice = input("\n请选择 (0-4): ").strip()
    
    if choice == '1':
        print_info("订单簿分析...")
        try:
            from code.strategy.market_microstructure import OrderBook
            
            print_info("模拟订单簿数据...")
            symbol = input("请输入股票代码 (默认 000001): ").strip() or "000001"
            
            order_book = OrderBook(symbol)
            
            import numpy as np
            from datetime import datetime
            np.random.seed(42)
            
            base_price = 10.0
            bids = [(base_price - np.random.uniform(0.01, 0.1), int(np.random.uniform(1000, 10000))) for _ in range(5)]
            asks = [(base_price + np.random.uniform(0.01, 0.1), int(np.random.uniform(1000, 10000))) for _ in range(5)]
            
            order_book.update(bids, asks, datetime.now())
            
            print(f"\n{Color.BOLD}【订单簿快照】{Color.ENDC}")
            print(f"  股票代码: {symbol}")
            print(f"  买一价: {order_book.get_bid_price():.2f}")
            print(f"  卖一价: {order_book.get_ask_price():.2f}")
            print(f"  买卖价差: {order_book.get_spread():.4f}")
            print(f"  中间价: {order_book.get_mid_price():.2f}")
            
            print(f"\n{Color.BOLD}【市场深度】{Color.ENDC}")
            bid_depth = order_book.get_market_depth('bid', depth=3)
            ask_depth = order_book.get_market_depth('ask', depth=3)
            
            print("  买盘:")
            for level, data in bid_depth.items():
                print(f"    {level}: 价格={data['price']:.2f}, 数量={data['size']}, 累计={data['total_size']}")
            
            print("  卖盘:")
            for level, data in ask_depth.items():
                print(f"    {level}: 价格={data['price']:.2f}, 数量={data['size']}, 累计={data['total_size']}")
            
        except ImportError as e:
            print_error(f"导入微观结构模块失败: {e}")
        except Exception as e:
            print_error(f"订单簿分析失败: {e}")
    
    elif choice == '2':
        print_info("流动性分析...")
        print_info("流动性指标:")
        print("  • 买卖价差(Bid-Ask Spread)")
        print("  • 市场深度(Market Depth)")
        print("  • 成交量(Volume)")
        print("  • 换手率(Turnover Rate)")
    
    elif choice == '3':
        print_info("价格冲击估算...")
        print_info("价格冲击模型:")
        print("  • 线性模型:冲击 = k × 交易量 / 平均成交量")
        print("  • 平方根模型:冲击 = k × √(交易量 / 平均成交量)")
        print("  • 分段线性模型:区分大单和小单")
    
    elif choice == '4':
        print_info("交易成本估算...")
        print_info("交易成本组成:")
        print("  • 佣金:约 0.03%(双边)")
        print("  • 印花税:0.1%(卖出)")
        print("  • 滑点:取决于流动性")
        print("  • 冲击成本:取决于交易量")
        print("\n估算公式:")
        print("  总成本 = 佣金 + 印花税 + 滑点 + 冲击成本")
 
# ==================== 一级菜单:回测验证 ====================
 
def backtest_verification_menu():
    """回测验证菜单"""
    while True:
        print_header("回测验证")
        
        print(f"{Color.BOLD}【功能说明】{Color.ENDC}")
        print("  回测验证是策略上线前的关键环节,验证策略有效性")
        print("  手册章节:第5章 回测系统、第6章 回测引擎架构")
        
        print(f"\n{Color.BOLD}【子功能】{Color.ENDC}")
        print("  1. 运行回测 ⭐推荐")
        print("  2. Brinson归因分析")
        print("  3. 滚动性能分析")
        print("  4. 压力测试")
        print("  5. 过拟合检测")
        print("  6. 绩效对比")
        print("  0. 返回主菜单")
        
        choice = input(f"\n{Color.OKCYAN}请选择 (0-6): {Color.ENDC}").strip()
        
        if choice == '1':
            run_backtest()
        elif choice == '2':
            brinson_attribution()
        elif choice == '3':
            rolling_performance()
        elif choice == '4':
            stress_test()
        elif choice == '5':
            overfitting_detection()
        elif choice == '6':
            performance_comparison()
        elif choice == '0':
            return
        else:
            print_error("无效的选择")
        
        input(f"\n{Color.OKCYAN}按回车键继续...{Color.ENDC}")
 
def run_backtest():
    """运行回测"""
    print_header("运行回测")
    return run_script('run_backtest.py', "运行回测")
 
def brinson_attribution():
    """Brinson归因分析"""
    print_header("Brinson归因分析")
    print_info("功能:分解投资组合收益来源(配置效应、选择效应、交互效应)")
    
    try:
        from code.backtest.brinson_attribution import BrinsonAttribution
        
        print("\n归因分析说明:")
        print("  • 配置效应:行业配置带来的超额收益")
        print("  • 选择效应:行业内选股带来的超额收益")
        print("  • 交互效应:配置和选择的交互影响")
        
        print("\n选择操作:")
        print("1. 运行归因分析")
        print("2. 查看历史归因结果")
        print("0. 返回")
        
        choice = input("\n请选择 (0-2): ").strip()
        
        if choice == '1':
            print_info("运行Brinson归因分析...")
            attribution = BrinsonAttribution()
            print_info("需要准备组合权重、基准权重、资产收益等数据")
        elif choice == '2':
            print_info("查看历史归因结果...")
            print("需要加载历史归因报告")
            
    except Exception as e:
        print_error(f"Brinson归因分析启动失败: {e}")
 
def rolling_performance():
    """滚动性能分析"""
    print_header("滚动性能分析")
    print_info("功能:分析策略在不同时间窗口的表现")
    
    try:
        from code.backtest.rolling_performance import RollingPerformanceAnalyzer, RollingConfig
        
        print("\n滚动分析指标:")
        print("  • 滚动收益率")
        print("  • 滚动波动率")
        print("  • 滚动夏普比率")
        print("  • 滚动最大回撤")
        print("  • 滚动Alpha/Beta")
        
        print("\n选择操作:")
        print("1. 运行滚动分析")
        print("2. 查看分析结果")
        print("0. 返回")
        
        choice = input("\n请选择 (0-2): ").strip()
        
        if choice == '1':
            print_info("运行滚动性能分析...")
            config = RollingConfig(window=252)
            analyzer = RollingPerformanceAnalyzer(config=config)
            print_info("需要准备净值序列数据")
        elif choice == '2':
            print_info("查看分析结果...")
            print("需要加载历史分析结果")
            
    except Exception as e:
        print_error(f"滚动性能分析启动失败: {e}")
 
def stress_test():
    """压力测试"""
    print_header("压力测试")
    print_info("功能:测试策略在极端市场情况下的表现")
    
    try:
        from code.backtest.stress_test import StressTest as StressTester
        
        print("\n压力测试情景:")
        print("  • 2008年式大跌:-50%")
        print("  • 2015年式股灾:-40%")
        print("  • 2020年式疫情:-30%")
        print("  • 科技泡沫破裂:-60%")
        
        print("\n选择操作:")
        print("1. 运行压力测试")
        print("2. 查看测试结果")
        print("0. 返回")
        
        choice = input("\n请选择 (0-2): ").strip()
        
        if choice == '1':
            print_info("运行压力测试...")
            data_path = project_root / 'data' / 'akshare_real_data_fixed.pkl'
            tester = StressTester(str(data_path))
            print_info("需要准备当前持仓数据")
        elif choice == '2':
            print_info("查看测试结果...")
            print("需要加载历史测试结果")
            
    except Exception as e:
        print_error(f"压力测试启动失败: {e}")
 
def overfitting_detection():
    """过拟合检测"""
    print_header("过拟合检测")
    print_info("功能:检测策略是否存在过拟合风险")
    
    try:
        from code.backtest.overfitting_detection_enhanced import EnhancedOverfittingDetector as OverfittingDetector
        
        print("\n过拟合检测方法:")
        print("  • 样本外验证:训练集/测试集划分")
        print("  • 参数敏感性分析:关键参数稳定性")
        print("  • 滚动窗口回测:多时间段验证")
        print("  • IC衰减分析:因子有效性衰减")
        
        print("\n选择操作:")
        print("1. 运行过拟合检测")
        print("2. 查看检测结果")
        print("0. 返回")
        
        choice = input("\n请选择 (0-2): ").strip()
        
        if choice == '1':
            print_info("运行过拟合检测...")
            data_path = project_root / 'data' / 'akshare_real_data_fixed.pkl'
            detector = OverfittingDetector(str(data_path))
            print_info("需要准备策略回测结果")
        elif choice == '2':
            print_info("查看检测结果...")
            print("需要加载历史检测结果")
            
    except Exception as e:
        print_error(f"过拟合检测启动失败: {e}")
 
def performance_comparison():
    """绩效对比"""
    print_header("绩效对比系统")
    
    print(f"\n{Color.BOLD}【功能说明】{Color.ENDC}")
    print("  绩效对比系统用于比较不同因子、指标和策略的表现")
    print("  • 因子绩效对比:比较不同因子的IC、IR等指标")
    print("  • 策略绩效对比:比较不同策略的收益率、夏普比率等指标")
    print("  • 指标对比分析:分析不同评估指标的表现")
    print("  • 生成对比报告:输出详细的对比结果和分析")
    
    print("\n选择对比类型:")
    print("1. 因子绩效对比")
    print("2. 策略绩效对比")
    print("3. 指标对比分析")
    print("4. 生成完整对比报告")
    print("0. 返回")
    
    choice = input("\n请选择 (0-4): ").strip()
    
    if choice == '1':
        print_info("运行因子绩效对比...")
        run_script('run_factor_backtest_fast.py', "因子绩效对比")
    elif choice == '2':
        print_info("运行策略绩效对比...")
        run_script('backtest_system.py', "策略绩效对比")
    elif choice == '3':
        print_info("运行指标对比分析...")
        print_info("指标对比分析功能正在开发中")
    elif choice == '4':
        print_info("生成完整对比报告...")
        print_info("完整对比报告功能正在开发中")
 
# ==================== 一级菜单:实盘工程 ====================
 
def live_trading_menu():
    """实盘工程菜单"""
    while True:
        print_header("实盘工程")
        
        print(f"{Color.BOLD}【功能说明】{Color.ENDC}")
        print("  实盘工程是量化系统的执行层,包含交易、风控、资金管理")
        print("  手册章节:第6章 实盘工程、第7章 运维指南")
        
        print(f"\n{Color.BOLD}【子功能】{Color.ENDC}")
        print("  1. 每日主控流程 ⭐推荐")
        print("  2. 推送系统")
        print("  3. 持仓管理")
        print("  4. 风控系统")
        print("  5. 资金管理")
        print("  6. 风险预警")
        print("  7. 交易员助手")
        print("  8. 模拟交易")
        print("  9. 券商API接入")
        print("  0. 返回主菜单")
        
        choice = input(f"\n{Color.OKCYAN}请选择 (0-9): {Color.ENDC}").strip()
        
        if choice == '1':
            daily_master()
        elif choice == '2':
            push_system()
        elif choice == '3':
            position_management()
        elif choice == '4':
            risk_control()
        elif choice == '5':
            fund_management()
        elif choice == '6':
            risk_early_warning()
        elif choice == '7':
            trader_assistant()
        elif choice == '8':
            paper_trading()
        elif choice == '9':
            broker_api()
        elif choice == '0':
            return
        else:
            print_error("无效的选择")
        
        input(f"\n{Color.OKCYAN}按回车键继续...{Color.ENDC}")
 
def daily_master():
    """每日主控流程"""
    print_header("每日主控流程")
    return run_script('daily_master.py', "每日完整主控流程")
 
def quick_start_quant():
    """从0-1量化投资 - 一键启动"""
    print_header("从0-1量化投资 - 一键启动")
    print_info("功能:完整量化投资流程检查与启动")
    print_info("流程:数据准备 → 因子研发 → 策略开发 → 回测验证 → 风控配置 → 实盘准备")
    
    result = run_script('quick_start_quant.py', "从0-1量化投资流程", return_result=True)
    
    if result and isinstance(result, dict):
        jump_to = result.get('jump_to')
        if jump_to:
            print_info(f"跳转到: {jump_to}")
            if jump_to == 'data_engineering_menu':
                data_engineering_menu()
            elif jump_to == 'factor_research_menu':
                factor_research_menu()
            elif jump_to == 'backtest_menu':
                backtest_menu()
            elif jump_to == 'live_trading_menu':
                live_trading_menu()
    
    return result
 
def push_system():
    """推送系统"""
    print_header("推送系统")
    
    print(f"\n{Color.BOLD}【功能说明】{Color.ENDC}")
    print("  统一的推送管理入口,使用 daily_master.py 作为主控脚本")
    print("  • 盘前推送:工作日8:00,包含选股建议、因子评估、持仓状态")
    print("  • 日报推送:工作日18:30,包含当日表现、风险分析、市场总结")
    print("  • 模拟交易推送:实时推送模拟交易信号")
    
    print("\n选择推送类型:")
    print("1. 盘前推送(工作日8:00)⭐推荐")
    print("2. 日报推送(工作日18:30)⭐推荐")
    print("3. 模拟交易推送")
    print("4. 推送监控系统")
    print("0. 返回")
    
    choice = input("\n请选择 (0-4): ").strip()
    
    if choice == '1':
        print_info("执行盘前推送...")
        script_path = scripts_dir / 'daily_master.py'
        try:
            result = subprocess.run(
                [sys.executable, str(script_path)],
                cwd=project_root,
                capture_output=True,
                text=True
            )
            if result.stdout:
                print(result.stdout)
            if result.stderr:
                print_error(result.stderr)
            return result.returncode == 0
        except Exception as e:
            print_error(f"执行失败: {e}")
            return False
            
    elif choice == '2':
        print_info("执行日报推送...")
        script_path = scripts_dir / 'daily_master.py'
        try:
            result = subprocess.run(
                [sys.executable, str(script_path)],
                cwd=project_root,
                capture_output=True,
                text=True
            )
            if result.stdout:
                print(result.stdout)
            if result.stderr:
                print_error(result.stderr)
            return result.returncode == 0
        except Exception as e:
            print_error(f"执行失败: {e}")
            return False
            
    elif choice == '3':
        return run_script('paper_trading_push_v2.py', "模拟交易推送")
        
    elif choice == '4':
        return run_script('push_monitor.py', "推送监控系统")
 
def position_management():
    """持仓管理"""
    print_header("持仓管理")
    
    print(f"\n{Color.BOLD}【功能说明】{Color.ENDC}")
    print("  持仓管理是风险控制的核心,确保投资组合的风险可控")
    print("  • 凯利公式:根据胜率和盈亏比计算最优仓位")
    print("  • 动态仓位调整:根据市场情况调整仓位")
    print("  • 止损止盈:自动触发止损止盈指令")
    print("  • 持仓风险监控:实时监控持仓风险")
    
    print(f"\n{Color.BOLD}【仓位控制规则】{Color.ENDC}")
    print("  • 单股票最大仓位: 5%")
    print("  • 单行业最大仓位: 30%")
    print("  • 止损线: -15%")
    print("  • 止盈线: +30%")
    
    print_info("\n功能:凯利公式仓位计算、动态仓位调整、止损止盈管理、持仓风险监控")
    
    try:
        from code.portfolio.portfolio_tracker import PortfolioTracker
        
        print("\n选择操作:")
        print("1. 凯利公式仓位计算 ⭐推荐")
        print("2. 动态仓位调整")
        print("3. 止损止盈管理")
        print("4. 持仓风险监控")
        print("5. 查看当前持仓")
        print("6. 买入操作")
        print("7. 卖出操作")
        print("8. 持仓调整建议")
        print("9. 手动修改持仓 ⭐新增")
        print("0. 返回")
        
        choice = input("\n请选择 (0-9): ").strip()
        
        if choice == '1':
            print_info("凯利公式仓位计算...")
            print_info("公式:f* = (p * b - q) / b")
            print_info("  f* = 最优仓位比例")
            print_info("  p = 胜率")
            print_info("  q = 败率 (1 - p)")
            print_info("  b = 盈亏比")
            
            print("\n输入参数:")
            win_rate = float(input("胜率 (0-1): "))
            win_loss_ratio = float(input("盈亏比: "))
            
            kelly_fraction = (win_rate * win_loss_ratio - (1 - win_rate)) / win_loss_ratio
            kelly_fraction = max(0, min(kelly_fraction, 1))
            
            print_success(f"凯利公式最优仓位: {kelly_fraction:.2%}")
            print_warning(f"建议实际仓位: {kelly_fraction * 0.5:.2%} (半凯利)")
            
        elif choice == '2':
            print_info("动态仓位调整...")
            tracker = PortfolioTracker()
            print_info(f"\n当前总资产: ¥{tracker.total_assets:,.2f}")
            print_info(f"持仓市值: ¥{tracker.portfolio_value:,.2f}")
            print_info(f"现金: ¥{tracker.cash:,.2f}")
            
        elif choice == '3':
            print_info("止损止盈管理...")
            tracker = PortfolioTracker()
            
            if tracker.positions:
                print(f"\n{Color.BOLD}持仓止损止盈状态:{Color.ENDC}")
                for code, pos in tracker.positions.items():
                    profit_pct = pos.profit_loss_pct
                    stop_loss_price = pos.avg_price * 0.85
                    take_profit_price = pos.avg_price * 1.30
                    
                    status = "正常"
                    if pos.current_price <= stop_loss_price:
                        status = f"{Color.FAIL}止损{Color.ENDC}"
                    elif pos.current_price >= take_profit_price:
                        status = f"{Color.OKGREEN}止盈{Color.ENDC}"
                    
                    profit_color = Color.OKGREEN if profit_pct > 0 else Color.FAIL
                    print(f"  {pos.stock_code}: 盈亏 {profit_color}{profit_pct:.2f}%{Color.ENDC} | 状态: {status}")
            else:
                print_warning("当前无持仓")
                
        elif choice == '4':
            print_info("持仓风险监控...")
            tracker = PortfolioTracker()
            print_info(f"\n持仓数量: {len(tracker.positions)}")
            print_info(f"总资产: ¥{tracker.total_assets:,.2f}")
            
        elif choice == '5':
            print_info("查看当前持仓...")
            tracker = PortfolioTracker()
            
            if tracker.positions:
                print(f"\n{Color.BOLD}当前持仓:{Color.ENDC}")
                for code, pos in tracker.positions.items():
                    profit_color = Color.OKGREEN if pos.profit_loss_pct > 0 else Color.FAIL
                    print(f"  {pos.stock_name}({pos.stock_code}): {profit_color}{pos.profit_loss_pct:.2f}%{Color.ENDC}")
            else:
                print_warning("当前无持仓")
                
        elif choice == '6':
            print_info("执行买入操作...")
            tracker = PortfolioTracker()
            print(f"\n当前现金: ¥{tracker.cash:,.2f}")
            
        elif choice == '7':
            print_info("执行卖出操作...")
            tracker = PortfolioTracker()
            
        elif choice == '8':
            print_info("生成持仓调整建议...")
            tracker = PortfolioTracker()
            
        elif choice == '9':
            print_info("手动修改持仓...")
            tracker = PortfolioTracker()
            
            print(f"\n{Color.BOLD}手动持仓管理{Color.ENDC}")
            print("1. 添加持仓")
            print("2. 修改持仓")
            print("3. 删除持仓")
            print("4. 设置现金")
            print("5. 清空所有持仓")
            print("6. 查看当前持仓")
            print("0. 返回")
            
            sub_choice = input("\n请选择 (0-6): ").strip()
            
            if sub_choice == '1':
                print(f"\n{Color.BOLD}添加新持仓{Color.ENDC}")
                stock_code = input("股票代码 (如 sh600519): ").strip()
                stock_name = input("股票名称: ").strip()
                quantity = int(input("持仓数量 (股): "))
                avg_price = float(input("成本价: "))
                current_price_input = input("当前价格 (回车=成本价): ").strip()
                current_price = float(current_price_input) if current_price_input else None
                sector = input("所属行业 (可选): ").strip()
                
                try:
                    tracker.manual_add_position(stock_code, stock_name, quantity, avg_price, current_price, sector)
                    print_success("持仓添加成功!")
                except Exception as e:
                    print_error(f"添加失败: {e}")
                    
            elif sub_choice == '2':
                print(f"\n{Color.BOLD}修改持仓{Color.ENDC}")
                if not tracker.positions:
                    print_warning("当前无持仓")
                else:
                    print("当前持仓:")
                    for i, (code, pos) in enumerate(tracker.positions.items(), 1):
                        print(f"  {i}. {pos.stock_name}({code}): {pos.quantity}股 @ {pos.avg_price:.2f}元")
                    
                    idx = input("\n选择要修改的持仓序号 (或输入股票代码): ").strip()
                    
                    if idx.isdigit():
                        idx = int(idx) - 1
                        stock_code = list(tracker.positions.keys())[idx] if 0 <= idx < len(tracker.positions) else None
                    else:
                        stock_code = idx
                    
                    if stock_code and stock_code in tracker.positions:
                        pos = tracker.positions[stock_code]
                        print(f"\n当前: {pos.stock_name} {pos.quantity}股 @ {pos.avg_price:.2f}元")
                        
                        print("\n输入新值 (回车保持不变):")
                        new_quantity = input(f"持仓数量 [{pos.quantity}]: ").strip()
                        new_avg_price = input(f"成本价 [{pos.avg_price:.2f}]: ").strip()
                        new_current_price = input(f"当前价 [{pos.current_price:.2f}]: ").strip()
                        
                        updates = {}
                        if new_quantity:
                            updates['quantity'] = int(new_quantity)
                        if new_avg_price:
                            updates['avg_price'] = float(new_avg_price)
                        if new_current_price:
                            updates['current_price'] = float(new_current_price)
                        
                        if updates:
                            try:
                                tracker.manual_update_position(stock_code, **updates)
                                print_success("持仓更新成功!")
                            except Exception as e:
                                print_error(f"更新失败: {e}")
                        else:
                            print_info("未做任何修改")
                    else:
                        print_error("无效的选择")
                        
            elif sub_choice == '3':
                print(f"\n{Color.BOLD}删除持仓{Color.ENDC}")
                if not tracker.positions:
                    print_warning("当前无持仓")
                else:
                    print("当前持仓:")
                    for i, (code, pos) in enumerate(tracker.positions.items(), 1):
                        print(f"  {i}. {pos.stock_name}({code}): {pos.quantity}股")
                    
                    idx = input("\n选择要删除的持仓序号 (或输入股票代码): ").strip()
                    
                    if idx.isdigit():
                        idx = int(idx) - 1
                        stock_code = list(tracker.positions.keys())[idx] if 0 <= idx < len(tracker.positions) else None
                    else:
                        stock_code = idx
                    
                    if stock_code and stock_code in tracker.positions:
                        confirm = input(f"确认删除 {tracker.positions[stock_code].stock_name}? (y/n): ").strip().lower()
                        if confirm == 'y':
                            try:
                                tracker.manual_delete_position(stock_code)
                                print_success("持仓已删除!")
                            except Exception as e:
                                print_error(f"删除失败: {e}")
                    else:
                        print_error("无效的选择")
                        
            elif sub_choice == '4':
                print(f"\n{Color.BOLD}设置现金{Color.ENDC}")
                print(f"当前现金: ¥{tracker.cash:,.2f}")
                new_cash = input("输入新的现金金额: ").strip()
                if new_cash:
                    try:
                        tracker.manual_set_cash(float(new_cash))
                        print_success("现金已更新!")
                    except Exception as e:
                        print_error(f"设置失败: {e}")
                        
            elif sub_choice == '5':
                print(f"\n{Color.BOLD}清空所有持仓{Color.ENDC}")
                print_warning(f"当前有 {len(tracker.positions)} 只持仓")
                confirm = input("确认清空所有持仓? (y/n): ").strip().lower()
                if confirm == 'y':
                    tracker.clear_all_positions()
                    print_success("所有持仓已清空!")
                    
            elif sub_choice == '6':
                print(f"\n{Color.BOLD}当前持仓{Color.ENDC}")
                if not tracker.positions:
                    print_warning("当前无持仓")
                else:
                    print(f"现金: ¥{tracker.cash:,.2f}")
                    print(f"持仓市值: ¥{tracker.portfolio_value:,.2f}")
                    print(f"总资产: ¥{tracker.total_assets:,.2f}")
                    print()
                    for code, pos in tracker.positions.items():
                        profit_color = Color.OKGREEN if pos.profit_loss_pct > 0 else Color.FAIL
                        print(f"  {pos.stock_name}({code}): {pos.quantity}股")
                        print(f"    成本: {pos.avg_price:.2f} → 当前: {pos.current_price:.2f}")
                        print(f"    盈亏: {profit_color}{pos.profit_loss_pct:.2f}%{Color.ENDC}")
    except Exception as e:
        print_error(f"持仓管理启动失败: {e}")
        import traceback
        print_error(traceback.format_exc())
 
def risk_control():
    """风控系统"""
    print_header("风控系统")
    print_info("功能:策略容量评估、冲击成本评估、流动性风险分析、风格暴露分析")
    
    data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
    print_data_freshness(data_file, max_age_hours=24)
    
    try:
        from code.risk.risk_control_system import RiskControlSystem
        
        print("\n选择操作:")
        print("1. 完整风控分析")
        print("2. 策略容量评估")
        print("3. 流动性风险分析")
        print("4. 风格暴露分析")
        print("0. 返回")
        
        choice = input("\n请选择 (0-4): ").strip()
        
        if choice == '1':
            print_info("运行完整风控分析...")
            if data_file.exists():
                system = RiskControlSystem(str(data_file))
                capacity = system.calculate_strategy_capacity()
                impact = system.calculate_impact_cost()
                liquidity = system.analyze_liquidity_risk()
                style = system.analyze_style_exposure()
                print_success("风控分析完成")
            else:
                print_error(f"数据文件不存在: {data_file}")
                
        elif choice == '2':
            print_info("运行策略容量评估...")
            if data_file.exists():
                system = RiskControlSystem(str(data_file))
                capacity = system.calculate_strategy_capacity()
                print(f"\n策略容量评估:")
                print(f"  流动性容量: ¥{capacity.get('liquidity_capacity', 0):,.0f}")
                print(f"  推荐容量: ¥{capacity.get('recommended_capacity', 0):,.0f}")
            else:
                print_error(f"数据文件不存在: {data_file}")
                
        elif choice == '3':
            print_info("运行流动性风险分析...")
            if data_file.exists():
                system = RiskControlSystem(str(data_file))
                liquidity = system.analyze_liquidity_risk()
                print(f"\n流动性风险分析:")
                print(f"  流动性得分: {liquidity.get('liquidity_score', 'N/A')}")
            else:
                print_error(f"数据文件不存在: {data_file}")
                
        elif choice == '4':
            print_info("运行风格暴露分析...")
            if data_file.exists():
                system = RiskControlSystem(str(data_file))
                style = system.analyze_style_exposure()
                print(f"\n风格暴露分析:")
                print(f"  大盘股占比: {style.get('large_cap_ratio', 0):.2%}")
                print(f"  小盘股占比: {style.get('small_cap_ratio', 0):.2%}")
            else:
                print_error(f"数据文件不存在: {data_file}")
                
    except Exception as e:
        print_error(f"风控系统启动失败: {e}")
 
def fund_management():
    """资金管理"""
    print_header("资金管理")
    print_info("功能:智能资金分配、风险预算管理")
    
    try:
        from code.risk.fund_management import FundManager, RiskBudgetManager
        
        print("\n资金管理功能:")
        print("  • 动态资金分配:根据策略表现调整资金")
        print("  • 风险预算:各策略风险贡献相等")
        print("  • 资金调度:自动调拨资金")
        
        print("\n选择操作:")
        print("1. 查看资金状态")
        print("2. 运行风险预算")
        print("3. 资金调度建议")
        print("0. 返回")
        
        choice = input("\n请选择 (0-3): ").strip()
        
        if choice == '1':
            print_info("查看资金状态...")
            manager = FundManager()
            print_info("需要加载账户数据")
        elif choice == '2':
            print_info("运行风险预算...")
            budget_manager = RiskBudgetManager()
            print_info("需要加载策略收益数据")
        elif choice == '3':
            print_info("资金调度建议...")
            print("需要加载当前资金分配状态")
            
    except Exception as e:
        print_error(f"资金管理启动失败: {e}")
 
def risk_early_warning():
    """风险预警"""
    print_header("风险预警")
    print_info("功能:实时风险监控和预警")
    
    try:
        from code.risk.risk_early_warning import RiskEarlyWarning
        
        print("\n预警类型:")
        print("  • 净值预警:净值低于阈值")
        print("  • 回撤预警:最大回撤超限")
        print("  • 仓位预警:仓位过高/过低")
        print("  • 因子暴露预警:因子暴露过大")
        
        print("\n选择操作:")
        print("1. 运行风险预警")
        print("2. 查看预警历史")
        print("0. 返回")
        
        choice = input("\n请选择 (0-2): ").strip()
        
        if choice == '1':
            print_info("运行风险预警...")
            warning = RiskEarlyWarning()
            print_info("需要加载当前持仓和净值数据")
        elif choice == '2':
            print_info("查看预警历史...")
            print("需要加载历史预警记录")
            
    except Exception as e:
        print_error(f"风险预警启动失败: {e}")
 
def trader_assistant():
    """交易员助手"""
    print_header("交易员助手")
    print_info("功能:交易报表生成、交易员反馈系统、策略同步管理")
    
    try:
        from code.trader.trader_assistant import TradingReportGenerator
        
        print("\n选择操作:")
        print("1. 生成每日交易报表")
        print("2. 生成每周交易报表")
        print("3. 查看交易员反馈")
        print("0. 返回")
        
        choice = input("\n请选择 (0-3): ").strip()
        
        if choice == '1':
            print_info("生成每日交易报表...")
            generator = TradingReportGenerator()
            print_info("交易报表功能需要配合实际交易数据使用")
            
        elif choice == '2':
            print_info("生成每周交易报表...")
            generator = TradingReportGenerator()
            print_info("交易报表功能需要配合实际交易数据使用")
            
        elif choice == '3':
            print_info("查看交易员反馈...")
            feedback_dir = project_root / 'data' / 'feedback'
            if feedback_dir.exists():
                feedback_files = list(feedback_dir.glob('*.json'))
                if feedback_files:
                    print(f"\n找到 {len(feedback_files)} 个反馈文件:")
                    for fb_file in feedback_files[-5:]:
                        with open(fb_file, 'r', encoding='utf-8') as f:
                            feedback = json.load(f)
                            print(f"  • {fb_file.name}: {feedback.get('type', 'N/A')}")
                else:
                    print_warning("暂无交易员反馈")
            else:
                print_warning("反馈目录不存在")
                
    except Exception as e:
        print_error(f"交易员助手启动失败: {e}")
 
def paper_trading():
    """模拟交易"""
    print_header("模拟交易")
    print_info("功能:模拟盘交易系统")
    
    try:
        from code.portfolio.paper_trading import PaperTrading
        
        print("\n模拟交易功能:")
        print("  • 模拟下单")
        print("  • 持仓跟踪")
        print("  • 盈亏计算")
        
        print("\n选择操作:")
        print("1. 启动模拟交易")
        print("2. 查看模拟持仓")
        print("0. 返回")
        
        choice = input("\n请选择 (0-2): ").strip()
        
        if choice == '1':
            print_info("启动模拟交易...")
            trading = PaperTrading()
            print_info("模拟交易系统已启动")
        elif choice == '2':
            print_info("查看模拟持仓...")
            print("需要加载模拟持仓数据")
            
    except Exception as e:
        print_error(f"模拟交易启动失败: {e}")
 
def broker_api():
    """券商API接入"""
    print_header("券商API接入")
    print_info("功能:连接券商API进行实盘交易")
    
    try:
        from code.backtest.broker_api import BrokerAPIFactory
        
        print("\n支持的券商:")
        print("  • 华泰证券:HTS API")
        print("  • 中信证券:XTP API")
        print("  • 国泰君安:GTJA API")
        
        print("\n选择操作:")
        print("1. 连接华泰证券")
        print("2. 连接中信证券")
        print("3. 连接国泰君安")
        print("0. 返回")
        
        choice = input("\n请选择 (0-3): ").strip()
        
        if choice == '1':
            print_info("连接华泰证券...")
            api = BrokerAPIFactory.create_broker_api('huatai')
            print_warning("需要配置API密钥")
        elif choice == '2':
            print_info("连接中信证券...")
            api = BrokerAPIFactory.create_broker_api('citics')
            print_warning("需要配置API密钥")
        elif choice == '3':
            print_info("连接国泰君安...")
            api = BrokerAPIFactory.create_broker_api('guotai')
            print_warning("需要配置API密钥")
            
    except Exception as e:
        print_error(f"券商API接入失败: {e}")
 
# ==================== 一级菜单:系统管理 ====================
 
def system_management_menu():
    """系统管理菜单"""
    while True:
        print_header("系统管理")
        
        print(f"{Color.BOLD}【功能说明】{Color.ENDC}")
        print("  系统管理包含系统配置、定时任务、日志管理等运维功能")
        print("  手册章节:第8章 运维指南")
        
        print(f"\n{Color.BOLD}【子功能】{Color.ENDC}")
        print("  1. 交易日检查")
        print("  2. 系统验证")
        print("  3. 定时任务管理")
        print("  4. 系统配置管理")
        print("  5. 查看日志")
        print("  6. 系统健康检查")
        print("  7. 质量控制")
        print("  8. 监控仪表板")
        print("  9. 事件驱动引擎")
        print("  0. 返回主菜单")
        
        choice = input(f"\n{Color.OKCYAN}请选择 (0-9): {Color.ENDC}").strip()
        
        if choice == '1':
            check_trading_day()
        elif choice == '2':
            verify_system()
        elif choice == '3':
            cron_management()
        elif choice == '4':
            system_config()
        elif choice == '5':
            view_logs()
        elif choice == '6':
            health_check()
        elif choice == '7':
            quality_control()
        elif choice == '8':
            monitoring_dashboard()
        elif choice == '9':
            event_engine_menu()
        elif choice == '0':
            return
        else:
            print_error("无效的选择")
        
        input(f"\n{Color.OKCYAN}按回车键继续...{Color.ENDC}")
 
def check_trading_day():
    """检查是否是交易日"""
    print_header("交易日检查")
    return run_script('is_trading_day.py', "检查今天是否是交易日")
 
def verify_system():
    """系统验证"""
    print_header("系统验证")
    
    version_file = project_root / 'VERSION'
    if version_file.exists():
        with open(version_file, 'r') as f:
            version = f.read().strip()
        print(f"\n{Color.BOLD}系统版本: v{version}{Color.ENDC}")
    
    print(f"\n{Color.BOLD}【功能说明】{Color.ENDC}")
    print("  系统验证用于检查系统完整性和配置正确性")
    print("  完整验证将检查所有菜单功能模块是否正常")
    
    print("\n选择操作:")
    print("1. 运行完整系统验证 ⭐推荐")
    print("2. 运行快速验证(仅检查关键模块)")
    print("3. 查看系统版本")
    print("4. 查看更新日志")
    print("5. 从Gitee更新系统")
    print("0. 返回")
    
    choice = input("\n请选择 (0-5): ").strip()
    
    if choice == '1':
        print_info("运行完整系统验证...")
        print_info("将检查所有菜单功能模块(约100+项)")
        verify_script = scripts_dir / 'system_verification.py'
        if verify_script.exists():
            try:
                result = subprocess.run(
                    [sys.executable, str(verify_script)],
                    cwd=project_root,
                    capture_output=False,
                    text=True
                )
                return result.returncode == 0
            except Exception as e:
                print_error(f"验证失败: {e}")
                return False
        else:
            print_warning("完整验证脚本不存在,使用快速验证...")
            return run_quick_verification()
            
    elif choice == '2':
        print_info("运行快速验证...")
        return run_quick_verification()
            
    elif choice == '3':
        print_info("系统版本信息:")
        if version_file.exists():
            with open(version_file, 'r') as f:
                version = f.read().strip()
            print(f"\n{Color.BOLD}当前版本: v{version}{Color.ENDC}")
        print(f"\n{Color.BOLD}系统信息:{Color.ENDC}")
        print(f"  Python版本: {sys.version.split()[0]}")
        print(f"  项目路径: {project_root}")
        print(f"  操作系统: {sys.platform}")
        
    elif choice == '4':
        print_info("查看更新日志...")
        changelog_file = project_root / 'CHANGELOG.md'
        if changelog_file.exists():
            with open(changelog_file, 'r', encoding='utf-8') as f:
                print(f.read())
        else:
            print_warning("更新日志不存在")
            
    elif choice == '5':
        print_info("从Gitee更新系统...")
        print_warning("注意:更新前请确保已提交本地更改")
        confirm = input("确认更新?(yes/no): ").strip().lower()
        
        if confirm == 'yes':
            try:
                print_info("正在从Gitee拉取最新代码...")
                result = subprocess.run(
                    ['git', 'pull', 'origin', 'main'],
                    cwd=project_root,
                    capture_output=True,
                    text=True
                )
                
                if result.returncode == 0:
                    print_success("系统更新成功")
                    print(result.stdout)
                else:
                    print_error("更新失败")
                    print_error(result.stderr)
            except Exception as e:
                print_error(f"更新失败: {e}")
 
def run_quick_verification():
    """快速验证关键模块"""
    print_info("检查关键模块...")
    
    checks = [
        ('数据获取', 'code.data.real_data_fetcher', 'RealDataFetcher'),
        ('多因子模型', 'code.strategy.multi_factor_model', 'MultiFactorModel'),
        ('Alpha选股器', 'code.strategy.alpha_stock_selector', 'AlphaStockSelector'),
        ('风控系统', 'code.risk.risk_control_system', 'RiskControlSystem'),
        ('持仓跟踪', 'code.portfolio.portfolio_tracker', 'PortfolioTracker'),
    ]
    
    passed = 0
    failed = 0
    
    for name, module_path, class_name in checks:
        try:
            module = __import__(module_path)
            for part in module_path.split('.')[1:]:
                module = getattr(module, part)
            cls = getattr(module, class_name)
            print_success(f"{name}: 正常")
            passed += 1
        except Exception as e:
            print_error(f"{name}: 异常 - {e}")
            failed += 1
    
    data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
    if data_file.exists():
        print_success(f"数据文件: 存在")
        passed += 1
    else:
        print_warning(f"数据文件: 不存在")
    
    print(f"\n{Color.BOLD}【快速验证结果】{Color.ENDC}")
    print(f"  通过: {passed}, 失败: {failed}")
    
    if failed == 0:
        print_success("关键模块验证通过")
        return True
    else:
        print_warning(f"有 {failed} 个模块验证失败")
        return False
 
def cron_management():
    """定时任务管理"""
    print_header("定时任务管理")
    print_info("功能:安装、卸载、查看定时任务")
    
    print("\n选择操作:")
    print("1. 安装定时任务")
    print("2. 卸载定时任务")
    print("3. 查看定时任务状态")
    print("4. 查看定时任务配置")
    print("0. 返回")
    
    choice = input("\n请选择 (0-4): ").strip()
    
    if choice == '1':
        print_info("正在安装定时任务...")
        install_script = scripts_dir / 'install_cron_v2.sh'
        if install_script.exists():
            try:
                result = subprocess.run(
                    ['bash', str(install_script)],
                    cwd=project_root,
                    capture_output=True,
                    text=True
                )
                if result.stdout:
                    print(result.stdout)
                if result.stderr:
                    print_error(result.stderr)
                if result.returncode == 0:
                    print_success("定时任务安装成功")
                else:
                    print_error("定时任务安装失败")
            except Exception as e:
                print_error(f"安装失败: {e}")
        else:
            print_warning("安装脚本不存在")
            
    elif choice == '2':
        print_info("正在卸载定时任务...")
        print_warning("这将移除所有系统定时任务")
        confirm = input("确认卸载?(yes/no): ").strip().lower()
        
        if confirm == 'yes':
            try:
                result = subprocess.run(
                    ['crontab', '-l'],
                    capture_output=True,
                    text=True
                )
                
                if result.returncode == 0:
                    current_cron = result.stdout
                    lines = current_cron.split('\n')
                    filtered_lines = [line for line in lines if 'a-stock-advisor' not in line]
                    new_cron = '\n'.join(filtered_lines)
                    
                    process = subprocess.Popen(
                        ['crontab', '-'],
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        text=True
                    )
                    stdout, stderr = process.communicate(input=new_cron)
                    
                    if process.returncode == 0:
                        print_success("定时任务卸载成功")
                    else:
                        print_error(f"卸载失败: {stderr}")
            except Exception as e:
                print_error(f"卸载失败: {e}")
                
    elif choice == '3':
        print_info("查看定时任务状态...")
        try:
            result = subprocess.run(
                ['crontab', '-l'],
                capture_output=True,
                text=True
            )
            
            if result.returncode == 0:
                cron_content = result.stdout
                if 'a-stock-advisor' in cron_content:
                    print(f"\n{Color.BOLD}当前定时任务:{Color.ENDC}")
                    for line in cron_content.split('\n'):
                        if 'a-stock-advisor' in line and not line.startswith('#'):
                            print(f"  {Color.OKGREEN}{line}{Color.ENDC}")
                else:
                    print_warning("没有找到项目相关的定时任务")
            else:
                print_warning("没有配置定时任务")
        except Exception as e:
            print_error(f"查看失败: {e}")
            
    elif choice == '4':
        print_info("查看定时任务配置...")
        config_file = project_root / 'config' / 'cron_config_v2.json'
        if config_file.exists():
            try:
                with open(config_file, 'r', encoding='utf-8') as f:
                    config = json.load(f)
                
                print(f"\n{Color.BOLD}定时任务配置:{Color.ENDC}")
                print(f"版本: {config.get('version', 'N/A')}")
                print(f"更新时间: {config.get('last_updated', 'N/A')}")
                
                print(f"\n{Color.BOLD}任务列表:{Color.ENDC}")
                for task in config.get('tasks', []):
                    status = f"{Color.OKGREEN}启用{Color.ENDC}" if task.get('enabled', True) else f"{Color.FAIL}禁用{Color.ENDC}"
                    print(f"\n  {task['name']} - {status}")
                    print(f"    描述: {task['description']}")
                    print(f"    调度: {task['schedule']}")
            except Exception as e:
                print_error(f"读取配置失败: {e}")
        else:
            print_warning("配置文件不存在")
 
def system_config():
    """系统配置管理"""
    print_header("系统配置管理")
    print_info("功能:管理系统配置、API密钥、功能开关")
    
    print("\n选择操作:")
    print("1. 查看系统配置")
    print("2. 配置飞书推送")
    print("3. 配置功能开关")
    print("4. 查看配置文件路径")
    print("0. 返回")
    
    choice = input("\n请选择 (0-4): ").strip()
    
    if choice == '1':
        print_info("查看系统配置...")
        
        feishu_config_file = project_root / 'config' / 'feishu_config.json'
        if feishu_config_file.exists():
            try:
                with open(feishu_config_file, 'r', encoding='utf-8') as f:
                    feishu_config = json.load(f)
                
                print(f"\n{Color.BOLD}飞书推送配置:{Color.ENDC}")
                print(f"  Webhook URL: {feishu_config.get('webhook_url', 'N/A')[:50]}...")
                print(f"  启用状态: {'✓ 启用' if feishu_config.get('enabled', False) else '✗ 禁用'}")
            except Exception as e:
                print_error(f"读取飞书配置失败: {e}")
        
        cron_config_file = project_root / 'config' / 'cron_config_v2.json'
        if cron_config_file.exists():
            try:
                with open(cron_config_file, 'r', encoding='utf-8') as f:
                    cron_config = json.load(f)
                
                print(f"\n{Color.BOLD}定时任务配置:{Color.ENDC}")
                print(f"  任务数量: {len(cron_config.get('tasks', []))}")
                print(f"  版本: {cron_config.get('version', 'N/A')}")
            except Exception as e:
                print_error(f"读取定时任务配置失败: {e}")
                
    elif choice == '2':
        print_info("配置飞书推送...")
        feishu_config_file = project_root / 'config' / 'feishu_config.json'
        
        if feishu_config_file.exists():
            try:
                with open(feishu_config_file, 'r', encoding='utf-8') as f:
                    config = json.load(f)
                
                print(f"\n{Color.BOLD}当前配置:{Color.ENDC}")
                print(f"  Webhook URL: {config.get('webhook_url', 'N/A')[:50]}...")
                print(f"  启用状态: {'启用' if config.get('enabled', False) else '禁用'}")
                
                print(f"\n{Color.BOLD}修改配置:{Color.ENDC}")
                print("1. 修改Webhook URL")
                print("2. 启用/禁用推送")
                print("0. 返回")
                
                config_choice = input("\n请选择 (0-2): ").strip()
                
                if config_choice == '1':
                    new_url = input("请输入新的Webhook URL: ").strip()
                    if new_url:
                        config['webhook_url'] = new_url
                        with open(feishu_config_file, 'w', encoding='utf-8') as f:
                            json.dump(config, f, indent=2, ensure_ascii=False)
                        print_success("Webhook URL已更新")
                        
                elif config_choice == '2':
                    current = config.get('enabled', False)
                    config['enabled'] = not current
                    with open(feishu_config_file, 'w', encoding='utf-8') as f:
                        json.dump(config, f, indent=2, ensure_ascii=False)
                    print_success(f"推送已{'启用' if not current else '禁用'}")
                    
            except Exception as e:
                print_error(f"配置失败: {e}")
        else:
            print_warning("飞书配置文件不存在")
            
    elif choice == '3':
        print_info("配置功能开关...")
        print_info("功能开关允许启用或禁用特定功能")
        
        config_file = project_root / 'config' / 'feature_flags.json'
        
        if not config_file.exists():
            default_config = {
                "factor_mining": {"enabled": True, "auto_mining": False},
                "position_management": {"enabled": True, "kelly_criterion": True},
                "factor_library": {"enabled": True, "auto_monitor": True},
                "innovation_lab": {"enabled": True, "auto_weekly_report": True}
            }
            
            with open(config_file, 'w', encoding='utf-8') as f:
                json.dump(default_config, f, indent=2, ensure_ascii=False)
            
            print_success("已创建默认功能开关配置")
        
        try:
            with open(config_file, 'r', encoding='utf-8') as f:
                config = json.load(f)
            
            print(f"\n{Color.BOLD}功能开关配置:{Color.ENDC}")
            
            feature_map = {
                '1': ('factor_mining', '因子挖掘系统'),
                '2': ('position_management', '持仓管理'),
                '3': ('factor_library', '因子库管理'),
                '4': ('innovation_lab', '创新实验室')
            }
            
            for key, (feature_key, feature_name) in feature_map.items():
                if feature_key in config:
                    enabled = config[feature_key].get('enabled', True)
                    status = f"{Color.OKGREEN}启用{Color.ENDC}" if enabled else f"{Color.FAIL}禁用{Color.ENDC}"
                    print(f"  {key}. {feature_name} - {status}")
            
        except Exception as e:
            print_error(f"配置失败: {e}")
            
    elif choice == '4':
        print_info("配置文件路径:")
        print(f"\n{Color.BOLD}配置文件:{Color.ENDC}")
        print(f"  飞书配置: config/feishu_config.json")
        print(f"  定时任务配置: config/cron_config_v2.json")
        print(f"  功能开关配置: config/feature_flags.json")
        
        print(f"\n{Color.BOLD}数据文件:{Color.ENDC}")
        print(f"  主数据文件: data/akshare_real_data_fixed.pkl")
        print(f"  持仓状态: data/portfolio_state.json")
        print(f"  因子历史: data/factor_history.json")
 
def view_logs():
    """查看日志"""
    print_header("日志管理")
    print_info("日志记录了系统运行的详细信息,用于问题排查和性能分析")
    
    logs_dir = project_root / 'logs'
    if not logs_dir.exists():
        print_warning("日志目录不存在")
        return
    
    log_files = list(logs_dir.glob('*.log'))
    json_files = list(logs_dir.glob('*.json'))
    
    print(f"\n{Color.BOLD}选择操作:{Color.ENDC}")
    print("1. 查看系统运行日志")
    print("2. 查看健康检查报告")
    print("3. 查看错误和警告摘要")
    print("4. 查看最近日志(最后50行)")
    print("5. 日志文件列表")
    print("0. 返回")
    
    choice = input("\n请选择 (0-5): ").strip()
    
    if choice == '1':
        print_info("系统运行日志:")
        if log_files:
            print("\n可用的日志文件:")
            for i, log_file in enumerate(log_files, 1):
                size = log_file.stat().st_size / 1024
                mtime = datetime.fromtimestamp(log_file.stat().st_mtime).strftime('%Y-%m-%d %H:%M:%S')
                print(f"  {i}. {log_file.name} ({size:.1f}KB, 更新: {mtime})")
            
            print("\n0. 返回")
            file_choice = input(f"\n请选择日志文件 (0-{len(log_files)}): ")
            if file_choice.isdigit():
                idx = int(file_choice)
                if 0 < idx <= len(log_files):
                    log_file = log_files[idx - 1]
                    print(f"\n{Color.BOLD}查看日志: {log_file.name}{Color.ENDC}\n")
                    try:
                        with open(log_file, 'r', encoding='utf-8') as f:
                            lines = f.readlines()
                            for line in lines[-100:]:
                                if 'ERROR' in line or 'error' in line.lower():
                                    print(f"{Color.FAIL}{line.rstrip()}{Color.ENDC}")
                                elif 'WARNING' in line or 'warning' in line.lower():
                                    print(f"{Color.WARNING}{line.rstrip()}{Color.ENDC}")
                                elif 'SUCCESS' in line or 'success' in line.lower():
                                    print(f"{Color.OKGREEN}{line.rstrip()}{Color.ENDC}")
                                else:
                                    print(line.rstrip())
                    except Exception as e:
                        print_error(f"读取日志失败: {e}")
        else:
            print_warning("没有找到日志文件")
    
    elif choice == '2':
        print_info("健康检查报告:")
        if json_files:
            for json_file in json_files:
                if 'health' in json_file.name or 'report' in json_file.name:
                    print(f"\n{Color.BOLD}报告文件: {json_file.name}{Color.ENDC}")
                    try:
                        with open(json_file, 'r', encoding='utf-8') as f:
                            report = json.load(f)
                            print(json.dumps(report, indent=2, ensure_ascii=False))
                    except Exception as e:
                        print_error(f"读取报告失败: {e}")
        else:
            print_warning("没有找到健康检查报告")
    
    elif choice == '3':
        print_info("错误和警告摘要:")
        if log_files:
            error_count = 0
            warning_count = 0
            errors = []
            warnings = []
            
            for log_file in log_files:
                try:
                    with open(log_file, 'r', encoding='utf-8') as f:
                        for line in f:
                            if 'ERROR' in line or 'error' in line.lower():
                                error_count += 1
                                if len(errors) < 10:
                                    errors.append(f"[{log_file.name}] {line.strip()}")
                            elif 'WARNING' in line or 'warning' in line.lower():
                                warning_count += 1
                                if len(warnings) < 10:
                                    warnings.append(f"[{log_file.name}] {line.strip()}")
                except:
                    pass
            
            print(f"\n{Color.FAIL}错误总数: {error_count}{Color.ENDC}")
            print(f"{Color.WARNING}警告总数: {warning_count}{Color.ENDC}")
            
            if errors:
                print(f"\n{Color.FAIL}最近10条错误:{Color.ENDC}")
                for error in errors:
                    print(f"  {Color.FAIL}• {error}{Color.ENDC}")
            
            if warnings:
                print(f"\n{Color.WARNING}最近10条警告:{Color.ENDC}")
                for warning in warnings:
                    print(f"  {Color.WARNING}• {warning}{Color.ENDC}")
            
            if not errors and not warnings:
                print_success("系统运行正常,无错误和警告")
        else:
            print_warning("没有找到日志文件")
    
    elif choice == '4':
        print_info("最近日志(最后50行):")
        if log_files:
            latest_log = max(log_files, key=lambda f: f.stat().st_mtime)
            print(f"\n{Color.BOLD}文件: {latest_log.name}{Color.ENDC}")
            try:
                with open(latest_log, 'r', encoding='utf-8') as f:
                    lines = f.readlines()
                    for line in lines[-50:]:
                        print(line.rstrip())
            except Exception as e:
                print_error(f"读取日志失败: {e}")
        else:
            print_warning("没有找到日志文件")
    
    elif choice == '5':
        print_info("日志文件列表:")
        all_files = log_files + json_files
        if all_files:
            print(f"\n{'文件名':<30} {'大小':<10} {'修改时间':<20}")
            print("-" * 60)
            for file in sorted(all_files, key=lambda f: f.stat().st_mtime, reverse=True):
                size = file.stat().st_size / 1024
                mtime = datetime.fromtimestamp(file.stat().st_mtime).strftime('%Y-%m-%d %H:%M:%S')
                print(f"{file.name:<30} {size:>6.1f}KB  {mtime:<20}")
        else:
            print_warning("没有找到日志文件")
 
def health_check():
    """健康检查"""
    print_header("系统健康检查")
    return run_script('health_check.py', "系统健康检查")
 
def quality_control():
    """质量控制"""
    print_header("质量控制")
    print_info("功能:自动化质量控制,确保系统输出可靠性")
    
    try:
        from code.quality_control.automated_quality_control import AutomatedQualityControl
        
        print("\n质量控制功能:")
        print("  • 数据质量检查")
        print("  • 因子有效性验证")
        print("  • 投资组合验证")
        print("  • 流程完整性检查")
        
        print("\n选择操作:")
        print("1. 运行质量检查")
        print("2. 查看质量摘要")
        print("3. 查看数据准备要求")
        print("0. 返回")
        
        choice = input("\n请选择 (0-3): ").strip()
        
        if choice == '1':
            print_info("运行质量检查...")
            
            data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
            factor_file = project_root / 'data' / 'factor_dynamic_weights.json'
            portfolio_file = project_root / 'data' / 'portfolio_state.json'
            
            missing_items = []
            if not data_file.exists():
                missing_items.append("股票数据 (data/akshare_real_data_fixed.pkl)")
            if not factor_file.exists():
                missing_items.append("因子数据 (data/factor_dynamic_weights.json)")
            if not portfolio_file.exists():
                missing_items.append("投资组合数据 (data/portfolio_state.json)")
            
            if missing_items:
                print_error("以下数据文件缺失:")
                for item in missing_items:
                    print(f"  ✗ {item}")
                print(f"\n{Color.BOLD}【解决方法】{Color.ENDC}")
                print("  1. 运行「数据工程 → 数据更新」获取股票数据")
                print("  2. 运行「因子研发 → 因子挖掘」生成因子数据")
                print("  3. 运行「实盘工程 → 持仓管理」初始化投资组合")
                return
            
            qc = AutomatedQualityControl()
            print_success("质量检查完成")
            
        elif choice == '2':
            print_info("查看质量摘要...")
            qc = AutomatedQualityControl()
            summary = qc.get_quality_summary()
            print(summary)
            
        elif choice == '3':
            print(f"\n{Color.BOLD}【数据准备要求】{Color.ENDC}")
            
            print(f"\n{Color.BOLD}1. 股票数据{Color.ENDC}")
            data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
            if data_file.exists():
                print(f"  {Color.OKGREEN}✓ 已就绪: {data_file}{Color.ENDC}")
            else:
                print(f"  {Color.FAIL}✗ 未找到{Color.ENDC}")
                print(f"    → 运行「数据工程 → 数据更新」获取")
            
            print(f"\n{Color.BOLD}2. 因子数据{Color.ENDC}")
            factor_file = project_root / 'data' / 'factor_dynamic_weights.json'
            if factor_file.exists():
                print(f"  {Color.OKGREEN}✓ 已就绪: {factor_file}{Color.ENDC}")
            else:
                print(f"  {Color.FAIL}✗ 未找到{Color.ENDC}")
                print(f"    → 运行「因子研发 → 因子挖掘」生成")
            
            print(f"\n{Color.BOLD}3. 投资组合数据{Color.ENDC}")
            portfolio_file = project_root / 'data' / 'portfolio_state.json'
            if portfolio_file.exists():
                print(f"  {Color.OKGREEN}✓ 已就绪: {portfolio_file}{Color.ENDC}")
            else:
                print(f"  {Color.FAIL}✗ 未找到{Color.ENDC}")
                print(f"    → 运行「实盘工程 → 持仓管理」初始化")
            
            ready_count = sum([
                data_file.exists(),
                factor_file.exists(),
                portfolio_file.exists()
            ])
            print(f"\n{Color.BOLD}【就绪状态】{Color.ENDC}")
            print(f"  已就绪: {ready_count}/3")
            
            if ready_count < 3:
                print(f"\n{Color.WARNING}⚠ 部分数据未就绪,质量检查功能可能受限{Color.ENDC}")
            
    except Exception as e:
        print_error(f"质量控制启动失败: {e}")
 
_monitoring_thread = None
_monitoring_dashboard = None
 
def monitoring_dashboard():
    """监控仪表板"""
    global _monitoring_thread, _monitoring_dashboard
    
    print_header("监控仪表板")
    print_info("功能:实时监控系统运行状态")
    
    try:
        from code.utils.monitoring_dashboard import MonitoringDashboard
        import threading
        
        print("\n监控功能:")
        print("  • 系统健康监控")
        print("  • 市场状态监控")
        print("  • 因子表现监控")
        print("  • 投资组合风险监控")
        
        if _monitoring_thread and _monitoring_thread.is_alive():
            print(f"\n{Color.OKGREEN}✓ 监控系统正在后台运行{Color.ENDC}")
        
        print("\n选择操作:")
        print("1. 启动后台监控 ⭐推荐")
        print("2. 查看监控摘要")
        print("3. 停止监控")
        print("0. 返回")
        
        choice = input("\n请选择 (0-3): ").strip()
        
        if choice == '1':
            if _monitoring_thread and _monitoring_thread.is_alive():
                print_warning("监控系统已在运行中")
            else:
                print_info("启动后台监控...")
                _monitoring_dashboard = MonitoringDashboard()
                
                def run_monitoring():
                    try:
                        while True:
                            import time
                            time.sleep(60)
                    except Exception:
                        pass
                
                _monitoring_thread = threading.Thread(target=run_monitoring, daemon=True)
                _monitoring_thread.start()
                print_success("监控系统已在后台启动,您可以继续其他操作")
                
        elif choice == '2':
            print_info("查看监控摘要...")
            if _monitoring_dashboard:
                try:
                    summary = _monitoring_dashboard.get_summary()
                    print(f"\n{Color.BOLD}【监控摘要】{Color.ENDC}")
                    for key, value in summary.items():
                        print(f"  {key}: {value}")
                except Exception as e:
                    print_warning(f"获取监控摘要失败: {e}")
            else:
                print_warning("监控系统未启动,请先启动监控")
                
        elif choice == '3':
            if _monitoring_thread and _monitoring_thread.is_alive():
                print_info("停止监控...")
                _monitoring_thread = None
                _monitoring_dashboard = None
                print_success("监控系统已停止")
            else:
                print_warning("监控系统未在运行")
            
    except Exception as e:
        print_error(f"监控仪表板操作失败: {e}")
 
def event_engine_menu():
    """事件驱动引擎菜单"""
    print_header("事件驱动引擎")
    print_info("功能:基于事件驱动的异步处理架构")
    
    try:
        from code.utils.event_engine import EventEngine, EventType, Event
        
        print(f"\n{Color.BOLD}【事件类型】{Color.ENDC}")
        for et in EventType:
            print(f"  • {et.value}")
        
        print(f"\n{Color.BOLD}【处理器类型】{Color.ENDC}")
        print("  • DataHandler - 市场数据处理")
        print("  • StrategyHandler - 策略信号处理")
        print("  • RiskHandler - 风险检查处理")
        
        print("\n选择操作:")
        print("1. 启动事件引擎")
        print("2. 发送测试事件")
        print("3. 查看已注册处理器")
        print("0. 返回")
        
        choice = input("\n请选择 (0-3): ").strip()
        
        if choice == '1':
            print_info("启动事件引擎...")
            engine = EventEngine()
            engine.start()
            print_success("事件引擎已启动")
            print_info("按回车键停止引擎...")
            input()
            engine.stop()
            print_info("事件引擎已停止")
        elif choice == '2':
            print_info("发送测试事件...")
            engine = EventEngine()
            engine.start()
            test_event = Event(EventType.TIMER, {"test": True})
            engine.put(test_event)
            print_success("测试事件已发送")
            engine.stop()
        elif choice == '3':
            print_info("查看已注册处理器...")
            engine = EventEngine()
            print(f"已注册事件类型: {list(engine._handlers.keys())}")
            
    except Exception as e:
        print_error(f"事件引擎操作失败: {e}")
 
# ==================== 辅助功能 ====================
 
def data_quality_check():
    """数据质量检查"""
    print_header("数据质量检查")
    print_info("功能:数据质量检查、数据清洗、数据验证")
    
    data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
    print_data_freshness(data_file, max_age_hours=24)
    
    try:
        from code.data.data_quality_framework import DataQualityPipeline, DataQualityChecker, DataCleaner
        import pickle
        
        print("\n选择操作:")
        print("1. 完整数据质量Pipeline(检查+清洗+验证)⭐推荐")
        print("2. 仅数据质量检查(不清洗数据)")
        print("3. 仅数据清洗(不检查质量)")
        print("4. 数据质量修复脚本")
        print("0. 返回")
        
        choice = input("\n请选择 (0-4): ").strip()
        
        if choice == '1':
            print_info("运行完整数据质量Pipeline...")
            
            if data_file.exists():
                with open(data_file, 'rb') as f:
                    data = pickle.load(f)
                
                print_info(f"加载数据: {len(data)} 条记录")
                
                pipeline = DataQualityPipeline()
                results = pipeline.run(data, source_name="akshare_real_data")
                
                print_success(f"Pipeline完成: {results['final_records']} 条记录")
                
                if results['is_valid']:
                    print_success("数据质量验证通过")
                else:
                    print_warning("数据质量验证发现问题")
                    
                    if 'issues' in results:
                        print(f"\n{Color.BOLD}【发现问题】{Color.ENDC}")
                        for issue in results['issues'][:5]:
                            print(f"  • {issue}")
                        if len(results['issues']) > 5:
                            print(f"  ... 还有 {len(results['issues']) - 5} 个问题")
                    
                    print(f"\n{Color.BOLD}【修复选项】{Color.ENDC}")
                    print("  1. 自动修复所有问题")
                    print("  2. 查看详细问题列表")
                    print("  3. 运行数据质量修复脚本")
                    print("  0. 返回上级菜单")
                    
                    fix_choice = input("\n请选择修复方式 (0-3): ").strip()
                    
                    if fix_choice == '1':
                        print_info("正在自动修复问题...")
                        cleaner = DataCleaner()
                        cleaned_data = cleaner.clean_data(data)
                        
                        output_path = data_file.parent / 'akshare_real_data_fixed.pkl'
                        with open(output_path, 'wb') as f:
                            pickle.dump(cleaned_data, f)
                        print_success(f"修复完成,已保存到: {output_path}")
                        
                        print_info("正在重新计算增强因子...")
                        run_enhanced_factor_system(output_path)
                        
                    elif fix_choice == '2':
                        print_info("详细问题列表:")
                        report_file = project_root / 'data' / 'akshare_real_data_quality_report.json'
                        if report_file.exists():
                            import json
                            with open(report_file, 'r', encoding='utf-8') as f:
                                report = json.load(f)
                            for i, issue in enumerate(report.get('issues', [])[:20], 1):
                                print(f"  {i}. {issue}")
                        else:
                            print_warning("未找到详细报告文件")
                            
                    elif fix_choice == '3':
                        result = run_script('fix_data_quality_v2.py', "数据质量修复")
                        if result:
                            print_info("正在重新计算增强因子...")
                            run_enhanced_factor_system()
                        return result
            else:
                print_error(f"数据文件不存在: {data_file}")
                
        elif choice == '2':
            print_info("运行快速数据质量检查(不修改数据)...")
            
            if data_file.exists():
                with open(data_file, 'rb') as f:
                    data = pickle.load(f)
                
                print_info(f"加载数据: {len(data)} 条记录")
                
                checker = DataQualityChecker()
                report = checker.check_data(data)
                
                print(f"\n数据质量结果: {'✓ 合格' if report.is_valid else '⚠ 不合格'}")
                print(f"总记录数: {report.total_records}")
                print(f"问题数: {len(report.issues)}")
                
                if not report.is_valid and len(report.issues) > 0:
                    print(f"\n{Color.BOLD}【发现问题】{Color.ENDC}")
                    for i, issue in enumerate(report.issues[:5], 1):
                        print(f"  {i}. {issue}")
                    if len(report.issues) > 5:
                        print(f"  ... 还有 {len(report.issues) - 5} 个问题")
                    
                    print(f"\n{Color.BOLD}【后续操作】{Color.ENDC}")
                    print("  1. 运行完整Pipeline修复")
                    print("  2. 运行数据清洗")
                    print("  3. 运行修复脚本")
                    print("  0. 返回上级菜单")
                    
                    fix_choice = input("\n请选择 (0-3): ").strip()
                    
                    if fix_choice == '1':
                        print_info("运行完整Pipeline...")
                        pipeline = DataQualityPipeline()
                        results = pipeline.run(data, source_name="akshare_real_data")
                        print_success(f"Pipeline完成: {results['final_records']} 条记录")
                    elif fix_choice == '2':
                        print_info("运行数据清洗...")
                        cleaner = DataCleaner()
                        cleaned_data = cleaner.clean_data(data)
                        output_path = data_file.parent / 'akshare_real_data_cleaned.pkl'
                        with open(output_path, 'wb') as f:
                            pickle.dump(cleaned_data, f)
                        print_success(f"清洗完成,已保存到: {output_path}")
                        
                        print_info("正在重新计算增强因子...")
                        run_enhanced_factor_system(output_path)
                    elif fix_choice == '3':
                        result = run_script('fix_data_quality_v2.py', "数据质量修复")
                        if result:
                            print_info("正在重新计算增强因子...")
                            run_enhanced_factor_system()
                        return result
            else:
                print_error(f"数据文件不存在: {data_file}")
                
        elif choice == '3':
            print_info("运行数据清洗(不检查质量)...")
            
            if data_file.exists():
                with open(data_file, 'rb') as f:
                    data = pickle.load(f)
                
                print_info(f"加载数据: {len(data)} 条记录")
                
                cleaner = DataCleaner()
                cleaned_data = cleaner.clean_data(data)
                
                print_success(f"清洗完成: {len(cleaned_data)} 条记录")
                
                output_path = data_file.parent / 'akshare_real_data_cleaned.pkl'
                with open(output_path, 'wb') as f:
                    pickle.dump(cleaned_data, f)
                print_success(f"已保存到: {output_path}")
                
                print(f"\n{Color.BOLD}【后续操作】{Color.ENDC}")
                print("  1. 验证清洗后的数据质量")
                print("  2. 替换原始数据文件")
                print("  0. 返回上级菜单")
                
                next_choice = input("\n请选择 (0-2): ").strip()
                
                if next_choice == '1':
                    print_info("验证清洗后的数据质量...")
                    checker = DataQualityChecker()
                    report = checker.check_data(cleaned_data)
                    print(f"\n数据质量结果: {'✓ 合格' if report.is_valid else '⚠ 不合格'}")
                    print(f"问题数: {len(report.issues)}")
                elif next_choice == '2':
                    import shutil
                    backup_path = data_file.parent / f'akshare_real_data_backup_{datetime.now().strftime("%Y%m%d_%H%M")}.pkl'
                    shutil.copy(data_file, backup_path)
                    print_success(f"原数据已备份到: {backup_path}")
                    shutil.copy(output_path, data_file)
                    print_success(f"已替换原始数据文件")
            else:
                print_error(f"数据文件不存在: {data_file}")
                
        elif choice == '4':
            print_info("运行数据质量修复脚本...")
            return run_script('fix_data_quality_v2.py', "数据质量修复")
            
    except Exception as e:
        print_error(f"数据质量检查失败: {e}")
        print_info("尝试直接运行脚本...")
        return run_script('fix_data_quality_v2.py', "数据质量修复")
 
def factor_library():
    """因子库管理"""
    print_header("因子库管理")
    print_info("功能:查看因子库、因子启用/禁用、因子表现分析")
    
    try:
        from code.quality_control.factor_monitor import FactorMonitor
        
        print("\n选择操作:")
        print("1. 查看因子库")
        print("2. 因子启用/禁用")
        print("3. 因子表现分析")
        print("0. 返回")
        
        choice = input("\n请选择 (0-3): ").strip()
        
        if choice == '1':
            print_info("查看因子库...")
            monitor = FactorMonitor()
            
            if monitor.factor_history:
                print(f"\n{Color.BOLD}因子库列表:{Color.ENDC}")
                for factor_name, history in monitor.factor_history.items():
                    status = f"{Color.OKGREEN}启用{Color.ENDC}" if history.get('enabled', True) else f"{Color.FAIL}禁用{Color.ENDC}"
                    ic_mean = history.get('ic_mean', 0)
                    print(f"  {factor_name}: {status} | IC均值: {ic_mean:.4f}")
            else:
                print_warning("因子库为空,请先运行因子挖掘")
                
        elif choice == '2':
            print_info("因子启用/禁用...")
            monitor = FactorMonitor()
            
            if monitor.factor_history:
                print("\n当前因子状态:")
                for i, (factor_name, history) in enumerate(monitor.factor_history.items(), 1):
                    status = "启用" if history.get('enabled', True) else "禁用"
                    print(f"  {i}. {factor_name} - {status}")
                
                print("\n输入因子编号进行切换(0返回):")
                factor_choice = input("请选择: ").strip()
                
                if factor_choice.isdigit():
                    idx = int(factor_choice)
                    if 0 < idx <= len(monitor.factor_history):
                        factor_name = list(monitor.factor_history.keys())[idx - 1]
                        current_status = monitor.factor_history[factor_name].get('enabled', True)
                        monitor.factor_history[factor_name]['enabled'] = not current_status
                        monitor._save_factor_history()
                        
                        new_status = "启用" if not current_status else "禁用"
                        print_success(f"因子 {factor_name} 已{new_status}")
            else:
                print_warning("因子库为空")
                
        elif choice == '3':
            print_info("因子表现分析...")
            print_info("需要加载因子历史数据进行详细分析")
            
    except Exception as e:
        print_error(f"因子库管理启动失败: {e}")
 
def factor_backtest():
    """因子回测"""
    print_header("因子回测")
    
    print(f"\n{Color.BOLD}【功能说明】{Color.ENDC}")
    print("  因子回测用于验证新挖掘因子的有效性")
    print("  • 支持单个因子回测")
    print("  • 支持多因子组合回测")
    print("  • 支持股票级别的因子表现分析")
    
    print("\n选择回测类型:")
    print("1. 单个因子回测")
    print("2. 多因子组合回测")
    print("3. 股票级别因子分析")
    print("4. 因子绩效对比(并行优化)⭐推荐")
    print("0. 返回")
    
    choice = input("\n请选择 (0-4): ").strip()
    
    if choice == '1':
        print_info("运行单个因子回测...")
        run_script('run_factor_backtest_fast.py', "单个因子回测")
    elif choice == '2':
        print_info("运行多因子组合回测...")
        run_script('run_factor_backtest_fast.py', "多因子组合回测")
    elif choice == '3':
        print_info("运行股票级别因子分析...")
        run_script('run_factor_backtest_fast.py', "股票级别因子分析")
    elif choice == '4':
        print_info("运行因子绩效对比(并行优化)...")
        run_script('run_factor_backtest_fast.py', "因子绩效对比")
 
def portfolio_optimization():
    """组合优化"""
    print_header("组合优化")
    print_info("功能:均值方差优化、风险平价、最大夏普、风险预算等优化方法")
    
    try:
        from code.portfolio.portfolio_optimizer import PortfolioOptimizer, OptimizationMethod
        
        print("\n选择优化方法:")
        print("1. 等权重优化")
        print("2. 均值方差优化")
        print("3. 最小方差优化")
        print("4. 最大夏普优化")
        print("5. 风险平价优化")
        print("6. 风险预算优化")
        print("0. 返回")
        
        choice = input("\n请选择 (0-6): ").strip()
        
        if choice == '0':
            return
        elif choice in ['1', '2', '3', '4', '5', '6']:
            print_info("运行组合优化...")
            print_warning("注意:需要准备预期收益率和协方差矩阵数据")
            print_info("请参考 code/portfolio/portfolio_optimizer.py 使用示例")
        else:
            print_error("无效的选择")
            
    except Exception as e:
        print_error(f"组合优化启动失败: {e}")
 
# ==================== 主菜单 ====================
 
def show_menu():
    """显示主菜单"""
    while True:
        print_header("A股量化系统 - 主菜单")
        
        data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
        if data_file.exists():
            freshness = check_data_freshness(data_file, max_age_hours=24)
            
            if not freshness['is_fresh']:
                print(f"\n{Color.WARNING}{'='*60}{Color.ENDC}")
                print(f"{Color.WARNING}⚠️ 数据时效性警告{Color.ENDC}")
                print(f"{Color.WARNING}  数据时间: {freshness['data_time'].strftime('%Y-%m-%d %H:%M:%S') if freshness['data_time'] else '未知'}{Color.ENDC}")
                print(f"{Color.WARNING}  数据年龄: {freshness['age_description']}{Color.ENDC}")
                print(f"{Color.WARNING}  建议: 运行选项1「数据工程」获取最新数据{Color.ENDC}")
                print(f"{Color.WARNING}{'='*60}{Color.ENDC}\n")
        
        print(f"{Color.BOLD}【量化流程五阶段】{Color.ENDC}")
        print(f"{Color.OKCYAN}  数据工程 → 因子研发 → 策略开发 → 回测验证 → 实盘工程{Color.ENDC}")
        
        print(f"\n{Color.BOLD}【一级菜单】{Color.ENDC}")
        print("  1. 数据工程        - 数据获取、质量检查、时效性管理")
        print("  2. 因子研发        - 因子挖掘、评估、监控")
        print("  3. 策略开发        - 多因子模型、选股器、再平衡")
        print("  4. 回测验证        - 回测、归因、压力测试")
        print("  5. 实盘工程        - 推送、风控、资金管理")
        print("  6. 系统管理        - 配置、日志、健康检查")
        
        print(f"\n{Color.BOLD}【快捷入口】{Color.ENDC}")
        print("  7. 从0-1量化投资(一键启动)⭐新增")
        print("  8. 每日主控流程(完整流水线)⭐推荐")
        print("  9. 盘前推送 ⭐推荐")
        print("  10. 日报推送 ⭐推荐")
        
        print(f"\n{Color.BOLD} 0. 退出系统{Color.ENDC}")
        
        choice = input(f"\n{Color.OKCYAN}请选择操作 (0-10): {Color.ENDC}")
        
        should_pause = True
        
        if choice == '0':
            print_success("感谢使用,再见!")
            break
        elif choice == '1':
            data_engineering_menu()
        elif choice == '2':
            factor_research_menu()
        elif choice == '3':
            strategy_development_menu()
        elif choice == '4':
            backtest_verification_menu()
        elif choice == '5':
            live_trading_menu()
        elif choice == '6':
            system_management_menu()
        elif choice == '7':
            quick_start_quant()
        elif choice == '8':
            daily_master()
        elif choice == '9':
            print_info("执行盘前推送...")
            script_path = scripts_dir / 'unified_daily_push.py'
            try:
                result = subprocess.run(
                    [sys.executable, str(script_path), '--type', 'morning'],
                    cwd=project_root,
                    capture_output=True,
                    text=True
                )
                if result.stdout:
                    print(result.stdout)
                if result.stderr:
                    print_error(result.stderr)
            except Exception as e:
                print_error(f"执行失败: {e}")
        elif choice == '10':
            print_info("执行日报推送...")
            script_path = scripts_dir / 'unified_daily_push.py'
            try:
                result = subprocess.run(
                    [sys.executable, str(script_path), '--type', 'evening'],
                    cwd=project_root,
                    capture_output=True,
                    text=True
                )
                if result.stdout:
                    print(result.stdout)
                if result.stderr:
                    print_error(result.stderr)
            except Exception as e:
                print_error(f"执行失败: {e}")
        else:
            print_error("无效的选择,请重新输入")
            should_pause = False
        
        if should_pause:
            input(f"\n{Color.OKCYAN}按回车键继续...{Color.ENDC}")
 
def main():
    """主函数"""
    logs_dir = project_root / 'logs'
    logs_dir.mkdir(exist_ok=True)
    
    os.chdir(project_root)
    
    print_header("欢迎使用A股量化系统")
    print(f"{Color.OKBLUE}项目路径: {project_root}{Color.ENDC}")
    print(f"{Color.OKBLUE}当前时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}{Color.ENDC}")
    print(f"{Color.OKGREEN}✓ 核心模块已加载{Color.ENDC}")
    
    print(f"\n{Color.BOLD}【系统架构】{Color.ENDC}")
    print(f"{Color.OKCYAN}  五阶段框架:数据工程 → 因子研发 → 策略开发 → 回测验证 → 实盘工程{Color.ENDC}")
    print(f"{Color.OKCYAN}  管理理念:从「寻找圣杯」到「管理不确定性」{Color.ENDC}")
    
    show_menu()
 
if __name__ == '__main__':
    main()