Spaces:
Runtime error
Runtime error
zwq2018
commited on
Commit
•
2b91026
1
Parent(s):
260efde
Add application file
Browse files- .DS_Store +0 -0
- SW2021_industry_L1.csv +32 -0
- SW2021_industry_L2.csv +135 -0
- SW2021_industry_L3.csv +347 -0
- app.py +209 -0
- backup.json +11 -0
- create_tool/Atomic_api_json.py +360 -0
- create_tool/all_atomic_api.json +151 -0
- lab_gpt4_call.py +173 -0
- logo/openai.png +0 -0
- logo/tushare.png +0 -0
- main.py +390 -0
- output/长沙银行及其相关信息.csv +2 -0
- prompt_lib/prompt_economic.json +12 -0
- prompt_lib/prompt_financial.json +11 -0
- prompt_lib/prompt_fund.json +14 -0
- prompt_lib/prompt_intent_detection.json +36 -0
- prompt_lib/prompt_stock.json +16 -0
- prompt_lib/prompt_task.json +32 -0
- prompt_lib/prompt_visualization.json +21 -0
- requirements.txt +16 -0
- tool.py +1928 -0
- tool_lib/.DS_Store +0 -0
- tool_lib/atomic_api.json +9 -0
- tool_lib/tool_backup.json +8 -0
- tool_lib/tool_economic.json +9 -0
- tool_lib/tool_financial.json +8 -0
- tool_lib/tool_fund.json +10 -0
- tool_lib/tool_stock.json +13 -0
- tool_lib/tool_visualization.json +6 -0
- tushare_daily_20230421211129.csv +0 -0
- tushare_fund_basic_20230508193747.csv +0 -0
- tushare_fund_basic_20230516041211.csv +0 -0
- tushare_fund_basic_20230605184116.csv +0 -0
- tushare_fund_basic_20230605184607.csv +0 -0
- tushare_fund_basic_all.csv +0 -0
- tushare_index_basic_20230427223903.csv +0 -0
- tushare_stock_basic_20230421210721.csv +0 -0
.DS_Store
ADDED
Binary file (14.3 kB). View file
|
|
SW2021_industry_L1.csv
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"index_code","industry_name","level","industry_code","is_pub","parent_code"
|
2 |
+
"801010.SI","农林牧渔","L1","110000","1","0"
|
3 |
+
"801030.SI","基础化工","L1","220000","1","0"
|
4 |
+
"801040.SI","钢铁","L1","230000","1","0"
|
5 |
+
"801050.SI","有色金属","L1","240000","1","0"
|
6 |
+
"801080.SI","电子","L1","270000","1","0"
|
7 |
+
"801880.SI","汽车","L1","280000","1","0"
|
8 |
+
"801110.SI","家用电器","L1","330000","1","0"
|
9 |
+
"801120.SI","食品饮料","L1","340000","1","0"
|
10 |
+
"801130.SI","纺织服饰","L1","350000","1","0"
|
11 |
+
"801140.SI","轻工制造","L1","360000","1","0"
|
12 |
+
"801150.SI","医药生物","L1","370000","1","0"
|
13 |
+
"801160.SI","公用事业","L1","410000","1","0"
|
14 |
+
"801170.SI","交通运输","L1","420000","1","0"
|
15 |
+
"801180.SI","房地产","L1","430000","1","0"
|
16 |
+
"801200.SI","商贸零售","L1","450000","1","0"
|
17 |
+
"801210.SI","社会服务","L1","460000","1","0"
|
18 |
+
"801780.SI","银行","L1","480000","1","0"
|
19 |
+
"801790.SI","非银金融","L1","490000","1","0"
|
20 |
+
"801230.SI","综合","L1","510000","1","0"
|
21 |
+
"801710.SI","建筑材料","L1","610000","1","0"
|
22 |
+
"801720.SI","建筑装饰","L1","620000","1","0"
|
23 |
+
"801730.SI","电力设备","L1","630000","1","0"
|
24 |
+
"801890.SI","机械设备","L1","640000","1","0"
|
25 |
+
"801740.SI","国防军工","L1","650000","1","0"
|
26 |
+
"801750.SI","计算机","L1","710000","1","0"
|
27 |
+
"801760.SI","传媒","L1","720000","1","0"
|
28 |
+
"801770.SI","通信","L1","730000","1","0"
|
29 |
+
"801950.SI","煤炭","L1","740000","1","0"
|
30 |
+
"801960.SI","石油石化","L1","750000","1","0"
|
31 |
+
"801970.SI","环保","L1","760000","1","0"
|
32 |
+
"801980.SI","美容护理","L1","770000","1","0"
|
SW2021_industry_L2.csv
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"index_code","industry_name","level","industry_code","is_pub","parent_code"
|
2 |
+
"801016.SI","种植业","L2","110100","1","110000"
|
3 |
+
"801015.SI","渔业","L2","110200","1","110000"
|
4 |
+
"801011.SI","林业Ⅱ","L2","110300","0","110000"
|
5 |
+
"801014.SI","饲料","L2","110400","1","110000"
|
6 |
+
"801012.SI","农产品加工","L2","110500","1","110000"
|
7 |
+
"801017.SI","养殖业","L2","110700","1","110000"
|
8 |
+
"801018.SI","动物保健Ⅱ","L2","110800","1","110000"
|
9 |
+
"801019.SI","农业综合Ⅱ","L2","110900","0","110000"
|
10 |
+
"801033.SI","化学原料","L2","220200","1","220000"
|
11 |
+
"801034.SI","化学制品","L2","220300","1","220000"
|
12 |
+
"801032.SI","化学纤维","L2","220400","1","220000"
|
13 |
+
"801036.SI","塑料","L2","220500","1","220000"
|
14 |
+
"801037.SI","橡胶","L2","220600","1","220000"
|
15 |
+
"801038.SI","农化制品","L2","220800","1","220000"
|
16 |
+
"801039.SI","非金属材料Ⅱ","L2","220900","1","220000"
|
17 |
+
"801043.SI","冶钢原料","L2","230300","1","230000"
|
18 |
+
"801044.SI","普钢","L2","230400","1","230000"
|
19 |
+
"801045.SI","特钢Ⅱ","L2","230500","1","230000"
|
20 |
+
"801051.SI","金属新材料","L2","240200","1","240000"
|
21 |
+
"801055.SI","工业金属","L2","240300","1","240000"
|
22 |
+
"801053.SI","贵金属","L2","240400","1","240000"
|
23 |
+
"801054.SI","小金属","L2","240500","1","240000"
|
24 |
+
"801056.SI","能源金属","L2","240600","1","240000"
|
25 |
+
"801081.SI","半导体","L2","270100","1","270000"
|
26 |
+
"801083.SI","元件","L2","270200","1","270000"
|
27 |
+
"801084.SI","光学光电子","L2","270300","1","270000"
|
28 |
+
"801082.SI","其他电子Ⅱ","L2","270400","1","270000"
|
29 |
+
"801085.SI","消费电子","L2","270500","1","270000"
|
30 |
+
"801086.SI","电子化学品Ⅱ","L2","270600","1","270000"
|
31 |
+
"801093.SI","汽车零部件","L2","280200","1","280000"
|
32 |
+
"801092.SI","汽车服务","L2","280300","1","280000"
|
33 |
+
"801881.SI","摩托车及其他","L2","280400","1","280000"
|
34 |
+
"801095.SI","乘用车","L2","280500","1","280000"
|
35 |
+
"801096.SI","商用车","L2","280600","1","280000"
|
36 |
+
"801111.SI","白色家电","L2","330100","1","330000"
|
37 |
+
"801112.SI","黑色家电","L2","330200","1","330000"
|
38 |
+
"801113.SI","小家电","L2","330300","1","330000"
|
39 |
+
"801114.SI","厨卫电器","L2","330400","1","330000"
|
40 |
+
"801115.SI","照明设备Ⅱ","L2","330500","1","330000"
|
41 |
+
"801116.SI","家电零部件Ⅱ","L2","330600","1","330000"
|
42 |
+
"801117.SI","其他家电Ⅱ","L2","330700","0","330000"
|
43 |
+
"801124.SI","食品加工","L2","340400","1","340000"
|
44 |
+
"801125.SI","白酒Ⅱ","L2","340500","1","340000"
|
45 |
+
"801126.SI","非白酒","L2","340600","1","340000"
|
46 |
+
"801127.SI","饮料乳品","L2","340700","1","340000"
|
47 |
+
"801128.SI","休闲食品","L2","340800","1","340000"
|
48 |
+
"801129.SI","调味发酵品Ⅱ","L2","340900","1","340000"
|
49 |
+
"801131.SI","纺织制造","L2","350100","1","350000"
|
50 |
+
"801132.SI","服装家纺","L2","350200","1","350000"
|
51 |
+
"801133.SI","饰品","L2","350300","1","350000"
|
52 |
+
"801143.SI","造纸","L2","360100","1","360000"
|
53 |
+
"801141.SI","包装印刷","L2","360200","1","360000"
|
54 |
+
"801142.SI","家居用品","L2","360300","1","360000"
|
55 |
+
"801145.SI","文娱用品","L2","360500","1","360000"
|
56 |
+
"801151.SI","化学制药","L2","370100","1","370000"
|
57 |
+
"801155.SI","中药Ⅱ","L2","370200","1","370000"
|
58 |
+
"801152.SI","生物制品","L2","370300","1","370000"
|
59 |
+
"801154.SI","医药商业","L2","370400","1","370000"
|
60 |
+
"801153.SI","医疗器械","L2","370500","1","370000"
|
61 |
+
"801156.SI","医疗服务","L2","370600","1","370000"
|
62 |
+
"801161.SI","电力","L2","410100","1","410000"
|
63 |
+
"801163.SI","燃气Ⅱ","L2","410300","1","410000"
|
64 |
+
"801178.SI","物流","L2","420800","1","420000"
|
65 |
+
"801179.SI","铁路公路","L2","420900","1","420000"
|
66 |
+
"801991.SI","航空机场","L2","421000","1","420000"
|
67 |
+
"801992.SI","航运港口","L2","421100","1","420000"
|
68 |
+
"801181.SI","房地产开发","L2","430100","1","430000"
|
69 |
+
"801183.SI","房地产服务","L2","430300","1","430000"
|
70 |
+
"801202.SI","贸易Ⅱ","L2","450200","1","450000"
|
71 |
+
"801203.SI","一般零售","L2","450300","1","450000"
|
72 |
+
"801204.SI","专业连锁Ⅱ","L2","450400","1","450000"
|
73 |
+
"801206.SI","互联网电商","L2","450600","1","450000"
|
74 |
+
"801207.SI","旅游零售Ⅱ","L2","450700","0","450000"
|
75 |
+
"801216.SI","体育Ⅱ","L2","460600","0","460000"
|
76 |
+
"801217.SI","本地生活服务Ⅱ","L2","460700","0","460000"
|
77 |
+
"801218.SI","专业服务","L2","460800","1","460000"
|
78 |
+
"801219.SI","酒店餐饮","L2","460900","1","460000"
|
79 |
+
"801993.SI","旅游及景区","L2","461000","1","460000"
|
80 |
+
"801994.SI","教育","L2","461100","1","460000"
|
81 |
+
"801782.SI","国有大型银行Ⅱ","L2","480200","1","480000"
|
82 |
+
"801783.SI","股份制银行Ⅱ","L2","480300","1","480000"
|
83 |
+
"801784.SI","城商行Ⅱ","L2","480400","1","480000"
|
84 |
+
"801785.SI","农商行Ⅱ","L2","480500","1","480000"
|
85 |
+
"801786.SI","其他银行Ⅱ","L2","480600","0","480000"
|
86 |
+
"801193.SI","证券Ⅱ","L2","490100","1","490000"
|
87 |
+
"801194.SI","保险Ⅱ","L2","490200","1","490000"
|
88 |
+
"801191.SI","多元金融","L2","490300","1","490000"
|
89 |
+
"801231.SI","综合Ⅱ","L2","510100","1","510000"
|
90 |
+
"801711.SI","水泥","L2","610100","1","610000"
|
91 |
+
"801712.SI","玻璃玻纤","L2","610200","1","610000"
|
92 |
+
"801713.SI","装修建材","L2","610300","1","610000"
|
93 |
+
"801721.SI","房屋建设Ⅱ","L2","620100","1","620000"
|
94 |
+
"801722.SI","装修装饰Ⅱ","L2","620200","1","620000"
|
95 |
+
"801723.SI","基础建设","L2","620300","1","620000"
|
96 |
+
"801724.SI","专业工程","L2","620400","1","620000"
|
97 |
+
"801726.SI","工程咨询服务Ⅱ","L2","620600","1","620000"
|
98 |
+
"801731.SI","电机Ⅱ","L2","630100","1","630000"
|
99 |
+
"801733.SI","其他电源设备Ⅱ","L2","630300","1","630000"
|
100 |
+
"801735.SI","光伏设备","L2","630500","1","630000"
|
101 |
+
"801736.SI","风电设备","L2","630600","1","630000"
|
102 |
+
"801737.SI","电池","L2","630700","1","630000"
|
103 |
+
"801738.SI","电网设备","L2","630800","1","630000"
|
104 |
+
"801072.SI","通用设备","L2","640100","1","640000"
|
105 |
+
"801074.SI","专用设备","L2","640200","1","640000"
|
106 |
+
"801076.SI","轨交设备Ⅱ","L2","640500","1","640000"
|
107 |
+
"801077.SI","工程机械","L2","640600","1","640000"
|
108 |
+
"801078.SI","自动化设备","L2","640700","1","640000"
|
109 |
+
"801741.SI","航天装备Ⅱ","L2","650100","1","650000"
|
110 |
+
"801742.SI","航空装备Ⅱ","L2","650200","1","650000"
|
111 |
+
"801743.SI","地面兵装Ⅱ","L2","650300","1","650000"
|
112 |
+
"801744.SI","航海装备Ⅱ","L2","650400","1","650000"
|
113 |
+
"801745.SI","军工电子Ⅱ","L2","650500","1","650000"
|
114 |
+
"801101.SI","计算机设备","L2","710100","1","710000"
|
115 |
+
"801103.SI","IT服务Ⅱ","L2","710300","1","710000"
|
116 |
+
"801104.SI","软件开发","L2","710400","1","710000"
|
117 |
+
"801764.SI","游戏Ⅱ","L2","720400","1","720000"
|
118 |
+
"801765.SI","广告营销","L2","720500","1","720000"
|
119 |
+
"801766.SI","影视院线","L2","720600","1","720000"
|
120 |
+
"801767.SI","数字媒体","L2","720700","1","720000"
|
121 |
+
"801768.SI","社交Ⅱ","L2","720800","0","720000"
|
122 |
+
"801769.SI","出版","L2","720900","1","720000"
|
123 |
+
"801995.SI","电视广播Ⅱ","L2","721000","1","720000"
|
124 |
+
"801223.SI","通信服务","L2","730100","1","730000"
|
125 |
+
"801102.SI","通信设备","L2","730200","1","730000"
|
126 |
+
"801951.SI","煤炭开采","L2","740100","1","740000"
|
127 |
+
"801952.SI","焦炭Ⅱ","L2","740200","1","740000"
|
128 |
+
"801961.SI","油气开采Ⅱ","L2","750100","0","750000"
|
129 |
+
"801962.SI","油服工程","L2","750200","1","750000"
|
130 |
+
"801963.SI","炼化及贸易","L2","750300","1","750000"
|
131 |
+
"801971.SI","环境治理","L2","760100","1","760000"
|
132 |
+
"801972.SI","环保设备Ⅱ","L2","760200","1","760000"
|
133 |
+
"801981.SI","个护用品","L2","770100","1","770000"
|
134 |
+
"801982.SI","化妆品","L2","770200","1","770000"
|
135 |
+
"801983.SI","医疗美容","L2","770300","0","770000"
|
SW2021_industry_L3.csv
ADDED
@@ -0,0 +1,347 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"index_code","industry_name","level","industry_code","is_pub","parent_code"
|
2 |
+
"850111.SI","种子","L3","110101","1","110100"
|
3 |
+
"850112.SI","粮食种植","L3","110102","0","110100"
|
4 |
+
"850113.SI","其他种植业","L3","110103","1","110100"
|
5 |
+
"850114.SI","食用菌","L3","110104","0","110100"
|
6 |
+
"850121.SI","海洋捕捞","L3","110201","0","110200"
|
7 |
+
"850122.SI","水产养殖","L3","110202","1","110200"
|
8 |
+
"850131.SI","林业Ⅲ","L3","110301","0","110300"
|
9 |
+
"850142.SI","畜禽饲料","L3","110402","1","110400"
|
10 |
+
"850143.SI","水产饲料","L3","110403","0","110400"
|
11 |
+
"850144.SI","宠物食品","L3","110404","0","110400"
|
12 |
+
"850151.SI","果蔬加工","L3","110501","1","110500"
|
13 |
+
"850152.SI","粮油加工","L3","110502","1","110500"
|
14 |
+
"850154.SI","其他农产品加工","L3","110504","1","110500"
|
15 |
+
"850172.SI","生猪养殖","L3","110702","1","110700"
|
16 |
+
"850173.SI","肉鸡养殖","L3","110703","1","110700"
|
17 |
+
"850174.SI","其他养殖","L3","110704","0","110700"
|
18 |
+
"850181.SI","动物保健Ⅲ","L3","110801","1","110800"
|
19 |
+
"850191.SI","农业综合Ⅲ","L3","110901","0","110900"
|
20 |
+
"850321.SI","纯碱","L3","220201","0","220200"
|
21 |
+
"850322.SI","氯碱","L3","220202","1","220200"
|
22 |
+
"850323.SI","无机盐","L3","220203","1","220200"
|
23 |
+
"850324.SI","其他化学原料","L3","220204","1","220200"
|
24 |
+
"850325.SI","煤化工","L3","220205","1","220200"
|
25 |
+
"850326.SI","钛白粉","L3","220206","1","220200"
|
26 |
+
"850335.SI","涂料油墨","L3","220305","1","220300"
|
27 |
+
"850337.SI","民爆制品","L3","220307","1","220300"
|
28 |
+
"850338.SI","纺织化学制品","L3","220308","1","220300"
|
29 |
+
"850339.SI","其他化学制品","L3","220309","1","220300"
|
30 |
+
"850382.SI","氟化工","L3","220311","1","220300"
|
31 |
+
"850372.SI","聚氨酯","L3","220313","1","220300"
|
32 |
+
"850135.SI","食品及饲料添加剂","L3","220315","1","220300"
|
33 |
+
"850136.SI","有机硅","L3","220316","1","220300"
|
34 |
+
"850137.SI","胶黏剂及胶带","L3","220317","0","220300"
|
35 |
+
"850341.SI","涤纶","L3","220401","1","220400"
|
36 |
+
"850343.SI","粘胶","L3","220403","1","220400"
|
37 |
+
"850344.SI","其他化学纤维","L3","220404","0","220400"
|
38 |
+
"850345.SI","氨纶","L3","220405","0","220400"
|
39 |
+
"850346.SI","锦纶","L3","220406","0","220400"
|
40 |
+
"850351.SI","其他塑料制品","L3","220501","1","220500"
|
41 |
+
"850353.SI","改性塑料","L3","220503","1","220500"
|
42 |
+
"850354.SI","合成树脂","L3","220504","1","220500"
|
43 |
+
"850355.SI","膜材料","L3","220505","1","220500"
|
44 |
+
"850362.SI","其他橡胶制品","L3","220602","1","220600"
|
45 |
+
"850363.SI","炭黑","L3","220603","1","220600"
|
46 |
+
"850364.SI","橡胶助剂","L3","220604","0","220600"
|
47 |
+
"850331.SI","氮肥","L3","220801","1","220800"
|
48 |
+
"850332.SI","磷肥及磷化工","L3","220802","1","220800"
|
49 |
+
"850333.SI","农药","L3","220803","1","220800"
|
50 |
+
"850336.SI","钾肥","L3","220804","0","220800"
|
51 |
+
"850381.SI","复合肥","L3","220805","1","220800"
|
52 |
+
"850523.SI","非金属材料Ⅲ","L3","220901","1","220900"
|
53 |
+
"850431.SI","铁矿石","L3","230301","0","230300"
|
54 |
+
"850432.SI","冶钢辅料","L3","230302","0","230300"
|
55 |
+
"850441.SI","长材","L3","230401","0","230400"
|
56 |
+
"850442.SI","板材","L3","230402","1","230400"
|
57 |
+
"850443.SI","钢铁管材","L3","230403","0","230400"
|
58 |
+
"850401.SI","特钢Ⅲ","L3","230501","1","230500"
|
59 |
+
"850521.SI","其他金属新材料","L3","240201","1","240200"
|
60 |
+
"850522.SI","磁性材料","L3","240202","1","240200"
|
61 |
+
"850551.SI","铝","L3","240301","1","240300"
|
62 |
+
"850552.SI","铜","L3","240302","1","240300"
|
63 |
+
"850553.SI","铅锌","L3","240303","1","240300"
|
64 |
+
"850531.SI","黄金","L3","240401","1","240400"
|
65 |
+
"850532.SI","白银","L3","240402","0","240400"
|
66 |
+
"850541.SI","稀土","L3","240501","0","240500"
|
67 |
+
"850542.SI","钨","L3","240502","0","240500"
|
68 |
+
"850544.SI","其他小金属","L3","240504","1","240500"
|
69 |
+
"850545.SI","钼","L3","240505","0","240500"
|
70 |
+
"850561.SI","钴","L3","240601","0","240600"
|
71 |
+
"850562.SI","镍","L3","240602","0","240600"
|
72 |
+
"850543.SI","锂","L3","240603","0","240600"
|
73 |
+
"850812.SI","分立器件","L3","270102","1","270100"
|
74 |
+
"850813.SI","半导体材料","L3","270103","1","270100"
|
75 |
+
"850814.SI","数字芯片设计","L3","270104","1","270100"
|
76 |
+
"850815.SI","模拟芯片设计","L3","270105","1","270100"
|
77 |
+
"850816.SI","集成电路制造","L3","270106","0","270100"
|
78 |
+
"850817.SI","集成电路封测","L3","270107","1","270100"
|
79 |
+
"850818.SI","半导体设备","L3","270108","1","270100"
|
80 |
+
"850822.SI","印制电路板","L3","270202","1","270200"
|
81 |
+
"850823.SI","被动元件","L3","270203","1","270200"
|
82 |
+
"850831.SI","面板","L3","270301","1","270300"
|
83 |
+
"850832.SI","LED","L3","270302","1","270300"
|
84 |
+
"850833.SI","光学元件","L3","270303","1","270300"
|
85 |
+
"850841.SI","其他电子Ⅲ","L3","270401","1","270400"
|
86 |
+
"850853.SI","品牌消费电子","L3","270503","1","270500"
|
87 |
+
"850854.SI","消费电子零部件及组装","L3","270504","1","270500"
|
88 |
+
"850861.SI","电子化学品Ⅲ","L3","270601","1","270600"
|
89 |
+
"850922.SI","车身附件及饰件","L3","280202","1","280200"
|
90 |
+
"850923.SI","底盘与发动机系统","L3","280203","1","280200"
|
91 |
+
"850924.SI","轮胎轮毂","L3","280204","1","280200"
|
92 |
+
"850925.SI","其他汽车零部件","L3","280205","1","280200"
|
93 |
+
"850926.SI","汽车电子电气系统","L3","280206","1","280200"
|
94 |
+
"850232.SI","汽车经销商","L3","280302","1","280300"
|
95 |
+
"850233.SI","汽车综合服务","L3","280303","1","280300"
|
96 |
+
"858811.SI","其他运输设备","L3","280401","1","280400"
|
97 |
+
"858812.SI","摩托车","L3","280402","1","280400"
|
98 |
+
"850951.SI","电动乘用车","L3","280501","0","280500"
|
99 |
+
"850952.SI","综合乘用车","L3","280502","1","280500"
|
100 |
+
"850912.SI","商用载货车","L3","280601","1","280600"
|
101 |
+
"850913.SI","商用载客车","L3","280602","1","280600"
|
102 |
+
"851112.SI","空调","L3","330102","1","330100"
|
103 |
+
"851116.SI","冰洗","L3","330106","1","330100"
|
104 |
+
"851121.SI","彩电","L3","330201","0","330200"
|
105 |
+
"851122.SI","其他黑色家电","L3","330202","1","330200"
|
106 |
+
"851131.SI","厨房小家电","L3","330301","1","330300"
|
107 |
+
"851132.SI","清洁小家电","L3","330302","0","330300"
|
108 |
+
"851133.SI","个护小家电","L3","330303","0","330300"
|
109 |
+
"851141.SI","厨房电器","L3","330401","1","330400"
|
110 |
+
"851142.SI","卫浴电器","L3","330402","0","330400"
|
111 |
+
"851151.SI","照明设备Ⅲ","L3","330501","1","330500"
|
112 |
+
"851161.SI","家电零部件Ⅲ","L3","330601","1","330600"
|
113 |
+
"851171.SI","其他家电Ⅲ","L3","330701","0","330700"
|
114 |
+
"851241.SI","肉制品","L3","340401","1","340400"
|
115 |
+
"851244.SI","其他食品","L3","340404","0","340400"
|
116 |
+
"851246.SI","预加工食品","L3","340406","1","340400"
|
117 |
+
"851247.SI","保健品","L3","340407","1","340400"
|
118 |
+
"851251.SI","白酒Ⅲ","L3","340501","1","340500"
|
119 |
+
"851232.SI","啤酒","L3","340601","1","340600"
|
120 |
+
"851233.SI","其他酒类","L3","340602","1","340600"
|
121 |
+
"851271.SI","软饮料","L3","340701","1","340700"
|
122 |
+
"851243.SI","乳品","L3","340702","1","340700"
|
123 |
+
"851281.SI","零食","L3","340801","1","340800"
|
124 |
+
"851282.SI","烘焙食品","L3","340802","1","340800"
|
125 |
+
"851283.SI","熟食","L3","340803","0","340800"
|
126 |
+
"851242.SI","调味发酵品Ⅲ","L3","340901","1","340900"
|
127 |
+
"851312.SI","棉纺","L3","350102","1","350100"
|
128 |
+
"851314.SI","印染","L3","350104","1","350100"
|
129 |
+
"851315.SI","辅料","L3","350105","1","350100"
|
130 |
+
"851316.SI","其他纺织","L3","350106","1","350100"
|
131 |
+
"851317.SI","纺织鞋类制造","L3","350107","0","350100"
|
132 |
+
"851325.SI","鞋帽及其他","L3","350205","1","350200"
|
133 |
+
"851326.SI","家纺","L3","350206","1","350200"
|
134 |
+
"851328.SI","运动服装","L3","350208","0","350200"
|
135 |
+
"851329.SI","非运动服装","L3","350209","1","350200"
|
136 |
+
"851331.SI","钟表珠宝","L3","350301","1","350300"
|
137 |
+
"851332.SI","多品类奢侈品","L3","350302","0","350300"
|
138 |
+
"851333.SI","其他饰品","L3","350303","0","350300"
|
139 |
+
"851412.SI","大宗用纸","L3","360102","1","360100"
|
140 |
+
"851413.SI","特种纸","L3","360103","1","360100"
|
141 |
+
"851422.SI","印刷","L3","360202","1","360200"
|
142 |
+
"851423.SI","金属包装","L3","360203","1","360200"
|
143 |
+
"851424.SI","塑料包装","L3","360204","1","360200"
|
144 |
+
"851425.SI","纸包装","L3","360205","1","360200"
|
145 |
+
"851426.SI","综合包装","L3","360206","0","360200"
|
146 |
+
"851436.SI","瓷砖地板","L3","360306","1","360300"
|
147 |
+
"851437.SI","成品家居","L3","360307","1","360300"
|
148 |
+
"851438.SI","定制家居","L3","360308","1","360300"
|
149 |
+
"851439.SI","卫浴制品","L3","360309","1","360300"
|
150 |
+
"851491.SI","其他家居用品","L3","360311","1","360300"
|
151 |
+
"851451.SI","文化用品","L3","360501","0","360500"
|
152 |
+
"851452.SI","娱乐用品","L3","360502","1","360500"
|
153 |
+
"851511.SI","原料药","L3","370101","1","370100"
|
154 |
+
"851512.SI","化学制剂","L3","370102","1","370100"
|
155 |
+
"851521.SI","中药Ⅲ","L3","370201","1","370200"
|
156 |
+
"851522.SI","血液制品","L3","370302","1","370300"
|
157 |
+
"851523.SI","疫苗","L3","370303","1","370300"
|
158 |
+
"851524.SI","其他生物制品","L3","370304","1","370300"
|
159 |
+
"851542.SI","医药流通","L3","370402","1","370400"
|
160 |
+
"851543.SI","线下药店","L3","370403","1","370400"
|
161 |
+
"851544.SI","互联网药店","L3","370404","0","370400"
|
162 |
+
"851532.SI","医疗设备","L3","370502","1","370500"
|
163 |
+
"851533.SI","医疗耗材","L3","370503","1","370500"
|
164 |
+
"851534.SI","体外诊断","L3","370504","1","370500"
|
165 |
+
"851562.SI","诊断服务","L3","370602","0","370600"
|
166 |
+
"851563.SI","医疗研发外包","L3","370603","1","370600"
|
167 |
+
"851564.SI","医院","L3","370604","1","370600"
|
168 |
+
"851565.SI","其他医疗服务","L3","370605","0","370600"
|
169 |
+
"851611.SI","火力发电","L3","410101","1","410100"
|
170 |
+
"851612.SI","水力发电","L3","410102","1","410100"
|
171 |
+
"851614.SI","热力服务","L3","410104","1","410100"
|
172 |
+
"851616.SI","光伏发电","L3","410106","1","410100"
|
173 |
+
"851617.SI","风力发电","L3","410107","1","410100"
|
174 |
+
"851618.SI","核力发电","L3","410108","0","410100"
|
175 |
+
"851619.SI","其他能源发电","L3","410109","0","410100"
|
176 |
+
"851610.SI","电能综合服务","L3","410110","1","410100"
|
177 |
+
"851631.SI","燃气Ⅲ","L3","410301","1","410300"
|
178 |
+
"851782.SI","原材料供应链服务","L3","420802","1","420800"
|
179 |
+
"851783.SI","中间产品及消费品供应链服务","L3","420803","1","420800"
|
180 |
+
"851784.SI","快递","L3","420804","1","420800"
|
181 |
+
"851785.SI","跨境物流","L3","420805","1","420800"
|
182 |
+
"851786.SI","仓储物流","L3","420806","1","420800"
|
183 |
+
"851787.SI","公路货运","L3","420807","1","420800"
|
184 |
+
"851731.SI","高速公路","L3","420901","1","420900"
|
185 |
+
"851721.SI","公交","L3","420902","1","420900"
|
186 |
+
"851771.SI","铁路运输","L3","420903","1","420900"
|
187 |
+
"851741.SI","航空运输","L3","421001","1","421000"
|
188 |
+
"851751.SI","机场","L3","421002","0","421000"
|
189 |
+
"851761.SI","航运","L3","421101","1","421100"
|
190 |
+
"851711.SI","港口","L3","421102","1","421100"
|
191 |
+
"851811.SI","住宅开发","L3","430101","1","430100"
|
192 |
+
"851812.SI","商业地产","L3","430102","1","430100"
|
193 |
+
"851813.SI","产业地产","L3","430103","1","430100"
|
194 |
+
"851831.SI","物业管理","L3","430301","1","430300"
|
195 |
+
"851832.SI","房产租赁经纪","L3","430302","0","430300"
|
196 |
+
"851833.SI","房地产综合服务","L3","430303","0","430300"
|
197 |
+
"852021.SI","贸易Ⅲ","L3","450201","1","450200"
|
198 |
+
"852031.SI","百货","L3","450301","1","450300"
|
199 |
+
"852032.SI","超市","L3","450302","1","450300"
|
200 |
+
"852033.SI","多业态零售","L3","450303","1","450300"
|
201 |
+
"852034.SI","商业物业经营","L3","450304","1","450300"
|
202 |
+
"852041.SI","专业连锁Ⅲ","L3","450401","1","450400"
|
203 |
+
"852061.SI","综合电商","L3","450601","0","450600"
|
204 |
+
"852062.SI","跨境电商","L3","450602","1","450600"
|
205 |
+
"852063.SI","电商服务","L3","450603","1","450600"
|
206 |
+
"852071.SI","旅游零售Ⅲ","L3","450701","0","450700"
|
207 |
+
"852161.SI","体育Ⅲ","L3","460601","0","460600"
|
208 |
+
"852171.SI","本地生活服务Ⅲ","L3","460701","0","460700"
|
209 |
+
"852181.SI","人力资源服务","L3","460801","0","460800"
|
210 |
+
"852182.SI","检测服务","L3","460802","1","460800"
|
211 |
+
"852183.SI","会展服务","L3","460803","1","460800"
|
212 |
+
"852184.SI","其他专业服务","L3","460804","0","460800"
|
213 |
+
"852121.SI","酒店","L3","460901","1","460900"
|
214 |
+
"852141.SI","餐饮","L3","460902","0","460900"
|
215 |
+
"859931.SI","博彩","L3","461001","0","461000"
|
216 |
+
"852111.SI","人工景区","L3","461002","1","461000"
|
217 |
+
"852112.SI","自然景区","L3","461003","1","461000"
|
218 |
+
"852131.SI","旅游综合","L3","461004","1","461000"
|
219 |
+
"859851.SI","学历教育","L3","461101","0","461100"
|
220 |
+
"859852.SI","培训教育","L3","461102","1","461100"
|
221 |
+
"859853.SI","教育运营及其他","L3","461103","0","461100"
|
222 |
+
"857821.SI","国有大型银行Ⅲ","L3","480201","1","480200"
|
223 |
+
"857831.SI","股份制银行Ⅲ","L3","480301","1","480300"
|
224 |
+
"857841.SI","城商行Ⅲ","L3","480401","1","480400"
|
225 |
+
"857851.SI","农商行Ⅲ","L3","480501","1","480500"
|
226 |
+
"857861.SI","其他银行Ⅲ","L3","480601","0","480600"
|
227 |
+
"851931.SI","证券Ⅲ","L3","490101","1","490100"
|
228 |
+
"851941.SI","保险Ⅲ","L3","490201","1","490200"
|
229 |
+
"851922.SI","金融控股","L3","490302","1","490300"
|
230 |
+
"851923.SI","期货","L3","490303","0","490300"
|
231 |
+
"851924.SI","信托","L3","490304","0","490300"
|
232 |
+
"851925.SI","租赁","L3","490305","0","490300"
|
233 |
+
"851926.SI","金融信息服务","L3","490306","0","490300"
|
234 |
+
"851927.SI","资产管理","L3","490307","1","490300"
|
235 |
+
"851928.SI","其他多元金融","L3","490308","0","490300"
|
236 |
+
"852311.SI","综合Ⅲ","L3","510101","1","510100"
|
237 |
+
"857111.SI","水泥制造","L3","610101","1","610100"
|
238 |
+
"857112.SI","水泥制品","L3","610102","1","610100"
|
239 |
+
"857121.SI","玻璃制造","L3","610201","1","610200"
|
240 |
+
"857122.SI","玻纤制造","L3","610202","1","610200"
|
241 |
+
"850615.SI","耐火材料","L3","610301","1","610300"
|
242 |
+
"850616.SI","管材","L3","610302","1","610300"
|
243 |
+
"850614.SI","其他建材","L3","610303","1","610300"
|
244 |
+
"850617.SI","防水材料","L3","610304","0","610300"
|
245 |
+
"850618.SI","涂料","L3","610305","0","610300"
|
246 |
+
"850623.SI","房屋建设Ⅲ","L3","620101","1","620100"
|
247 |
+
"857221.SI","装修装饰Ⅲ","L3","620201","1","620200"
|
248 |
+
"857236.SI","基建市政工程","L3","620306","1","620300"
|
249 |
+
"857251.SI","园林工程","L3","620307","1","620300"
|
250 |
+
"857241.SI","钢结构","L3","620401","1","620400"
|
251 |
+
"857242.SI","化学工程","L3","620402","1","620400"
|
252 |
+
"857243.SI","国际工程","L3","620403","1","620400"
|
253 |
+
"857244.SI","其他专业工程","L3","620404","1","620400"
|
254 |
+
"857261.SI","工程咨询服务Ⅲ","L3","620601","1","620600"
|
255 |
+
"850741.SI","电机Ⅲ","L3","630101","1","630100"
|
256 |
+
"857331.SI","综合电力设备商","L3","630301","0","630300"
|
257 |
+
"857334.SI","火电设备","L3","630304","1","630300"
|
258 |
+
"857336.SI","其他电源设备Ⅲ","L3","630306","1","630300"
|
259 |
+
"857351.SI","硅料硅片","L3","630501","0","630500"
|
260 |
+
"857352.SI","光伏电池组件","L3","630502","1","630500"
|
261 |
+
"857353.SI","逆变器","L3","630503","0","630500"
|
262 |
+
"857354.SI","光伏辅材","L3","630504","1","630500"
|
263 |
+
"857355.SI","光伏加工设备","L3","630505","1","630500"
|
264 |
+
"857361.SI","风电整机","L3","630601","0","630600"
|
265 |
+
"857362.SI","风电零部件","L3","630602","1","630600"
|
266 |
+
"857371.SI","锂电池","L3","630701","1","630700"
|
267 |
+
"857372.SI","电池化学品","L3","630702","1","630700"
|
268 |
+
"857373.SI","锂电专用设备","L3","630703","1","630700"
|
269 |
+
"857374.SI","燃料电池","L3","630704","0","630700"
|
270 |
+
"857375.SI","蓄电池及其他电池","L3","630705","1","630700"
|
271 |
+
"857381.SI","输变电设备","L3","630801","1","630800"
|
272 |
+
"857382.SI","配电设备","L3","630802","1","630800"
|
273 |
+
"857321.SI","电网自动化设备","L3","630803","1","630800"
|
274 |
+
"857323.SI","电工仪器仪表","L3","630804","1","630800"
|
275 |
+
"857344.SI","线缆部件及其他","L3","630805","1","630800"
|
276 |
+
"850711.SI","机床工具","L3","640101","1","640100"
|
277 |
+
"850713.SI","磨具磨料","L3","640103","1","640100"
|
278 |
+
"850715.SI","制冷空调设备","L3","640105","1","640100"
|
279 |
+
"850716.SI","其他通用设备","L3","640106","1","640100"
|
280 |
+
"850731.SI","仪器仪表","L3","640107","1","640100"
|
281 |
+
"850751.SI","金属制品","L3","640108","1","640100"
|
282 |
+
"850725.SI","能源及重型设备","L3","640203","1","640200"
|
283 |
+
"850728.SI","楼宇设备","L3","640204","1","640200"
|
284 |
+
"850721.SI","纺织服装设备","L3","640206","1","640200"
|
285 |
+
"850723.SI","农用机械","L3","640207","0","640200"
|
286 |
+
"850726.SI","印刷包装机械","L3","640208","1","640200"
|
287 |
+
"850727.SI","其他专用设备","L3","640209","1","640200"
|
288 |
+
"850936.SI","轨交设备Ⅲ","L3","640501","1","640500"
|
289 |
+
"850771.SI","工程机械整机","L3","640601","1","640600"
|
290 |
+
"850772.SI","工程机械器件","L3","640602","1","640600"
|
291 |
+
"850781.SI","机器人","L3","640701","1","640700"
|
292 |
+
"850782.SI","工控设备","L3","640702","1","640700"
|
293 |
+
"850783.SI","激光设备","L3","640703","1","640700"
|
294 |
+
"850784.SI","其他自动化设备","L3","640704","1","640700"
|
295 |
+
"857411.SI","航天装备Ⅲ","L3","650101","1","650100"
|
296 |
+
"857421.SI","航空装备Ⅲ","L3","650201","1","650200"
|
297 |
+
"857431.SI","地面兵装Ⅲ","L3","650301","1","650300"
|
298 |
+
"850935.SI","航海装备Ⅲ","L3","650401","1","650400"
|
299 |
+
"857451.SI","军工电子Ⅲ","L3","650501","1","650500"
|
300 |
+
"850702.SI","安防设备","L3","710102","1","710100"
|
301 |
+
"850703.SI","其他计算机设备","L3","710103","1","710100"
|
302 |
+
"852226.SI","IT服务Ⅲ","L3","710301","1","710300"
|
303 |
+
"851041.SI","垂直应用软件","L3","710401","1","710400"
|
304 |
+
"851042.SI","横向通用软件","L3","710402","1","710400"
|
305 |
+
"857641.SI","游戏Ⅲ","L3","720401","1","720400"
|
306 |
+
"857651.SI","营销代理","L3","720501","1","720500"
|
307 |
+
"857652.SI","广告媒体","L3","720502","0","720500"
|
308 |
+
"857661.SI","影视动漫制作","L3","720601","1","720600"
|
309 |
+
"857662.SI","院线","L3","720602","0","720600"
|
310 |
+
"857671.SI","视频媒体","L3","720701","0","720700"
|
311 |
+
"857672.SI","音频媒体","L3","720702","0","720700"
|
312 |
+
"857673.SI","图片媒体","L3","720703","0","720700"
|
313 |
+
"857674.SI","门户网站","L3","720704","1","720700"
|
314 |
+
"857675.SI","文字媒体","L3","720705","0","720700"
|
315 |
+
"857676.SI","其他数字媒体","L3","720706","0","720700"
|
316 |
+
"857681.SI","社交Ⅲ","L3","720801","0","720800"
|
317 |
+
"857691.SI","教育出版","L3","720901","1","720900"
|
318 |
+
"857692.SI","大众出版","L3","720902","1","720900"
|
319 |
+
"857693.SI","其他出版","L3","720903","0","720900"
|
320 |
+
"859951.SI","电视广播Ⅲ","L3","721001","1","721000"
|
321 |
+
"852212.SI","电信运营商","L3","730102","0","730100"
|
322 |
+
"852213.SI","通信工程及服务","L3","730103","1","730100"
|
323 |
+
"852214.SI","通信应用增值服务","L3","730104","1","730100"
|
324 |
+
"851024.SI","通信网络设备及器件","L3","730204","1","730200"
|
325 |
+
"851025.SI","通信线缆及配套","L3","730205","1","730200"
|
326 |
+
"851026.SI","通信终端及配件","L3","730206","1","730200"
|
327 |
+
"851027.SI","其他通信设备","L3","730207","1","730200"
|
328 |
+
"859511.SI","动力煤","L3","740101","1","740100"
|
329 |
+
"859512.SI","焦煤","L3","740102","1","740100"
|
330 |
+
"859521.SI","焦炭Ⅲ","L3","740201","1","740200"
|
331 |
+
"859611.SI","油气开采Ⅲ","L3","750101","0","750100"
|
332 |
+
"859621.SI","油田服务","L3","750201","1","750200"
|
333 |
+
"859622.SI","油气及炼化工程","L3","750202","1","750200"
|
334 |
+
"859631.SI","炼油化工","L3","750301","1","750300"
|
335 |
+
"859632.SI","油品石化贸易","L3","750302","1","750300"
|
336 |
+
"859633.SI","其他石化","L3","750303","1","750300"
|
337 |
+
"859711.SI","大气治理","L3","760101","1","760100"
|
338 |
+
"859712.SI","水务及水治理","L3","760102","1","760100"
|
339 |
+
"859713.SI","固废治理","L3","760103","1","760100"
|
340 |
+
"859714.SI","综合环境治理","L3","760104","1","760100"
|
341 |
+
"859721.SI","环保设备Ⅲ","L3","760201","1","760200"
|
342 |
+
"859811.SI","生活用纸","L3","770101","1","770100"
|
343 |
+
"859812.SI","洗护用品","L3","770102","0","770100"
|
344 |
+
"859821.SI","化妆品制造及其他","L3","770201","1","770200"
|
345 |
+
"859822.SI","品牌化妆品","L3","770202","1","770200"
|
346 |
+
"859831.SI","医美耗材","L3","770301","0","770300"
|
347 |
+
"859832.SI","医美服务","L3","770302","0","770300"
|
app.py
ADDED
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
+
from main import run, add_to_queue,gradio_interface
|
7 |
+
import io
|
8 |
+
import sys
|
9 |
+
import time
|
10 |
+
import os
|
11 |
+
import pandas as pd
|
12 |
+
OPENAI_KEY = None
|
13 |
+
css = """#col-container {max-width: 90%; margin-left: auto; margin-right: auto; display: flex; flex-direction: column;}
|
14 |
+
#header {text-align: center;}
|
15 |
+
#col-chatbox {flex: 1; max-height: min(750px, 100%);}
|
16 |
+
#label {font-size: 4em; padding: 0.5em; margin: 0;}
|
17 |
+
.scroll-hide {overflow-y: scroll; max-height: 100px;}
|
18 |
+
.wrap {max-height: 680px;}
|
19 |
+
.message {font-size: 3em;}
|
20 |
+
.message-wrap {max-height: min(700px, 100vh);}
|
21 |
+
"""
|
22 |
+
|
23 |
+
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
|
24 |
+
plt.rcParams['axes.unicode_minus'] = False
|
25 |
+
|
26 |
+
example_stock =['给我画一下可孚医疗2022年年中到今天的股价','北向资金今年的每日流入和累计流入','看一下近三年宁德时代和贵州茅台的pb变化','画一下五粮液和泸州老窖从2019年年初到2022年年中的收益率走势','成都银行近一年的k线图和kdj指标','比较下沪深300,创业板指,中证1000指数今年的收益率','今年上证50所有成分股的收益率是多少']
|
27 |
+
example_economic =['中国过去十年的cpi走势是什么','过去五年中国的货币供应量走势,并且打印保存','我想看看现在的新闻或者最新的消息','我想看看中国近十年gdp的走势','预测中国未来12个季度的GDP增速']
|
28 |
+
example_fund =['易方达的张坤管理了几个基金','基金经理周海栋名下的所有基金今年的收益率情况','我想看看周海栋管理的华商优势行业的近三年来的的净值曲线','比较下华商优势行业和易方达蓝筹精选这两只基金的近三年的收益率']
|
29 |
+
example_company =['介绍下贵州茅台,这公司是干什么的,主营业务是什么','我想比较下工商银行和贵州茅台近十年的净资产回报率','今年一季度上证50的成分股的归母净利润同比增速分别是']
|
30 |
+
|
31 |
+
class Client:
|
32 |
+
def __init__(self) -> None:
|
33 |
+
self.OPENAI_KEY = OPENAI_KEY
|
34 |
+
self.OPENAI_API_BASED_AZURE = None
|
35 |
+
self.OPENAI_ENGINE_AZURE = None
|
36 |
+
self.OPENAI_API_KEY_AZURE = None
|
37 |
+
self.stop = False # 添加停止标志
|
38 |
+
|
39 |
+
def set_key(self, openai_key, openai_key_azure, api_base_azure, engine_azure):
|
40 |
+
self.OPENAI_KEY = openai_key
|
41 |
+
self.OPENAI_API_BASED_AZURE = api_base_azure
|
42 |
+
self.OPENAI_API_KEY_AZURE = openai_key_azure
|
43 |
+
self.OPENAI_ENGINE_AZURE = engine_azure
|
44 |
+
return self.OPENAI_KEY, self.OPENAI_API_KEY_AZURE, self.OPENAI_API_BASED_AZURE, self.OPENAI_ENGINE_AZURE
|
45 |
+
|
46 |
+
|
47 |
+
def run(self, messages):
|
48 |
+
if self.OPENAI_KEY == '' and self.OPENAI_API_KEY_AZURE == '':
|
49 |
+
yield '', np.zeros((100, 100, 3), dtype=np.uint8), "Please set your OpenAI API key first!!!", pd.DataFrame()
|
50 |
+
else:
|
51 |
+
# self.stop = False
|
52 |
+
gen = gradio_interface(messages, self.OPENAI_KEY, self.OPENAI_API_KEY_AZURE, self.OPENAI_API_BASED_AZURE, self.OPENAI_ENGINE_AZURE)
|
53 |
+
while not self.stop: #
|
54 |
+
try:
|
55 |
+
yield next(gen)
|
56 |
+
except StopIteration:
|
57 |
+
print("StopIteration")
|
58 |
+
break
|
59 |
+
|
60 |
+
# yield from gradio_interface(messages, self.OPENAI_KEY)
|
61 |
+
#return finally_text, img, output, df
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
with gr.Blocks() as demo:
|
68 |
+
state = gr.State(value={"client": Client()})
|
69 |
+
def change_textbox(query):
|
70 |
+
# 根据不同输入对输出控件进行更新
|
71 |
+
return gr.update(lines=2, visible=True, value=query)
|
72 |
+
# 图片框显示
|
73 |
+
|
74 |
+
with gr.Row():
|
75 |
+
gr.Markdown(
|
76 |
+
"""
|
77 |
+
# Hello Data-Copilot ! 😀
|
78 |
+
A powerful AI system connects humans and data.
|
79 |
+
The current version only supports Chinese financial data, in the future we will support for other country data
|
80 |
+
""")
|
81 |
+
|
82 |
+
|
83 |
+
if not OPENAI_KEY:
|
84 |
+
with gr.Row().style():
|
85 |
+
with gr.Column(scale=0.95):
|
86 |
+
openai_api_key = gr.Textbox(
|
87 |
+
show_label=False,
|
88 |
+
placeholder="Set your OpenAI API key here and press Submit (e.g. sk-xxx)",
|
89 |
+
lines=1,
|
90 |
+
type="password"
|
91 |
+
).style(container=False)
|
92 |
+
|
93 |
+
with gr.Row():
|
94 |
+
openai_api_key_azure = gr.Textbox(
|
95 |
+
show_label=False,
|
96 |
+
placeholder="Set your Azure-OpenAI key here",
|
97 |
+
lines=1,
|
98 |
+
type="password"
|
99 |
+
).style(container=False)
|
100 |
+
openai_api_base_azure = gr.Textbox(
|
101 |
+
show_label=False,
|
102 |
+
placeholder="Set your Azure-OpenAI api_base here"
|
103 |
+
"(e.g. https://zwq0525.openai.azure.com)",
|
104 |
+
lines=2,
|
105 |
+
type="password"
|
106 |
+
).style(container=False)
|
107 |
+
openai_api_engine_azure = gr.Textbox(
|
108 |
+
show_label=False,
|
109 |
+
placeholder="Set your Azure-OpenAI engine here "
|
110 |
+
"(e.g. gpt35)",
|
111 |
+
lines=2,
|
112 |
+
type="password"
|
113 |
+
).style(container=False)
|
114 |
+
|
115 |
+
|
116 |
+
gr.Markdown(
|
117 |
+
"""
|
118 |
+
It is recommended to use the Openai paid API or Azure-OpenAI service, because the free Openai API will be limited by the access speed and 3 Requests per minute (very slow).
|
119 |
+
""")
|
120 |
+
|
121 |
+
|
122 |
+
with gr.Column(scale=0.1, min_width=0):
|
123 |
+
btn1 = gr.Button("Submit").style(full_height=True)
|
124 |
+
|
125 |
+
with gr.Row():
|
126 |
+
with gr.Column(scale=0.85):
|
127 |
+
input_text = gr.inputs.Textbox(lines=1, placeholder='Please input your problem...', label='what do you want to find?')
|
128 |
+
|
129 |
+
with gr.Column(scale=0.15, min_width=0):
|
130 |
+
start_btn = gr.Button("Start").style(full_height=True)
|
131 |
+
# end_btn = gr.Button("Stop").style(full_height=True)
|
132 |
+
|
133 |
+
|
134 |
+
gr.Markdown(
|
135 |
+
"""
|
136 |
+
# Try these examples ➡️➡️
|
137 |
+
""")
|
138 |
+
with gr.Row():
|
139 |
+
|
140 |
+
example_selector1 = gr.Dropdown(choices=example_stock, interactive=True,
|
141 |
+
label="查股票 Query stock:", show_label=True)
|
142 |
+
example_selector2 = gr.Dropdown(choices=example_economic, interactive=True,
|
143 |
+
label="查经济 Query Economy:", show_label=True)
|
144 |
+
example_selector3 = gr.Dropdown(choices=example_company, interactive=True,
|
145 |
+
label="查公司 Query Company:", show_label=True)
|
146 |
+
example_selector4 = gr.Dropdown(choices=example_fund, interactive=True,
|
147 |
+
label="查基金 Query Fund:", show_label=True)
|
148 |
+
|
149 |
+
|
150 |
+
|
151 |
+
def set_key(state, openai_api_key,openai_api_key_azure, openai_api_base_azure, openai_api_engine_azure):
|
152 |
+
return state["client"].set_key(openai_api_key, openai_api_key_azure,openai_api_base_azure, openai_api_engine_azure)
|
153 |
+
|
154 |
+
|
155 |
+
def run(state, chatbot):
|
156 |
+
generator = state["client"].run(chatbot)
|
157 |
+
for solving_step, img, res, df in generator:
|
158 |
+
# if state["client"].stop:
|
159 |
+
# print('Stopping generation')
|
160 |
+
# break
|
161 |
+
yield solving_step, img, res, df
|
162 |
+
|
163 |
+
|
164 |
+
# def stop(state):
|
165 |
+
# print('Stop signal received!')
|
166 |
+
# state["client"].stop = True
|
167 |
+
|
168 |
+
|
169 |
+
|
170 |
+
|
171 |
+
with gr.Row():
|
172 |
+
with gr.Column(scale=0.3, min_width="500px", max_width="500px", min_height="500px", max_height="500px"):
|
173 |
+
Res = gr.Textbox(label="Summary and Result: ")
|
174 |
+
with gr.Column(scale=0.7, min_width="500px", max_width="500px", min_height="500px", max_height="500px"):
|
175 |
+
solving_step = gr.Textbox(label="Solving Step: ", lines=5)
|
176 |
+
|
177 |
+
|
178 |
+
img = gr.outputs.Image(type='numpy')
|
179 |
+
df = gr.outputs.Dataframe(type='pandas')
|
180 |
+
with gr.Row():
|
181 |
+
gr.Markdown(
|
182 |
+
"""
|
183 |
+
[Tushare](https://tushare.pro/) provides financial data support for our Data-Copilot.
|
184 |
+
|
185 |
+
[OpenAI](https://openai.com/) provides the powerful Chatgpt model for our Data-Copilot.
|
186 |
+
""")
|
187 |
+
|
188 |
+
|
189 |
+
outputs = [solving_step ,img, Res, df]
|
190 |
+
#设置change事件
|
191 |
+
example_selector1.change(fn = change_textbox, inputs = example_selector1, outputs = input_text)
|
192 |
+
example_selector2.change(fn = change_textbox, inputs = example_selector2, outputs = input_text)
|
193 |
+
example_selector3.change(fn = change_textbox, inputs = example_selector3, outputs = input_text)
|
194 |
+
example_selector4.change(fn = change_textbox, inputs = example_selector4, outputs = input_text)
|
195 |
+
|
196 |
+
|
197 |
+
if not OPENAI_KEY:
|
198 |
+
openai_api_key.submit(set_key, [state, openai_api_key, openai_api_key_azure,openai_api_base_azure, openai_api_engine_azure], [openai_api_key, openai_api_key_azure,openai_api_base_azure, openai_api_engine_azure])
|
199 |
+
btn1.click(set_key, [state, openai_api_key, openai_api_key_azure,openai_api_base_azure, openai_api_engine_azure], [openai_api_key,openai_api_key_azure, openai_api_base_azure, openai_api_engine_azure])
|
200 |
+
|
201 |
+
start_btn.click(fn = run, inputs = [state, input_text], outputs=outputs)
|
202 |
+
# end_btn.click(stop, state)
|
203 |
+
|
204 |
+
|
205 |
+
|
206 |
+
demo.queue()
|
207 |
+
demo.launch(share=True)
|
208 |
+
|
209 |
+
|
backup.json
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"global prompt:": "请根据给定的指令,利用下面的函数一步一步地完成指令任务,每一步你必须要从以下函数库[get_stock_code,get_stock_prices,plot_stock_prices,save_stock_prices_to_csv,compare_stocks_Earnings_Rate,get_company_info]中选择一个函数,并且为该函数生成对应的参数。以json生成的格式如下:{\nstep1:{\n arg:(arg1,arg2...),\n function:%s,\n output:%s},\nstep2:{\n arg:(arg1,arg2..),\n function:%s,\n output:%s},\n}, 请注意每一步只能从函数库中选择一个合适的函数,生成一个result_i,后面步骤的函数使用之前的result作为参数输入",
|
3 |
+
"Function Library:": "",
|
4 |
+
"Instruction:请帮我绘制贵州茅台的2018年1月23日到2019年3月13日的股价走势图": "###Solving Process:根据要求我需要调用get_stock_code()函数获取贵州茅台股票代码,然后调用get_stock_prices()函数获取每日的价格,最后使用plot_stock_prices()画图. ###Funciton Call:{\nstep1:{\n arg:('贵州茅台'),\n function:get_stock_code,\n output:result1},\nstep2:{\n arg:(result1,'20180123','20190313'),\n function:get_stock_prices,\n output:result2},\nstep3:{\n arg:(result2,'贵州茅台','close'),\n function:plot_stock_prices,\n output:result3}\n}",
|
5 |
+
"Instruction:画一下五粮液和泸州老窖从2019年年初到2022年年中的收益率走势": "###Solving Process:根据要求我需要调用两次get_stock_code()分别获取两只股票的股票代码,然后调用compare_stocks_Earnings_Rate()函数比较两只股票的累计收益率走势. ###Function Call:{\nstep1:{\n arg:('五粮液'),\n function:get_stock_code,\n output:result1},\nstep2:{\n arg:('泸州老窖'),\n function:get_stock_code,\n output:result2}, \nstep3:{\n arg:({result1:'五粮液',result2:'泸州老窖'},'20190101','20220630'),\n function:compare_stocks_Earnings_Rate,\n output:result3}\n}\n",
|
6 |
+
}
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
step1={ \n "arg": "(arg1,arg2...)", \n "function": "%s", \n "output": "%s" \n},step2={ \n "arg": "(arg1,arg2..)", \n "function": "%s", \n "output": "%s" \n}, \n}
|
create_tool/Atomic_api_json.py
ADDED
@@ -0,0 +1,360 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tushare as ts
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import pandas as pd
|
4 |
+
import os
|
5 |
+
import random
|
6 |
+
from matplotlib.ticker import MaxNLocator
|
7 |
+
import matplotlib.font_manager as fm
|
8 |
+
from prettytable import PrettyTable
|
9 |
+
from blessed import Terminal
|
10 |
+
import time
|
11 |
+
from datetime import datetime, timedelta
|
12 |
+
import numpy as np
|
13 |
+
import mplfinance as mpf
|
14 |
+
from prettytable import PrettyTable
|
15 |
+
from typing import Optional
|
16 |
+
import matplotlib.font_manager as fm
|
17 |
+
from matplotlib.lines import Line2D
|
18 |
+
from typing import Union, Any
|
19 |
+
import json
|
20 |
+
|
21 |
+
token=os.getenv("TUSHARE_TOKEN")
|
22 |
+
pro = ts.pro_api(token)
|
23 |
+
all_atomic_api = {}
|
24 |
+
|
25 |
+
|
26 |
+
#######################################################################################################################
|
27 |
+
df = pro.fina_indicator(ts_code = "600000.SH",start_date = "20200104",end_date = "20220104",fields=["ts_code","end_date","eps","current_ratio","quick_ratio","inv_turn","netprofit_margin","grossprofit_margin","roe","roa","roic","debt_to_assets","netprofit_yoy","dt_netprofit_yoy"])
|
28 |
+
# 取出第一行和最后一行的数据
|
29 |
+
df_sample_str = df.iloc[[0,-1],:].to_string(header=False, index=False)
|
30 |
+
|
31 |
+
|
32 |
+
columns = df.columns.tolist()
|
33 |
+
columns_means= ['股票代码','报告期','每股收益','流动比率','速动比率','存货周转率','销售净利率','销售毛利率','净资产收益率','总资产净利率','投入资本回报率','资产负债率','净利润同比增长率','扣非净利润同比增长率']
|
34 |
+
|
35 |
+
columns_dict = dict(zip(columns, columns_means))
|
36 |
+
|
37 |
+
atomic_api = {"func_desc":None,"Column_name":None,"example_input":None,"output_first_and_last_row":None}
|
38 |
+
atomic_api["func_desc"] = "财务指标"
|
39 |
+
atomic_api["Column_name"] = columns_dict
|
40 |
+
atomic_api["example_input"] = "pro.fina_indicator(ts_code = \"600000.SH\",start_date = \"20200104\",end_date = \"20220104\",fields=[\"ts_code\",\"end_date\",\"eps\",\"current_ratio\",\"quick_ratio\",\"inv_turn\",\"netprofit_margin\",\"grossprofit_margin\",\"roe\",\"roa\",\"roic\",\"debt_to_assets\",\"netprofit_yoy\",\"dt_netprofit_yoy\"])"
|
41 |
+
|
42 |
+
atomic_api["output_first_and_last_row"] = df_sample_str
|
43 |
+
|
44 |
+
all_atomic_api["pro.fina_indicator"] = atomic_api
|
45 |
+
|
46 |
+
#######################################################################################################################
|
47 |
+
|
48 |
+
df = pro.stock_company(ts_code = '600230.SH', fields=[
|
49 |
+
"ts_code","exchange","chairman", "manager","secretary", "reg_capital","setup_date", "province","city","introduction",
|
50 |
+
"website", "email","office","employees","main_business","business_scope"])
|
51 |
+
|
52 |
+
df_sample_str = df.iloc[0, :].to_string(header=False, index=False)
|
53 |
+
columns = df.columns.tolist()
|
54 |
+
columns_means = ['股票代码','交易所代码','法人代表','总经理','董秘','注册资本','注册日期','所在省份','所在城市','公司介绍','公司主页','电子邮件','办公室地址','员工人数','主要业务及产品','经营范围' ]
|
55 |
+
columns_dict = dict(zip(columns, columns_means))
|
56 |
+
atomic_api = {"func_desc":None,"Column_name":None,"example_input":None,"output_first_and_last_row":None}
|
57 |
+
atomic_api["func_desc"] = "获取上市公司基本信息如公司业务描述,员工人数等基本信息"
|
58 |
+
atomic_api["Column_name"] = columns_dict
|
59 |
+
atomic_api["example_input"] = "pro.stock_company(ts_code = '600230.SH', fields=[\"ts_code\",\"exchange\",\"chairman\", \"manager\",\"secretary\", \"reg_capital\",\"setup_date\", \"province\",\"city\",\"introduction\",\"website\", \"email\",\"office\",\"employees\",\"main_business\",\"business_scope\"])"
|
60 |
+
atomic_api["output_first_and_last_row"] = df_sample_str
|
61 |
+
|
62 |
+
all_atomic_api["pro.stock_company"] = atomic_api
|
63 |
+
|
64 |
+
|
65 |
+
#######################################################################################################################
|
66 |
+
|
67 |
+
df = pro.daily_basic(ts_code = "600230.SH",start_date = "20180726",end_date = "20200726", fields=[
|
68 |
+
"ts_code", "trade_date","turnover_rate","turnover_rate_f","volume_ratio",
|
69 |
+
"pe_ttm","pb","ps_ttm","dv_ttm","total_share",
|
70 |
+
"float_share","free_share","total_mv","circ_mv"])
|
71 |
+
|
72 |
+
df_sample_str = df.iloc[[0, -1], :].to_string(header=False, index=False)
|
73 |
+
columns = df.columns.tolist()
|
74 |
+
columns_means = ['股票代码','交易日期','换手率(总股本)','换手率(自由流通股本)','量比','市盈率(动态)','市净率','市销率(动态)','股息率(动态)','总股本','流通股本','自由流通股本','总市值','流通市值' ]
|
75 |
+
|
76 |
+
columns_dict = dict(zip(columns, columns_means))
|
77 |
+
atomic_api = {"func_desc":None,"Column_name":None,"example_input":None,"output_first_and_last_row":None}
|
78 |
+
atomic_api["func_desc"] = "获取股票每日基本指标例如换手率,市盈率市净率股息率等"
|
79 |
+
atomic_api["Column_name"] = columns_dict
|
80 |
+
atomic_api["example_input"] = "pro.daily_basic(ts_code = \"600230.SH\",start_date = \"20180726\",end_date = \"20200726\", fields=[\"ts_code\", \"trade_date\",\"turnover_rate\",\"turnover_rate_f\",\"volume_ratio\",\"pe_ttm\",\"pb\",\"ps_ttm\",\"dv_ttm\",\"total_share\",\"float_share\",\"free_share\",\"total_mv\",\"circ_mv\"])"
|
81 |
+
|
82 |
+
atomic_api["output_first_and_last_row"] = df_sample_str
|
83 |
+
|
84 |
+
all_atomic_api["pro.daily_basic"] = atomic_api
|
85 |
+
|
86 |
+
#######################################################################################################################
|
87 |
+
|
88 |
+
df = pro.stk_factor(ts_code="600000.SH",start_date= "20220520",end_date= "20230520",
|
89 |
+
fields=["ts_code","trade_date","close","macd_dif","macd_dea","macd","kdj_k","kdj_d","kdj_j",
|
90 |
+
"rsi_6","rsi_12","rsi_24","boll_upper","boll_mid","boll_lower","cci"])
|
91 |
+
df_sample_str = df.iloc[[0, -1], :].to_string(header=False, index=False)
|
92 |
+
columns = df.columns.tolist()
|
93 |
+
columns_means = ['股票代码','交易日期','收盘价','MACD_DIF','MACD_DEA','MACD','KDJ_K','KDJ_D','KDJ_J','RSI_6','RSI_12','RSI_24','BOLL_UPPER','BOLL_MID','BOLL_LOWER','CCI' ]
|
94 |
+
|
95 |
+
columns_dict = dict(zip(columns, columns_means))
|
96 |
+
|
97 |
+
atomic_api = {"func_desc":None,"Column_name":None,"example_input":None,"output_first_and_last_row":None}
|
98 |
+
|
99 |
+
atomic_api["func_desc"] = "获取股票的每日的技术指标数据"
|
100 |
+
atomic_api["example_input"] = "pro.stk_factor(ts_code=stock_code,start_date= start_date,end_date= end_date,fields=[\"ts_code\",\"trade_date\",\"close\",\"macd_dif\",\"macd_dea\",\"macd\",\"kdj_k\",\"kdj_d\",\"kdj_j\",\"rsi_6\",\"rsi_12\",\"rsi_24\",\"boll_upper\",\"boll_mid\",\"boll_lower\",\"cci\"])"
|
101 |
+
atomic_api["output_first_and_last_row"] = df_sample_str
|
102 |
+
atomic_api["Column_name"] = str(columns_dict)
|
103 |
+
|
104 |
+
all_atomic_api["pro.stk_factor"] = atomic_api
|
105 |
+
|
106 |
+
#######################################################################################################################
|
107 |
+
|
108 |
+
df = pro.moneyflow_hsgt(start_date="20220101", end_date="20230101", fields=["trade_date","ggt_ss","ggt_sz","hgt","sgt","north_money","south_money"])
|
109 |
+
df_sample_str = df.iloc[[0, -1], :].to_string(header=False, index=False)
|
110 |
+
columns = df.columns.tolist()
|
111 |
+
columns_means = ['交易日期','港股通(上海)','港股通(深圳)','沪股通(百万元)','深股通(百万元)','北向资金(百万元)','南向资金(百万元)' ]
|
112 |
+
|
113 |
+
columns_dict = dict(zip(columns, columns_means))
|
114 |
+
|
115 |
+
atomic_api = {"func_desc":None,"Column_name":None,"example_input":None,"output_first_and_last_row":None}
|
116 |
+
|
117 |
+
atomic_api["func_desc"] = "获取沪深港通资金每日的资金流向数据"
|
118 |
+
atomic_api["example_input"] = "pro.moneyflow_hsgt(start_date=\"20220101\", end_date=\"20230101\", fields=[\"trade_date\",\"ggt_ss\",\"ggt_sz\",\"hgt\",\"sgt\",\"north_money\",\"south_money\"])"
|
119 |
+
atomic_api["output_first_and_last_row"] = df_sample_str
|
120 |
+
atomic_api["Column_name"] = str(columns_dict)
|
121 |
+
|
122 |
+
all_atomic_api["pro.moneyflow_hsgt"] = atomic_api
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
#######################################################################################################################
|
127 |
+
|
128 |
+
df = pro.index_member(index_code= "850531.SI" , fields=["index_code","index_name","con_code","con_name","in_date","out_date","is_new"])
|
129 |
+
df_sample_str = df.iloc[[0, -1], :].to_string(header=False, index=False)
|
130 |
+
columns = df.columns.tolist()
|
131 |
+
columns_means = ['指数代码','指数名称', '成分股代码','成分股名称', '纳入日期', '剔除日期', '是否最新' ]
|
132 |
+
columns_dict = dict(zip(columns, columns_means))
|
133 |
+
|
134 |
+
atomic_api = {"func_desc":None,"Column_name":None,"example_input":None,"output_first_and_last_row":None}
|
135 |
+
|
136 |
+
atomic_api["func_desc"] = "获取申万行业指数的成分股信息"
|
137 |
+
atomic_api["example_input"] = "pro.index_member(index_code= \"850531.SI \", fields=[\"index_code\",\"con_code\",\"in_date\",\"out_date\",\"is_new\",\"index_name\",\"con_name\"])"
|
138 |
+
atomic_api["output_first_and_last_row"] = df_sample_str
|
139 |
+
atomic_api["Column_name"] = str(columns_dict)
|
140 |
+
|
141 |
+
|
142 |
+
all_atomic_api["pro.index_member"] = atomic_api
|
143 |
+
|
144 |
+
|
145 |
+
|
146 |
+
|
147 |
+
#######################################################################################################################
|
148 |
+
atomic_api = {"func_desc":None,"Column_name":None,"example_input":None,"output_first_and_last_row":None}
|
149 |
+
|
150 |
+
df = pro.index_classify(level='L1', src='SW2021',fields=["index_code","industry_name","level"])
|
151 |
+
|
152 |
+
|
153 |
+
df_sample_str = df.iloc[[0, -1], :].to_string(header=False, index=False)
|
154 |
+
columns = df.columns.tolist()
|
155 |
+
columns_means = ['行业代码', '行业名称', '行业级别']
|
156 |
+
columns_dict = dict(zip(columns, columns_means))
|
157 |
+
|
158 |
+
atomic_api["func_desc"] = "获取申万一级(L1),二级(L2),三级(L3)的行业信息"
|
159 |
+
atomic_api["example_input"] = "pro.index_classify(level='L1', src='SW2021',filter=[\"index_code\",\"industry_name\",\"level\"])"
|
160 |
+
atomic_api["output_first_and_last_row"] = df_sample_str
|
161 |
+
atomic_api["Column_name"] = str(columns_dict)
|
162 |
+
|
163 |
+
all_atomic_api["pro.index_classify"] = atomic_api
|
164 |
+
|
165 |
+
###############################################################
|
166 |
+
|
167 |
+
|
168 |
+
atomic_api = {"func_desc":None,"Column_name":None,"example_input":None,"output_first_and_last_row":None}
|
169 |
+
df_stock_basic = pro.stock_basic(ts_code="", name="贵州茅台", fields = ["ts_code","name","area","industry","market","list_date"])
|
170 |
+
df_stock_basic_sample_str = df_stock_basic.iloc[0, :].to_string(header=False, index=False)
|
171 |
+
columns = df_stock_basic.columns.tolist()
|
172 |
+
columns_means = ['股票代码', '股票名称', '地域', '所属行业', '市场类型', '上市日期']
|
173 |
+
columns_dict = dict(zip(columns, columns_means))
|
174 |
+
|
175 |
+
atomic_api["func_desc"] = "按照股票名称或者股票代码查询股票相关信息"
|
176 |
+
atomic_api["example_input"] = "pro.stock_basic(ts_code=\"\", name=\"贵州茅台\", fields = [\"ts_code\",\"name\",\"area\",\"industry\",\"market\",\"list_date\"])"
|
177 |
+
atomic_api["output_first_and_last_row"] = df_stock_basic_sample_str
|
178 |
+
atomic_api["Column_name"] = str(columns_dict)
|
179 |
+
|
180 |
+
|
181 |
+
all_atomic_api["pro.stock_basic"] = atomic_api
|
182 |
+
|
183 |
+
|
184 |
+
|
185 |
+
###############################################################
|
186 |
+
atomic_api = {"func_desc":None,"Column_name":None,"example_input":None,"output_first_and_last_row":None}
|
187 |
+
|
188 |
+
df_adj_factor = pro.adj_factor(ts_code='000001.SZ',start_date='20180718',end_date='20190718', fields=["ts_code","trade_date","adj_factor"])
|
189 |
+
df_adj_factor_sample_str = df_adj_factor.iloc[[0, -1], :].to_string(header=False, index=False)
|
190 |
+
columns = df_adj_factor.columns.tolist()
|
191 |
+
columns_means = ['股票代码', '交易日期', '复权因子']
|
192 |
+
columns_dict = dict(zip(columns, columns_means))
|
193 |
+
|
194 |
+
atomic_api["func_desc"] = "获取股票每日的复权因子"
|
195 |
+
atomic_api["example_input"] = "pro.adj_factor(ts_code='000001.SZ',start_date='20180718',end_date='20190718', fields=[\"ts_code\",\"trade_date\",\"adj_factor\"])"
|
196 |
+
atomic_api["output_first_and_last_row"] = df_adj_factor_sample_str
|
197 |
+
atomic_api["Column_name"] = str(columns_dict)
|
198 |
+
|
199 |
+
all_atomic_api["pro.adj_factor"] = atomic_api
|
200 |
+
###################################################################################################################
|
201 |
+
atomic_api = {"func_desc":None,"Column_name":None,"example_input":None,"output_first_and_last_row":None}
|
202 |
+
df_daily = pro.daily(ts_code='000001.SZ', start_date='20180701', end_date='20180718')
|
203 |
+
# 第一行第二行和最后一行
|
204 |
+
df_daily_sample_str = df_daily.iloc[[0, -1], :].to_string(header=False, index=False)
|
205 |
+
# 整个表格转换成字符串
|
206 |
+
# 获取列名
|
207 |
+
columns = df_daily.columns.tolist()
|
208 |
+
columns_means = ['股票代码', '交易日期', '开盘价', '最高价', '最低价', '收盘价', '昨收价', '涨跌额', '涨跌幅%', '成交量', '成交额']
|
209 |
+
columns_dict = dict(zip(columns, columns_means))
|
210 |
+
|
211 |
+
|
212 |
+
# 获取函数描述
|
213 |
+
atomic_api["func_desc"] = "获取股票每日的行情数据"
|
214 |
+
# 获取函数输入样例
|
215 |
+
atomic_api["example_input"] = "pro.daily(ts_code='000001.SZ', start_date='20180701', end_date='20180718')"
|
216 |
+
# 获取函数输出样例
|
217 |
+
atomic_api["output_first_and_last_row"] = df_daily_sample_str
|
218 |
+
# 获取列名
|
219 |
+
atomic_api["Column_name"] = str(columns_dict)
|
220 |
+
|
221 |
+
all_atomic_api["pro.daily"] = atomic_api
|
222 |
+
#######################################################################################################################
|
223 |
+
|
224 |
+
atomic_api = {"func_desc":None,"Column_name":None,"example_input":None,"output_first_and_last_row":None}
|
225 |
+
|
226 |
+
df_weekly = pro.weekly(ts_code='000001.SZ', start_date='20180101', end_date='20181101')
|
227 |
+
df_weekly_sample_str = df_weekly.iloc[[0, -1], :].to_string(header=False, index=False)
|
228 |
+
columns = df_weekly.columns.tolist()
|
229 |
+
columns_means = ['股票代码', '交易日期','周收盘价' ,'周开盘价', '周最高价', '周最低价', '上一周收盘价','周涨跌额', '周涨跌幅%', '周成交量', '周成交额']
|
230 |
+
columns_dict = dict(zip(columns, columns_means))
|
231 |
+
|
232 |
+
atomic_api["func_desc"] = "获取股票每周的行情数据"
|
233 |
+
atomic_api["example_input"] = "pro.weekly(ts_code='000001.SZ', start_date='20180101', end_date='20181101')"
|
234 |
+
atomic_api["output_first_and_last_row"] = df_weekly_sample_str
|
235 |
+
atomic_api["Column_name"] = str(columns_dict)
|
236 |
+
|
237 |
+
|
238 |
+
all_atomic_api["pro.weekly"] = atomic_api
|
239 |
+
|
240 |
+
#######################################################################################################################
|
241 |
+
|
242 |
+
atomic_api = {"func_desc":None,"Column_name":None,"example_input":None,"output_first_and_last_row":None}
|
243 |
+
|
244 |
+
df_monthly = pro.monthly(ts_code='000001.SZ', start_date='20180101', end_date='20181101')
|
245 |
+
df_monthly_sample_str = df_monthly.iloc[[0, -1], :].to_string(header=False, index=False)
|
246 |
+
columns = df_monthly.columns.tolist()
|
247 |
+
# columns 转换成dict
|
248 |
+
columns_means = ['股票代码', '交易日期','月收盘价' ,'月开盘价', '月最高价', '月最低价', '上一月收盘价','月涨跌额', '月涨跌幅%', '月成交量', '月成交额']
|
249 |
+
columns_dict = dict(zip(columns, columns_means))
|
250 |
+
|
251 |
+
atomic_api["func_desc"] = "获取股票每月的行情数据"
|
252 |
+
atomic_api["example_input"] = "pro.monthly(ts_code='000001.SZ', start_date='20180101', end_date='20181101')"
|
253 |
+
atomic_api["output_first_and_last_row"] = df_monthly_sample_str
|
254 |
+
atomic_api["Column_name"] = str(columns_dict)
|
255 |
+
|
256 |
+
all_atomic_api["pro.monthly"] = atomic_api
|
257 |
+
|
258 |
+
#######################################################################################################################
|
259 |
+
|
260 |
+
atomic_api = {"func_desc":None, "Column_name":None, "example_input":None, "output_first_and_last_row":None}
|
261 |
+
df = pro.index_daily(ts_code='399300.SZ', start_date='20180101', end_date='20191010')
|
262 |
+
df_sample_str = df.iloc[[0, -1], :].to_string(header=False, index=False)
|
263 |
+
columns = df.columns.tolist()
|
264 |
+
columns_means = ['指数代码', '交易日期', '收盘点位', '开盘点位', '最高点位', '最低点位', '昨收盘点位', '涨跌点', '涨跌幅%', '成交量', '成交额']
|
265 |
+
columns_dict = dict(zip(columns, columns_means))
|
266 |
+
|
267 |
+
atomic_api["func_desc"] = "获取指数每日的行情数据"
|
268 |
+
atomic_api["example_input"] = "pro.index_daily(ts_code='399300.SZ', start_date='20180101', end_date='20191010')"
|
269 |
+
atomic_api["output_first_and_last_row"] = df_sample_str
|
270 |
+
atomic_api["Column_name"] = str(columns_dict)
|
271 |
+
|
272 |
+
all_atomic_api["pro.index_daily"] = atomic_api
|
273 |
+
|
274 |
+
#######################################################################################################################
|
275 |
+
atomic_api = {"func_desc":None, "Column_name":None, "example_input":None, "output_first_and_last_row":None}
|
276 |
+
df = pro.index_weekly(ts_code='399300.SZ', start_date='20180101', end_date='20191010')
|
277 |
+
df_sample_str = df.iloc[[0, -1], :].to_string(header=False, index=False)
|
278 |
+
columns = df.columns.tolist()
|
279 |
+
columns_means = ['指数代码', '交易日期', '周收盘点位', '周开盘点位', '周最高点位', '周最低点位', '上周收盘点位', '周涨跌点', '周涨跌幅%', '周成交量', '周成交额']
|
280 |
+
columns_dict = dict(zip(columns, columns_means))
|
281 |
+
|
282 |
+
atomic_api["func_desc"] = "获取指数每周的行情数据"
|
283 |
+
atomic_api["example_input"] = "pro.index_weekly(ts_code='399300.SZ', start_date='20180101', end_date='20191010')"
|
284 |
+
atomic_api["output_first_and_last_row"] = df_sample_str
|
285 |
+
atomic_api["Column_name"] = str(columns_dict)
|
286 |
+
|
287 |
+
all_atomic_api["pro.index_weekly"] = atomic_api
|
288 |
+
|
289 |
+
#######################################################################################################################
|
290 |
+
atomic_api = {"func_desc":None, "Column_name":None, "example_input":None, "output_first_and_last_row":None}
|
291 |
+
df = pro.index_monthly(ts_code='399300.SZ', start_date='20180101', end_date='20191010')
|
292 |
+
df_sample_str = df.iloc[[0, -1], :].to_string(header=False, index=False)
|
293 |
+
columns = df.columns.tolist()
|
294 |
+
columns_means = ['指数代码', '交易日期', '月收盘点位', '月开盘点位', '月最高点位', '月最低点位', '上月收盘点位', '月涨跌点', '月涨跌幅%', '月成交量', '月成交额']
|
295 |
+
columns_dict = dict(zip(columns, columns_means))
|
296 |
+
|
297 |
+
atomic_api["func_desc"] = "获取指数每月的行情数据"
|
298 |
+
atomic_api["example_input"] = "pro.index_monthly(ts_code='399300.SZ', start_date='20180101', end_date='20191010')"
|
299 |
+
atomic_api["output_first_and_last_row"] = df_sample_str
|
300 |
+
atomic_api["Column_name"] = str(columns_dict)
|
301 |
+
|
302 |
+
all_atomic_api["pro.index_monthly"] = atomic_api
|
303 |
+
|
304 |
+
#######################################################################################################################
|
305 |
+
atomic_api = {"func_desc":None, "Column_name":None, "example_input":None, "output_first_and_last_row":None}
|
306 |
+
|
307 |
+
df = pro.index_basic(name = "沪深300", fields=["ts_code","name"])
|
308 |
+
df_sample_str = df.iloc[[0, -1], :].to_string(header=False, index=False)
|
309 |
+
columns = df.columns.tolist()
|
310 |
+
columns_means = ['指数代码', '指数名称']
|
311 |
+
columns_dict = dict(zip(columns, columns_means))
|
312 |
+
|
313 |
+
atomic_api["func_desc"] = "获取指数基本信息"
|
314 |
+
atomic_api["example_input"] = "pro.index_basic(name = '沪深300', fields=['ts_code','name'])"
|
315 |
+
atomic_api["output_first_and_last_row"] = df_sample_str
|
316 |
+
atomic_api["Column_name"] = str(columns_dict)
|
317 |
+
|
318 |
+
all_atomic_api["pro.index_basic"] = atomic_api
|
319 |
+
|
320 |
+
|
321 |
+
#######################################################################################################################
|
322 |
+
atomic_api = {"func_desc":None, "Column_name":None, "example_input":None, "output_first_and_last_row":None}
|
323 |
+
|
324 |
+
df = pro.index_weight(index_code="399300.SZ",start_date="20210101",end_date="20220101", fields=["index_code","con_code","trade_date","weight"])
|
325 |
+
df_sample_str = df.iloc[[0, -1], :].to_string(header=False, index=False)
|
326 |
+
columns = df.columns.tolist()
|
327 |
+
columns_means = ['指数代码', '成分股代码', '交易日期', '成分股权重']
|
328 |
+
columns_dict = dict(zip(columns, columns_means))
|
329 |
+
|
330 |
+
atomic_api["func_desc"] = "获取指数的成分股和成分股的权重"
|
331 |
+
atomic_api["example_input"] = "pro.index_weight(index_code='399300.SZ',start_date='20210101',end_date='20220101', fields=['index_code','con_code','trade_date','weight'])"
|
332 |
+
atomic_api["output_first_and_last_row"] = df_sample_str
|
333 |
+
atomic_api["Column_name"] = str(columns_dict)
|
334 |
+
|
335 |
+
all_atomic_api["pro.index_weight"] = atomic_api
|
336 |
+
|
337 |
+
|
338 |
+
|
339 |
+
|
340 |
+
#######################################################################################################################
|
341 |
+
|
342 |
+
|
343 |
+
|
344 |
+
#######################################################################################################################
|
345 |
+
|
346 |
+
# 创建文件夹如果不存在
|
347 |
+
if not os.path.exists(""):
|
348 |
+
os.mkdir("")
|
349 |
+
|
350 |
+
with open("all_atomic_api.json", "w") as f:
|
351 |
+
json.dump(all_atomic_api, f, ensure_ascii=False, indent=4)
|
352 |
+
|
353 |
+
|
354 |
+
|
355 |
+
|
356 |
+
|
357 |
+
|
358 |
+
|
359 |
+
|
360 |
+
|
create_tool/all_atomic_api.json
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"pro.fina_indicator": {
|
3 |
+
"func_desc": "财务指标",
|
4 |
+
"Column_name": {
|
5 |
+
"ts_code": "股票代码",
|
6 |
+
"end_date": "报告期",
|
7 |
+
"eps": "每股收益",
|
8 |
+
"current_ratio": "流动比率",
|
9 |
+
"quick_ratio": "速动比率",
|
10 |
+
"inv_turn": "存货周转率",
|
11 |
+
"netprofit_margin": "销售净利率",
|
12 |
+
"grossprofit_margin": "销售毛利率",
|
13 |
+
"roe": "净资产收益率",
|
14 |
+
"roa": "总资产净利率",
|
15 |
+
"roic": "投入资本回报率",
|
16 |
+
"debt_to_assets": "资产负债率",
|
17 |
+
"netprofit_yoy": "净利润同比增长率",
|
18 |
+
"dt_netprofit_yoy": "扣非净利润同比增长率"
|
19 |
+
},
|
20 |
+
"example_input": "pro.fina_indicator(ts_code = \"600000.SH\",start_date = \"20200104\",end_date = \"20220104\",fields=[\"ts_code\",\"end_date\",\"eps\",\"current_ratio\",\"quick_ratio\",\"inv_turn\",\"netprofit_margin\",\"grossprofit_margin\",\"roe\",\"roa\",\"roic\",\"debt_to_assets\",\"netprofit_yoy\",\"dt_netprofit_yoy\"])",
|
21 |
+
"output_first_and_last_row": "600000.SH 20211231 1.62 None None None 28.1524 None 8.1032 None None 91.6648 -9.1247 -9.26\n600000.SH 20200331 0.56 None None None 31.6289 None 3.0746 None None 92.0170 5.4803 5.23"
|
22 |
+
},
|
23 |
+
"pro.stock_company": {
|
24 |
+
"func_desc": "获取上市公司基本信息如公司业务描述,员工人数等基本信息",
|
25 |
+
"Column_name": {
|
26 |
+
"ts_code": "股票代码",
|
27 |
+
"exchange": "交易所代码",
|
28 |
+
"chairman": "法人代表",
|
29 |
+
"manager": "总经理",
|
30 |
+
"secretary": "董秘",
|
31 |
+
"reg_capital": "注册资本",
|
32 |
+
"setup_date": "注册日期",
|
33 |
+
"province": "所在省份",
|
34 |
+
"city": "所在城市",
|
35 |
+
"introduction": "公司介绍",
|
36 |
+
"website": "公司主页",
|
37 |
+
"email": "电子邮件",
|
38 |
+
"office": "办公室地址",
|
39 |
+
"business_scope": "员工人数",
|
40 |
+
"employees": "主要业务及产品",
|
41 |
+
"main_business": "经营范围"
|
42 |
+
},
|
43 |
+
"example_input": "pro.stock_company(ts_code = '600230.SH', fields=[\"ts_code\",\"exchange\",\"chairman\", \"manager\",\"secretary\", \"reg_capital\",\"setup_date\", \"province\",\"city\",\"introduction\",\"website\", \"email\",\"office\",\"employees\",\"main_business\",\"business_scope\"])",
|
44 |
+
"output_first_and_last_row": " 600230.SH SSE 谢华生 杜森肴 刘晓婧 41835.5802 19980924 河北 沧州市 公司是由中国化工集团控股、沧州市政府参股,以化肥、TDI为主导产品的大型综合性化工企业 www.czdh.chemchina.com [email protected] 河北省沧州市运河区永济东路20号 货物进出口;(以下限分支机构经营)化工机械、电气、仪表的技术服务;水污染治理;尿素销售;工业循... 1150 主要产品:尿素,TDI."
|
45 |
+
},
|
46 |
+
"pro.daily_basic": {
|
47 |
+
"func_desc": "获取股票每日基本指标例如换手率,市盈率市净率股息率等",
|
48 |
+
"Column_name": {
|
49 |
+
"ts_code": "股票代码",
|
50 |
+
"trade_date": "交易日期",
|
51 |
+
"turnover_rate": "换手率(总股本)",
|
52 |
+
"turnover_rate_f": "换手率(自由流通股本)",
|
53 |
+
"volume_ratio": "量比",
|
54 |
+
"pe_ttm": "市盈率(动态)",
|
55 |
+
"pb": "市净率",
|
56 |
+
"ps_ttm": "市销率(动态)",
|
57 |
+
"dv_ttm": "股息率(动态)",
|
58 |
+
"total_share": "总股本",
|
59 |
+
"float_share": "流通股本",
|
60 |
+
"free_share": "自由流通股本",
|
61 |
+
"total_mv": "总市值",
|
62 |
+
"circ_mv": "流通市值"
|
63 |
+
},
|
64 |
+
"example_input": "pro.daily_basic(ts_code = \"600230.SH\",start_date = \"20180726\",end_date = \"20200726\", fields=[\"ts_code\", \"trade_date\",\"turnover_rate\",\"turnover_rate_f\",\"volume_ratio\",\"pe_ttm\",\"pb\",\"ps_ttm\",\"dv_ttm\",\"total_share\",\"float_share\",\"free_share\",\"total_mv\",\"circ_mv\"])",
|
65 |
+
"output_first_and_last_row": "600230.SH 20200724 2.2876 4.2557 0.86 NaN 1.0717 2.1456 NaN 41186.3502 41186.3502 22139.6078 375619.5138 375619.5138\n600230.SH 20180726 2.4584 4.5734 0.72 8.8805 3.7203 2.4699 1.3188 41186.3502 41186.3502 22139.6078 1115326.3634 1115326.3634"
|
66 |
+
},
|
67 |
+
"pro.stk_factor": {
|
68 |
+
"func_desc": "获取股票的每日的技术指标数据",
|
69 |
+
"Column_name": "{'ts_code': '股票代码', 'trade_date': '交易日期', 'close': '收盘价', 'macd_dif': 'MACD_DIF', 'macd_dea': 'MACD_DEA', 'macd': 'MACD', 'kdj_k': 'KDJ_K', 'kdj_d': 'KDJ_D', 'kdj_j': 'KDJ_J', 'rsi_6': 'RSI_6', 'rsi_12': 'RSI_12', 'rsi_24': 'RSI_24', 'boll_upper': 'BOLL_UPPER', 'boll_mid': 'BOLL_MID', 'boll_lower': 'BOLL_LOWER', 'cci': 'CCI'}",
|
70 |
+
"example_input": "pro.stk_factor(ts_code=stock_code,start_date= start_date,end_date= end_date,fields=[\"ts_code\",\"trade_date\",\"close\",\"macd_dif\",\"macd_dea\",\"macd\",\"kdj_k\",\"kdj_d\",\"kdj_j\",\"rsi_6\",\"rsi_12\",\"rsi_24\",\"boll_upper\",\"boll_mid\",\"boll_lower\",\"cci\"])",
|
71 |
+
"output_first_and_last_row": "600000.SH 20230519 7.54 0.074 0.104 -0.060 21.127 33.030 -2.678 38.182 47.410 51.933 7.944 7.652 7.36 -71.419\n600000.SH 20220520 8.04 -0.013 -0.026 0.026 72.966 64.718 89.460 60.530 54.424 50.138 7.713 7.531 7.35 76.207"
|
72 |
+
},
|
73 |
+
"pro.moneyflow_hsgt": {
|
74 |
+
"func_desc": "获取沪深港通资金每日的资金流向数据",
|
75 |
+
"Column_name": "{'trade_date': '交易日期', 'ggt_ss': '港股通(上海)', 'ggt_sz': '港股通(深圳)', 'hgt': '沪股通(百万元)', 'sgt': '深股通(百万元)', 'north_money': '北向资金(百万元)', 'south_money': '南向资金(百万元)'}",
|
76 |
+
"example_input": "pro.moneyflow_hsgt(start_date=\"20220101\", end_date=\"20230101\", fields=[\"trade_date\",\"ggt_ss\",\"ggt_sz\",\"hgt\",\"sgt\",\"north_money\",\"south_money\"])",
|
77 |
+
"output_first_and_last_row": "20221230 504.31 1509.60 1568.20 -1475.09 93.11 2013.91\n20220104 -1.57 -1573.01 -940.71 1400.87 460.16 -1574.58"
|
78 |
+
},
|
79 |
+
"pro.index_member": {
|
80 |
+
"func_desc": "获取申万行业指数的成分股信息",
|
81 |
+
"Column_name": "{'index_code': '指数代码', 'index_name': '指数名称', 'con_code': '成分股代码', 'con_name': '成分股名称', 'in_date': '纳入日期', 'out_date': '剔除日期', 'is_new': '是否最新'}",
|
82 |
+
"example_input": "pro.index_member(index_code= \"850531.SI \", fields=[\"index_code\",\"con_code\",\"in_date\",\"out_date\",\"is_new\",\"index_name\",\"con_name\"])",
|
83 |
+
"output_first_and_last_row": "850531.SI 黄金(申万) 000506.SZ 中润资源 20150701 20160630 N\n850531.SI 黄金(申万) 601899.SH 紫金矿业 20140221 20211210 N"
|
84 |
+
},
|
85 |
+
"pro.index_classify": {
|
86 |
+
"func_desc": "获取申万一级(L1),二级(L2),三级(L3)的行业信息",
|
87 |
+
"Column_name": "{'index_code': '行业代码', 'industry_name': '行业名称', 'level': '行业级别'}",
|
88 |
+
"example_input": "pro.index_classify(level='L1', src='SW2021',filter=[\"index_code\",\"industry_name\",\"level\"])",
|
89 |
+
"output_first_and_last_row": "801010.SI 农林牧渔 L1\n801980.SI 美容护理 L1"
|
90 |
+
},
|
91 |
+
"pro.stock_basic": {
|
92 |
+
"func_desc": "按照股票名称或者股票代码查询股票相关信息",
|
93 |
+
"Column_name": "{'ts_code': '股票代码', 'name': '股票名称', 'area': '地域', 'industry': '所属行业', 'market': '市场类型', 'list_date': '上市日期'}",
|
94 |
+
"example_input": "pro.stock_basic(ts_code=\"\", name=\"贵州茅台\", fields = [\"ts_code\",\"name\",\"area\",\"industry\",\"market\",\"list_date\"])",
|
95 |
+
"output_first_and_last_row": "600519.SH 贵州茅台 贵州 白酒 主板 20010827"
|
96 |
+
},
|
97 |
+
"pro.adj_factor": {
|
98 |
+
"func_desc": "获取股票每日的复权因子",
|
99 |
+
"Column_name": "{'ts_code': '股票代码', 'trade_date': '交易日期', 'adj_factor': '复权因子'}",
|
100 |
+
"example_input": "pro.adj_factor(ts_code='000001.SZ',start_date='20180718',end_date='20190718', fields=[\"ts_code\",\"trade_date\",\"adj_factor\"])",
|
101 |
+
"output_first_and_last_row": "000001.SZ 20190718 109.169\n000001.SZ 20180718 108.031"
|
102 |
+
},
|
103 |
+
"pro.daily": {
|
104 |
+
"func_desc": "获取股票每日的行情数据",
|
105 |
+
"Column_name": "{'ts_code': '股票代码', 'trade_date': '交易日期', 'open': '开盘价', 'high': '最高价', 'low': '最低价', 'close': '收盘价', 'pre_close': '昨收价', 'change': '涨跌额', 'pct_chg': '涨跌幅%', 'vol': '成交量', 'amount': '成交额'}",
|
106 |
+
"example_input": "pro.daily(ts_code='000001.SZ', start_date='20180701', end_date='20180718')",
|
107 |
+
"output_first_and_last_row": "000001.SZ 20180718 8.75 8.85 8.69 8.70 8.72 -0.02 -0.23 525152.77 460697.377\n000001.SZ 20180702 9.05 9.05 8.55 8.61 9.09 -0.48 -5.28 1315520.13 1158545.868"
|
108 |
+
},
|
109 |
+
"pro.weekly": {
|
110 |
+
"func_desc": "获取股票每周的行情数据",
|
111 |
+
"Column_name": "{'ts_code': '股票代码', 'trade_date': '交易日期', 'close': '周收盘价', 'open': '周开盘价', 'high': '周最高价', 'low': '周最低价', 'pre_close': '上一周收盘价', 'change': '周涨跌额', 'pct_chg': '周涨跌幅%', 'vol': '周成交量', 'amount': '周成交额'}",
|
112 |
+
"example_input": "pro.weekly(ts_code='000001.SZ', start_date='20180101', end_date='20181101')",
|
113 |
+
"output_first_and_last_row": "000001.SZ 20181026 11.18 10.81 11.46 10.71 10.76 0.42 0.039 906250014.0 1.002282e+10\n000001.SZ 20180105 13.30 13.35 13.93 13.13 13.30 0.00 0.000 810891313.0 1.092060e+10"
|
114 |
+
},
|
115 |
+
"pro.monthly": {
|
116 |
+
"func_desc": "获取股票每月的行情数据",
|
117 |
+
"Column_name": "{'ts_code': '股票代码', 'trade_date': '交易日期', 'close': '月收盘价', 'open': '月开盘价', 'high': '月最高价', 'low': '月最低价', 'pre_close': '上一月收盘价', 'change': '月涨跌额', 'pct_chg': '月涨跌幅%', 'vol': '月成交量', 'amount': '月成交额'}",
|
118 |
+
"example_input": "pro.monthly(ts_code='000001.SZ', start_date='20180101', end_date='20181101')",
|
119 |
+
"output_first_and_last_row": "000001.SZ 20181031 10.91 10.70 11.46 9.70 11.05 -0.14 -0.0127 2780155709.0 2.960878e+10\n000001.SZ 20180131 14.05 13.35 15.13 12.86 13.30 0.75 0.0564 4614537646.0 6.454870e+10"
|
120 |
+
},
|
121 |
+
"pro.index_daily": {
|
122 |
+
"func_desc": "获取指数每日的行情数据",
|
123 |
+
"Column_name": "{'ts_code': '指数代码', 'trade_date': '交易日期', 'close': '收盘点位', 'open': '开盘点位', 'high': '最高点位', 'low': '最低点位', 'pre_close': '昨收盘点位', 'change': '涨跌点', 'pct_chg': '涨跌幅%', 'vol': '成交量', 'amount': '成交额'}",
|
124 |
+
"example_input": "pro.index_daily(ts_code='399300.SZ', start_date='20180101', end_date='20191010')",
|
125 |
+
"output_first_and_last_row": "399300.SZ 20191010 3874.6391 3838.4865 3877.1420 3829.4328 3843.2392 31.3999 0.8170 79222694.0 127474042.8\n399300.SZ 20180102 4087.4012 4045.2086 4087.7789 4045.2086 4030.8549 56.5463 1.4028 146935435.0 178402032.8"
|
126 |
+
},
|
127 |
+
"pro.index_weekly": {
|
128 |
+
"func_desc": "获取指数每周的行情数据",
|
129 |
+
"Column_name": "{'ts_code': '指数代码', 'trade_date': '交易日期', 'close': '周收盘点位', 'open': '周开盘点位', 'high': '周最高点位', 'low': '周最低点位', 'pre_close': '上周收盘点位', 'change': '周涨跌点', 'pct_chg': '周涨跌幅%', 'vol': '周成交量', 'amount': '周成交额'}",
|
130 |
+
"example_input": "pro.index_weekly(ts_code='399300.SZ', start_date='20180101', end_date='20191010')",
|
131 |
+
"output_first_and_last_row": "399300.SZ 20190930 3814.528 3842.074 3857.231 3813.549 3852.6534 -38.1254 -0.0099 6.593131e+09 1.004873e+11\n399300.SZ 20180105 4138.750 4045.208 4151.281 4045.208 4030.8540 107.8960 0.0268 5.823413e+10 7.438588e+11"
|
132 |
+
},
|
133 |
+
"pro.index_monthly": {
|
134 |
+
"func_desc": "获取指数每月的行情数据",
|
135 |
+
"Column_name": "{'ts_code': '指数代码', 'trade_date': '交易日期', 'close': '月收盘点位', 'open': '月开盘点位', 'high': '月最高点位', 'low': '月最低点位', 'pre_close': '上月收盘点位', 'change': '月涨跌点', 'pct_chg': '月涨跌幅%', 'vol': '月成交量', 'amount': '月成交额'}",
|
136 |
+
"example_input": "pro.index_monthly(ts_code='399300.SZ', start_date='20180101', end_date='20191010')",
|
137 |
+
"output_first_and_last_row": "399300.SZ 20190930 3814.528 3803.689 3985.239 3799.817 3799.586 14.9419 0.0039 2.167131e+11 3.151053e+12\n399300.SZ 20180131 4275.898 4045.208 4403.337 4045.208 4030.854 245.0440 0.0608 3.651823e+11 4.793171e+12"
|
138 |
+
},
|
139 |
+
"pro.index_basic": {
|
140 |
+
"func_desc": "获取指数基本信息",
|
141 |
+
"Column_name": "{'ts_code': '指数代码', 'name': '指数名称'}",
|
142 |
+
"example_input": "pro.index_basic(name = '沪深300', fields=['ts_code','name'])",
|
143 |
+
"output_first_and_last_row": "000300.SH 沪深300\n399300.SZ 沪深300"
|
144 |
+
},
|
145 |
+
"pro.index_weight": {
|
146 |
+
"func_desc": "获取大盘指数的成分股和成分股的权重",
|
147 |
+
"Column_name": "{'index_code': '指数代码', 'con_code': '成分股代码', 'trade_date': '交易日期', 'weight': '成分股权重'}",
|
148 |
+
"example_input": "pro.index_weight(index_code='399300.SZ',start_date='20210101',end_date='20220101', fields=['index_code','con_code','trade_date','weight'])",
|
149 |
+
"output_first_and_last_row": "399300.SZ 688012.SH 20211231 0.1410\n399300.SZ 002624.SZ 20210104 0.1767"
|
150 |
+
}
|
151 |
+
}
|
lab_gpt4_call.py
ADDED
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import requests
|
3 |
+
import openai
|
4 |
+
import tiktoken
|
5 |
+
import os
|
6 |
+
import time
|
7 |
+
from functools import wraps
|
8 |
+
|
9 |
+
import threading
|
10 |
+
|
11 |
+
|
12 |
+
def retry(exception_to_check, tries=3, delay=5, backoff=1):
|
13 |
+
"""
|
14 |
+
Decorator used to automatically retry a failed function. Parameters:
|
15 |
+
|
16 |
+
exception_to_check: The type of exception to catch.
|
17 |
+
tries: Maximum number of retry attempts.
|
18 |
+
delay: Waiting time between each retry.
|
19 |
+
backoff: Multiplicative factor to increase the waiting time after each retry.
|
20 |
+
"""
|
21 |
+
|
22 |
+
def deco_retry(f):
|
23 |
+
@wraps(f)
|
24 |
+
def f_retry(*args, **kwargs):
|
25 |
+
mtries, mdelay = tries, delay
|
26 |
+
while mtries > 1:
|
27 |
+
try:
|
28 |
+
return f(*args, **kwargs)
|
29 |
+
except exception_to_check as e:
|
30 |
+
print(f"{str(e)}, Retrying in {mdelay} seconds...")
|
31 |
+
time.sleep(mdelay)
|
32 |
+
mtries -= 1
|
33 |
+
mdelay *= backoff
|
34 |
+
return f(*args, **kwargs)
|
35 |
+
|
36 |
+
return f_retry # true decorator
|
37 |
+
|
38 |
+
return deco_retry
|
39 |
+
|
40 |
+
def timeout_decorator(timeout):
|
41 |
+
class TimeoutException(Exception):
|
42 |
+
pass
|
43 |
+
|
44 |
+
def decorator(func):
|
45 |
+
@wraps(func)
|
46 |
+
def wrapper(*args, **kwargs):
|
47 |
+
result = [TimeoutException('Function call timed out')] # Nonlocal mutable variable
|
48 |
+
def target():
|
49 |
+
try:
|
50 |
+
result[0] = func(*args, **kwargs)
|
51 |
+
except Exception as e:
|
52 |
+
result[0] = e
|
53 |
+
|
54 |
+
thread = threading.Thread(target=target)
|
55 |
+
thread.start()
|
56 |
+
thread.join(timeout)
|
57 |
+
if thread.is_alive():
|
58 |
+
print(f"Function {func.__name__} timed out, retrying...")
|
59 |
+
return wrapper(*args, **kwargs)
|
60 |
+
if isinstance(result[0], Exception):
|
61 |
+
raise result[0]
|
62 |
+
return result[0]
|
63 |
+
return wrapper
|
64 |
+
return decorator
|
65 |
+
|
66 |
+
|
67 |
+
def send_chat_request(request):
|
68 |
+
endpoint = 'http://10.15.82.10:8006/v1/chat/completions'
|
69 |
+
model = 'gpt-3.5-turbo'
|
70 |
+
# gpt4 gpt4-32k和gpt-3.5-turbo
|
71 |
+
headers = {
|
72 |
+
'Content-Type': 'application/json',
|
73 |
+
}
|
74 |
+
temperature = 0.7
|
75 |
+
top_p = 0.95
|
76 |
+
frequency_penalty = 0
|
77 |
+
presence_penalty = 0
|
78 |
+
max_tokens = 8000
|
79 |
+
stream = False
|
80 |
+
stop = None
|
81 |
+
messages = [{"role": "user", "content": request}]
|
82 |
+
data = {
|
83 |
+
'model': model,
|
84 |
+
'messages': messages,
|
85 |
+
'temperature': temperature,
|
86 |
+
'top_p': top_p,
|
87 |
+
'frequency_penalty': frequency_penalty,
|
88 |
+
'presence_penalty': presence_penalty,
|
89 |
+
'max_tokens': max_tokens,
|
90 |
+
'stream': stream,
|
91 |
+
'stop': stop,
|
92 |
+
}
|
93 |
+
|
94 |
+
response = requests.post(endpoint, headers=headers, data=json.dumps(data))
|
95 |
+
|
96 |
+
if response.status_code == 200:
|
97 |
+
data = json.loads(response.text)
|
98 |
+
data_res = data['choices'][0]['message']
|
99 |
+
|
100 |
+
return data_res
|
101 |
+
else:
|
102 |
+
raise Exception(f"Request failed with status code {response.status_code}: {response.text}")
|
103 |
+
|
104 |
+
def num_tokens_from_string(string: str, encoding_name: str) -> int:
|
105 |
+
"""Returns the number of tokens in a text string."""
|
106 |
+
encoding = tiktoken.get_encoding(encoding_name)
|
107 |
+
num_tokens = len(encoding.encode(string))
|
108 |
+
print('num_tokens:',num_tokens)
|
109 |
+
return num_tokens
|
110 |
+
|
111 |
+
@timeout_decorator(45)
|
112 |
+
def send_chat_request_Azure(query, openai_key, api_base, engine):
|
113 |
+
openai.api_type = "azure"
|
114 |
+
openai.api_version = "2023-03-15-preview"
|
115 |
+
|
116 |
+
openai.api_base = api_base
|
117 |
+
openai.api_key = openai_key
|
118 |
+
|
119 |
+
|
120 |
+
max_token_num = 8000 - num_tokens_from_string(query,'cl100k_base')
|
121 |
+
#
|
122 |
+
openai.api_request_timeout = 1 # 设置超时时间为10秒
|
123 |
+
|
124 |
+
response = openai.ChatCompletion.create(
|
125 |
+
engine = engine,
|
126 |
+
messages=[{"role": "system", "content": "You are an useful AI assistant that helps people solve the problem step by step."},
|
127 |
+
{"role": "user", "content": "" + query}],
|
128 |
+
temperature=0.0,
|
129 |
+
max_tokens=max_token_num,
|
130 |
+
top_p=0.95,
|
131 |
+
frequency_penalty=0,
|
132 |
+
presence_penalty=0,
|
133 |
+
stop=None)
|
134 |
+
|
135 |
+
|
136 |
+
|
137 |
+
data_res = response['choices'][0]['message']['content']
|
138 |
+
return data_res
|
139 |
+
#Note: The openai-python library support for Azure OpenAI is in preview.
|
140 |
+
|
141 |
+
|
142 |
+
|
143 |
+
@retry(Exception, tries=10, delay=20, backoff=2)
|
144 |
+
@timeout_decorator(45)
|
145 |
+
def send_official_call(query, openai_key='', api_base='', engine=''):
|
146 |
+
start = time.time()
|
147 |
+
# 转换成可阅读的时间
|
148 |
+
start = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start))
|
149 |
+
print(start)
|
150 |
+
openai.api_key = openai_key
|
151 |
+
|
152 |
+
response = openai.ChatCompletion.create(
|
153 |
+
# engine="gpt35",
|
154 |
+
model="gpt-3.5-turbo",
|
155 |
+
messages = [{"role": "system", "content": "You are an useful AI assistant that helps people solve the problem step by step."},
|
156 |
+
{"role": "user", "content": "" + query}],
|
157 |
+
#max_tokens=max_token_num,
|
158 |
+
temperature=0.1,
|
159 |
+
top_p=0.1,
|
160 |
+
frequency_penalty=0,
|
161 |
+
presence_penalty=0,
|
162 |
+
stop=None)
|
163 |
+
|
164 |
+
data_res = response['choices'][0]['message']['content']
|
165 |
+
return data_res
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
+
|
170 |
+
|
171 |
+
|
172 |
+
|
173 |
+
|
logo/openai.png
ADDED
logo/tushare.png
ADDED
main.py
ADDED
@@ -0,0 +1,390 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 导入tushare
|
2 |
+
import tushare as ts
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import pandas as pd
|
5 |
+
import os
|
6 |
+
import json
|
7 |
+
from matplotlib.ticker import MaxNLocator
|
8 |
+
import matplotlib.font_manager as fm
|
9 |
+
from lab_gpt4_call import send_chat_request,send_chat_request_Azure,send_official_call
|
10 |
+
import ast
|
11 |
+
import re
|
12 |
+
from tool import *
|
13 |
+
import tiktoken
|
14 |
+
import concurrent.futures
|
15 |
+
import datetime
|
16 |
+
from PIL import Image
|
17 |
+
from io import BytesIO
|
18 |
+
import queue
|
19 |
+
import datetime
|
20 |
+
from threading import Thread
|
21 |
+
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
|
22 |
+
plt.rcParams['axes.unicode_minus'] = False
|
23 |
+
import openai
|
24 |
+
|
25 |
+
|
26 |
+
# To override the Thread method
|
27 |
+
class MyThread(Thread):
|
28 |
+
|
29 |
+
def __init__(self, target, args):
|
30 |
+
super(MyThread, self).__init__()
|
31 |
+
self.func = target
|
32 |
+
self.args = args
|
33 |
+
|
34 |
+
def run(self):
|
35 |
+
self.result = self.func(*self.args)
|
36 |
+
|
37 |
+
def get_result(self):
|
38 |
+
return self.result
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
def parse_and_exe(call_dict, result_buffer, parallel_step: str='1'):
|
44 |
+
"""
|
45 |
+
Parse the input and call the corresponding function to obtain the result.
|
46 |
+
:param call_dict: dict, including arg, func, and output
|
47 |
+
:param result_buffer: dict, storing the corresponding intermediate results
|
48 |
+
:param parallel_step: int, parallel step
|
49 |
+
:return: Returns func(arg) and stores the corresponding result in result_buffer.
|
50 |
+
"""
|
51 |
+
arg_list = call_dict['arg' + parallel_step]
|
52 |
+
replace_arg_list = [result_buffer[item][0] if isinstance(item, str) and ('result' in item or 'input' in item) else item for item in arg_list] # 参数
|
53 |
+
func_name = call_dict['function' + parallel_step] #
|
54 |
+
output = call_dict['output' + parallel_step] #
|
55 |
+
desc = call_dict['description' + parallel_step] #
|
56 |
+
if func_name == 'loop_rank':
|
57 |
+
replace_arg_list[1] = eval(replace_arg_list[1])
|
58 |
+
result = eval(func_name)(*replace_arg_list)
|
59 |
+
result_buffer[output] = (result, desc) # 'result1': (df1, desc)
|
60 |
+
return result_buffer
|
61 |
+
|
62 |
+
def load_tool_and_prompt(tool_lib, tool_prompt ):
|
63 |
+
'''
|
64 |
+
Read two JSON files.
|
65 |
+
:param tool_lib: Tool description
|
66 |
+
:param tool_prompt: Tool prompt
|
67 |
+
:return: Flattened prompt
|
68 |
+
'''
|
69 |
+
#
|
70 |
+
|
71 |
+
with open(tool_lib, 'r') as f:
|
72 |
+
tool_lib = json.load(f)
|
73 |
+
|
74 |
+
with open(tool_prompt, 'r') as f:
|
75 |
+
#
|
76 |
+
tool_prompt = json.load(f)
|
77 |
+
|
78 |
+
for key, value in tool_lib.items():
|
79 |
+
tool_prompt["Function Library:"] = tool_prompt["Function Library:"] + key + " " + value+ '\n\n'
|
80 |
+
|
81 |
+
|
82 |
+
prompt_flat = ''
|
83 |
+
|
84 |
+
for key, value in tool_prompt.items():
|
85 |
+
prompt_flat = prompt_flat + key +' '+ value + '\n\n'
|
86 |
+
|
87 |
+
|
88 |
+
return prompt_flat
|
89 |
+
|
90 |
+
# callback function
|
91 |
+
intermediate_results = queue.Queue() # Create a queue to store intermediate results.
|
92 |
+
|
93 |
+
def add_to_queue(intermediate_result):
|
94 |
+
intermediate_results.put(f"After planing, the intermediate result is {intermediate_result}")
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
+
def check_RPM(run_time_list, new_time, max_RPM=1):
|
99 |
+
# Check if there are already 3 timestamps in the run_time_list, with a maximum of 3 accesses per minute.
|
100 |
+
# False means no rest is needed, True means rest is needed.
|
101 |
+
if len(run_time_list) < 3:
|
102 |
+
run_time_list.append(new_time)
|
103 |
+
return 0
|
104 |
+
else:
|
105 |
+
if (new_time - run_time_list[0]).seconds < max_RPM:
|
106 |
+
# Calculate the required rest time.
|
107 |
+
sleep_time = 60 - (new_time - run_time_list[0]).seconds
|
108 |
+
print('sleep_time:', sleep_time)
|
109 |
+
run_time_list.pop(0)
|
110 |
+
run_time_list.append(new_time)
|
111 |
+
return sleep_time
|
112 |
+
else:
|
113 |
+
run_time_list.pop(0)
|
114 |
+
run_time_list.append(new_time)
|
115 |
+
return 0
|
116 |
+
|
117 |
+
def run(instruction, add_to_queue=None, send_chat_request_Azure = send_official_call, openai_key = '', api_base='', engine=''):
|
118 |
+
output_text = ''
|
119 |
+
################################# Step-1:Task select ###########################################
|
120 |
+
current_time = datetime.datetime.now()
|
121 |
+
formatted_time = current_time.strftime("%Y-%m-%d")
|
122 |
+
# If the time has not exceeded 3 PM, use yesterday's data.
|
123 |
+
if current_time.hour < 15:
|
124 |
+
formatted_time = (current_time - datetime.timedelta(days=1)).strftime("%Y-%m-%d")
|
125 |
+
|
126 |
+
print('===============================Intent Detecting===========================================')
|
127 |
+
with open('./prompt_lib/prompt_intent_detection.json', 'r') as f:
|
128 |
+
prompt_task_dict = json.load(f)
|
129 |
+
prompt_intent_detection = ''
|
130 |
+
for key, value in prompt_task_dict.items():
|
131 |
+
prompt_intent_detection = prompt_intent_detection + key + ": " + value+ '\n\n'
|
132 |
+
|
133 |
+
prompt_intent_detection = prompt_intent_detection + '\n\n' + 'Instruction:' + '今天的日期是'+ formatted_time +', '+ instruction + ' ###New Instruction: '
|
134 |
+
# Record the running time.
|
135 |
+
# current_time = datetime.datetime.now()
|
136 |
+
# sleep_time = check_RPM(run_time, current_time)
|
137 |
+
# if sleep_time > 0:
|
138 |
+
# time.sleep(sleep_time)
|
139 |
+
response = send_chat_request_Azure(prompt_intent_detection, openai_key=openai_key, api_base=api_base, engine=engine)
|
140 |
+
|
141 |
+
|
142 |
+
|
143 |
+
|
144 |
+
new_instruction = response
|
145 |
+
print('new_instruction:', new_instruction)
|
146 |
+
output_text = output_text + '\n======Intent Detecting Stage=====\n\n'
|
147 |
+
output_text = output_text + new_instruction +'\n\n'
|
148 |
+
|
149 |
+
if add_to_queue is not None:
|
150 |
+
add_to_queue(output_text)
|
151 |
+
|
152 |
+
event_happen = True
|
153 |
+
print('===============================Task Planing===========================================')
|
154 |
+
output_text= output_text + '=====Task Planing Stage=====\n\n'
|
155 |
+
|
156 |
+
with open('./prompt_lib/prompt_task.json', 'r') as f:
|
157 |
+
prompt_task_dict = json.load(f)
|
158 |
+
prompt_task = ''
|
159 |
+
for key, value in prompt_task_dict.items():
|
160 |
+
prompt_task = prompt_task + key + ": " + value+ '\n\n'
|
161 |
+
|
162 |
+
prompt_task = prompt_task + '\n\n' + 'Instruction:' + new_instruction + ' ###Plan:'
|
163 |
+
# current_time = datetime.datetime.now()
|
164 |
+
# sleep_time = check_RPM(run_time, current_time)
|
165 |
+
# if sleep_time > 0:
|
166 |
+
# time.sleep(sleep_time)
|
167 |
+
|
168 |
+
response = send_chat_request_Azure(prompt_task, openai_key=openai_key,api_base=api_base,engine=engine)
|
169 |
+
|
170 |
+
task_select = response
|
171 |
+
pattern = r"(task\d+=)(\{[^}]*\})"
|
172 |
+
matches = re.findall(pattern, task_select)
|
173 |
+
task_plan = {}
|
174 |
+
for task in matches:
|
175 |
+
task_step, task_select = task
|
176 |
+
task_select = task_select.replace("'", "\"") # Replace single quotes with double quotes.
|
177 |
+
task_select = json.loads(task_select)
|
178 |
+
task_name = list(task_select.keys())[0]
|
179 |
+
task_instruction = list(task_select.values())[0]
|
180 |
+
|
181 |
+
task_plan[task_name] = task_instruction
|
182 |
+
|
183 |
+
# task_plan
|
184 |
+
for key, value in task_plan.items():
|
185 |
+
print(key, ':', value)
|
186 |
+
output_text = output_text + key + ': ' + str(value) + '\n'
|
187 |
+
|
188 |
+
output_text = output_text +'\n'
|
189 |
+
if add_to_queue is not None:
|
190 |
+
add_to_queue(output_text)
|
191 |
+
|
192 |
+
|
193 |
+
|
194 |
+
################################# Step-2:Tool select and use ###########################################
|
195 |
+
print('===============================Tool select and using Stage===========================================')
|
196 |
+
output_text = output_text + '======Tool select and using Stage======\n\n'
|
197 |
+
# Read the task_select JSON file name.
|
198 |
+
task_name = list(task_plan.keys())[0].split('_task')[0]
|
199 |
+
task_instruction = list(task_plan.values())[0]
|
200 |
+
|
201 |
+
tool_lib = './tool_lib/' + 'tool_' + task_name + '.json'
|
202 |
+
tool_prompt = './prompt_lib/' + 'prompt_' + task_name + '.json'
|
203 |
+
prompt_flat = load_tool_and_prompt(tool_lib, tool_prompt)
|
204 |
+
prompt_flat = prompt_flat + '\n\n' +'Instruction :'+ task_instruction+ ' ###Function Call'
|
205 |
+
|
206 |
+
#response = "step1={\n \"arg1\": [\"贵州茅台\"],\n \"function1\": \"get_stock_code\",\n \"output1\": \"result1\"\n},step2={\n \"arg1\": [\"result1\",\"20180123\",\"20190313\",\"daily\"],\n \"function1\": \"get_stock_prices_data\",\n \"output1\": \"result2\"\n},step3={\n \"arg1\": [\"result2\",\"close\"],\n \"function1\": \"calculate_stock_index\",\n \"output1\": \"result3\"\n}, ###Output:{\n \"贵州茅台在2018年1月23日到2019年3月13的每日收盘价格的时序表格\": \"result3\",\n}"
|
207 |
+
# current_time = datetime.datetime.now()
|
208 |
+
# sleep_time = check_RPM(run_time, current_time)
|
209 |
+
# if sleep_time > 0:
|
210 |
+
# time.sleep(sleep_time)
|
211 |
+
|
212 |
+
response = send_chat_request_Azure(prompt_flat, openai_key=openai_key,api_base=api_base, engine=engine)
|
213 |
+
|
214 |
+
#response = "Function Call:step1={\n \"arg1\": [\"五粮液\"],\n \"function1\": \"get_stock_code\",\n \"output1\": \"result1\",\n \"arg2\": [\"泸州老窖\"],\n \"function2\": \"get_stock_code\",\n \"output2\": \"result2\"\n},step2={\n \"arg1\": [\"result1\",\"20190101\",\"20220630\",\"daily\"],\n \"function1\": \"get_stock_prices_data\",\n \"output1\": \"result3\",\n \"arg2\": [\"result2\",\"20190101\",\"20220630\",\"daily\"],\n \"function2\": \"get_stock_prices_data\",\n \"output2\": \"result4\"\n},step3={\n \"arg1\": [\"result3\",\"Cumulative_Earnings_Rate\"],\n \"function1\": \"calculate_stock_index\",\n \"output1\": \"result5\",\n \"arg2\": [\"result4\",\"Cumulative_Earnings_Rate\"],\n \"function2\": \"calculate_stock_index\",\n \"output2\": \"result6\"\n}, ###Output:{\n \"五粮液在2019年1月1日到2022年06月30的每日收盘价格时序表格\": \"result5\",\n \"泸州老窖在2019年1月1日到2022年06月30的每日收盘价格时序表格\": \"result6\"\n}"
|
215 |
+
call_steps, _ = response.split('###')
|
216 |
+
pattern = r"(step\d+=)(\{[^}]*\})"
|
217 |
+
matches = re.findall(pattern, call_steps)
|
218 |
+
result_buffer = {} # The stored format is as follows: {'result1': (000001.SH, 'Stock code of China Ping An'), 'result2': (df2, 'Stock data of China Ping An from January to June 2021')}.
|
219 |
+
output_buffer = [] # Store the variable names [result5, result6] that will be passed as the final output to the next task.
|
220 |
+
# print(task_output)
|
221 |
+
#
|
222 |
+
|
223 |
+
for match in matches:
|
224 |
+
step, content = match
|
225 |
+
content = content.replace("'", "\"") # Replace single quotes with double quotes.
|
226 |
+
print('==================')
|
227 |
+
print("\n\nstep:", step)
|
228 |
+
print('content:',content)
|
229 |
+
call_dict = json.loads(content)
|
230 |
+
print('It has parallel steps:', len(call_dict) / 4)
|
231 |
+
output_text = output_text + step + ': ' + str(call_dict) + '\n\n'
|
232 |
+
|
233 |
+
|
234 |
+
# Execute the following code in parallel using multiple processes.
|
235 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
236 |
+
# Submit tasks to thread pool
|
237 |
+
futures = {executor.submit(parse_and_exe, call_dict, result_buffer, str(parallel_step))
|
238 |
+
for parallel_step in range(1, int(len(call_dict) / 4) + 1)}
|
239 |
+
|
240 |
+
# Collect results as they become available
|
241 |
+
for idx, future in enumerate(concurrent.futures.as_completed(futures)):
|
242 |
+
# Handle possible exceptions
|
243 |
+
try:
|
244 |
+
result = future.result()
|
245 |
+
# Print the current parallel step number.
|
246 |
+
print('parallel step:', idx+1)
|
247 |
+
# print(list(result[1].keys())[0])
|
248 |
+
# print(list(result[1].values())[0])
|
249 |
+
except Exception as exc:
|
250 |
+
print(f'Generated an exception: {exc}')
|
251 |
+
|
252 |
+
if step == matches[-1][0]:
|
253 |
+
# Current task's final step. Save the output of the final step.
|
254 |
+
for parallel_step in range(1, int(len(call_dict) / 4) + 1):
|
255 |
+
output_buffer.append(call_dict['output' + str(parallel_step)])
|
256 |
+
output_text = output_text + '\n'
|
257 |
+
if add_to_queue is not None:
|
258 |
+
add_to_queue(output_text)
|
259 |
+
|
260 |
+
|
261 |
+
|
262 |
+
|
263 |
+
|
264 |
+
################################# Step-3:visualization ###########################################
|
265 |
+
print('===============================Visualization Stage===========================================')
|
266 |
+
output_text = output_text + '======Visualization Stage====\n\n'
|
267 |
+
task_name = list(task_plan.keys())[1].split('_task')[0] #visualization_task
|
268 |
+
#task_name = 'visualization'
|
269 |
+
task_instruction = list(task_plan.values())[1] #''
|
270 |
+
|
271 |
+
|
272 |
+
tool_lib = './tool_lib/' + 'tool_' + task_name + '.json'
|
273 |
+
tool_prompt = './prompt_lib/' + 'prompt_' + task_name + '.json'
|
274 |
+
|
275 |
+
result_buffer_viz={}
|
276 |
+
Previous_result = {}
|
277 |
+
for output_name in output_buffer:
|
278 |
+
rename = 'input'+ str(output_buffer.index(output_name)+1)
|
279 |
+
Previous_result[rename] = result_buffer[output_name][1]
|
280 |
+
result_buffer_viz[rename] = result_buffer[output_name]
|
281 |
+
|
282 |
+
prompt_flat = load_tool_and_prompt(tool_lib, tool_prompt)
|
283 |
+
prompt_flat = prompt_flat + '\n\n' +'Instruction: '+ task_instruction + ', Previous_result: '+ str(Previous_result) + ' ###Function Call'
|
284 |
+
|
285 |
+
# current_time = datetime.datetime.now()
|
286 |
+
# sleep_time = check_RPM(run_time, current_time)
|
287 |
+
# if sleep_time > 0:
|
288 |
+
# time.sleep(sleep_time)
|
289 |
+
|
290 |
+
response = send_chat_request_Azure(prompt_flat, openai_key=openai_key, api_base=api_base, engine=engine)
|
291 |
+
call_steps, _ = response.split('###')
|
292 |
+
pattern = r"(step\d+=)(\{[^}]*\})"
|
293 |
+
matches = re.findall(pattern, call_steps)
|
294 |
+
for match in matches:
|
295 |
+
step, content = match
|
296 |
+
content = content.replace("'", "\"") # Replace single quotes with double quotes.
|
297 |
+
print('==================')
|
298 |
+
print("\n\nstep:", step)
|
299 |
+
print('content:',content)
|
300 |
+
call_dict = json.loads(content)
|
301 |
+
print('It has parallel steps:', len(call_dict) / 4)
|
302 |
+
result_buffer_viz = parse_and_exe(call_dict, result_buffer_viz, parallel_step = '' )
|
303 |
+
output_text = output_text + step + ': ' + str(call_dict) + '\n\n'
|
304 |
+
|
305 |
+
if add_to_queue is not None:
|
306 |
+
add_to_queue(output_text)
|
307 |
+
|
308 |
+
finally_output = list(result_buffer_viz.values()) # plt.Axes
|
309 |
+
|
310 |
+
#
|
311 |
+
df = pd.DataFrame()
|
312 |
+
str_out = output_text + 'Finally result: '
|
313 |
+
for ax in finally_output:
|
314 |
+
if isinstance(ax[0], plt.Axes): # If the output is plt.Axes, display it.
|
315 |
+
plt.grid()
|
316 |
+
#plt.show()
|
317 |
+
str_out = str_out + ax[1]+ ':' + 'plt.Axes' + '\n\n'
|
318 |
+
#
|
319 |
+
elif isinstance(ax[0], pd.DataFrame):
|
320 |
+
df = ax[0]
|
321 |
+
str_out = str_out + ax[1]+ ':' + 'pd.DataFrame' + '\n\n'
|
322 |
+
|
323 |
+
else:
|
324 |
+
str_out = str_out + str(ax[1])+ ':' + str(ax[0]) + '\n\n'
|
325 |
+
|
326 |
+
|
327 |
+
#
|
328 |
+
print('===============================Summary Stage===========================================')
|
329 |
+
output_prompt = "请用第一人称总结一下整个任务规划和解决过程,并且输出结果,用[Task]表示每个规划任务,用\{function\}表示每个任务里调用的函数." + \
|
330 |
+
"示例1:###我用将您的问题拆分成两个任务,首先第一个任务[stock_task],我依次获取五粮液和贵州茅台从2013年5月20日到2023年5月20日的净资产回报率roe的时序数据. \n然后第二个任务[visualization_task],我用折线图绘制五粮液和贵州茅台从2013年5月20日到2023年5月20日的净资产回报率,并计算它们的平均值和中位数. \n\n在第一个任务���我分别使用了2个工具函数\{get_stock_code\},\{get_Financial_data_from_time_range\}获取到两只股票的roe数据,在第二个任务里我们使用折线图\{plot_stock_data\}工具函数来绘制他们的roe十年走势,最后并计算了两只股票十年ROE的中位数\{output_median_col\}和均值\{output_mean_col\}.\n\n最后贵州茅台的ROE的均值和中位数是\{\},{},五粮液的ROE的均值和中位数是\{\},\{\}###" + \
|
331 |
+
"示例2:###我用将您的问题拆分成两个任务,首先第一个任务[stock_task],我依次获取20230101到20230520这段时间北向资金每日净流入和每日累计流入时序数据,第二个任务是[visualization_task],因此我在同一张图里同时绘制北向资金20230101到20230520的每日净流入柱状图和每日累计流入的折线图 \n\n为了完成第一个任务中我分别使用了2个工具函数\{get_north_south_money\},\{calculate_stock_index\}分别获取到北上资金的每日净流入量和每日的累计净流入量,第二个任务里我们使用折线图\{plot_stock_data\}绘制来两个指标的变化走势.\n\n最后我们给您提供了包含两个指标的折线图和数据表格." + \
|
332 |
+
"示例3:###我用将您的问题拆分成两个任务,首先第一个任务[economic_task],我爬取了上市公司贵州茅台和其主营业务介绍信息. \n然后第二个任务[visualization_task],我用表格打印贵州茅台及其相关信息. \n\n在第一个任务中我分别使用了1个工具函数\{get_company_info\} 获取到贵州茅台的公司信息,在第二个任务里我们使用折线图\{print_save_table\}工具函数来输出表格.\n"
|
333 |
+
output_result = send_chat_request_Azure(output_prompt + str_out + '###', openai_key=openai_key, api_base=api_base,engine=engine)
|
334 |
+
print(output_result)
|
335 |
+
buf = BytesIO()
|
336 |
+
plt.savefig(buf, format='png')
|
337 |
+
buf.seek(0)
|
338 |
+
#
|
339 |
+
#
|
340 |
+
image = Image.open(buf)
|
341 |
+
|
342 |
+
|
343 |
+
return output_text, image, output_result, df
|
344 |
+
|
345 |
+
|
346 |
+
def gradio_interface(query, openai_key, openai_key_azure, api_base,engine):
|
347 |
+
# Create a new thread to run the function.
|
348 |
+
if openai_key.startswith('sk') and openai_key_azure == '':
|
349 |
+
print('send_official_call')
|
350 |
+
thread = MyThread(target=run, args=(query, add_to_queue, send_official_call, openai_key))
|
351 |
+
elif openai_key =='' and len(openai_key_azure)>0:
|
352 |
+
print('send_chat_request_Azure')
|
353 |
+
thread = MyThread(target=run, args=(query, add_to_queue, send_chat_request_Azure, openai_key_azure, api_base, engine))
|
354 |
+
thread.start()
|
355 |
+
placeholder_image = np.zeros((100, 100, 3), dtype=np.uint8) # Create a placeholder image.
|
356 |
+
placeholder_dataframe = pd.DataFrame() #
|
357 |
+
|
358 |
+
# Wait for the result of the calculate function and display the intermediate results simultaneously.
|
359 |
+
while thread.is_alive():
|
360 |
+
while not intermediate_results.empty():
|
361 |
+
yield intermediate_results.get(), placeholder_image, 'Running' , placeholder_dataframe # Use the yield keyword to return intermediate results in real-time
|
362 |
+
time.sleep(0.1) # Avoid excessive resource consumption.
|
363 |
+
|
364 |
+
finally_text, img, output, df = thread.get_result()
|
365 |
+
yield finally_text, img, output, df
|
366 |
+
# Return the final result.
|
367 |
+
|
368 |
+
|
369 |
+
|
370 |
+
instruction = '预测未来中国4个季度的GDP增长率'
|
371 |
+
|
372 |
+
if __name__ == '__main__':
|
373 |
+
|
374 |
+
# 初始化pro接口
|
375 |
+
#openai_call = send_chat_request_Azure #
|
376 |
+
openai_call = send_official_call #
|
377 |
+
openai_key = os.getenv("OPENAI_KEY")
|
378 |
+
|
379 |
+
|
380 |
+
|
381 |
+
output, image, df , output_result = run(instruction, send_chat_request_Azure = openai_call, openai_key=openai_key, api_base='', engine='')
|
382 |
+
print(output_result)
|
383 |
+
plt.show()
|
384 |
+
|
385 |
+
|
386 |
+
|
387 |
+
|
388 |
+
|
389 |
+
|
390 |
+
|
output/长沙银行及其相关信息.csv
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
股票名称,股票代码,交易所代码,法人代表,总经理,董秘,注册资本,注册日期,所在省份,所在城市,公司介绍,公司主页,电子邮件,办公室,公告日期,经营范围,员工人数,主要业务及产品
|
2 |
+
长沙银行,601577.SH,SSE,赵小中,唐力勇,彭敬恩,402155.3754,19970818,湖南,长沙市,公司是湖南省首家区域性股份制商业银行和法人金融企业。目前,长沙银行营业网点实现了湖南全域覆盖,为广大客户提供365天*24小时手机银行、网上银行、微信银行和电话银行服务。长沙银行始终秉承“正道而行、信泽大众”的发展使命,坚持“聚焦客户、实干为本、快乐同行”的核心价值观,坚持深耕湖南,做“湖南人的主办银行”的定位,构建了以大批发为业务主体,大零售、大资管为两翼,网络金融为一尾的“一体两翼一尾”业务格局,着力打造智慧金融、县域金融、绿色金融、科技金融四大特色战略品牌。近年来,长沙银行的品牌影响力不断提升。,www.bankofchangsha.com,[email protected],湖南省长沙市岳麓区滨江路53号楷林商务中心B座,20230429,吸收公众存款;发放短期、中期和长期贷款;办理国内外结算;办理票据承兑与贴现;发行金融债券;代理发行、代理兑付、承销政府债券;买卖政府债券、金融债券;从事同业拆借;买卖、代理买卖外汇;从事银行卡业务;提供信用证服务及担保;代理收付款项及代理保险业务;提供保管箱服务;经国务院银行业监督管理机构批准的其他业务。,8893,"主营业务包括公司金融业务,零售金融业务和资金业务等."
|
prompt_lib/prompt_economic.json
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"Global prompt:": "请根据给定的指令,利用给定的函数一步一步地完成任务,每一步你必须要从以下函数库中选择一个或者多个无依赖关系的函数,并且为函数生成对应的参数。以json的格式生成每一步如下:step1={\n \"arg1\": [arg1,arg2...],\n \"function1\": \"%s\",\n \"output1\": \"%s\",\n \"description1\": \"%s\"\n},step2={\n \"arg1\": [arg1,arg2..],\n \"function1\": \"%s\",\n \"output1\": \"%s\",\n \"description1\": \"%s\"\n}###, 请注意每步的选择的多个函数之间必须无依赖关系, 并行地生成对应多个result_i, 后面步骤的函数使用之前的result作为参数输入,以###结尾",
|
3 |
+
"Function Library:": "",
|
4 |
+
"Instruction: 爬取新浪财经当前最新的财经消息和市场动态新闻": "###Function Call: step1={\n \"arg1\": [\"sina\"],\n \"function1\": \"get_latest_new_from_web\",\n \"output1\": \"result1\",\n \"description1\": \"当前最新的财经新闻数据\"\n}###",
|
5 |
+
"Instruction: 获取20120711到20220711期间每月的cpi全国同比变化时序数据": "###Function Call: step1={\n \"arg1\": [\"20120711\",\"20220711\",\"cpi\",\"nt_yoy\"],\n \"function1\": \"get_cpi_ppi_currency_supply_data\",\n \"output1\": \"result1\",\n \"description1\": \"cpi全国同比时序数据\"\n}###",
|
6 |
+
"Instruction: 获取20120711到20220711中国每季度GDP增速同比数据": "###Function Call: step1={\n \"arg1\": [\"20120711\",\"20220711\",\"gdp_yoy\"],\n \"function1\": \"get_GDP_data\",\n \"output1\": \"result1\",\n \"description1\": \"gdp同比增速数据\"\n}###",
|
7 |
+
"Instruction: 获取20120711到20220711中国每月货币供应M2增速同比数据": "###Function Call: step1={\n \"arg1\": [\"20120711\",\"20220711\",\"currency_supply\",\"m2_yoy\"],\n \"function1\": \"get_cpi_ppi_currency_supply_data\",\n \"output1\": \"result1\",\n \"description1\": \"M2同比增速数据\"\n}###",
|
8 |
+
"Instruction: 获取上市公司贵州茅台和其主营业务介绍信息": "###Function Call: step1={\n \"arg1\": [\"贵州茅台\"],\n \"function1\": \"get_company_info\",\n \"output1\": \"result1\",\n \"description1\": \"贵州茅台的信息\"\n}###",
|
9 |
+
"Instruction: 获取20220101到20230510这段时间北向资金每日净流入和每日累计流入时序数据": "###Function Call: step1={\n \"arg1\": [\"20220101\",\"20230510\"],\n \"function1\": \"get_north_south_money\",\n \"output1\": \"result1\",\n \"description1\": \"北向资金的每日的数据\"\n},step2={\n \"arg1\": [\"result1\",\"accumulate_north_money\"],\n \"function1\": \"calculate_stock_index\",\n \"output1\": \"result2\",\n \"description1\": \"北向资金每日的累计净流入量时序数据\",\n \"arg2\": [\"result1\",\"north_money\"],\n \"function2\": \"calculate_stock_index\",\n \"output2\": \"result3\",\n \"description2\": \"北向资金每日的净流入量的时序数据\"\n}###",
|
10 |
+
"Instruction: 获取从20000101到20210523的季度GDP数据并预测此后的12个月的数据": "###Function Call: step1={\n \"arg1\": [\"2000101\",\"20210523\",\"gdp_yoy\"],\n \"function1\": \"get_GDP_data\",\n \"output1\": \"result1\",\n \"description1\": \"gdp同比增速数据\"\n},step2={\n \"arg1\": [\"result1\",\"gdp_yoy\",12],\n \"function1\": \"predict_next_value\",\n \"output1\": \"result2\",\n \"description1\": \"未来12个季度gdp同比增速数据预测数据\"\n}###"
|
11 |
+
}
|
12 |
+
|
prompt_lib/prompt_financial.json
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"Global prompt:": "请根据给定的指令,利用给定的函数一步一步地完成任务,每一步你必须要从以下函数库中选择一个或者多个无依赖关系的函数,并且为函数生成对应的参数。以json的格式生成每一步如下:step1={\n \"arg1\": [arg1,arg2...],\n \"function1\": \"%s\",\n \"output1\": \"%s\",\n \"description1\": \"%s\"\n},step2={\n \"arg1\": [arg1,arg2..],\n \"function1\": \"%s\",\n \"output1\": \"%s\",\n \"description1\": \"%s\"\n}###, 请注意每步的选择的多个函数之间必须无依赖关系, 并行地生成对应多个result_i, 后面步骤的函数使用之前的result作为参数输入,以###结尾",
|
3 |
+
"Function Library:": "",
|
4 |
+
"Instruction: 获取上市公司贵州茅台和其主营业务介绍信息": "###Function Call: step1={\n \"arg1\": [\"贵州茅台\"],\n \"function1\": \"get_company_info\",\n \"output1\": \"result1\",\n \"description1\": \"贵州茅台的信息\"\n}###",
|
5 |
+
"Instruction: 获取上市公司五粮液相关信息": "###Function Call: step1={\n \"arg1\": [\"五粮液\"],\n \"function1\": \"get_company_info\",\n \"output1\": \"result1\",\n \"description1\": \"五粮液的信息\"\n}###",
|
6 |
+
"Instruction: 依次获取中国平安和贵州茅台从2011年5月23日到2021年5月23日期间的净资产回报率roe的时序数据": "###Function Call: step1={\n \"arg1\": [\"中国平安\"],\n \"function1\": \"get_stock_code\",\n \"output1\": \"result1\",\n \"description1\": \"中国平安的股票代码\",\n \"arg2\": [\"贵州茅台\"],\n \"function2\": \"get_stock_code\",\n \"output2\": \"result2\",\n \"description2\": \"贵州茅台的股票代码\"\n},step2={\n \"arg1\": [\"result1\",\"20110523\",\"20210523\",\"roe\"],\n \"function1\": \"get_Financial_data_from_time_range\",\n \"output1\": \"result3\",\n \"description1\": \"中国平安的十年roe时序数据\",\n \"arg2\": [\"result2\",\"20110523\",\"20210523\",\"roe\"],\n \"function2\": \"get_Financial_data_from_time_range\",\n \"output2\": \"result4\",\n \"description2\": \"贵州茅台这十年净资产收益率的时序数据\"\n}###",
|
7 |
+
"Instruction: 循环获取上证50所有成分股的一季报20230331日的净资产回报率的截面数据": "###Function Call: step1={\n \"arg1\": [\"上证50\", \"20230331\", \"20230331\"],\n \"function1\": \"get_index_constituent\",\n \"output1\": \"result1\",\n \"description1\": \"上证50成分股信息\"\n},step2={\n \"arg1\": [\"result1\", \"stock_name\", -1],\n \"function1\": \"select_value_by_column\",\n \"output1\": \"result2\",\n \"description1\": \"上证50成分股的股票名称列表\"\n},step3={\n \"arg1\": [\"result2\",\"get_Financial_data_from_time_range\", \"20230331\", \"20230331\", \"roe\"],\n \"function1\": \"loop_rank\",\n \"output1\": \"result3\",\n \"description1\": \"上证50的每只成分股的一季报20230331里roe的截面数据\"\n}###",
|
8 |
+
"Instruction: 循环获取创业板指所有成分股的20220630日半年报的归母净利润增速同比截面数据": "###Function Call: step1={\n \"arg1\": [\"创业板指\", \"20220630\", \"20220630\"],\n \"function1\": \"get_index_constituent\",\n \"output1\": \"result1\",\n \"description1\": \"创业板指的成分股信息\"\n},step2={\n \"arg1\": [\"result1\", \"stock_name\", -1],\n \"function1\": \"select_value_by_column\",\n \"output1\": \"result2\",\n \"description1\": \"创业板指成分股的股票名称列表\"\n},step3={\n \"arg1\": [\"result2\",\"get_Financial_data_from_time_range\", \"20220630\", \"20220630\", \"netprofit_yoy\"],\n \"function1\": \"loop_rank\",\n \"output1\": \"result3\",\n \"description1\": \"创业板指的每只成分股的半年报20220630归母净利润同比增速的截面数据\"\n}###"
|
9 |
+
}
|
10 |
+
|
11 |
+
|
prompt_lib/prompt_fund.json
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"Global prompt:": "请根据给定的指令,利用给定的函数一步一步地完成任务,每一步你只能从以下函数库中选择一个或者多个无依赖关系的函数,并且为函数生成对应的参数。以json的格式生成每一步如下:step1={\n \"arg1\": [arg1,arg2...],\n \"function1\": \"%s\",\n \"output1\": \"%s\",\n \"description1\": \"%s\"\n},step2={\n \"arg1\": [arg1,arg2..],\n \"function1\": \"%s\",\n \"output1\": \"%s\",\n \"description1\": \"%s\"\n}###, 请注意每步的选择的多个函数之间必须无依赖关系, 并行地生成对应多个result_i, 后面步骤的函数使用之前的result作为参数输入,以###结尾.请区分到底是查询基金经理还是查询某只基金. 查询基金经理只需要第一步query_fund_Manager即可.",
|
3 |
+
"Function Library:": "",
|
4 |
+
"Instruction: 查询基金,获取20200111到20230111期间华商优势精选基金的累计收益率的时序数据": "###Function Call: step1={\n \"arg1\": [\"华商优势精选\", \"\"],\n \"function1\": \"query_fund_name_or_code\",\n \"output1\": \"result1\",\n \"description1\": \"基金代码\"\n},step2={\n \"arg1\": [\"result1\", \"20200111\", \"20230111\"],\n \"function1\": \"query_fund_data\",\n \"output1\": \"result2\",\n \"description1\": \"基金相关的时序数据\"\n},step3={\n \"arg1\": [\"result2\", \"Cumulative_Earnings_Rate\"],\n \"function1\": \"calculate_stock_index\",\n \"output1\": \"result3\",\n \"description1\": \"基金的每日累计收益率的时序数据\"\n}###",
|
5 |
+
"Instruction: 查询基金,依次获取20200111到20230111期间华商优势行业和易方达蓝筹精选的累计收益率的时序数据,直接查询基金": "###Function Call: step1={\n \"arg1\": [\"华商优势行业\", \"\"],\n \"function1\": \"query_fund_name_or_code\",\n \"output1\": \"result1\",\n \"description1\": \"华商优势行业的基金代码\",\n \"arg2\": [\"易方达蓝筹精选\", \"\"],\n \"function2\": \"query_fund_name_or_code\",\n \"output2\": \"result2\",\n \"description2\": \"易方达蓝筹精选的基金代码\"\n},step2={\n \"arg1\": [\"result1\", \"20200111\", \"20230111\"],\n \"function1\": \"query_fund_data\",\n \"output1\": \"result3\",\n \"description1\": \"华商优势行业相关的时序数据\",\n \"arg2\": [\"result2\", \"20200111\", \"20230111\"],\n \"function2\": \"query_fund_data\",\n \"output2\": \"result4\",\n \"description2\": \"易方达蓝筹精选相关的时序数据\"\n},step3={\n \"arg1\": [\"result3\", \"Cumulative_Earnings_Rate\"],\n \"function1\": \"calculate_stock_index\",\n \"output1\": \"result5\",\n \"description1\": \"华商优势行业的每日累计收益率的时序数据\",\n \"arg2\": [\"result4\", \"Cumulative_Earnings_Rate\"],\n \"function2\": \"calculate_stock_index\",\n \"output2\": \"result6\",\n \"description2\": \"易方达蓝筹精选的每日累计收益率的时序数据\"\n}",
|
6 |
+
"Instruction: 查询基金,依次获取20200111到20230111期间华商优势行业和易方达蓝筹精选的净值时序数据": "###Function Call: step1={\n \"arg1\": [\"华商优势行业\", \"\"],\n \"function1\": \"query_fund_name_or_code\",\n \"output1\": \"result1\",\n \"description1\": \"华商优势行业的基金代码\",\n \"arg2\": [\"易方达蓝筹精选\", \"\"],\n \"function2\": \"query_fund_name_or_code\",\n \"output2\": \"result2\",\n \"description2\": \"易方达蓝筹精选的基金代码\"\n},step2={\n \"arg1\": [\"result1\", \"20200111\", \"20230111\"],\n \"function1\": \"query_fund_data\",\n \"output1\": \"result3\",\n \"description1\": \"华商优势行业相关的时序数据\",\n \"arg2\": [\"result2\", \"20200111\", \"20230111\"],\n \"function2\": \"query_fund_data\",\n \"output2\": \"result4\",\n \"description2\": \"易方达蓝筹精选相关的时序数据\"\n},step3={\n \"arg1\": [\"result3\", \"adj_nav\"],\n \"function1\": \"calculate_stock_index\",\n \"output1\": \"result5\",\n \"description1\": \"华商优势行业的每日净值的时序数据\",\n \"arg2\": [\"result4\", \"adj_nav\"],\n \"function2\": \"calculate_stock_index\",\n \"output2\": \"result6\",\n \"description2\": \"易方达蓝筹精选的每日净值的时序数据\"\n}",
|
7 |
+
"Instruction: 查询基金净值,获取20200111到20230111期间华商优势行业的基金净值时序数据": "###Function Call: step1={\n \"arg1\": [\"华商优势行业\", \"\"],\n \"function1\": \"query_fund_name_or_code\",\n \"output1\": \"result1\",\n \"description1\": \"基金代码\"\n},step2={\n \"arg1\": [\"result1\", \"20200111\", \"20230111\"],\n \"function1\": \"query_fund_data\",\n \"output1\": \"result2\",\n \"description1\": \"基金相关的时序数据\"\n},step3={\n \"arg1\": [\"result2\", \"adj_nav\"],\n \"function1\": \"calculate_stock_index\",\n \"output1\": \"result3\",\n \"description1\": \"基金的每日净值的时序数据\"\n}###",
|
8 |
+
"Instruction: 查询基金净值,获取何帅管理的交银持续成长基金从2020年5月30日到2023年5月30日的净值时序数据": "###Function Call: step1={\n \"arg1\": [\"交银持续成长\", \"\"],\n \"function1\": \"query_fund_name_or_code\",\n \"output1\": \"result1\",\n \"description1\": \"交银持续成长的基金代码\"\n},step2={\n \"arg1\": [\"result1\", \"20200530\", \"20230530\"],\n \"function1\": \"query_fund_data\",\n \"output1\": \"result2\",\n \"description1\": \"交银持续成长的时序数据\"\n},step3={\n \"arg1\": [\"result2\", \"adj_nav\"],\n \"function1\": \"calculate_stock_index\",\n \"output1\": \"result3\",\n \"description1\": \"交银持续成长的每日净值的时序数据\"\n}###",
|
9 |
+
"Instruction: 查询基金经理,获取截止至20230123赵晓东所管理的所有基金名字": "###Function Call: step1={\n \"arg1\": [\"赵晓东\"],\n \"function1\": \"query_fund_Manager\",\n \"output1\": \"result1\",\n \"description1\": \"赵晓东管理的所有基金名字和代码等信息\"\n},step2={\n \"arg1\": [\"result1\",\"fund_name\",-1],\n \"function1\": \"select_value_by_column\",\n \"output1\": \"result1\",\n \"description1\": \"提取赵晓东管理的所有基金名字\"\n}###",
|
10 |
+
"Instruction: 查询多只基金,循环获取20230101到20230503期间周海栋名下所有基金的收益率截面数据": "###Function Call: step1={\n \"arg1\": [\"周海栋\"],\n \"function1\": \"query_fund_Manager\",\n \"output1\": \"result1\",\n \"description1\": \"周海栋管理的所有基金名和代码列表\"\n},step2={\n \"arg1\": [\"result1\",\"fund_code\",-1],\n \"function1\": \"select_value_by_column\",\n \"output1\": \"result2\",\n \"description1\": \"提取所有基金的代码列表\"\n},step3={\n \"arg1\": [\"result2\",\"calculate_earning_between_two_time\",\"20230101\",\"20230503\",\"adj_nav\"],\n \"function1\": \"loop_rank\",\n \"output1\": \"result3\",\n \"description1\": \"周海栋名下每只基金从20230101到20230503的收益率截面数据\"\n}###",
|
11 |
+
"Instruction: 查询多只基金,循环获取2022年01月1日到20230503期间易方达张坤管理的每只基金的收益率截面数据": "###Function Call: step1={\n \"arg1\": [\"张坤\"],\n \"function1\": \"query_fund_Manager\",\n \"output1\": \"result1\",\n \"description1\": \"张坤管理的所有基金数据名字和代码\"\n},step2={\n \"arg1\": [\"result1\",\"fund_code\",-1],\n \"function1\": \"select_value_by_column\",\n \"output1\": \"result2\",\n \"description1\": \"张坤名下的所有基金的代码列表\"\n},step3={\n \"arg1\": [\"result2\",\"calculate_earning_between_two_time\",\"20220101\",\"20230503\",\"adj_nav\"],\n \"function1\": \"loop_rank\",\n \"output1\": \"result3\",\n \"description1\": \"张坤名下每只基金从20220101到20230503的收益率截面数据\"\n}###"
|
12 |
+
}
|
13 |
+
|
14 |
+
|
prompt_lib/prompt_intent_detection.json
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"Task prompt": "根据今天的日期填入Instruction,改写后的New Instruction必须保持语义完全一致并含有具体对应的时间和具体的指标,请务必将\"今天\",\"本季度\",\"今年\",\"近一年\"等描述词替换成具体时间例如:2023年05月10日.对于上市公司财务报表,0331为一季报,0630为半年报,0930为三季报,1231为年报.财报指标包括:# current_ratio\t流动比率 # quick_ratio\t速动比率 # netprofit_margin\t销售净利率 # grossprofit_margin\t销售毛利率 # roe\t净资产收益率 # roe_dt\t净资产收益率(扣除非经常损益)# roa\t总资产报酬率 # debt_to_assets 资产负债率 # roa_yearly\t年化总资产净利率 # q_dtprofit\t扣除非经常损益后的单季度净利润 # q_eps\t每股收益(单季度) # q_netprofit_margin\t销售净利率(单季度) # q_gsprofit_margin\t销售毛利率(单季度) # basic_eps_yoy 基本每股收益同比增长率(%) # netprofit_yoy\t归属母公司股东的净利润同比增长率(%) # q_netprofit_yoy\t归属母公司股东的净利润同比增长率(%)(单季度) # q_netprofit_qoq\t归属母公司股东的净利润环比增长率(%)(单季度) # equity_yoy\t净资产同比增长率\n. 每日技术指标包括:macd, kdj, rsi, boll, cci, turnover_rate, turnover_rate_f, volume_ratio, pe_ttm, pb, ps_ttm, dv_ttm, total_share, float_share, free_share, total_mv, circ_mv.请注意如果Instruction中没有明确要求计算中位数和均值年化收益率等指标,则无需计算均值和中位数或其他指标,只需要画图展示即可. 此外,除非明确开盘价/最高价等,其他情况下股价就是指收盘价.如果Instruction中提及一段时间里的收益率,优先考虑展示累计收益率的走势图.如果instruction中提取某个指数所有成分股的收益率或其他财务指标对比,则展示所有成分股收益率或者对应指标的分布.优先用画图来比较不同股票之间的差异.预测任务的起始时间都从20000101算,且只打印表格",
|
3 |
+
"Instruction:今天的日期是2019-03-13,请帮我绘制贵州茅台的2018年1月23日到今天的股价走势图": "###New Instruction: 请帮我绘制贵州茅台的2018年1月23日到2019年3月13日的收盘价的走势图",
|
4 |
+
"Instruction:今天的日期是2019-03-13,我想看中证500和科创50今年的指数走势": "###New Instruction: 请帮我绘制中证500和科创50指数收盘价格从2019年1月1日到20190313的走势图",
|
5 |
+
"Instruction:今天的日期是2023-05-13,画一下五粮液和泸州老窖从2019年年初到2022年年中的收益率走势": "###New Instruction: 画一下五粮液和泸州老窖从2019年1月1日到2022年6月30日的期间的每天累积收益率走势",
|
6 |
+
"Instruction:今天的日期是2023-05-13,画一下格力电器从2019年年初到2022年年中的收益率": "###New Instruction: 画一下格力电器从2019年1月1日到2022年6月30日的期间的每天累积收益率走势",
|
7 |
+
"Instruction:今天的日期是2023-05-13,比较洋河集团和泸州老窖从2019年年初到2022年年中的收益率是怎么样的": "###New Instruction: 绘制洋河集团和泸州老窖从2019年1月1日到2022年6月30日的期间的每天累积收益率走势来比较两者这段时间收益率差异",
|
8 |
+
"Instruction:今天的日期是2023-05-10,申万二级行业城商行Ⅱ里所有成分股近三个月的收益率": "###New Instruction: 申万二级行业城商行Ⅱ里所有成分股从2023年2月10日到2023年5月10日的收益率分布情况",
|
9 |
+
"Instruction:今天的日期是2023-05-10,我想看看中国近十年gdp的走势": "###New Instruction: 我想看看中国从2013年5月10日到2023年5月10日的gdp的走势",
|
10 |
+
"Instruction:今天的日期是2023-05-10,易方达旗下的那个名叫张坤的基金经理他管理的基金有几只": "###New Instruction: 易方达旗下的那个名叫张坤的基金经理在2023年5月10日管理的基金有几只",
|
11 |
+
"Instruction:今天的日期是2023-05-10,今年一季度贵州茅台的净资产收益率是多少": "###New Instruction: 2023年1月1日到2023年3月31日贵州茅台的净资产收益率是多少",
|
12 |
+
"Instruction:今天的日期是2022-05-10,比较下沪深300,创业板指,中证1000指数今年的收益率": "###New Instruction: 绘制沪深300,创业板指,中证1000指数从2022年1月1日到2022年5月10日的收益率走势图来比较三者差异###",
|
13 |
+
"Instruction:今天的日期是2019-10-15,上证50的成分股近一年的涨跌幅": "###New Instruction: 展示上证50的每只成分股从2018年10月15日到2019年10月15日期间各自的收益率分布情况",
|
14 |
+
"Instruction:今天的日期是2021-04-01,今年北上资金的净流入情况": "###New Instruction: 2021年1月1日到2021年4月1日北上资金的净流入情况",
|
15 |
+
"Instruction:今天的日期是2021-04-01,我想看看宁德时代从2019年1月之后半年的股价走势": "###New Instruction: 我想看看宁德时代从2019年1月01到2019年6月30日的每日收盘价股走势图",
|
16 |
+
"Instruction:今天的日期��2022-07-11,我想看看现在的新闻或者最新的消息": "###New Instruction: 展示财经网站上当前的最新的财经新闻和市场动态",
|
17 |
+
"Instruction:今天的日期是2022-07-11,最新的新闻和财经消息有什么": "###New Instruction: 展示财经网站上当前的最新的财经新闻和市场动态",
|
18 |
+
"Instruction:今天的日期是2022-07-11,中国过去十年的cpi走势是什么": "###New Instruction: 展示下2012年7月11日到2022年7月11日的中国每个月的月度cpi同比变化",
|
19 |
+
"Instruction:今天的日期是2022-07-11,过去十年中国(我们国家)的GDP": "###New Instruction: 展示下从2012年7月11日到2022年7月11日的中国每个季度的季度GDP增速同比",
|
20 |
+
"Instruction:今天的日期是2022-07-11,过去五年中国的货币供应量走势,并且打印保存": "###New Instruction: 展示下从2017年7月11日到2022年7月11日的每个月的货币供应M2的增速同比,并将数据打印保存表格",
|
21 |
+
"Instruction:今天的日期是2023-01-23,我想看看周海栋管理的华商优势行业的近三年来的价格走势(净值)": "###New Instruction: 查询基金净值,即展示下从2020年1月23日到2023年1月23日期间华商优势行业的基金净值曲线",
|
22 |
+
"Instruction:今天的日期是2023-01-23,易方达的张坤管理了几个基金": "###New Instruction: 查询基金经理,展示下2023年01月23日易方达公司的基金经理张坤最新管理的基金名称",
|
23 |
+
"Instruction:今天的日期是2023-05-03,基金经理周海栋名下的所有基金今年的收益率情况": "###New Instruction: 查询多只基金收益率,查询基金经理,展示周海栋旗下的管理的每只基金从2023年1月1日到2023年5月3日期间的收益率截面数据",
|
24 |
+
"Instruction:今天的日期是2023-05-03,基金经理姜诚名下的所有基金": "###New Instruction: 查询基金经理,展示姜诚管理的基金名称",
|
25 |
+
"Instruction:今天的日期是2023-05-03,介绍下贵州茅台,这公司是干什么的,主营业务是什么": "###New Instruction: 简要文字介绍下上市公司贵州茅台和其主营业务",
|
26 |
+
"Instruction:今天的日期是2021-05-23,我想比较下中国平安和贵州茅台近十年的净资产回报率": "###New Instruction: 绘制中国平安和贵州茅台从2011年5月23日到2021年5月23日期间的财务指标净资产回报率(roe)走势图来比较两者差异",
|
27 |
+
"Instruction:今天的日期是2021-05-23,我想看看下成都银行,杭州银行和成都银行近三年的净利润增速": "###New Instruction: 展示成都银行、杭州银行和宁波银行从2020年5月16日到2023年5月16日的财务指标每年的归母净利润增幅(netprofit_yoy)",
|
28 |
+
"Instruction:今天的日期是2021-05-23,今年一季度上证50是成分股的归母净利润同比增速分别是": "###New Instruction: 展示上证50的所有成分股的一季报(2023年03月31日)的财务指标归母净利润增速同比(netprofit_yoy)的分布情况",
|
29 |
+
"Instruction:今天的日期是2021-05-23,去年上证50所有成分股年报里净资产回报率的中位数和均值分别是": "###New Instruction: 展示并计算上证50的所有成分股的年报(2022年12月31日)的财务指标净资产回报率(roe)的中位数和均值",
|
30 |
+
"Instruction:今天的日期是2021-05-23,看一下近三年宁德时代和贵州茅台的估值变化": "###New Instruction: 展示宁德时代和贵州茅台从2018年5月23日到2021年5月23日的技术指标市盈率估值(pe-ttm)走势",
|
31 |
+
"Instruction:今天的日期是2021-05-23,预测未来12个季度的中国季度GDP增速数据": "###New Instruction: 展示基于20000101日到20210523日(今日)中国每个季度的GDP增速的数据预测未来12个季度的数据并打印表格"
|
32 |
+
}
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
|
prompt_lib/prompt_stock.json
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"Global prompt:": "请利用给定的函数一步一步地完成Instruction,每一步你只能从以下函数库中选择一个或者多个无依赖关系的函数,并且为函数生成对应的参数,参数格式要严格按照函数说明,并行地生成对应多个result_i, 后面步骤的函数使用之前的result作为参数输入,以###结尾",
|
3 |
+
"Function Library:": "",
|
4 |
+
"Instruction: 获取贵州茅台的2018年1月23日到2019年3月13日的每日收盘价的时序数据": "###Function Call: step1={\n \"arg1\": [\"贵州茅台\",\"20180123\",\"20190313\",\"daily\"],\n \"function1\": \"get_stock_prices_data\",\n \"output1\": \"result1\",\n \"description1\": \"贵州茅台每日股价(开盘,收盘,最高,最低等相关价格)的时序数据\"\n},step2={\n \"arg1\": [\"result1\",\"close\"],\n \"function1\": \"calculate_stock_index\",\n \"output1\": \"result2\",\n \"description1\": \"贵州茅台的收盘价时序数据\"\n}###",
|
5 |
+
"Instruction: 依次获取五粮液和泸州老窖从2019年年初到2022年年中的累积收益率时序数据": "###Function Call: step1={\n \"arg1\": [\"五粮液\",\"20190101\",\"20220630\",\"daily\"],\n \"function1\": \"get_stock_prices_data\",\n \"output1\": \"result1\",\n \"description1\": \"五粮液相关价格的时序数据\",\n \"arg2\": [\"泸州老窖\",\"20190101\",\"20220630\",\"daily\"],\n \"function2\": \"get_stock_prices_data\",\n \"output2\": \"result2\",\n \"description2\": \"泸州老窖每日相关价格的时序数据\"\n},step2={\n \"arg1\": [\"result1\",\"Cumulative_Earnings_Rate\"],\n \"function1\": \"calculate_stock_index\",\n \"output1\": \"result3\",\n \"description1\": \"五粮液的累计收益率时序数据\",\n \"arg2\": [\"result2\",\"Cumulative_Earnings_Rate\"],\n \"function2\": \"calculate_stock_index\",\n \"output2\": \"result4\",\n \"description2\": \"泸州老窖的累计收益率的时序数据\"\n}\n###",
|
6 |
+
"Instruction: 依次获取2022年05月10日到2023年5月10的成都银行K线和macd指标时序数据": "###Function Call: \nstep1={\n \"arg1\": [\"成都银行\",\"20220510\",\"20230510\",\"daily\"],\n \"function1\": \"get_stock_prices_data\",\n \"output1\": \"result1\",\n \"description1\": \"成都银行每日股价(开盘,收盘,最高,最低等相关价格)时序数据\",\n \"arg2\": [\"成都银行\",\"20220510\",\"20230510\"],\n \"function2\": \"get_stock_technical_data\",\n \"output2\": \"result2\",\n \"description2\": \"成都银行技术指标的时序数据\"\n},step2={\n \"arg1\": [\"result1\",\"candle_K\"],\n \"function1\": \"calculate_stock_index\",\n \"output1\": \"result3\",\n \"description1\": \"成都银行k线图相关的时序数据\",\n \"arg2\": [\"result2\",\"macd\"],\n \"function2\": \"calculate_stock_index\",\n \"output2\": \"result4\",\n \"description2\": \"成都银行macd指标的时序数据\"\n},step3={\n \"arg1\": [\"result3\",\"result4\"],\n \"function1\": \"merge_indicator_for_same_stock\",\n \"output1\": \"result5\",\n \"description1\": \"成都银行k线指标和macd指标合并后的时序数据\"\n}###",
|
7 |
+
"Instruction: 循环获取上证50指数的每只成分股里从2023年1月1日到2023年5月10日的收益率截面数据": "###Function Call: step1={\n \"arg1\": [\"上证50\",\"20230101\",\"20230510\"],\n \"function1\": \"get_index_constituent\",\n \"output1\": \"result1\",\n \"description1\": \"上证50的所有成分股列表\"\n},step2={\n \"arg1\": [\"result1\",\"stock_name\",-1],\n \"function1\": \"select_value_by_column\",\n \"output1\": \"result2\",\n \"description1\": \"所有上证50成分股的股票名称\"\n},step3={\n \"arg1\": [\"result2\",\"calculate_earning_between_two_time\",\"20230101\",\"20230510\"],\n \"function1\": \"loop_rank\",\n \"output1\": \"result3\",\n \"description1\": \"每只成分股的收益率\"\n}###请注意get_index_constituent是查询指数所有的成分股信息,get_index_data是获取指数本身的价格信息",
|
8 |
+
"Instruction: 循环获取申万二级行业城商行Ⅱ的每只成分股里从2023年1月1日到2023年5月10日的收益率截面数据": "###Function Call: step1={\n \"arg1\": [\"申万二级行业城商行Ⅱ\",\"20230101\",\"20230510\"],\n \"function1\": \"get_index_constituent\",\n \"output1\": \"result1\",\n \"description1\": \"城商行的所有成分股列表\"\n},step2={\n \"arg1\": [\"result1\",\"stock_name\",-1],\n \"function1\": \"select_value_by_column\",\n \"output1\": \"result2\",\n \"description1\": \"所有成分股的股票名称\"\n},step3={\n \"arg1\": [\"result2\",\"calculate_earning_between_two_time\",\"20230101\",\"20230510\"],\n \"function1\": \"loop_rank\",\n \"output1\": \"result3\",\n \"description1\": \"每只成分股的收益率\"\n}###",
|
9 |
+
"Instruction: 依次获取沪深300,创业板指这两个指数在20230101到20230510日的每日累计收益率时序数据": "###Function Call: step1={\n \"arg1\": [\"沪深300\",\"20230101\",\"20230510\",\"daily\"],\n \"function1\": \"get_index_data\",\n \"output1\": \"result1\",\n \"description1\": \"沪深300指数的日频指数价格时序数据\",\n \"arg2\": [\"创业板指\",\"20230101\",\"20230510\",\"daily\"],\n \"function2\": \"get_index_data\",\n \"output2\": \"result2\",\n \"description2\": \"创业板指数的指数价格时序数据\"\n},step2={\n \"arg1\": [\"result1\",\"Cumulative_Earnings_Rate\"],\n \"function1\": \"calculate_stock_index\",\n \"output1\": \"result4\",\n \"description1\": \"沪深300的累计收益率时序数据\",\n \"arg2\": [\"result2\",\"Cumulative_Earnings_Rate\"],\n \"function2\": \"calculate_stock_index\",\n \"output2\": \"result5\",\n \"description2\": \"创业板指的累计收益率时序数据\"\n}###",
|
10 |
+
"Instruction: 获取中证500在20230101到20230510日的指数收盘价格时序数据": "###Function Call: step1={\n \"arg1\": [\"中证500\",\"20230101\",\"20230510\",\"daily\"],\n \"function1\": \"get_index_data\",\n \"output1\": \"result1\",\n \"description1\": \"中证500指数的指数价格时序数据\"\n},step2={\n \"arg1\": [\"result1\",\"close\"],\n \"function1\": \"calculate_stock_index\",\n \"output1\": \"result3\",\n \"description1\": \"中证500的收盘价时序数据\"\n}###这个指令不需要调用loop_rank和merge_indicator_for_same_stock这些不相干的函数,指数价格一般都是收盘价",
|
11 |
+
"Instruction: 依次获取2018年01月01日到2023年5月10日宁德时代和天齐锂业的技术指标市盈率(pe-ttm)时序数据": "###Function Call: step1={\n \"arg1\": [\"宁德时代\",\"20180101\",\"20230510\"],\n \"function1\": \"get_stock_technical_data\",\n \"output1\": \"result1\",\n \"description1\": \"宁德时代的技术指标时序数据\",\n \"arg2\": [\"天齐锂业\",\"20180101\",\"20230510\"],\n \"function2\": \"get_stock_technical_data\",\n \"output2\": \"result2\",\n \"description2\": \"天齐锂业的技术指标时序数据\"\n},step2={\n \"arg1\": [\"result1\",\"pe_ttm\"],\n \"function1\": \"calculate_stock_index\",\n \"output1\": \"result3\",\n \"description1\": \"宁德时代的pe估值时序数据\",\n \"arg2\": [\"result2\",\"pe_ttm\"],\n \"function2\": \"calculate_stock_index\",\n \"output2\": \"result4\",\n \"description2\": \"天齐锂业的pe估值时序数据\"\n}###",
|
12 |
+
"Instruction: 依次获取中国平安和贵州茅台从2011年5月23日到2021年5月23日期间的净资产回报率(roe)的时序数据": "###Function Call: step1={\n \"arg1\": [\"中国平安\",\"20110523\",\"20210523\",\"roe\"],\n \"function1\": \"get_Financial_data_from_time_range\",\n \"output1\": \"result1\",\n \"description1\": \"中国平安的十年roe时序数据\",\n \"arg2\": [\"贵州茅台\",\"20110523\",\"20210523\",\"roe\"],\n \"function2\": \"get_Financial_data_from_time_range\",\n \"output2\": \"result2\",\n \"description2\": \"贵州茅台这十年净资产收益率的时序数据\"\n}###",
|
13 |
+
"Instruction: 循环获取创业板指所有成分股的20220630日半年报的归母净利润增速同比(netprofit_yoy)截面数据": "###Function Call: step1={\n \"arg1\": [\"创业板指\", \"20220601\", \"20220630\"],\n \"function1\": \"get_index_constituent\",\n \"output1\": \"result1\",\n \"description1\": \"当月的创业板指的所有成分股信息\"\n},step2={\n \"arg1\": [\"result1\", \"stock_name\", -1],\n \"function1\": \"select_value_by_column\",\n \"output1\": \"result2\",\n \"description1\": \"创业板指成分股的股票名称\"\n},step3={\n \"arg1\": [\"result2\",\"get_Financial_data_from_time_range\", \"20220630\", \"20220630\", \"netprofit_yoy\"],\n \"function1\": \"loop_rank\",\n \"output1\": \"result3\",\n \"description1\": \"创业板指的每只成分股的半年报20220630归母净利润同比增速的截面数据\"\n}"
|
14 |
+
}
|
15 |
+
|
16 |
+
|
prompt_lib/prompt_task.json
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"Task prompt": "请根据给定的指令,选择最合适的task并生成其task_instruction,格式是task1={'task_name':'task_instruction'},可选的task有四类,包括[fund_task:用于提取和处理关于所有公募基金任务, stock_task: 用于提取和处理关于所有股票价格,指数信息,公司的财务数据等任务, economic_task: 用于提取和处理关于所有中国宏观经济和货币政策等任务以及查询公司和北向资金, visualization_task: 用于一张或者多张绘制K线图,走势图或者输出统计结果]. 生成task_instruction时涉及多个指标可以采用\"依次获取\",\"循环获取\"等词. 时序数据一般绘制折线图,截面数据通常绘制柱状图,用以下格式输出task1={%s:%s},task2={%s:%s}. 对于预测任务只打印表格",
|
3 |
+
"Instruction:请帮我绘制贵州茅台的2018年1月23日到2019年03月13日的收盘价的走势图": " ###Plan: task1={\"stock_task\":\"获取贵州茅台的股价20180123到20190313的收盘价的时序数据\"},task2={\"visualization_task\":\"绘制贵州茅台的股价20180123到20190313的股价折线图\"}",
|
4 |
+
"Instruction:画一下五粮液和泸州老窖从2019年01月1日到2022年06月30日的累积收益率走势来比较两只股票这段时间收益率差异": "###Plan: task1={\"stock_task\":\"依次获取五粮液和泸州老窖从20190101到20220630的每日的累计收益率的时序数据\"}, task2={\"visualization_task\":\"在一张图里绘制五粮液和泸州老窖从20190101到20220630的累计收益率折线图\"}",
|
5 |
+
"Instruction:申万二级行业城商行Ⅱ里所有成分股近三个月的收益率分布情况": "###Plan: task1={\"stock_task\":\"循环获取城商行Ⅱ(申万二级行业)的所有成分股在从20230210日至20230510期间的各自的收益率截面数据\"}, task2={\"visualization_task\":\"在一张柱状图里绘制城商行Ⅱ(申万二级行业)的每只成分股在20230210日至20230510期间的收益率\"}",
|
6 |
+
"Instruction:我想看看中国2013年5月到2023年05月gdp的走势": "###Plan: task1={\"economic_task\":\"获取中国2013年5月到2023年5月的十年GDP时序数据\"},task2={\"visualization_task\":\"绘制2013年5月到2023年5月整十年的gdp走势图\"}",
|
7 |
+
"Instruction:2023年5月10日,易方达旗下的那个名叫张坤的基金经理他管理的基金有几只": "###Plan: task1={\"fund_task\":\"获取今日20230510张坤管理的最新的基金列表数据\"},task2={\"visualization_task\":\"统计并输出基金数量\"}",
|
8 |
+
"Instruction:2023年01月01日到2023年03月30日贵州茅台的净资产收益率是多少": "###Plan: task1={\"finanical_task\":\"获取贵州茅台的2023年1季度的净资产收益率数据\"},task2={\"visualization_task\":\"统计并输出贵州茅台2023一季度的净资产收益率\"}",
|
9 |
+
"Instruction:绘图展示下沪深300,创业板指,中证1000指数从2023年01月01日到2023年05月10日期间的收益率走势图": "###Plan: task1={\"stock_task\":\"依次获取沪深300,创业板指,中证1000指数三个指数2023年1月1日到2023年5月10日的每日累计收益率的时序数据\"},task2={\"visualization_task\":\"在一张图里分别绘制沪深300,创业板指,中证1000指数三个指数从2023年1月1日到2023年5月10日的每日的累计收益率折线图\"}",
|
10 |
+
"Instruction:展示上证50的每只成分股从2022年05月15日到2023年05月15日期间的各自的收益率分布": "###Plan: task1={\"stock_task\":\"循环获取上证50的每只成分股的2022年05月15日到2023年5月15日的各自收益率的截面数据\"},task2={\"visualization_task\":\"绘制上证50指数的所有成分股里从2022年05月15日到2023年5月15日的收益率的柱状图\"}",
|
11 |
+
"Instruction:从2023年01月1日到2023年05月15日北上资金的净流入情况": "###Plan: task1={\"economic_task\":\"依次获取20230101到20230515这段时间北向资金每日净流入和每日累计流入时序数据\"},task2={\"visualization_task\":\"在同一张图里绘制北向资金20220101到20230515的每日净流入柱状图和每日累计流入的折线图\"}",
|
12 |
+
"Instruction:我想看看2022年05月10日到2023年5月10的天坛生物的K线图和macd指标": "###Plan: task1={\"stock_task\":\"依次获取20220510到20230510这段时间天坛生物K线和MACD指标的时序数据\"},task2={\"visualization_task\":\"同时绘制天坛生物从2022年05月10日到2023年05月10日k线图和MACD指标图\"}",
|
13 |
+
"Instruction:2020年01月10日到2020年2月10期间杭州银行的K线图和cci指标": "###Plan: task1={\"stock_task\":\"依次获取20200110到20200210期间杭州银行的K线和cci指标的时序数据\"},task2={\"visualization_task\":\"同时绘制杭州银行从2020年01月10日到2020年02月10日k线图和MACD指标图\"}",
|
14 |
+
"Instruction:滚动展示财经网站上当前的最新的财经新闻和市场动态": "###Plan: task1={\"economic_task\":\"爬取新浪财经当前最新的财经消息和市场动态新闻\"},task2={\"visualization_task\":\"滚动显示当前最新的财经��息和市场动态新闻\"}",
|
15 |
+
"Instruction:展示下2012年7月11日到2022年7月11日的中国每月的cpi同比变化": "###Plan: task1={\"economic_task\":\"获取20120711到20220711期间每月的cpi全国同比变化时序数据\"},task2={\"visualization_task\":\"绘制2012年7月至2022年7月中国消费者价格指数CPI的月度同比(全国)折线图\"}",
|
16 |
+
"Instruction:展示下从2012年7月11日到2022年7月11日的中国每季度的GDP增速同比,并将数据表格打印保存": "###Plan: task1={\"economic_task\":\"获取20120711到20220711中国每季度GDP增速同比数据\"},task2={\"visualization_task\":\"绘制2012年二季度至2022年二季度中国季度GDP同比增速折线图,打印并保存2012年二季度至2022年二季度中国季度GDP同比增速表\"}",
|
17 |
+
"Instruction:查询基金净值,展示下从2020年1月11日到2023年1月11日期间华商优势行业的基金净值曲线": "###Plan: task1={\"fund_task\":\"查询基金净值,获取20200111到20230111期间华商优势行业的基金净值时序数据\"},task2={\"visualization_task\":\"绘制从2020年01月11日到2023年01月11日这段时间华商优势行业的基金净值折线图\"}",
|
18 |
+
"Instruction:查询基金经理,展示下2023年01月23日工银瑞信公司的基金经理杜洋最新管理的基金名称": "###Plan: task1={\"fund_task\":\"查询基金经理,获取截止至20230123基金经理杜洋的所管理的所有基金名列表\"},task2={\"visualization_task\":\"打印截止至2023年01月23日基金经理杜洋的管理的所有基金名称表格\"}",
|
19 |
+
"Instruction:查询多只基金,展示周海栋旗下管理的所有基金从2023年1月1日到2023年5月3日期间的收益率截面数据": "###Plan: task1={\"fund_task\":\"查询多只基金,循环获取从2023010到20230503期间基金经理周海栋名下所有基金的收益率的截面数据\"},task2={\"visualization_task\":\"绘制柱状图展示周海栋管理的每只基金在2023年1月1日到2023年5月3日期间的收益率\"}",
|
20 |
+
"Instruction:查询多只基金收益率走势,展示华商优势行业和易方达蓝筹精选从2023年1月1日到2023年5月16日的累计收益率对比走势图": "###Plan: task1={\"fund_task\":\"查询多只基金收益率走势,依次获取从2023010到20230516期间华商优势行业和易方达蓝筹精选的每日累计收益率截面数据,直接查询基金\"},task2={\"visualization_task\":\"在同张图里用折线绘制华商优势行业和易方达蓝筹精选从2023年01月01日到2023年05月16日期间的每日累计收益率走势\"}",
|
21 |
+
"Instruction:简要文字介绍下上市公司贵州茅台和其主营业务": "###Plan: task1={\"economic_task\":\"获取上市公司贵州茅台和其主营业务介绍信息\"},task2={\"visualization_task\":\"打印贵州茅台及其相关信息\"}",
|
22 |
+
"Instruction:绘图展示中国平安和贵州茅台从2011年5月23日到2021年5月23日期间的净资产回报率(roe)": "###Plan: task1={\"stock_task\":\"依次获取中国平安和贵州茅台从2011年5月23日到2021年5月23日期间的净资产回报率(roe)的时序数据\"},task2={\"visualization_task\":\"在同张图里用折线绘制中国平安和贵州茅台从2011年5月23日到2021年5月23日期间的净资产回报率roe\"}",
|
23 |
+
"Instruction:展示成都银行、杭州银行和宁波银行从2020年5月16日到2023年5月16日的财务指标每年的归母净利润增幅": "###Plan: task1={\"stock_task\":\"依次获取成都银行,杭州银行和宁波银行从2020年5月16日到2023年5月16日期间的归母净利润同比增长率(netprofit_yoy)的时序数据\"},task2={\"visualization_task\":\"在同张图里用折线绘制成都银行,杭州银行和宁波银行从2020年5月16日到2023年5月16日期间的归母净利润同比增长率\"}",
|
24 |
+
"Instruction:展示上证50的所有成分股的一季报(2023年03月31日)的财务指标归母净利润增速同比(netprofit_yoy)的分布": "###Plan: task1={\"stock_task\":\"循环获取上证50所有成分股的一季报20230331日的归母净利润增速同比(netprofit_yoy)截面数据\"},task2={\"visualization_task\":\"在同张图里用柱状图绘制上证50每只成分股的一季报20230331归母净利润增速同比\"}",
|
25 |
+
"Instruction:展示并计算上证50的所有成分股的年报(2022年12月31日)的财务指标净资产回报率(roe)的中位数和均值": "###Plan: task1={\"stock_task\":\"循环获取上证50所有成分股的年报202201231日的净资产回报率(roe)的同比增速截面数据\"},task2={\"visualization_task\":\"在同张图里用柱状图绘制上证50每只成分股去年报20221231的净资产回报率,并计算所有股票的roe中位数和均值\"}",
|
26 |
+
"Instruction:展示宁德时代和贵州茅台从2018年5月23日到2021年5月23日的技术指标市盈率估值(pe-ttm)估值走势": "###Plan: task1={\"stock_task\":\"依次获取宁德时代和贵州茅台的从20180523到20210523的技术指标市盈率估值(pe-ttm)时序数据\"},task2={\"visualization_task\":\"在一张图里用折线图绘制宁德时代和贵州茅台从20180523到20210523的的每日pe数据\"}",
|
27 |
+
"Instruction:展示基于20000101日到20210523日(今日)中国每个季度的GDP增速的数据预测未来12个季度的数据并打印表格": "###Plan: task1={\"stock_task\":\"获取从20000101到20210523的季度GDP数据并预测此后的12个月的数据\"},task2={\"visualization_task\":\"打印未来12个季度的GDP预测数据表格\"}"
|
28 |
+
}
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
|
prompt_lib/prompt_visualization.json
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"Global prompt:": "根据Instruction和之前的结果Previous_result,利用给定的函数一步一步地完成指令,每一步你必须要从以下函数库中选择一个函数,并且生成函数参数。以json的格式生成每一步如下:step1={\n \"arg\": [arg1,arg2...],\n \"function\": \"%s\",\n \"output\": \"%s\",\n \"description\": \"%s\"\n}###, 每一步可以选择Previous_result中的input的作为参数,或者之前步骤里生成的result作为参数,以###结尾.请注意如果指令中提及多个对象绘制图像则需要多次调用相应函数",
|
3 |
+
"Function Library:": "",
|
4 |
+
"Instruction: 一张图里绘制贵州茅台和宁波银行的2018年1月23日到2019年3月13日的收盘价的折线图, Previous_result: {\"input1\":\"贵州茅台20180123到20190313的收盘价时序数据\",\"input2\":\"宁波银行20180123到20190313的收盘价时序数据\"}": "###Function Call: step1={\n \"arg\": [\"input1\", null, \"line\", \"贵州茅台和宁波银行2018年1月23日到2019年3月13日的收盘价图\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result1\",\n \"description\": \"贵州茅台股价折线图\"\n},step2={\n \"arg\": [\"input2\", \"result1\", \"line\", \"贵州茅台和宁波银行2018年1月23日到2019年3月13日的收盘价图\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result2\",\n \"description\": \"继续绘制宁波银行股价折线图\"\n}\n###",
|
5 |
+
"Instruction: 在一张图里绘制五粮液和泸州老窖从20190101到20220630的每日累计收益率的折线图, Previous_result: {\"input1\":\"五粮液的每日累计收益率的时序数据\",\"input2\":\"泸州老窖的每日累计收益率的时序数据\"}": "###Function Call: step1={\n \"arg\": [\"input1\", null, \"line\", \"五粮液和泸州老窖2019年01月1日到2022年6月30日的收益率图\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result1\",\n \"description\": \"五粮液收益率折线图\"\n},step2={\n \"arg\": [\"input2\", \"result1\", \"line\", \"五粮液和泸州老窖2019年01月1日到2022年6月30日的收益率图\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result2\",\n \"description\": \"同张图继续绘制泸州老窖收益率折线图\"\n}###",
|
6 |
+
"Instruction: 同时绘制成都银行从2022年01月1日到2023年05月10日k线图和MACD指标图, Previous_result: {\"input1\":\"成都银行k线指标和macd指标合并的时序数据\"}": "###Function Call: step1={\n \"arg\": [\"input1\", \"The K plot and MACD index for chengduyinhang from 2022/01/01 to 2023/05/10 \"],\n \"function\": \"plot_k_line\",\n \"output\": \"result1\",\n \"description\": \"绘制k线图和macd图\"\n}###",
|
7 |
+
"Instruction: 绘制上证50指数的所有成分股里从2023年1月1日到2023年5月10日的股价涨跌幅(收益率)的柱状图, Previous_result: {\"input1\":\"上证50所有成分股票的收益率截面数据\"}": "###Function Call: step1={\n \"arg\": [\"input1\", null ,\"bar\", \"上证50所有成分股在2023年1月1日到2023年5月10日期间的涨跌幅\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result1\",\n \"description\": \"绘制涨跌幅柱状图\"\n}###",
|
8 |
+
"Instruction: 在同一张图里绘制北向资金20220510到20230510的每日净流入柱状图和每日累计流入的折线图, Previous_result: {\"input1\":\"北向资金每日的累计净流入量时序数据\", \"input2\":\"北向资金每日的净流入量时序数据\"}": "###Function Call: step1={\n \"arg\": [\"input1\", null ,\"line\", \"北向资金2022年05月10日到2023年05月10日的每日净流入和每日累计流入的折线图\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result1\",\n \"description\": \"绘制累计流进入的折线图\"\n},step2={\n \"arg\": [\"input2\", \"result1\" ,\"bar\", \"北向资金2022年05月10日到2023年05月10日的每日净流入和每日累计流入的折线图\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result2\",\n \"description\": \"继续绘制每日净流入柱状图\"\n}###",
|
9 |
+
"Instruction: 在一张柱状图里绘制城商行Ⅱ每个成分股从2023年2月10日到2023年5月10日的收益率分布, Previous_result: {\"input1\":\"城商行Ⅱ的每只成分股的收益率截面数据\"}": "###Function Call: step1={\n \"arg\": [\"input1\", null ,\"bar\", \"城商行Ⅱ的成分股从2023年2月10日到2023年5月10日的收益率图\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result1\",\n \"description\": \"绘制收益率图\"\n}###",
|
10 |
+
"Instruction: 同时绘制2018年01月01日到2023年5月10日宁德时代和天齐锂业的PE估值的折线图, Previous_result: {\"input1\":\"宁德时代的pe估值时序数据\", \"input2\":\"天齐锂业的pe估值时序数据\"}": "###Function Call: step1={\n \"arg\": [\"input1\", null, \"line\", \"2018年01月01日到2023年5月10日宁德时代和天齐锂业的PE估值对比\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result1\",\n \"description\": \"绘制宁德时代pe走势\"\n},step2={\n \"arg\": [\"input2\", \"result1\" ,\"line\", \"2018年01月01日到2023年5月10日宁德时代和天齐锂业的PE估值对比\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result2\",\n \"description\": \"绘制天齐锂业pe走势\"\n}###",
|
11 |
+
"Instruction: 绘制2023年1月4日当日全市场所有股票的涨幅前10的股票的收益率的柱状图,打印并保存涨幅前10的股票收益率的表格, Previous_result: {\"input1\":\"当天涨幅前十只股票的收益率的截面数据\"}": "###Function Call: step1={\n \"arg\": [\"input1\", \"2023年1月4日当日全市场所有股票的涨幅前10的股票表\",\"true\"],\n \"function\": \"print_save_table\",\n \"output\": \"result1\",\n \"description\": \"打印并保存表格\"\n},step2={\n \"arg\": [\"input1\", null, \"bar\", \"2023年1月4日当日全市场所有股票的涨幅前10的股票收益图\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result2\",\n \"description\": \"绘制涨幅前十股票收益\"\n}###",
|
12 |
+
"Instruction: 显示最新的财经和市场动态新闻或者打印贵州茅台及其相关信息, Previous_result: {\"input1\":\"当前最新的财经新闻数据\"}": "###Function Call: step1={\n \"arg\": [\"input1\", \"今日财经新闻和市场动态\", false],\n \"function\": \"print_save_table\",\n \"output\": \"result1\",\n \"description\": \"显示新闻\"\n}###",
|
13 |
+
"Instruction: 绘制从2020年01月11日到2023年01月11日这段时间华商优势精选基金的净值折线图, Previous_result: {\"input1\":\"基金的每日净值的时序数据\"}": "###Function Call: step1={\n \"arg\": [\"input1\", null , \"line\", \"2020年01月11日到2023年01月11日期间华商优势精选基金的净值走势\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result1\",\n \"description\": \"绘制净值走势图\"\n}###",
|
14 |
+
"Instruction: 打印保存截止至2023年01月23日易方达基金经理张坤的管理的所有基金的信息表格, Previous_result: {\"input1\":\"张坤管理的所有基金表格数据\"}": "###Function Call: step1={\n \"arg\": [\"input1\", \"2023年01月23日易方达基金经理张坤管理的所有基金列表\", true],\n \"function\": \"print_save_table\",\n \"output\": \"result1\",\n \"description\": \"张坤名下基金列表\"\n}###",
|
15 |
+
"Instruction: 绘制柱状图展示周海栋管理的每只基金在2023年1月1日到2023年5月3日期间的收益率, Previous_result: {\"input1\":\"周海栋名下每只基金从20230101到20230503期间的收益率截面数据\"}": "###Function Call: step1={\n \"arg\": [\"input1\", null, \"bar\", \"2023年01月1日到2023年5月3日期间经理基金周海栋管理的每只基金收益率\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result1\",\n \"description\": \"收益率柱状图\"\n}###",
|
16 |
+
"Instruction: 在同张图里用柱状图绘制上证50每只成分股去年报20221231的净资产回报率,并计算所有股票的roe的中位数和均值, Previous_result: {\"input1\":\"上证50指数咋20221231年报中每只成分股roe截面数据\"}": "###Function Call: step1={\n \"arg\": [\"input1\", null, \"bar\", \"上证50每只成分股去年年报(20221231)的净资产回报率\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result1\",\n \"description\": \"柱状图绘制每只成分股的roe\"\n},step2={\n \"arg\": [\"input1\", \"new_feature\",\"上证50每只成分股去年年报(20221231)的净资产回报率的中位数是:\"],\n \"function\": \"output_mean_median_col\",\n \"output\": \"result2\",\n \"description\": \"上证50每只成分股去年年报20221231的ROE的中位数\"\n}###",
|
17 |
+
"Instruction: 展示五粮液和贵州茅台从2013年5月20日到2023年5月20日的净资产回报率roe,并比较它们的平均值和中位数, Previous_result: {\"input1\":\"五粮液的2013年0520到2023年0520的净资产回报率roe\",\"input2\":\"贵州茅台的2013年0520到2023年0520的净资产回报率roe\"}": "###Function Call: step1={\n \"arg\": [\"input1\", null, \"line\", \"五粮液和贵州茅台从2013年5月20日到2023年5月20日的净资产回报率roe\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result1\",\n \"description\": \"绘制五粮液的股价折线图\"\n},step2=\n{\n \"arg\": [\"input2\", \"result1\", \"line\", \"五粮液和贵州茅台从2013年5月20日到2023年5月20日的净资产回报率roe\"],\n \"function\": \"plot_stock_data\",\n \"output\": \"result2\",\n \"description\": \"绘制贵州茅台的股价折线图\"\n},step3=\n{\n \"arg\": [\"input1\", \"roe\"],\n \"function\": \"output_mean_median_col\",\n \"output\": \"result3\",\n \"description\": \"计算五粮液近十年ROE的均值和中位数\"\n},step4=\n{\n \"arg\": [\"input2\", \"roe\"],\n \"function\": \"output_mean_median_col\",\n \"output\": \"result4\",\n \"description\": \"贵州茅台近十年ROE的均值和中位数\"\n}###"
|
18 |
+
}
|
19 |
+
|
20 |
+
|
21 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas==1.4.3
|
2 |
+
matplotlib==3.5.2
|
3 |
+
matplotlib-inline==0.1.3
|
4 |
+
numpy==1.22.4
|
5 |
+
Pillow==9.1.1
|
6 |
+
tushare==1.2.89
|
7 |
+
mplfinance==0.12.9b7
|
8 |
+
typed-ast==1.5.4
|
9 |
+
typer==0.4.0
|
10 |
+
typing_extensions==4.5.0
|
11 |
+
scikit-learn==1.0
|
12 |
+
scipy==1.7.3
|
13 |
+
tiktoken==0.4.0
|
14 |
+
openai==0.27.0
|
15 |
+
requests==2.28.0
|
16 |
+
requests-oauthlib==1.3.0
|
tool.py
ADDED
@@ -0,0 +1,1928 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tushare as ts
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import pandas as pd
|
4 |
+
import os
|
5 |
+
import random
|
6 |
+
from matplotlib.ticker import MaxNLocator
|
7 |
+
import matplotlib.font_manager as fm
|
8 |
+
from prettytable import PrettyTable
|
9 |
+
from blessed import Terminal
|
10 |
+
import time
|
11 |
+
from datetime import datetime, timedelta
|
12 |
+
import numpy as np
|
13 |
+
import mplfinance as mpf
|
14 |
+
from prettytable import PrettyTable
|
15 |
+
from typing import Optional
|
16 |
+
import matplotlib.font_manager as fm
|
17 |
+
from matplotlib.lines import Line2D
|
18 |
+
from typing import Union, Any
|
19 |
+
from sklearn.linear_model import LinearRegression
|
20 |
+
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
|
21 |
+
plt.rcParams['axes.unicode_minus'] = False
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
tushare_token = os.getenv('TUSHARE_TOKEN')
|
28 |
+
pro = ts.pro_api(tushare_token)
|
29 |
+
|
30 |
+
# def last_month_end(date_str:str=''):
|
31 |
+
# date_obj = datetime.strptime(date_str, '%Y%m%d')
|
32 |
+
# current_month = date_obj.month
|
33 |
+
# current_year = date_obj.year
|
34 |
+
#
|
35 |
+
# if current_month == 1:
|
36 |
+
# last_month = 12
|
37 |
+
# last_year = current_year - 1
|
38 |
+
# else:
|
39 |
+
# last_month = current_month - 1
|
40 |
+
# last_year = current_year
|
41 |
+
#
|
42 |
+
# if date_obj.month != (date_obj + timedelta(days=1)).month:
|
43 |
+
# last_month_end_date = date_obj
|
44 |
+
# else:
|
45 |
+
# last_day_of_last_month = (date_obj.replace(day=1) - timedelta(days=1)).day
|
46 |
+
# last_month_end_date = datetime(last_year, last_month, last_day_of_last_month)
|
47 |
+
#
|
48 |
+
# return last_month_end_date.strftime('%Y%m%d')
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
def get_last_year_date(date_str: str = '') -> str:
|
53 |
+
"""
|
54 |
+
This function takes a date string in the format YYYYMMDD and returns the date string one year prior to the input date.
|
55 |
+
|
56 |
+
Args:
|
57 |
+
- date_str: string, the input date in the format YYYYMMDD
|
58 |
+
|
59 |
+
Returns:
|
60 |
+
- string, the date one year prior to the input date in the format YYYYMMDD
|
61 |
+
"""
|
62 |
+
dt = datetime.strptime(date_str, '%Y%m%d')
|
63 |
+
# To calculate the date one year ago
|
64 |
+
one_year_ago = dt - timedelta(days=365)
|
65 |
+
|
66 |
+
# To format the date as a string
|
67 |
+
one_year_ago_str = one_year_ago.strftime('%Y%m%d')
|
68 |
+
|
69 |
+
return one_year_ago_str
|
70 |
+
|
71 |
+
|
72 |
+
def get_adj_factor(stock_code: str = '', start_date: str = '', end_date: str = '') -> pd.DataFrame:
|
73 |
+
# Get stock price adjustment factors. Retrieve the stock price adjustment factors for a single stock's entire historical data or for all stocks on a single trading day.
|
74 |
+
# The input includes the stock code, start date, end date, and trading date, all in string format with the date in the YYYYMMDD format
|
75 |
+
# The return value is a dataframe containing the stock code, trading date, and adjustment factor
|
76 |
+
# ts_code str 股票代码
|
77 |
+
# adj_factor float 复权因子
|
78 |
+
"""
|
79 |
+
This function retrieves the adjusted stock prices for a given stock code and date range.
|
80 |
+
|
81 |
+
Args:
|
82 |
+
- stock_code: string, the stock code to retrieve data for
|
83 |
+
- start_date: string, the start date in the format YYYYMMDD
|
84 |
+
- end_date: string, the end date in the format YYYYMMDD
|
85 |
+
|
86 |
+
Returns:
|
87 |
+
- dataframe, a dataframe containing the stock code, trade date, and adjusted factor
|
88 |
+
|
89 |
+
This will retrieve the adjusted stock prices for the stock with code '000001.SZ' between the dates '20220101' and '20220501'.
|
90 |
+
"""
|
91 |
+
df = pro.adj_factor(**{
|
92 |
+
"ts_code": stock_code,
|
93 |
+
"trade_date": "",
|
94 |
+
"start_date": start_date,
|
95 |
+
"end_date": end_date,
|
96 |
+
"limit": "",
|
97 |
+
"offset": ""
|
98 |
+
}, fields=[
|
99 |
+
"ts_code",
|
100 |
+
"trade_date",
|
101 |
+
"adj_factor"
|
102 |
+
])
|
103 |
+
|
104 |
+
return df
|
105 |
+
|
106 |
+
def get_stock_code(stock_name: str) -> str:
|
107 |
+
# Retrieve the stock code of a given stock name. If we call get_stock_code('贵州茅台'), it will return '600519.SH'.
|
108 |
+
|
109 |
+
|
110 |
+
df = pd.read_csv('tushare_stock_basic_20230421210721.csv')
|
111 |
+
try:
|
112 |
+
code = df.loc[df.name==stock_name].ts_code.iloc[0]
|
113 |
+
return code
|
114 |
+
except:
|
115 |
+
return None
|
116 |
+
|
117 |
+
|
118 |
+
|
119 |
+
|
120 |
+
def get_stock_name_from_code(stock_code: str) -> str:
|
121 |
+
"""
|
122 |
+
Reads a local file to retrieve the stock name from a given stock code.
|
123 |
+
|
124 |
+
Args:
|
125 |
+
- stock_code (str): The code of the stock.
|
126 |
+
|
127 |
+
Returns:
|
128 |
+
- str: The stock name of the given stock code.
|
129 |
+
"""
|
130 |
+
# For example,if we call get_stock_name_from_code('600519.SH'), it will return '贵州茅台'.
|
131 |
+
|
132 |
+
|
133 |
+
df = pd.read_csv('tushare_stock_basic_20230421210721.csv')
|
134 |
+
name = df.loc[df.ts_code == stock_code].name.iloc[0]
|
135 |
+
|
136 |
+
return name
|
137 |
+
|
138 |
+
def get_stock_prices_data(stock_name: str='', start_date: str='', end_date: str='', freq:str='daily') -> pd.DataFrame:
|
139 |
+
"""
|
140 |
+
Retrieves the daily/weekly/monthly price data for a given stock code during a specific time period. get_stock_prices_data('贵州茅台','20200120','20220222','daily')
|
141 |
+
|
142 |
+
Args:
|
143 |
+
- stock_name (str)
|
144 |
+
- start_date (str): The start date in the format 'YYYYMMDD'.
|
145 |
+
- end_date (str): The end date in 'YYYYMMDD'.
|
146 |
+
- freq (str): The frequency of the price data, can be 'daily', 'weekly', or 'monthly'.
|
147 |
+
|
148 |
+
Returns:
|
149 |
+
- pd.DataFrame: A dataframe that contains the daily/weekly/monthly data. The output columns contain stock_code, trade_date, open, high, low, close, pre_close(昨天收盘价), change(涨跌额), pct_chg(涨跌幅),vol(成交量),amount(成交额)
|
150 |
+
"""
|
151 |
+
|
152 |
+
stock_code = get_stock_code(stock_name)
|
153 |
+
|
154 |
+
if freq == 'daily':
|
155 |
+
stock_data = pro.daily(**{
|
156 |
+
"ts_code": stock_code,
|
157 |
+
"trade_date": '',
|
158 |
+
"start_date": start_date,
|
159 |
+
"end_date": end_date,
|
160 |
+
"offset": "",
|
161 |
+
"limit": ""
|
162 |
+
}, fields=[
|
163 |
+
"ts_code",
|
164 |
+
"trade_date",
|
165 |
+
"open",
|
166 |
+
"high",
|
167 |
+
"low",
|
168 |
+
"close",
|
169 |
+
"pre_close",
|
170 |
+
"change",
|
171 |
+
"pct_chg",
|
172 |
+
"vol",
|
173 |
+
"amount"
|
174 |
+
])
|
175 |
+
|
176 |
+
elif freq == 'weekly':
|
177 |
+
stock_data = pro.weekly(**{
|
178 |
+
"ts_code": stock_code,
|
179 |
+
"trade_date": '',
|
180 |
+
"start_date": start_date,
|
181 |
+
"end_date": end_date,
|
182 |
+
"limit": "",
|
183 |
+
"offset": ""
|
184 |
+
}, fields=[
|
185 |
+
"ts_code",
|
186 |
+
"trade_date",
|
187 |
+
"close",
|
188 |
+
"open",
|
189 |
+
"high",
|
190 |
+
"low",
|
191 |
+
"pre_close",
|
192 |
+
"change",
|
193 |
+
"pct_chg",
|
194 |
+
"vol",
|
195 |
+
"amount"
|
196 |
+
])
|
197 |
+
elif freq == 'monthly':
|
198 |
+
stock_data = pro.monthly(**{
|
199 |
+
"ts_code": stock_code,
|
200 |
+
"trade_date": '',
|
201 |
+
"start_date": start_date,
|
202 |
+
"end_date": end_date,
|
203 |
+
"limit": "",
|
204 |
+
"offset": ""
|
205 |
+
}, fields=[
|
206 |
+
"ts_code",
|
207 |
+
"trade_date",
|
208 |
+
"close",
|
209 |
+
"open",
|
210 |
+
"high",
|
211 |
+
"low",
|
212 |
+
"pre_close",
|
213 |
+
"change",
|
214 |
+
"pct_chg",
|
215 |
+
"vol",
|
216 |
+
"amount"
|
217 |
+
])
|
218 |
+
|
219 |
+
|
220 |
+
adj_f = get_adj_factor(stock_code, start_date, end_date)
|
221 |
+
stock_data = pd.merge(stock_data, adj_f, on=['ts_code', 'trade_date'])
|
222 |
+
# Multiply the values of open, high, low, and close by their corresponding adjustment factors.
|
223 |
+
# To obtain the adjusted close price
|
224 |
+
stock_data[['open', 'high', 'low', 'close']] *= stock_data['adj_factor'].values.reshape(-1, 1)
|
225 |
+
|
226 |
+
#stock_data.rename(columns={'vol': 'volume'}, inplace=True)
|
227 |
+
df = pd.read_csv('tushare_stock_basic_20230421210721.csv')
|
228 |
+
stock_data_merged = pd.merge(stock_data, df, on='ts_code')
|
229 |
+
stock_data_merged.rename(columns={'ts_code': 'stock_code'}, inplace=True)
|
230 |
+
stock_data_merged.rename(columns={'name': 'stock_name'}, inplace=True)
|
231 |
+
stock_data_merged = stock_data_merged.sort_values(by='trade_date', ascending=True) # To sort the DataFrame by date in ascending order
|
232 |
+
return stock_data_merged
|
233 |
+
|
234 |
+
|
235 |
+
|
236 |
+
def get_stock_technical_data(stock_name: str, start_date: str, end_date: str) -> pd.DataFrame:
|
237 |
+
"""
|
238 |
+
Retrieves the daily technical data of a stock including macd turnover rate, volume, PE ratio, etc. Those technical indicators are usually plotted as subplots in a k-line chart.
|
239 |
+
|
240 |
+
Args:
|
241 |
+
stock_name (str):
|
242 |
+
start_date (str): Start date "YYYYMMDD"
|
243 |
+
end_date (str): End date "YYYYMMDD"
|
244 |
+
|
245 |
+
Returns:
|
246 |
+
pd.DataFrame: A DataFrame containing the technical data of the stock,
|
247 |
+
including various indicators such as ts_code, trade_date, close, macd_dif, macd_dea, macd, kdj_k, kdj_d, kdj_j, rsi_6, rsi_12, boll_upper, boll_mid, boll_lower, cci, turnover_rate, turnover_rate_f, volume_ratio, pe_ttm(市盈率), pb(市净率), ps_ttm, dv_ttm, total_share, float_share, free_share, total_mv, circ_mv
|
248 |
+
|
249 |
+
"""
|
250 |
+
|
251 |
+
# Technical factors
|
252 |
+
stock_code = get_stock_code(stock_name)
|
253 |
+
stock_data1 = pro.stk_factor(**{
|
254 |
+
"ts_code": stock_code,
|
255 |
+
"start_date": start_date,
|
256 |
+
"end_date": end_date,
|
257 |
+
"trade_date": '',
|
258 |
+
"limit": "",
|
259 |
+
"offset": ""
|
260 |
+
}, fields=[
|
261 |
+
"ts_code",
|
262 |
+
"trade_date",
|
263 |
+
"close",
|
264 |
+
"macd_dif",
|
265 |
+
"macd_dea",
|
266 |
+
"macd",
|
267 |
+
"kdj_k",
|
268 |
+
"kdj_d",
|
269 |
+
"kdj_j",
|
270 |
+
"rsi_6",
|
271 |
+
"rsi_12",
|
272 |
+
"rsi_24",
|
273 |
+
"boll_upper",
|
274 |
+
"boll_mid",
|
275 |
+
"boll_lower",
|
276 |
+
"cci"
|
277 |
+
])
|
278 |
+
# Trading factors
|
279 |
+
stock_data2 = pro.daily_basic(**{
|
280 |
+
"ts_code": stock_code,
|
281 |
+
"trade_date": '',
|
282 |
+
"start_date": start_date,
|
283 |
+
"end_date": end_date,
|
284 |
+
"limit": "",
|
285 |
+
"offset": ""
|
286 |
+
}, fields=[
|
287 |
+
"ts_code", #
|
288 |
+
"trade_date",
|
289 |
+
"turnover_rate",
|
290 |
+
"turnover_rate_f",
|
291 |
+
"volume_ratio",
|
292 |
+
"pe_ttm",
|
293 |
+
"pb",
|
294 |
+
"ps_ttm",
|
295 |
+
"dv_ttm",
|
296 |
+
"total_share",
|
297 |
+
"float_share",
|
298 |
+
"free_share",
|
299 |
+
"total_mv",
|
300 |
+
"circ_mv"
|
301 |
+
])
|
302 |
+
|
303 |
+
#
|
304 |
+
stock_data = pd.merge(stock_data1, stock_data2, on=['ts_code', 'trade_date'])
|
305 |
+
df = pd.read_csv('tushare_stock_basic_20230421210721.csv')
|
306 |
+
stock_data_merged = pd.merge(stock_data, df, on='ts_code')
|
307 |
+
stock_data_merged = stock_data_merged.sort_values(by='trade_date', ascending=True)
|
308 |
+
|
309 |
+
stock_data_merged.drop(['symbol'], axis=1, inplace=True)
|
310 |
+
|
311 |
+
stock_data_merged.rename(columns={'ts_code': 'stock_code'}, inplace=True)
|
312 |
+
stock_data_merged.rename(columns={'name': 'stock_name'}, inplace=True)
|
313 |
+
|
314 |
+
return stock_data_merged
|
315 |
+
|
316 |
+
|
317 |
+
def plot_stock_data(stock_data: pd.DataFrame, ax: Optional[plt.Axes] = None, figure_type: str = 'line', title_name: str ='') -> plt.Axes:
|
318 |
+
|
319 |
+
"""
|
320 |
+
This function plots stock data.
|
321 |
+
|
322 |
+
Args:
|
323 |
+
- stock_data: pandas DataFrame, the stock data to plot. The DataFrame should contain three columns:
|
324 |
+
- Column 1: trade date in 'YYYYMMDD'
|
325 |
+
- Column 2: Stock name or code (string format)
|
326 |
+
- Column 3: Index value (numeric format)
|
327 |
+
The DataFrame can be time series data or cross-sectional data. If it is time-series data, the first column represents different trade time, the second column represents the same name. For cross-sectional data, the first column is the same, the second column contains different stocks.
|
328 |
+
|
329 |
+
- ax: matplotlib Axes object, the axes to plot the data on
|
330 |
+
- figure_type: the type of figure (either 'line' or 'bar')
|
331 |
+
- title_name
|
332 |
+
|
333 |
+
Returns:
|
334 |
+
- matplotlib Axes object, the axes containing the plot
|
335 |
+
"""
|
336 |
+
|
337 |
+
index_name = stock_data.columns[2]
|
338 |
+
name_list = stock_data.iloc[:,1]
|
339 |
+
date_list = stock_data.iloc[:,0]
|
340 |
+
if name_list.nunique() == 1 and date_list.nunique() != 1:
|
341 |
+
# Time Series Data
|
342 |
+
unchanged_var = name_list.iloc[0] # stock name
|
343 |
+
x_dim = date_list # tradingdate
|
344 |
+
x_name = stock_data.columns[0]
|
345 |
+
|
346 |
+
elif name_list.nunique() != 1 and date_list.nunique() == 1:
|
347 |
+
# Cross-sectional Data
|
348 |
+
unchanged_var = date_list.iloc[0] # tradingdate
|
349 |
+
x_dim = name_list # stock name
|
350 |
+
x_name = stock_data.columns[1]
|
351 |
+
|
352 |
+
data_size = x_dim.shape[0]
|
353 |
+
|
354 |
+
|
355 |
+
|
356 |
+
start_x_dim, end_x_dim = x_dim.iloc[0], x_dim.iloc[-1]
|
357 |
+
|
358 |
+
start_y = stock_data.iloc[0, 2]
|
359 |
+
end_y = stock_data.iloc[-1, 2]
|
360 |
+
|
361 |
+
|
362 |
+
def generate_random_color():
|
363 |
+
r = random.randint(0, 255)/ 255.0
|
364 |
+
g = random.randint(0, 100)/ 255.0
|
365 |
+
b = random.randint(0, 255)/ 255.0
|
366 |
+
return (r, g, b)
|
367 |
+
|
368 |
+
color = generate_random_color()
|
369 |
+
if ax is None:
|
370 |
+
_, ax = plt.subplots()
|
371 |
+
|
372 |
+
if figure_type =='line':
|
373 |
+
#
|
374 |
+
|
375 |
+
ax.plot(x_dim, stock_data.iloc[:, 2], label = unchanged_var+'_' + index_name, color=color,linewidth=3)
|
376 |
+
#
|
377 |
+
plt.scatter(x_dim, stock_data.iloc[:, 2], color=color,s=3) # Add markers to the data points
|
378 |
+
|
379 |
+
#
|
380 |
+
#ax.scatter(x_dim, stock_data.iloc[:, 2],label = unchanged_var+'_' + index_name, color=color, s=3)
|
381 |
+
#
|
382 |
+
|
383 |
+
ax.annotate(unchanged_var + ':' + str(round(start_y, 2)) + ' @' + start_x_dim, xy=(start_x_dim, start_y),
|
384 |
+
xytext=(start_x_dim, start_y),
|
385 |
+
textcoords='data', fontsize=9,color=color, horizontalalignment='right')
|
386 |
+
|
387 |
+
ax.annotate(unchanged_var + ':' + str(round(end_y, 2)) +' @' + end_x_dim, xy=(end_x_dim, end_y),
|
388 |
+
xytext=(end_x_dim, end_y),
|
389 |
+
textcoords='data', fontsize=9, color=color, horizontalalignment='left')
|
390 |
+
|
391 |
+
|
392 |
+
elif figure_type == 'bar':
|
393 |
+
ax.bar(x_dim, stock_data.iloc[:, 2], label = unchanged_var + '_' + index_name, width=0.3, color=color)
|
394 |
+
ax.annotate(unchanged_var + ':' + str(round(start_y, 2)) + ' @' + start_x_dim, xy=(start_x_dim, start_y),
|
395 |
+
xytext=(start_x_dim, start_y),
|
396 |
+
textcoords='data', fontsize=9, color=color, horizontalalignment='right')
|
397 |
+
|
398 |
+
ax.annotate(unchanged_var + ':' + str(round(end_y, 2)) + ' @' + end_x_dim, xy=(end_x_dim, end_y),
|
399 |
+
xytext=(end_x_dim, end_y),
|
400 |
+
textcoords='data', fontsize=9, color=color, horizontalalignment='left')
|
401 |
+
|
402 |
+
plt.xticks(x_dim,rotation=45) #
|
403 |
+
ax.xaxis.set_major_locator(MaxNLocator( integer=True, prune=None, nbins=100)) #
|
404 |
+
|
405 |
+
|
406 |
+
plt.xlabel(x_name)
|
407 |
+
plt.ylabel(f'{index_name}')
|
408 |
+
ax.set_title(title_name )
|
409 |
+
plt.legend() # 显示图例
|
410 |
+
fig = plt.gcf()
|
411 |
+
fig.set_size_inches(18, 12)
|
412 |
+
|
413 |
+
return ax
|
414 |
+
|
415 |
+
|
416 |
+
def query_fund_Manager(Manager_name: str) -> pd.DataFrame:
|
417 |
+
# 代码fund_code,公告日期ann_date,基金经理名字name,性别gender,出生年份birth_year,学历edu,国籍nationality,开始管理日期begin_date,结束日期end_date,简历resume
|
418 |
+
"""
|
419 |
+
Retrieves information about a fund manager.
|
420 |
+
|
421 |
+
Args:
|
422 |
+
Manager_name (str): The name of the fund manager.
|
423 |
+
|
424 |
+
Returns:
|
425 |
+
df (DataFrame): A DataFrame containing the fund manager's information, including the fund codes, announcement dates,
|
426 |
+
manager's name, gender, birth year, education, nationality, start and end dates of managing funds,
|
427 |
+
and the manager's resume.
|
428 |
+
"""
|
429 |
+
|
430 |
+
df = pro.fund_manager(**{
|
431 |
+
"ts_code": "",
|
432 |
+
"ann_date": "",
|
433 |
+
"name": Manager_name,
|
434 |
+
"offset": "",
|
435 |
+
"limit": ""
|
436 |
+
}, fields=[
|
437 |
+
"ts_code",
|
438 |
+
"ann_date",
|
439 |
+
"name",
|
440 |
+
"gender",
|
441 |
+
"birth_year",
|
442 |
+
"edu",
|
443 |
+
"nationality",
|
444 |
+
"begin_date",
|
445 |
+
"end_date",
|
446 |
+
"resume"
|
447 |
+
])
|
448 |
+
#
|
449 |
+
df.rename(columns={'ts_code': 'fund_code'}, inplace=True)
|
450 |
+
# To query the fund name based on the fund code and store it in a new column called fund_name, while removing the rows where the fund name is not found
|
451 |
+
df['fund_name'] = df['fund_code'].apply(lambda x: query_fund_name_or_code('', x))
|
452 |
+
df.dropna(subset=['fund_name'], inplace=True)
|
453 |
+
df.rename(columns={'name': 'manager_name'}, inplace=True)
|
454 |
+
#
|
455 |
+
df_out = df[['fund_name','fund_code','ann_date','manager_name','begin_date','end_date']]
|
456 |
+
|
457 |
+
return df_out
|
458 |
+
|
459 |
+
|
460 |
+
# def save_stock_prices_to_csv(stock_prices: pd.DataFrame, stock_name: str, file_path: str) -> None:
|
461 |
+
#
|
462 |
+
# """
|
463 |
+
# Saves the price data of a specific stock symbol during a specific time period to a local CSV file.
|
464 |
+
#
|
465 |
+
# Args:
|
466 |
+
# - stock_prices (pd.DataFrame): A pandas dataframe that contains the daily price data for the given stock symbol during the specified time period.
|
467 |
+
# - stock_name (str): The name of the stock.
|
468 |
+
# - file_path (str): The file path where the CSV file will be saved.
|
469 |
+
#
|
470 |
+
# Returns:
|
471 |
+
# - None: The function only saves the CSV file to the specified file path.
|
472 |
+
# """
|
473 |
+
# # The function checks if the directory to save the CSV file exists and creates it if it does not exist.
|
474 |
+
# # The function then saves the price data of the specified stock symbol during the specified time period to a local CSV file with the name {stock_name}_price_data.csv in the specified file path.
|
475 |
+
#
|
476 |
+
#
|
477 |
+
# if not os.path.exists(file_path):
|
478 |
+
# os.makedirs(file_path)
|
479 |
+
#
|
480 |
+
#
|
481 |
+
# file_path = f"{file_path}{stock_name}_stock_prices.csv"
|
482 |
+
# stock_prices.to_csv(file_path, index_label='Date')
|
483 |
+
# print(f"Stock prices for {stock_name} saved to {file_path}")
|
484 |
+
|
485 |
+
|
486 |
+
def calculate_stock_index(stock_data: pd.DataFrame, index:str='close') -> pd.DataFrame:
|
487 |
+
"""
|
488 |
+
Calculate a specific index of a stock based on its price information.
|
489 |
+
|
490 |
+
Args:
|
491 |
+
stock_data (pd.DataFrame): DataFrame containing the stock's price information.
|
492 |
+
index (str, optional): The index to calculate. The available options depend on the column names in the
|
493 |
+
input stock price data. Additionally, there are two special indices: 'candle_K' and 'Cumulative_Earnings_Rate'.
|
494 |
+
|
495 |
+
Returns:
|
496 |
+
DataFrame containing the corresponding index data of the stock. In general, it includes three columns: 'trade_date', 'name', and the corresponding index value.
|
497 |
+
Besides, if index is 'candle_K', the function returns the DataFrame containing 'trade_date', 'Open', 'High', 'Low', 'Close', 'Volume','name' column.
|
498 |
+
If index is a technical index such as 'macd' or a trading index likes 'pe_ttm', the function returns the DataFrame with corresponding columns.
|
499 |
+
"""
|
500 |
+
|
501 |
+
|
502 |
+
if 'stock_name' not in stock_data.columns and 'index_name' in stock_data.columns:
|
503 |
+
stock_data.rename(columns={'index_name': 'stock_name'}, inplace=True)
|
504 |
+
#
|
505 |
+
index = index.lower()
|
506 |
+
if index=='Cumulative_Earnings_Rate' or index =='Cumulative_Earnings_Rate'.lower() :
|
507 |
+
stock_data[index] = (1 + stock_data['pct_chg'] / 100.).cumprod() - 1.
|
508 |
+
stock_data[index] = stock_data[index] * 100.
|
509 |
+
if 'stock_name' in stock_data.columns :
|
510 |
+
selected_index = stock_data[['trade_date', 'stock_name', index]].copy()
|
511 |
+
#
|
512 |
+
if 'fund_name' in stock_data.columns:
|
513 |
+
selected_index = stock_data[['trade_date', 'fund_name', index]].copy()
|
514 |
+
return selected_index
|
515 |
+
|
516 |
+
elif index == 'candle_K' or index == 'candle_K'.lower():
|
517 |
+
#tech_df = tech_df.drop(['name', 'symbol', 'industry', 'area','market','list_date','ts_code','close'], axis=1)
|
518 |
+
# Merge two DataFrames based on the 'trade_date' column.
|
519 |
+
|
520 |
+
stock_data = stock_data.rename(
|
521 |
+
columns={'open': 'Open', 'high': 'High', 'low': 'Low', 'close': 'Close',
|
522 |
+
'vol': 'Volume'})
|
523 |
+
selected_index = stock_data[['trade_date', 'Open', 'High', 'Low', 'Close', 'Volume','stock_name']].copy()
|
524 |
+
return selected_index
|
525 |
+
|
526 |
+
elif index =='macd':
|
527 |
+
selected_index = stock_data[['trade_date','macd','macd_dea','macd_dif']].copy()
|
528 |
+
return selected_index
|
529 |
+
|
530 |
+
elif index =='rsi':
|
531 |
+
selected_index = stock_data[['trade_date','rsi_6','rsi_12']].copy()
|
532 |
+
return selected_index
|
533 |
+
|
534 |
+
elif index =='boll':
|
535 |
+
selected_index = stock_data[['trade_date', 'boll_upper', 'boll_lower','boll_mid']].copy()
|
536 |
+
return selected_index
|
537 |
+
|
538 |
+
elif index =='kdj':
|
539 |
+
selected_index = stock_data[['trade_date', 'kdj_k', 'kdj_d','kdj_j']].copy()
|
540 |
+
return selected_index
|
541 |
+
|
542 |
+
elif index =='cci':
|
543 |
+
selected_index = stock_data[['trade_date', 'cci']].copy()
|
544 |
+
return selected_index
|
545 |
+
|
546 |
+
elif index == '换手率':
|
547 |
+
selected_index = stock_data[['trade_date', 'turnover_rate','turnover_rate_f']].copy()
|
548 |
+
return selected_index
|
549 |
+
|
550 |
+
elif index == '市值':
|
551 |
+
selected_index = stock_data[['trade_date', 'total_mv','circ_mv']].copy()
|
552 |
+
return selected_index
|
553 |
+
|
554 |
+
|
555 |
+
elif index in stock_data.columns:
|
556 |
+
stock_data = stock_data
|
557 |
+
|
558 |
+
if 'stock_name' in stock_data.columns :
|
559 |
+
selected_index = stock_data[['trade_date', 'stock_name', index]].copy()
|
560 |
+
|
561 |
+
if 'fund_name' in stock_data.columns:
|
562 |
+
selected_index = stock_data[['trade_date', 'fund_name', index]].copy()
|
563 |
+
# Except for candlestick chart and technical indicators, the remaining outputs consist of three columns: date, name, and indicator.
|
564 |
+
return selected_index
|
565 |
+
|
566 |
+
|
567 |
+
|
568 |
+
def rank_index_cross_section(stock_data: pd.DataFrame, Top_k: int = -1, ascending: bool = False) -> pd.DataFrame:
|
569 |
+
"""
|
570 |
+
Sort the cross-sectional data based on the given index.
|
571 |
+
|
572 |
+
Args:
|
573 |
+
stock_data : DataFrame containing the cross-sectional data. It should have three columns, and the last column represents the variable to be sorted.
|
574 |
+
Top_k : The number of data points to retain after sorting. (Default: -1, which retains all data points)
|
575 |
+
ascending: Whether to sort the data in ascending order or not. (Default: False)
|
576 |
+
|
577 |
+
Returns:
|
578 |
+
stock_data_selected : DataFrame containing the sorted data. It has the same structure as the input DataFrame.
|
579 |
+
"""
|
580 |
+
|
581 |
+
index = stock_data.columns[-1]
|
582 |
+
stock_data = stock_data.sort_values(by=index, ascending=ascending)
|
583 |
+
#stock_data_selected = stock_data[['trade_date','stock_name', index]].copy()
|
584 |
+
stock_data_selected = stock_data[:Top_k]
|
585 |
+
stock_data_selected = stock_data_selected.drop_duplicates(subset=['stock_name'], keep='first')
|
586 |
+
return stock_data_selected
|
587 |
+
|
588 |
+
|
589 |
+
def get_company_info(stock_name: str='') -> pd.DataFrame:
|
590 |
+
# ts_code: str 股票代码, exchange:str 交易所代码SSE上交所 SZSE深交所, chairman:str 法人代表, manager:str 总经理, secretary:str 董秘 # reg_capital:float 注册资本, setup_date:str 注册日期, province:str 所在省份 ,city:str 所在城市
|
591 |
+
# introduction:str 公司介绍, website:str 公司主页 , email:str 电子邮件, office:str 办公室 # ann_date: str 公告日期, business_scope:str 经营范围, employees:int 员工人数, main_business:str 主要业务及产品
|
592 |
+
"""
|
593 |
+
This function retrieves company information including stock code, exchange, chairman, manager, secretary,
|
594 |
+
registered capital, setup date, province, city, website, email, employees, business scope, main business,
|
595 |
+
introduction, office, and announcement date.
|
596 |
+
|
597 |
+
Args:
|
598 |
+
- stock_name (str): The name of the stock.
|
599 |
+
|
600 |
+
Returns:
|
601 |
+
- pd.DataFrame: A DataFrame that contains the company information.
|
602 |
+
"""
|
603 |
+
|
604 |
+
stock_code = get_stock_code(stock_name)
|
605 |
+
df = pro.stock_company(**{
|
606 |
+
"ts_code": stock_code,"exchange": "","status": "", "limit": "","offset": ""
|
607 |
+
}, fields=[
|
608 |
+
"ts_code","exchange","chairman", "manager","secretary", "reg_capital","setup_date", "province","city",
|
609 |
+
"website", "email","employees","business_scope","main_business","introduction","office", "ann_date"
|
610 |
+
])
|
611 |
+
|
612 |
+
|
613 |
+
en_to_cn = {
|
614 |
+
'ts_code': '股票代码',
|
615 |
+
'exchange': '交易所代码',
|
616 |
+
'chairman': '法人代表',
|
617 |
+
'manager': '总经理',
|
618 |
+
'secretary': '董秘',
|
619 |
+
'reg_capital': '注册资本',
|
620 |
+
'setup_date': '注册日期',
|
621 |
+
'province': '所在省份',
|
622 |
+
'city': '所在城市',
|
623 |
+
'introduction': '公司介绍',
|
624 |
+
'website': '公司主页',
|
625 |
+
'email': '电子邮件',
|
626 |
+
'office': '办公室',
|
627 |
+
'ann_date': '公告日期',
|
628 |
+
'business_scope': '经营范围',
|
629 |
+
'employees': '员工人数',
|
630 |
+
'main_business': '主要业务及产品'
|
631 |
+
}
|
632 |
+
|
633 |
+
df.rename(columns=en_to_cn, inplace=True)
|
634 |
+
df.insert(0, '股票名称', stock_name)
|
635 |
+
# for column in df.columns:
|
636 |
+
# print(f"[{column}]: {df[column].values[0]}")
|
637 |
+
|
638 |
+
|
639 |
+
return df
|
640 |
+
|
641 |
+
|
642 |
+
|
643 |
+
|
644 |
+
|
645 |
+
# def get_Financial_data(stock_code: str, report_date: str, financial_index: str = '' ) -> pd.DataFrame:
|
646 |
+
# # report_date的格式为"YYYYMMDD",包括"yyyy0331"为一季报,"yyyy0630"为半年报,"yyyy0930"为三季报,"yyyy1231"为年报
|
647 |
+
# # index包含: # current_ratio 流动比率 # quick_ratio 速动比率 # netprofit_margin 销售净利率 # grossprofit_margin 销售毛利率 # roe 净资产收益率 # roe_dt 净资产收益率(扣除非经常损益)
|
648 |
+
# # roa 总资产报酬率 # debt_to_assets 资产负债率 # roa_yearly 年化总资产净利率 # q_dtprofit 扣除非经常损益后的单季度净利润 # q_eps 每股收益(单季度)
|
649 |
+
# # q_netprofit_margin 销售净利率(单季度) # q_gsprofit_margin 销售毛利率(单季度) # basic_eps_yoy 基本每股收益同比增长率(%) # netprofit_yoy 归属母公司股东的净利润同比增长率(%) # q_netprofit_yoy 归属母公司股东的净利润同比增长率(%)(单季度) # q_netprofit_qoq 归属母公司股东的净利润环比增长率(%)(单季度) # equity_yoy 净资产同比增长率
|
650 |
+
# """
|
651 |
+
# Retrieves financial data for a specific stock within a given date range.
|
652 |
+
#
|
653 |
+
# Args:
|
654 |
+
# stock_code (str): The stock code or symbol of the company for which financial data is requested.
|
655 |
+
# report_date (str): The report date in the format "YYYYMMDD" .
|
656 |
+
# financial_index (str, optional): The financial indicator to be queried. If not specified, all available financial
|
657 |
+
# indicators will be included.
|
658 |
+
#
|
659 |
+
# Returns:
|
660 |
+
# pd.DataFrame: A DataFrame containing the financial data for the specified stock and date range. The DataFrame
|
661 |
+
# consists of the following columns: "stock_name",
|
662 |
+
# "trade_date" (reporting period), and the requested financial indicator(s).
|
663 |
+
#
|
664 |
+
# """
|
665 |
+
# stock_data = pro.fina_indicator(**{
|
666 |
+
# "ts_code": stock_code,
|
667 |
+
# "ann_date": "",
|
668 |
+
# "start_date": '',
|
669 |
+
# "end_date": '',
|
670 |
+
# "period": report_date,
|
671 |
+
# "update_flag": "1",
|
672 |
+
# "limit": "",
|
673 |
+
# "offset": ""
|
674 |
+
# }, fields=["ts_code","end_date", financial_index])
|
675 |
+
#
|
676 |
+
# stock_name = get_stock_name_from_code(stock_code)
|
677 |
+
# stock_data['stock_name'] = stock_name
|
678 |
+
# stock_data = stock_data.sort_values(by='end_date', ascending=True) # 按照日期升序排列
|
679 |
+
# # 把end_data列改名为trade_date
|
680 |
+
# stock_data.rename(columns={'end_date': 'trade_date'}, inplace=True)
|
681 |
+
# stock_financial_data = stock_data[['stock_name', 'trade_date', financial_index]]
|
682 |
+
# return stock_financial_data
|
683 |
+
|
684 |
+
|
685 |
+
def get_Financial_data_from_time_range(stock_name:str, start_date:str, end_date:str, financial_index:str='') -> pd.DataFrame:
|
686 |
+
# start_date='20190101',end_date='20221231',financial_index='roe', The returned data consists of the ROE values for the entire three-year period from 2019 to 2022.
|
687 |
+
# To query quarterly or annual financial report data for a specific moment, "yyyy0331"为一季报,"yyyy0630"为半年报,"yyyy0930"为三季报,"yyyy1231"为年报,例如get_Financial_data_from_time_range("600519.SH", "20190331", "20190331", "roe") means to query the return on equity (ROE) data from the first quarter of 2019,
|
688 |
+
# # current_ratio 流动比率 # quick_ratio 速动比率 # netprofit_margin 销售净利率 # grossprofit_margin 销售毛利率 # roe 净资产收益率 # roe_dt 净资产收益率(扣除非经常损益)
|
689 |
+
# roa 总资产报酬率 # debt_to_assets 资产负债率 # roa_yearly 年化总资产净利率 # q_dtprofit 扣除非经常损益后的单季度净利润 # q_eps 每股收益(单季度)
|
690 |
+
# q_netprofit_margin 销售净利率(单季度) # q_gsprofit_margin 销售毛利率(单季度) # basic_eps_yoy 基本每股收益同比增长率(%) # netprofit_yoy 归属母公司股东的净利润同比增长率(%) # q_netprofit_yoy 归属母公司股东的净利润同比增长率(%)(单季度) # q_netprofit_qoq 归属母公司股东的净利润环比增长率(%)(单季度) # equity_yoy 净资产同比增长率
|
691 |
+
"""
|
692 |
+
Retrieves the financial data for a given stock within a specified date range.
|
693 |
+
|
694 |
+
Args:
|
695 |
+
stock_name (str): The stock code.
|
696 |
+
start_date (str): The start date of the data range in the format "YYYYMMDD".
|
697 |
+
end_date (str): The end date of the data range in the format "YYYYMMDD".
|
698 |
+
financial_index (str, optional): The financial indicator to be queried.
|
699 |
+
|
700 |
+
Returns:
|
701 |
+
pd.DataFrame: A DataFrame containin financial data for the specified stock and date range.
|
702 |
+
|
703 |
+
"""
|
704 |
+
stock_code = get_stock_code(stock_name)
|
705 |
+
stock_data = pro.fina_indicator(**{
|
706 |
+
"ts_code": stock_code,
|
707 |
+
"ann_date": "",
|
708 |
+
"start_date": start_date,
|
709 |
+
"end_date": end_date,
|
710 |
+
"period": '',
|
711 |
+
"update_flag": "1",
|
712 |
+
"limit": "",
|
713 |
+
"offset": ""
|
714 |
+
}, fields=["ts_code", "end_date", financial_index])
|
715 |
+
|
716 |
+
#stock_name = get_stock_name_from_code(stock_code)
|
717 |
+
stock_data['stock_name'] = stock_name
|
718 |
+
stock_data = stock_data.sort_values(by='end_date', ascending=True) # 按照日期升序排列
|
719 |
+
# 把end_data列改名为trade_date
|
720 |
+
stock_data.rename(columns={'end_date': 'trade_date'}, inplace=True)
|
721 |
+
stock_financial_data = stock_data[['stock_name', 'trade_date', financial_index]]
|
722 |
+
return stock_financial_data
|
723 |
+
|
724 |
+
|
725 |
+
def get_GDP_data(start_quarter:str='', end_quarter:str='', index:str='gdp_yoy') -> pd.DataFrame:
|
726 |
+
# The available indicators for query include the following 9 categories: # gdp GDP累计值(亿元)# gdp_yoy 当季同比增速(%)# pi 第一产���累计值(亿元)# pi_yoy 第一产业同比增速(%)# si 第二产业累计值(亿元)# si_yoy 第二产业同比增速(%)# ti 第三产业累计值(亿元) # ti_yoy 第三产业同比增速(%)
|
727 |
+
"""
|
728 |
+
Retrieves GDP data for the chosen index and specified time period.
|
729 |
+
|
730 |
+
Args:
|
731 |
+
- start_quarter (str): The start quarter of the query, in YYYYMMDD format.
|
732 |
+
- end_quarter (str): The end quarter, in YYYYMMDD format.
|
733 |
+
- index (str): The specific GDP index to retrieve. Default is `gdp_yoy`.
|
734 |
+
|
735 |
+
Returns:
|
736 |
+
- pd.DataFrame: A pandas DataFrame with three columns: `quarter`, `country`, and the selected `index`.
|
737 |
+
"""
|
738 |
+
|
739 |
+
# The output is a DataFrame with three columns:
|
740 |
+
# the first column represents the quarter (quarter), the second column represents the country (country), and the third column represents the index (index).
|
741 |
+
df = pro.cn_gdp(**{
|
742 |
+
"q":'',
|
743 |
+
"start_q": start_quarter,
|
744 |
+
"end_q": end_quarter,
|
745 |
+
"limit": "",
|
746 |
+
"offset": ""
|
747 |
+
}, fields=[
|
748 |
+
"quarter",
|
749 |
+
"gdp",
|
750 |
+
"gdp_yoy",
|
751 |
+
"pi",
|
752 |
+
"pi_yoy",
|
753 |
+
"si",
|
754 |
+
"si_yoy",
|
755 |
+
"ti",
|
756 |
+
"ti_yoy"
|
757 |
+
])
|
758 |
+
df = df.sort_values(by='quarter', ascending=True) #
|
759 |
+
df['country'] = 'China'
|
760 |
+
df = df[['quarter', 'country', index]].copy()
|
761 |
+
|
762 |
+
|
763 |
+
return df
|
764 |
+
|
765 |
+
def get_cpi_ppi_currency_supply_data(start_month: str = '', end_month: str = '', type: str = 'cpi', index: str = '') -> pd.DataFrame:
|
766 |
+
# The query types (type) include three categories: CPI, PPI, and currency supply. Each type corresponds to different indices.
|
767 |
+
# Specifically, CPI has 12 indices, PPI has 30 indices, and currency supply has 9 indices.
|
768 |
+
# The output is a DataFrame table with three columns: the first column represents the month (month), the second column represents the country (country), and the third column represents the index (index).
|
769 |
+
|
770 |
+
# type='cpi',monthly CPI data include the following 12 categories:
|
771 |
+
# nt_val 全国当月值 # nt_yoy 全国同比(%)# nt_mom 全国环比(%)# nt_accu 全国累计值# town_val 城市当月值# town_yoy 城市同比(%)# town_mom 城市环比(%)# town_accu 城市累计值# cnt_val 农村当月值# cnt_yoy 农村同比(%)# cnt_mom 农村环比(%)# cnt_accu 农村累计值
|
772 |
+
|
773 |
+
# type = 'ppi', monthly PPI data include the following 30 categories:
|
774 |
+
# ppi_yoy PPI:全部工业品:当月同比
|
775 |
+
# ppi_mp_yoy PPI:生产资料:当月同比
|
776 |
+
# ppi_mp_qm_yoy PPI:生产资料:采掘业:当月同比
|
777 |
+
# ppi_mp_rm_yoy PPI:生产资料:原料业:当月同比
|
778 |
+
# ppi_mp_p_yoy PPI:生产资料:加工业:当月同比
|
779 |
+
# ppi_cg_yoy PPI:生活资料:当月同比
|
780 |
+
# ppi_cg_f_yoy PPI:生活资料:食品类:当月同比
|
781 |
+
# ppi_cg_c_yoy PPI:生活资料:衣着类:当月同比
|
782 |
+
# ppi_cg_adu_yoy PPI:生活资料:一般日用品类:当月同比
|
783 |
+
# ppi_cg_dcg_yoy PPI:生活资料:耐用消费品类:当月同比
|
784 |
+
# ppi_mom PPI:全部工业品:环比
|
785 |
+
# ppi_mp_mom PPI:生产资料:环比
|
786 |
+
# ppi_mp_qm_mom PPI:生产资料:采掘业:环比
|
787 |
+
# ppi_mp_rm_mom PPI:生产资料:原料业:环比
|
788 |
+
# ppi_mp_p_mom PPI:生产资料:加工业:环比
|
789 |
+
# ppi_cg_mom PPI:生活资料:环比
|
790 |
+
# ppi_cg_f_mom PPI:生活资料:食品类:环比
|
791 |
+
# ppi_cg_c_mom PPI:生活资料:衣着类:环比
|
792 |
+
# ppi_cg_adu_mom PPI:生活资料:一般日用品类:环比
|
793 |
+
# ppi_cg_dcg_mom PPI:生活资料:耐用消费品类:环比
|
794 |
+
# ppi_accu PPI:全部工业品:累计同比
|
795 |
+
# ppi_mp_accu PPI:生产资料:累计同比
|
796 |
+
# ppi_mp_qm_accu PPI:生产资料:采掘业:累计同比
|
797 |
+
# ppi_mp_rm_accu PPI:生产资料:原料业:累计同比
|
798 |
+
# ppi_mp_p_accu PPI:生产资料:加工业:累计同比
|
799 |
+
# ppi_cg_accu PPI:生活资料:累计同比
|
800 |
+
# ppi_cg_f_accu PPI:生活资料:食品类:累计同比
|
801 |
+
# ppi_cg_c_accu PPI:生活资料:衣着类:累计同比
|
802 |
+
# ppi_cg_adu_accu PPI:生活资料:一般日用品类:累计同比
|
803 |
+
# ppi_cg_dcg_accu PPI:生活资料:耐用消费品类:累计同比
|
804 |
+
|
805 |
+
# type = 'currency_supply', monthly currency supply data include the following 9 categories:
|
806 |
+
# m0 M0(亿元)# m0_yoy M0同比(%)# m0_mom M0环比(%)# m1 M1(亿元)# m1_yoy M1同比(%)# m1_mom M1环比(%)# m2 M2(亿元)# m2_yoy M2同比(%)# m2_mom M2环比(%)
|
807 |
+
|
808 |
+
"""
|
809 |
+
This function is used to retrieve China's monthly CPI (Consumer Price Index), PPI (Producer Price Index),
|
810 |
+
and monetary supply data published by the National Bureau of Statistics,
|
811 |
+
and return a DataFrame table containing month, country, and index values.
|
812 |
+
The function parameters include start month, end month, query type, and query index.
|
813 |
+
For query indexes that are not within the query range, the default index for the corresponding type is returned.
|
814 |
+
|
815 |
+
Args:
|
816 |
+
- start_month (str): start month of the query, in the format of YYYYMMDD.
|
817 |
+
- end_month (str):end month in YYYYMMDD
|
818 |
+
- type (str): required parameter, query type, including three types: cpi, ppi, and currency_supply.
|
819 |
+
- index (str): optional parameter, query index, the specific index depends on the query type.
|
820 |
+
If the query index is not within the range, the default index for the corresponding type is returned.
|
821 |
+
|
822 |
+
Returns:
|
823 |
+
- pd.DataFrame: DataFrame type, including three columns: month, country, and index value.
|
824 |
+
"""
|
825 |
+
|
826 |
+
if type == 'cpi':
|
827 |
+
|
828 |
+
df = pro.cn_cpi(**{
|
829 |
+
"m": '',
|
830 |
+
"start_m": start_month,
|
831 |
+
"end_m": end_month,
|
832 |
+
"limit": "",
|
833 |
+
"offset": ""
|
834 |
+
}, fields=[
|
835 |
+
"month", "nt_val","nt_yoy", "nt_mom","nt_accu", "town_val", "town_yoy", "town_mom",
|
836 |
+
"town_accu", "cnt_val", "cnt_yoy", "cnt_mom", "cnt_accu"])
|
837 |
+
# If the index is not within the aforementioned range, the index is set as "nt_yoy".
|
838 |
+
if index not in df.columns:
|
839 |
+
index = 'nt_yoy'
|
840 |
+
|
841 |
+
|
842 |
+
elif type == 'ppi':
|
843 |
+
df = pro.cn_ppi(**{
|
844 |
+
"m": '',
|
845 |
+
"start_m": start_month,
|
846 |
+
"end_m": end_month,
|
847 |
+
"limit": "",
|
848 |
+
"offset": ""
|
849 |
+
}, fields=[
|
850 |
+
"month", "ppi_yoy", "ppi_mp_yoy", "ppi_mp_qm_yoy", "ppi_mp_rm_yoy", "ppi_mp_p_yoy", "ppi_cg_yoy",
|
851 |
+
"ppi_cg_f_yoy", "ppi_cg_c_yoy", "ppi_cg_adu_yoy", "ppi_cg_dcg_yoy",
|
852 |
+
"ppi_mom", "ppi_mp_mom", "ppi_mp_qm_mom", "ppi_mp_rm_mom", "ppi_mp_p_mom", "ppi_cg_mom", "ppi_cg_f_mom",
|
853 |
+
"ppi_cg_c_mom", "ppi_cg_adu_mom", "ppi_cg_dcg_mom",
|
854 |
+
"ppi_accu", "ppi_mp_accu", "ppi_mp_qm_accu", "ppi_mp_rm_accu", "ppi_mp_p_accu", "ppi_cg_accu",
|
855 |
+
"ppi_cg_f_accu", "ppi_cg_c_accu", "ppi_cg_adu_accu", "ppi_cg_dcg_accu"
|
856 |
+
])
|
857 |
+
if index not in df.columns:
|
858 |
+
index = 'ppi_yoy'
|
859 |
+
|
860 |
+
elif type == 'currency_supply':
|
861 |
+
df = pro.cn_m(**{
|
862 |
+
"m": '',
|
863 |
+
"start_m": start_month,
|
864 |
+
"end_m": end_month,
|
865 |
+
"limit": "",
|
866 |
+
"offset": ""
|
867 |
+
}, fields=[
|
868 |
+
"month", "m0", "m0_yoy","m0_mom", "m1",
|
869 |
+
"m1_yoy", "m1_mom", "m2", "m2_yoy", "m2_mom"])
|
870 |
+
if index not in df.columns:
|
871 |
+
index = 'm2_yoy'
|
872 |
+
|
873 |
+
|
874 |
+
df = df.sort_values(by='month', ascending=True) #
|
875 |
+
df['country'] = 'China'
|
876 |
+
df = df[['month', 'country', index]].copy()
|
877 |
+
return df
|
878 |
+
|
879 |
+
def predict_next_value(df: pd.DataFrame, pred_index: str = 'nt_yoy', pred_num:int = 1. ) -> pd.DataFrame:
|
880 |
+
"""
|
881 |
+
Predict the next n values of a specific column in the DataFrame using linear regression.
|
882 |
+
|
883 |
+
Parameters:
|
884 |
+
df (pandas.DataFrame): The input DataFrame.
|
885 |
+
pred_index (str): The name of the column to predict.
|
886 |
+
pred_num (int): The number of future values to predict.
|
887 |
+
|
888 |
+
Returns:
|
889 |
+
pandas.DataFrame: The DataFrame with the predicted values appended to the specified column
|
890 |
+
and other columns filled as pred+index.
|
891 |
+
"""
|
892 |
+
input_array = df[pred_index].values
|
893 |
+
|
894 |
+
# Convert the input array into the desired format.
|
895 |
+
x = np.array(range(len(input_array))).reshape(-1, 1)
|
896 |
+
y = input_array.reshape(-1, 1)
|
897 |
+
|
898 |
+
# Train a linear regression model.
|
899 |
+
model = LinearRegression()
|
900 |
+
model.fit(x, y)
|
901 |
+
|
902 |
+
# Predict the future n values.
|
903 |
+
next_indices = np.array(range(len(input_array), len(input_array) + pred_num)).reshape(-1, 1)
|
904 |
+
predicted_values = model.predict(next_indices).flatten()
|
905 |
+
|
906 |
+
for i, value in enumerate(predicted_values, 1):
|
907 |
+
row_data = {pred_index: value}
|
908 |
+
for other_col in df.columns:
|
909 |
+
if other_col != pred_index:
|
910 |
+
row_data[other_col] = 'pred' + str(i)
|
911 |
+
df = df.append(row_data, ignore_index=True)
|
912 |
+
|
913 |
+
# Return the updated DataFrame
|
914 |
+
return df
|
915 |
+
|
916 |
+
|
917 |
+
|
918 |
+
|
919 |
+
|
920 |
+
|
921 |
+
def get_latest_new_from_web(src: str = 'sina') -> pd.DataFrame:
|
922 |
+
|
923 |
+
# 新浪财经 sina 获取新浪财经实时资讯
|
924 |
+
# 同花顺 10jqka 同花顺财经新闻
|
925 |
+
# 东方财富 eastmoney 东方财富财经新闻
|
926 |
+
# 云财经 yuncaijing 云财经新闻
|
927 |
+
"""
|
928 |
+
Retrieves the latest news data from major news websites, including Sina Finance, 10jqka, Eastmoney, and Yuncaijing.
|
929 |
+
|
930 |
+
Args:
|
931 |
+
src (str): The name of the news website. Default is 'sina'. Optional parameters include: 'sina' for Sina Finance,
|
932 |
+
'10jqka' for 10jqka, 'eastmoney' for Eastmoney, and 'yuncaijing' for Yuncaijing.
|
933 |
+
|
934 |
+
Returns:
|
935 |
+
pd.DataFrame: A DataFrame containing the news data, including two columns for date/time and content.
|
936 |
+
"""
|
937 |
+
|
938 |
+
df = pro.news(**{
|
939 |
+
"start_date": '',
|
940 |
+
"end_date": '',
|
941 |
+
"src": src,
|
942 |
+
"limit": "",
|
943 |
+
"offset": ""
|
944 |
+
}, fields=[
|
945 |
+
"datetime",
|
946 |
+
"content",
|
947 |
+
])
|
948 |
+
df = df.apply(lambda x: '[' + x.name + ']' + ': ' + x.astype(str))
|
949 |
+
return df
|
950 |
+
|
951 |
+
|
952 |
+
# def show_dynamic_table(df: pd.DataFrame) -> None:
|
953 |
+
# '''
|
954 |
+
# This function displays a dynamic table in the terminal window, where each row of the input DataFrame is shown one by one.
|
955 |
+
# Arguments:
|
956 |
+
# df: A Pandas DataFrame containing the data to be displayed in the dynamic table.
|
957 |
+
#
|
958 |
+
# Returns: None. This function does not return anything.
|
959 |
+
#
|
960 |
+
# '''
|
961 |
+
#
|
962 |
+
# return df
|
963 |
+
# # table = PrettyTable(df.columns.tolist(),align='l')
|
964 |
+
#
|
965 |
+
# # 将 DataFrame 的数据添加到表格中
|
966 |
+
# # for row in df.itertuples(index=False):
|
967 |
+
# # table.add_row(row)
|
968 |
+
#
|
969 |
+
# # 初始化终端
|
970 |
+
# # term = Terminal()
|
971 |
+
# #
|
972 |
+
# # # 在终端窗口中滚动显示表格
|
973 |
+
# # with term.fullscreen():
|
974 |
+
# # with term.cbreak():
|
975 |
+
# # print(term.clear())
|
976 |
+
# # with term.location(0, 0):
|
977 |
+
# # # 将表格分解为多行,并遍历每一行
|
978 |
+
# # lines = str(table).split('\n')
|
979 |
+
# # for i, line in enumerate(lines):
|
980 |
+
# # with term.location(0, i):
|
981 |
+
# # print(line)
|
982 |
+
# # time.sleep(1)
|
983 |
+
# #
|
984 |
+
# # while True:
|
985 |
+
# # # 读取输入
|
986 |
+
# # key = term.inkey(timeout=0.1)
|
987 |
+
# #
|
988 |
+
# # # 如果收到q键,则退出
|
989 |
+
# # if key.lower() == 'q':
|
990 |
+
# # break
|
991 |
+
|
992 |
+
|
993 |
+
def get_index_constituent(index_name: str = '', start_date:str ='', end_date:str ='') -> pd.DataFrame:
|
994 |
+
"""
|
995 |
+
Query the constituent stocks of basic index (中证500) or a specified SW (申万) industry index
|
996 |
+
|
997 |
+
args:
|
998 |
+
index_name: the name of the index.
|
999 |
+
start_date: the start date in "YYYYMMDD".
|
1000 |
+
end_date: the end date in "YYYYMMDD".
|
1001 |
+
|
1002 |
+
return:
|
1003 |
+
A pandas DataFrame containing the following columns:
|
1004 |
+
index_code
|
1005 |
+
index_name
|
1006 |
+
stock_code: the code of the constituent stock.
|
1007 |
+
stock_name: the name of the constituent stock.
|
1008 |
+
weight: the weight of the constituent stock.
|
1009 |
+
"""
|
1010 |
+
|
1011 |
+
if '申万' in index_name:
|
1012 |
+
if '申万一级行业' in index_name:
|
1013 |
+
# index_name取后面的名字
|
1014 |
+
index_name = index_name[6:]
|
1015 |
+
df1 = pd.read_csv('SW2021_industry_L1.csv')
|
1016 |
+
index_code = df1[df1['industry_name'] == index_name]['index_code'].iloc[0]
|
1017 |
+
elif '申万二级行业' in index_name:
|
1018 |
+
index_name = index_name[6:]
|
1019 |
+
df1 = pd.read_csv('SW2021_industry_L2.csv')
|
1020 |
+
index_code = df1[df1['industry_name'] == index_name]['index_code'].iloc[0]
|
1021 |
+
elif '申万三级行业' in index_name:
|
1022 |
+
index_name = index_name[6:]
|
1023 |
+
df1 = pd.read_csv('SW2021_industry_L3.csv')
|
1024 |
+
index_code = df1[df1['industry_name'] == index_name]['index_code'].iloc[0]
|
1025 |
+
|
1026 |
+
print('The industry code for ', index_name, ' is: ', index_code)
|
1027 |
+
|
1028 |
+
# 拉取数据
|
1029 |
+
df = pro.index_member(**{
|
1030 |
+
"index_code": index_code , #'851251.SI'
|
1031 |
+
"is_new": "",
|
1032 |
+
"ts_code": "",
|
1033 |
+
"limit": "",
|
1034 |
+
"offset": ""
|
1035 |
+
}, fields=[
|
1036 |
+
"index_code",
|
1037 |
+
"con_code",
|
1038 |
+
"in_date",
|
1039 |
+
"out_date",
|
1040 |
+
"is_new",
|
1041 |
+
"index_name",
|
1042 |
+
"con_name"
|
1043 |
+
])
|
1044 |
+
#
|
1045 |
+
# For each stock, filter the start_date and end_date that are between in_date and out_date.
|
1046 |
+
df = df[(df['in_date'] <= start_date)]
|
1047 |
+
df = df[(df['out_date'] >= end_date) | (df['out_date'].isnull())]
|
1048 |
+
|
1049 |
+
|
1050 |
+
|
1051 |
+
df.rename(columns={'con_code': 'stock_code'}, inplace=True)
|
1052 |
+
|
1053 |
+
df.rename(columns={'con_name': 'stock_name'}, inplace=True)
|
1054 |
+
#
|
1055 |
+
df['weight'] = np.nan
|
1056 |
+
|
1057 |
+
df = df[['index_code', "index_name", 'stock_code', 'stock_name','weight']]
|
1058 |
+
|
1059 |
+
else: # 宽基指数
|
1060 |
+
df1 = pro.index_basic(**{
|
1061 |
+
"ts_code": "",
|
1062 |
+
"market": "",
|
1063 |
+
"publisher": "",
|
1064 |
+
"category": "",
|
1065 |
+
"name": index_name,
|
1066 |
+
"limit": "",
|
1067 |
+
"offset": ""
|
1068 |
+
}, fields=[
|
1069 |
+
"ts_code",
|
1070 |
+
"name",
|
1071 |
+
])
|
1072 |
+
|
1073 |
+
index_code = df1["ts_code"][0]
|
1074 |
+
print(f'index_code for basic index {index_name} is {index_code}')
|
1075 |
+
|
1076 |
+
|
1077 |
+
# Step 2: Retrieve the constituents of an index based on the index code and given date.
|
1078 |
+
df = pro.index_weight(**{
|
1079 |
+
"index_code": index_code,
|
1080 |
+
"trade_date": '',
|
1081 |
+
"start_date": start_date,
|
1082 |
+
"end_date": end_date,
|
1083 |
+
"limit": "",
|
1084 |
+
"offset": ""
|
1085 |
+
}, fields=[
|
1086 |
+
"index_code",
|
1087 |
+
"con_code",
|
1088 |
+
"trade_date",
|
1089 |
+
"weight"
|
1090 |
+
])
|
1091 |
+
# df = df.sort_values(by='trade_date', ascending=True) #
|
1092 |
+
df['index_name'] = index_name
|
1093 |
+
last_day = df['trade_date'][0]
|
1094 |
+
# for the last trading day
|
1095 |
+
df = df[df['trade_date'] == last_day]
|
1096 |
+
df_stock = pd.read_csv('tushare_stock_basic_20230421210721.csv')
|
1097 |
+
# Merge based on the stock code.
|
1098 |
+
df = pd.merge(df, df_stock, how='left', left_on='con_code', right_on='ts_code')
|
1099 |
+
# df.rename(columns={'name_y': 'name'}, inplace=True)
|
1100 |
+
df = df.drop(columns=['symbol', 'area', 'con_code'])
|
1101 |
+
df.sort_values(by='weight', ascending=False, inplace=True)
|
1102 |
+
df.rename(columns={'name': 'stock_name'}, inplace=True)
|
1103 |
+
df.rename(columns={'ts_code': 'stock_code'}, inplace=True)
|
1104 |
+
df.dropna(axis=0, how='any', inplace=True)
|
1105 |
+
#
|
1106 |
+
df = df[['index_code', "index_name", 'stock_code', 'stock_name', 'weight']]
|
1107 |
+
|
1108 |
+
return df
|
1109 |
+
|
1110 |
+
# Determine whether the given name is a stock or a fund.,
|
1111 |
+
def is_fund(ts_name: str = '') -> bool:
|
1112 |
+
# call get_stock_code()和query_fund_name_or_code()
|
1113 |
+
if get_stock_code(ts_name) is not None and query_fund_name_or_code(ts_name) is None:
|
1114 |
+
return False
|
1115 |
+
elif get_stock_code(ts_name) is None and query_fund_name_or_code(ts_name) is not None:
|
1116 |
+
return True
|
1117 |
+
|
1118 |
+
|
1119 |
+
|
1120 |
+
|
1121 |
+
def calculate_earning_between_two_time(stock_name: str = '', start_date: str = '', end_date: str = '', index: str = 'close') -> float:
|
1122 |
+
"""
|
1123 |
+
Calculates the rate of return for a specified stock/fund between two dates.
|
1124 |
+
|
1125 |
+
Args:
|
1126 |
+
stock_name: stock_name or fund_name
|
1127 |
+
start_date
|
1128 |
+
end_date
|
1129 |
+
index (str): The index used to calculate the stock return, including 'open' and 'close'.
|
1130 |
+
|
1131 |
+
Returns:
|
1132 |
+
float: The rate of return for the specified stock between the two dates.
|
1133 |
+
"""
|
1134 |
+
if is_fund(stock_name):
|
1135 |
+
fund_code = query_fund_name_or_code(stock_name)
|
1136 |
+
stock_data = query_fund_data(fund_code, start_date, end_date)
|
1137 |
+
if index =='':
|
1138 |
+
index = 'adj_nav'
|
1139 |
+
else:
|
1140 |
+
stock_data = get_stock_prices_data(stock_name, start_date, end_date,'daily')
|
1141 |
+
try:
|
1142 |
+
end_price = stock_data.iloc[-1][index]
|
1143 |
+
start_price = stock_data.iloc[0][index]
|
1144 |
+
earning = cal_dt(end_price, start_price)
|
1145 |
+
# earning = round((end_price - start_price) / start_price * 100, 2)
|
1146 |
+
except:
|
1147 |
+
print(ts_code,start_date,end_date)
|
1148 |
+
print('##################### 该股票没有数据 #####################')
|
1149 |
+
return None
|
1150 |
+
# percent = earning * 100
|
1151 |
+
# percent_str = '{:.2f}%'.format(percent)
|
1152 |
+
|
1153 |
+
return earning
|
1154 |
+
|
1155 |
+
|
1156 |
+
def loop_rank(df: pd.DataFrame, func: callable, *args, **kwargs) -> pd.DataFrame:
|
1157 |
+
"""
|
1158 |
+
It iteratively applies the given function to each row and get a result using function. It then stores the calculated result in 'new_feature' column.
|
1159 |
+
|
1160 |
+
Args:
|
1161 |
+
df: DataFrame with a single column
|
1162 |
+
func : The function to be applied to each row: func(row, *args, **kwargs)
|
1163 |
+
*args: Additional positional arguments for `func` function.
|
1164 |
+
**kwargs: Additional keyword arguments for `func` function.
|
1165 |
+
|
1166 |
+
Returns:
|
1167 |
+
pd.DataFrame: A output DataFrame with three columns: the constant column, input column, and new_feature column.
|
1168 |
+
The DataFrame is sorted based on the new_feature column in descending order.
|
1169 |
+
|
1170 |
+
"""
|
1171 |
+
df['new_feature'] = None
|
1172 |
+
loop_var = df.columns[0]
|
1173 |
+
for _, row in df.iterrows():
|
1174 |
+
res = None
|
1175 |
+
var = row[loop_var] #
|
1176 |
+
|
1177 |
+
if var is not None:
|
1178 |
+
if loop_var == 'stock_name':
|
1179 |
+
stock_name = var
|
1180 |
+
elif loop_var == 'stock_code':
|
1181 |
+
stock_name = get_stock_name_from_code(var)
|
1182 |
+
elif loop_var == 'fund_name':
|
1183 |
+
stock_name = var
|
1184 |
+
elif loop_var == 'fund_code':
|
1185 |
+
stock_name = query_fund_name_or_code('',var)
|
1186 |
+
time.sleep(0.4)
|
1187 |
+
try:
|
1188 |
+
res = func(stock_name, *args, **kwargs) #
|
1189 |
+
except:
|
1190 |
+
raise ValueError('#####################Error for func#####################')
|
1191 |
+
# res represents the result obtained for the variable. For example, if the variable is a stock name, res could be the return rate of that stock over a certain period or a specific feature value of that stock. Therefore, res should be a continuous value.
|
1192 |
+
# If the format of res is a float, then it can be used directly. However, if res is in DataFrame format, you can retrieve the value corresponding to the index.
|
1193 |
+
if isinstance(res, pd.DataFrame) and not res.empty:
|
1194 |
+
#
|
1195 |
+
try:
|
1196 |
+
res = round(res.loc[:,args[-1]][0], 2)
|
1197 |
+
df.loc[df[loop_var] == var, 'new_feature'] = res
|
1198 |
+
except:
|
1199 |
+
raise ValueError('##################### Error ######################')
|
1200 |
+
elif isinstance(res, float): #
|
1201 |
+
res = res
|
1202 |
+
df.loc[df[loop_var] == var, 'new_feature'] = res
|
1203 |
+
print(var, res)
|
1204 |
+
|
1205 |
+
|
1206 |
+
# Remove the rows where the new_feature column is empty.
|
1207 |
+
df = df.dropna(subset=['new_feature'])
|
1208 |
+
stock_data = df.sort_values(by='new_feature', ascending=False)
|
1209 |
+
#
|
1210 |
+
stock_data.insert(0, 'unchanged', loop_var)
|
1211 |
+
stock_data = stock_data.loc[:,[stock_data.columns[0], loop_var, 'new_feature']]
|
1212 |
+
|
1213 |
+
return stock_data
|
1214 |
+
|
1215 |
+
def output_mean_median_col(data: pd.DataFrame, col: str = 'new_feature') -> float:
|
1216 |
+
# It calculates the mean and median value for the specified column.
|
1217 |
+
|
1218 |
+
mean = round(data[col].mean(), 2)
|
1219 |
+
median = round(data[col].median(), 2)
|
1220 |
+
#
|
1221 |
+
#print(title, mean)
|
1222 |
+
return (mean, median)
|
1223 |
+
|
1224 |
+
|
1225 |
+
# def output_median_col(data: pd.DataFrame, col: str, title_name: str = '') -> float:
|
1226 |
+
# # It calculates the median value for the specified column and returns the median as a float value.
|
1227 |
+
#
|
1228 |
+
# median = round(data[col].median(), 2)
|
1229 |
+
# #print(title_name, median)
|
1230 |
+
#
|
1231 |
+
# return median
|
1232 |
+
|
1233 |
+
|
1234 |
+
def output_weighted_mean_col(data: pd.DataFrame, col: str, weight_col: pd.Series) -> float:
|
1235 |
+
|
1236 |
+
"""
|
1237 |
+
Calculates the weighted mean of a column and returns the result as a float.
|
1238 |
+
|
1239 |
+
Args:
|
1240 |
+
data (pd.DataFrame): The input cross-sectional or time-series data containing the feature columns.
|
1241 |
+
col (str): The name of the feature column to calculate the weighted mean for.
|
1242 |
+
weight_col (pd.Series): The weights used for the calculation, as a pandas Series.
|
1243 |
+
|
1244 |
+
Returns:
|
1245 |
+
float: The weighted mean of the specified feature column.
|
1246 |
+
"""
|
1247 |
+
|
1248 |
+
weighted_mean = round(np.average(data[col], weights = weight_col)/100., 2)
|
1249 |
+
return weighted_mean
|
1250 |
+
|
1251 |
+
|
1252 |
+
|
1253 |
+
def get_index_data(index_name: str = '', start_date: str = '', end_date: str = '', freq: str = 'daily') -> pd.DataFrame:
|
1254 |
+
"""
|
1255 |
+
This function retrieves daily, weekly, or monthly data for a given stock index.
|
1256 |
+
|
1257 |
+
Arguments:
|
1258 |
+
- index_name: Name of the index
|
1259 |
+
- start_date: Start date in 'YYYYMMDD'
|
1260 |
+
- end_date: End date in 'YYYYMMDD'
|
1261 |
+
- freq: Frequency 'daily', 'weekly', or 'monthly'
|
1262 |
+
|
1263 |
+
Returns:
|
1264 |
+
A DataFrame containing the following columns:
|
1265 |
+
trade_date, ts_code, close, open, high, low, pre_close: Previous day's closing price, change(涨跌额), pct_chg(涨跌幅), vol(成交量), amount(成交额), name: Index Name
|
1266 |
+
"""
|
1267 |
+
df1 = pro.index_basic(**{
|
1268 |
+
"ts_code": "",
|
1269 |
+
"market": "",
|
1270 |
+
"publisher": "",
|
1271 |
+
"category": "",
|
1272 |
+
"name": index_name,
|
1273 |
+
"limit": "",
|
1274 |
+
"offset": ""
|
1275 |
+
}, fields=[
|
1276 |
+
"ts_code",
|
1277 |
+
"name",
|
1278 |
+
])
|
1279 |
+
|
1280 |
+
index_code = df1["ts_code"][0]
|
1281 |
+
print(f'index_code for index {index_name} is {index_code}')
|
1282 |
+
#
|
1283 |
+
if freq == 'daily':
|
1284 |
+
df = pro.index_daily(**{
|
1285 |
+
"ts_code": index_code,
|
1286 |
+
"trade_date": '',
|
1287 |
+
"start_date": start_date,
|
1288 |
+
"end_date": end_date,
|
1289 |
+
"limit": "",
|
1290 |
+
"offset": ""
|
1291 |
+
}, fields=[
|
1292 |
+
"trade_date",
|
1293 |
+
"ts_code",
|
1294 |
+
"close",
|
1295 |
+
"open",
|
1296 |
+
"high",
|
1297 |
+
"low",
|
1298 |
+
"pre_close",
|
1299 |
+
"change",
|
1300 |
+
"pct_chg",
|
1301 |
+
"vol",
|
1302 |
+
"amount"
|
1303 |
+
])
|
1304 |
+
elif freq == 'weekly':
|
1305 |
+
df = pro.index_weekly(**{
|
1306 |
+
"ts_code": index_code,
|
1307 |
+
"trade_date": '',
|
1308 |
+
"start_date": start_date,
|
1309 |
+
"end_date": end_date,
|
1310 |
+
"limit": "",
|
1311 |
+
"offset": ""
|
1312 |
+
}, fields=[
|
1313 |
+
"trade_date",
|
1314 |
+
"ts_code",
|
1315 |
+
"close",
|
1316 |
+
"open",
|
1317 |
+
"high",
|
1318 |
+
"low",
|
1319 |
+
"pre_close",
|
1320 |
+
"change",
|
1321 |
+
"pct_chg",
|
1322 |
+
"vol",
|
1323 |
+
"amount"
|
1324 |
+
])
|
1325 |
+
elif freq == 'monthly':
|
1326 |
+
df = pro.index_monthly(**{
|
1327 |
+
"ts_code": index_code,
|
1328 |
+
"trade_date": '',
|
1329 |
+
"start_date": start_date,
|
1330 |
+
"end_date": end_date,
|
1331 |
+
"limit": "",
|
1332 |
+
"offset": ""
|
1333 |
+
}, fields=[
|
1334 |
+
"trade_date",
|
1335 |
+
"ts_code",
|
1336 |
+
"close",
|
1337 |
+
"open",
|
1338 |
+
"high",
|
1339 |
+
"low",
|
1340 |
+
"pre_close",
|
1341 |
+
"change",
|
1342 |
+
"pct_chg",
|
1343 |
+
"vol",
|
1344 |
+
"amount"
|
1345 |
+
])
|
1346 |
+
|
1347 |
+
df = df.sort_values(by='trade_date', ascending=True) #
|
1348 |
+
df['index_name'] = index_name
|
1349 |
+
return df
|
1350 |
+
|
1351 |
+
|
1352 |
+
|
1353 |
+
|
1354 |
+
|
1355 |
+
def get_north_south_money(start_date: str = '', end_date: str = '', trade_date: str = '') -> pd.DataFrame:
|
1356 |
+
#
|
1357 |
+
# trade_date: 交易日期
|
1358 |
+
# ggt_ss: 港股通(上海)
|
1359 |
+
# ggt_sz: 港股通(深圳)
|
1360 |
+
# hgt: 沪股通(亿元)
|
1361 |
+
# sgt: 深股通(亿元)
|
1362 |
+
# north_money: 北向资金(亿元)= hgt + sgt
|
1363 |
+
# south_money: 南向资金(亿元)= ggt_ss + ggt_sz
|
1364 |
+
# name: 固定为'A-H',代表A股和H股
|
1365 |
+
# accumulate_north_money: 累计北向资金流入
|
1366 |
+
# accumulate_south_money: 累计南向资金流入
|
1367 |
+
|
1368 |
+
|
1369 |
+
month_df = pro.moneyflow_hsgt(**{
|
1370 |
+
"trade_date": trade_date,
|
1371 |
+
"start_date": start_date,
|
1372 |
+
"end_date": end_date,
|
1373 |
+
"limit": "",
|
1374 |
+
"offset": ""
|
1375 |
+
}, fields=[
|
1376 |
+
"trade_date",
|
1377 |
+
"ggt_ss",
|
1378 |
+
"ggt_sz",
|
1379 |
+
"hgt",
|
1380 |
+
"sgt",
|
1381 |
+
"north_money",
|
1382 |
+
"south_money"
|
1383 |
+
])
|
1384 |
+
|
1385 |
+
month_df[['ggt_ss','ggt_sz','hgt','sgt','north_money','south_money']] = month_df[['ggt_ss','ggt_sz','hgt','sgt','north_money','south_money']]/100.0
|
1386 |
+
month_df = month_df.sort_values(by='trade_date', ascending=True) #
|
1387 |
+
month_df['stock_name'] = 'A-H'
|
1388 |
+
month_df['accumulate_north_money'] = month_df['north_money'].cumsum()
|
1389 |
+
month_df['accumulate_south_money'] = month_df['south_money'].cumsum()
|
1390 |
+
return month_df
|
1391 |
+
|
1392 |
+
|
1393 |
+
|
1394 |
+
def plot_k_line(stock_data: pd.DataFrame, title: str = '') -> None:
|
1395 |
+
"""
|
1396 |
+
Plots a K-line chart of stock price and volume.
|
1397 |
+
|
1398 |
+
Args:
|
1399 |
+
stock_data : A pandas DataFrame containing the stock price information, in which each row
|
1400 |
+
represents a daily record. The DataFrame must contain the 'trade_date','open', 'close', 'high', 'low','volume', 'name' columns, which is used for k-line and volume.
|
1401 |
+
如果dataframe中还含有'macd','kdj', 'rsi', 'cci', 'boll','pe_ttm','turnover_rate'等列,则在k线图下方绘制这些指标的子图.
|
1402 |
+
title : The title of the K-line chart.
|
1403 |
+
|
1404 |
+
Returns:
|
1405 |
+
None
|
1406 |
+
"""
|
1407 |
+
|
1408 |
+
#
|
1409 |
+
stock_data['trade_date'] = pd.to_datetime(stock_data['trade_date'], format='%Y%m%d')
|
1410 |
+
stock_data.set_index('trade_date', inplace=True)
|
1411 |
+
#
|
1412 |
+
custom_style = mpf.make_marketcolors(up='r', down='k', inherit=True)
|
1413 |
+
china_style = mpf.make_mpf_style(marketcolors=custom_style)
|
1414 |
+
|
1415 |
+
# MACD
|
1416 |
+
# stock_data['macd1'] = stock_data['Close'].ewm(span=12).mean() - stock_data['Close'].ewm(span=26).mean()
|
1417 |
+
# stock_data['macd_signal1'] = stock_data['macd'].ewm(span=9).mean()
|
1418 |
+
|
1419 |
+
#
|
1420 |
+
#mpf.plot(stock_data, type='candle', volume=True, title=title, mav=(5, 10, 20), style = china_style, addplot = macd)
|
1421 |
+
add_plot = []
|
1422 |
+
# The index column is located after the name column in the last few columns.
|
1423 |
+
# Retrieve the column names after the 'name' column.
|
1424 |
+
index_list = stock_data.columns[stock_data.columns.get_loc('stock_name')+1:]
|
1425 |
+
|
1426 |
+
index_df = stock_data[index_list]
|
1427 |
+
|
1428 |
+
color_list = ['green','blue','red','yellow','black','purple','orange','pink','brown','gray']
|
1429 |
+
custom_lines = []
|
1430 |
+
for i in range(len(index_list)):
|
1431 |
+
# If the column names contain 'boll', set panel to 0. Otherwise, set panel to 2.
|
1432 |
+
if 'boll' in index_list[i]:
|
1433 |
+
sub_plot = mpf.make_addplot(index_df[index_list[i]], panel=0, ylabel=index_list[i], color=color_list[i], type='line', secondary_y=True)
|
1434 |
+
elif index_list[i] =='macd':
|
1435 |
+
sub_plot = mpf.make_addplot(index_df[index_list[i]], panel=2, ylabel=index_list[i], color=color_list[i], type='bar', secondary_y=False)
|
1436 |
+
|
1437 |
+
else:
|
1438 |
+
sub_plot = mpf.make_addplot(index_df[index_list[i]], panel=2, ylabel=index_list[i], color=color_list[i], type='line', secondary_y=False)
|
1439 |
+
|
1440 |
+
custom_line = Line2D([0], [0], color=color_list[i], lw=1, linestyle='dashed')
|
1441 |
+
|
1442 |
+
|
1443 |
+
add_plot.append(sub_plot)
|
1444 |
+
custom_lines.append(custom_line)
|
1445 |
+
|
1446 |
+
mav_colors = ['red', 'green', 'blue']
|
1447 |
+
|
1448 |
+
fig, axes = mpf.plot(stock_data, type='candle', volume=True, title=title, mav=(5, 10, 20), mavcolors=mav_colors, style=china_style, addplot=add_plot, returnfig=True)
|
1449 |
+
|
1450 |
+
|
1451 |
+
mav_labels = ['5-day MA', '10-day MA', '20-day MA']
|
1452 |
+
#
|
1453 |
+
legend_lines = [plt.Line2D([0], [0], color=color, lw=2) for color in mav_colors]
|
1454 |
+
|
1455 |
+
#
|
1456 |
+
axes[0].legend(legend_lines, mav_labels)
|
1457 |
+
|
1458 |
+
if len(index_list) ==1:
|
1459 |
+
label = index_list[0]
|
1460 |
+
elif len(index_list) > 1:
|
1461 |
+
label_list = [i.split('_')[0] for i in index_list]
|
1462 |
+
#
|
1463 |
+
label = list(set(label_list))[0]
|
1464 |
+
|
1465 |
+
if len(index_list) >= 1:
|
1466 |
+
if 'boll' in label:
|
1467 |
+
axes[0].legend(custom_lines, index_list, loc='lower right')
|
1468 |
+
|
1469 |
+
elif len(index_list) > 1:
|
1470 |
+
axes[-2].set_ylabel(label)
|
1471 |
+
axes[-2].legend(custom_lines, index_list, loc='lower right')
|
1472 |
+
|
1473 |
+
#
|
1474 |
+
fig.set_size_inches(20, 16)
|
1475 |
+
#
|
1476 |
+
for ax in axes:
|
1477 |
+
ax.grid(True)
|
1478 |
+
|
1479 |
+
#fig.show()
|
1480 |
+
return axes
|
1481 |
+
|
1482 |
+
|
1483 |
+
def cal_dt(num_at_time_2: float = 0.0, num_at_time_1: float = 0.0) -> float:
|
1484 |
+
"""
|
1485 |
+
This function calculates the percentage change of a metric from one time to another.
|
1486 |
+
|
1487 |
+
Args:
|
1488 |
+
- num_at_time_2: the metric value at time 2 (end time)
|
1489 |
+
- num_at_time_1: the metric value at time 1 (start time)
|
1490 |
+
|
1491 |
+
Returns:
|
1492 |
+
- float: the percentage change of the metric from time 1 to time 2
|
1493 |
+
|
1494 |
+
"""
|
1495 |
+
if num_at_time_1 == 0:
|
1496 |
+
num_at_time_1 = 0.0000000001
|
1497 |
+
return round((num_at_time_2 - num_at_time_1) / num_at_time_1, 4)
|
1498 |
+
|
1499 |
+
|
1500 |
+
def query_fund_info(fund_code: str = '') -> pd.DataFrame:
|
1501 |
+
#
|
1502 |
+
# fund_code str Y 基金代码 # fund_name str Y 简称 # management str Y 管理人 # custodian str Y 托管人 # fund_type str Y 投资类型 # found_date str Y 成立日期 # due_date str Y 到期日期 # list_date str Y 上市时间 # issue_date str Y 发行日期 # delist_date str Y 退市日期 # issue_amount float Y 发行份额(亿) # m_fee float Y 管理费 # c_fee float Y 托管费
|
1503 |
+
# duration_year float Y 存续期 # p_value float Y 面值 # min_amount float Y 起点金额(万元) # benchmark str Y 业绩比较基准 # status str Y 存续状态D摘牌 I发行 L已上市 # invest_type str Y 投资风格 # type str Y 基金类型 # purc_startdate str Y 日常申购起始日 # redm_startdate str Y 日常赎回起始日 # market str Y E场内O场外
|
1504 |
+
"""
|
1505 |
+
Retrieves information about a fund based on the fund code.
|
1506 |
+
|
1507 |
+
Args:
|
1508 |
+
fund_code (str, optional): Fund code. Defaults to ''.
|
1509 |
+
|
1510 |
+
Returns:
|
1511 |
+
df (DataFrame): A DataFrame containing various information about the fund, including fund code, fund name,
|
1512 |
+
management company, custodian company, investment type, establishment date, maturity date,
|
1513 |
+
listing date, issuance date, delisting date, issue amount, management fee, custodian fee,
|
1514 |
+
fund duration, face value, minimum investment amount, benchmark, fund status, investment style,
|
1515 |
+
fund type, start date for daily purchases, start date for daily redemptions, and market type.
|
1516 |
+
The column 'ts_code' is renamed to 'fund_code', and 'name' is renamed to 'fund_name' in the DataFrame.
|
1517 |
+
"""
|
1518 |
+
df = pro.fund_basic(**{
|
1519 |
+
"ts_code": fund_code,
|
1520 |
+
"market": "",
|
1521 |
+
"update_flag": "",
|
1522 |
+
"offset": "",
|
1523 |
+
"limit": "",
|
1524 |
+
"status": "",
|
1525 |
+
"name": ""
|
1526 |
+
}, fields=[
|
1527 |
+
"ts_code",
|
1528 |
+
"name",
|
1529 |
+
"management",
|
1530 |
+
"custodian",
|
1531 |
+
"fund_type",
|
1532 |
+
"found_date",
|
1533 |
+
"due_date",
|
1534 |
+
"list_date",
|
1535 |
+
"issue_date",
|
1536 |
+
"delist_date",
|
1537 |
+
"issue_amount",
|
1538 |
+
"m_fee",
|
1539 |
+
"c_fee",
|
1540 |
+
"duration_year",
|
1541 |
+
"p_value",
|
1542 |
+
"min_amount",
|
1543 |
+
"benchmark",
|
1544 |
+
"status",
|
1545 |
+
"invest_type",
|
1546 |
+
"type",
|
1547 |
+
"purc_startdate",
|
1548 |
+
"redm_startdate",
|
1549 |
+
"market"
|
1550 |
+
])
|
1551 |
+
#
|
1552 |
+
df.rename(columns={'ts_code': 'fund_code'}, inplace=True)
|
1553 |
+
df.rename(columns={'name': 'fund_name'}, inplace=True)
|
1554 |
+
return df
|
1555 |
+
|
1556 |
+
def query_fund_data(fund_code: str = '', start_date: str = '', end_date: str = '') -> pd.DataFrame:
|
1557 |
+
#
|
1558 |
+
# ts_code str Y TS代码 # ann_date str Y 公告日期 # nav_date str Y 净值日期 # unit_nav float Y 单位净值 # accum_nav float Y 累计净值
|
1559 |
+
# accum_div float Y 累计分红 # net_asset float Y 资产净值 # total_netasset float Y 合计资产净值 # adj_nav float Y 复权单位净值 pct_chg 每日涨跌幅
|
1560 |
+
"""
|
1561 |
+
Retrieves fund data based on the fund code, start date, and end date.
|
1562 |
+
|
1563 |
+
Args:
|
1564 |
+
fund_code (str, optional): Fund code. Defaults to ''.
|
1565 |
+
start_date (str, optional): Start date in YYYYMMDD format. Defaults to ''.
|
1566 |
+
end_date (str, optional): End date in YYYYMMDD format. Defaults to ''.
|
1567 |
+
|
1568 |
+
Returns:
|
1569 |
+
df (DataFrame): A DataFrame containing fund data, including TS code, announcement date, net asset value date,
|
1570 |
+
unit net asset value, accumulated net asset value, accumulated dividends, net asset value,
|
1571 |
+
total net asset value, adjusted unit net asset value, and fund name. The 'ts_code' column is renamed
|
1572 |
+
to 'fund_code', 'nav_date' is renamed to 'trade_date', and the DataFrame is sorted by the trade date
|
1573 |
+
in ascending order. If the fund code does not exist, None is returned.
|
1574 |
+
"""
|
1575 |
+
df = pro.fund_nav(**{
|
1576 |
+
"ts_code": fund_code,
|
1577 |
+
"nav_date": "",
|
1578 |
+
"offset": "",
|
1579 |
+
"limit": "",
|
1580 |
+
"market": "",
|
1581 |
+
"start_date": start_date,
|
1582 |
+
"end_date": end_date
|
1583 |
+
}, fields=[
|
1584 |
+
"ts_code",
|
1585 |
+
"ann_date",
|
1586 |
+
"nav_date",
|
1587 |
+
"unit_nav",
|
1588 |
+
"accum_nav",
|
1589 |
+
"accum_div",
|
1590 |
+
"net_asset",
|
1591 |
+
"total_netasset",
|
1592 |
+
"adj_nav",
|
1593 |
+
"update_flag"
|
1594 |
+
])
|
1595 |
+
try:
|
1596 |
+
fund_name= query_fund_name_or_code(fund_code=fund_code)
|
1597 |
+
df['fund_name'] = fund_name
|
1598 |
+
#
|
1599 |
+
df.rename(columns={'ts_code': 'fund_code'}, inplace=True)
|
1600 |
+
df.rename(columns={'nav_date': 'trade_date'}, inplace=True)
|
1601 |
+
df.sort_values(by='trade_date', ascending=True, inplace=True)
|
1602 |
+
except:
|
1603 |
+
print(fund_code,'基金代码不存在')
|
1604 |
+
return None
|
1605 |
+
#
|
1606 |
+
df['pct_chg'] = df['adj_nav'].pct_change()
|
1607 |
+
#
|
1608 |
+
df.loc[0, 'pct_chg'] = 0.0
|
1609 |
+
|
1610 |
+
|
1611 |
+
return df
|
1612 |
+
|
1613 |
+
def query_fund_name_or_code(fund_name: str = '', fund_code: str = '') -> str:
|
1614 |
+
#
|
1615 |
+
"""
|
1616 |
+
Retrieves the fund code based on the fund name or Retrieves the fund name based on the fund code.
|
1617 |
+
|
1618 |
+
Args:
|
1619 |
+
fund_name (str, optional): Fund name. Defaults to ''.
|
1620 |
+
fund_code (str, optional): Fund code. Defaults to ''.
|
1621 |
+
|
1622 |
+
Returns:
|
1623 |
+
code or name: Fund code if fund_name is provided and fund_code is empty. Fund name if fund_code is provided and fund_name is empty.
|
1624 |
+
"""
|
1625 |
+
|
1626 |
+
|
1627 |
+
#df = pd.read_csv('./tushare_fund_basic_20230508193747.csv')
|
1628 |
+
# Query the fund code based on the fund name.
|
1629 |
+
if fund_name != '' and fund_code == '':
|
1630 |
+
#
|
1631 |
+
df = pd.read_csv('./tushare_fund_basic_all.csv')
|
1632 |
+
#
|
1633 |
+
# df = pro.fund_basic(**{
|
1634 |
+
# "ts_code": "",
|
1635 |
+
# "market": "",
|
1636 |
+
# "update_flag": "",
|
1637 |
+
# "offset": "",
|
1638 |
+
# "limit": "",
|
1639 |
+
# "status": "",
|
1640 |
+
# "name": fund_name
|
1641 |
+
# }, fields=[
|
1642 |
+
# "ts_code",
|
1643 |
+
# "name"
|
1644 |
+
# ])
|
1645 |
+
try:
|
1646 |
+
#
|
1647 |
+
code = df[df['name'] == fund_name]['ts_code'].values[0]
|
1648 |
+
except:
|
1649 |
+
#print(fund_name,'基金名称不存在')
|
1650 |
+
return None
|
1651 |
+
return code
|
1652 |
+
# Query the fund name based on the fund code.
|
1653 |
+
if fund_code != '' and fund_name == '':
|
1654 |
+
df = pd.read_csv('./tushare_fund_basic_all.csv')
|
1655 |
+
try:
|
1656 |
+
name = df[df['ts_code'] == fund_code]['name'].values[0]
|
1657 |
+
except:
|
1658 |
+
#print(fund_code,'基金代码不存在')
|
1659 |
+
return None
|
1660 |
+
return name
|
1661 |
+
|
1662 |
+
|
1663 |
+
|
1664 |
+
def print_save_table(df: pd.DataFrame, title_name: str, save:bool = False ,file_path: str = './output/') -> None:
|
1665 |
+
"""
|
1666 |
+
It prints the dataframe as a formatted table using the PrettyTable library and saves it to a CSV file at the specified file path.
|
1667 |
+
|
1668 |
+
Args:
|
1669 |
+
- df: the dataframe to be printed and saved to a CSV file
|
1670 |
+
- title_name: the name of the table to be printed and saved
|
1671 |
+
- save: whether to save the table to a CSV file
|
1672 |
+
- file_path: the file path where the CSV file should be saved.
|
1673 |
+
|
1674 |
+
Returns: None
|
1675 |
+
"""
|
1676 |
+
|
1677 |
+
# 创建表格table.max_width = 20
|
1678 |
+
|
1679 |
+
# table = PrettyTable(df.columns.tolist())
|
1680 |
+
# table.align = 'l'
|
1681 |
+
# table.max_width = 40
|
1682 |
+
#
|
1683 |
+
# #
|
1684 |
+
# for row in df.itertuples(index=False):
|
1685 |
+
# table.add_row(row)
|
1686 |
+
|
1687 |
+
#print(table)
|
1688 |
+
|
1689 |
+
|
1690 |
+
if not os.path.exists(file_path):
|
1691 |
+
os.makedirs(file_path)
|
1692 |
+
|
1693 |
+
if file_path is not None and save == True:
|
1694 |
+
file_path = file_path + title_name + '.csv'
|
1695 |
+
df.to_csv(file_path, index=False)
|
1696 |
+
return df
|
1697 |
+
|
1698 |
+
|
1699 |
+
|
1700 |
+
#
|
1701 |
+
def merge_indicator_for_same_stock(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:
|
1702 |
+
"""
|
1703 |
+
Merges two DataFrames (two indicators of the same stock) based on common names for same stock. Data from two different stocks cannot be merged
|
1704 |
+
|
1705 |
+
Args:
|
1706 |
+
df1: DataFrame contains some indicators for stock A.
|
1707 |
+
df2: DataFrame contains other indicators for stock A.
|
1708 |
+
|
1709 |
+
Returns:
|
1710 |
+
pd.DataFrame: The merged DataFrame contains two different indicators.
|
1711 |
+
"""
|
1712 |
+
if len(set(df1.columns).intersection(set(df2.columns))) > 0:
|
1713 |
+
# If there are identical column names, merge the two DataFrames based on the matching column names.
|
1714 |
+
#
|
1715 |
+
common_cols = list(set(df1.columns).intersection(set(df2.columns)))
|
1716 |
+
#
|
1717 |
+
df = pd.merge(df1, df2, on=common_cols)
|
1718 |
+
return df
|
1719 |
+
else:
|
1720 |
+
#
|
1721 |
+
raise ValueError('The two dataframes have no columns in common.')
|
1722 |
+
|
1723 |
+
def select_value_by_column(df1:pd.DataFrame, col_name: str = '', row_index: int = -1) -> Union[pd.DataFrame, Any]:
|
1724 |
+
"""
|
1725 |
+
Selects a specific column or a specific value within a DataFrame.
|
1726 |
+
|
1727 |
+
Args:
|
1728 |
+
df1: The input DataFrame.
|
1729 |
+
col_name: The name of the column to be selected.
|
1730 |
+
row_index: The index of the row to be selected.
|
1731 |
+
|
1732 |
+
Returns:
|
1733 |
+
Union[pd.DataFrame, Any]. row_index=-1: df1[col_name].to_frame() or df1[col_name][row_index]
|
1734 |
+
"""
|
1735 |
+
if row_index == -1:
|
1736 |
+
#
|
1737 |
+
return df1[col_name].to_frame()
|
1738 |
+
else:
|
1739 |
+
#
|
1740 |
+
return df1[col_name][row_index]
|
1741 |
+
|
1742 |
+
|
1743 |
+
|
1744 |
+
if __name__ == "__main__":
|
1745 |
+
stock_name='成都银行'
|
1746 |
+
stock_name2='五粮液'
|
1747 |
+
stock_name3 = '宁德时代'
|
1748 |
+
start = '20230104'
|
1749 |
+
end = '20230504'
|
1750 |
+
fund_name = "华商优势行业" #'易方达蓝筹精选'
|
1751 |
+
|
1752 |
+
start_quarter = '201001'
|
1753 |
+
end_quarter = '202303'
|
1754 |
+
title_name ='上证50成分股收益率'
|
1755 |
+
ax = None
|
1756 |
+
res = is_fund('易方达蓝筹精选')
|
1757 |
+
#_, ax = plt.subplots()
|
1758 |
+
# code = query_fund_name_or_code('华商优势行业')
|
1759 |
+
# ------------step1 数据查询层 获取股票代码
|
1760 |
+
# start_last_year = get_last_year_date(start)
|
1761 |
+
# end_last_year = get_last_year_date(end)
|
1762 |
+
#stock_code = get_stock_code(stock_name)
|
1763 |
+
# name = get_stock_name_from_code(stock_code)
|
1764 |
+
# print(name)
|
1765 |
+
# print(stock_code)
|
1766 |
+
# stock_code2 = get_stock_code(stock_name2)
|
1767 |
+
# stock_code3 = get_stock_code(stock_name3)
|
1768 |
+
# stock_technical_data = get_Financial_data(stock_code, start, end)
|
1769 |
+
# macrodata = get_ppi_data('', start_quarter, end_quarter, 'ppi_yoy')
|
1770 |
+
# index_daily = get_index_data('沪深300',start,end,'daily')
|
1771 |
+
# index_daily2 = get_index_data('中证500',start,end,'daily')
|
1772 |
+
# index_daily3 = get_index_data('中证1000',start,end,'daily')
|
1773 |
+
# index_daily4 = get_index_data('创业板指',start,end,'daily')
|
1774 |
+
#stock_data = get_index_constituent('上证50','20230101','20230508')
|
1775 |
+
# money = get_north_south_money('20230425', '20230426')
|
1776 |
+
# stock_data = get_stock_prices_data(stock_code, start, end)
|
1777 |
+
|
1778 |
+
# stock_data = get_stock_monthly_prices_data("","", "",'20230331')
|
1779 |
+
# stock_data = get_stock_prices_data('', start, end, 'daily')
|
1780 |
+
# fund_df = query_fund_Manager('周海栋')
|
1781 |
+
#
|
1782 |
+
# fund_code = select_value_by_column(fund_df, 'fund_code', -1)
|
1783 |
+
# res_earning = loop_rank(fund_code, calculate_earning_between_two_time, start, end, 'adj_nav')
|
1784 |
+
# print(res_earning)
|
1785 |
+
#fund_code = query_fund_name_or_code(fund_name,'')
|
1786 |
+
|
1787 |
+
|
1788 |
+
#fund_data = query_fund_data(fund_code, start, end)
|
1789 |
+
#df_daily = get_daily_trading_data(stock_code,'20200101', '20230526')
|
1790 |
+
# stock_data2 = get_stock_prices_data(stock_code2, start, end,'daily')
|
1791 |
+
# stock_data3 = get_stock_prices_data(stock_code3, start, end,'daily')
|
1792 |
+
|
1793 |
+
# dynamic_new = get_latest_new_from_web('sina')
|
1794 |
+
#stock_df = get_sw_industry_stock('城商行Ⅱ','L2')
|
1795 |
+
# df_macro = get_cpi_ppi_currency_supply_data('200101','202304','cpi','nt_yoy')
|
1796 |
+
# df_macro = get_cpi_ppi_currency_supply_data('200101','202304','ppi','ppi_yoy')
|
1797 |
+
# df_macro = get_cpi_ppi_currency_supply_data('200101','202304','currency_supply','m2_yoy')
|
1798 |
+
df_gdp = get_GDP_data('2001Q1','2023Q1','gdp_yoy')
|
1799 |
+
df_gdp = predict_next_value(df_gdp, 'gdp_yoy', 4)
|
1800 |
+
#company_df = get_company_info('贵州茅台')
|
1801 |
+
#print_save_table(company_df, '贵州茅台公司信息')
|
1802 |
+
#fin_df = get_Financial_data_from_time_range(stock_code, '20200101', '20230526','roe')
|
1803 |
+
|
1804 |
+
#tech_df = get_stock_technical_data(stock_code, start, end)
|
1805 |
+
|
1806 |
+
|
1807 |
+
|
1808 |
+
# ----------------------------------step2 数据处理层 在截面或者时序数据-------------------------------------------------------
|
1809 |
+
# 提取相应指标, 数据处理, 排序,提取,求差,加工..,
|
1810 |
+
# fund_info = query_fund_info('005827.OF')
|
1811 |
+
# value = select_value_by_column(fund_info, 'fund_name', 0)
|
1812 |
+
#fund_index = calculate_stock_index(fund_data,'adj_nav')
|
1813 |
+
#stock_index = rank_index_cross_section(stock_data, 'pct_chg', -1, False)
|
1814 |
+
#stock_index = calculate_stock_index(stock_data, 'pct_chg')
|
1815 |
+
#stock_index_each_day = calculate_stock_index(money, 'north_money')
|
1816 |
+
#stock_index = calculate_stock_index(fin_df, 'roe')
|
1817 |
+
# stock_index2 = calculate_stock_index(stock_data2, 'Cumulative_Earnings_Rate')
|
1818 |
+
# stock_index3 = calculate_stock_index(stock_data3, 'Cumulative_Earnings_Rate')
|
1819 |
+
# stock_index4 = calculate_stock_index(index_daily4, 'Cumulative_Earnings_Rate')
|
1820 |
+
# stock_index2 = calculate_stock_index(stock_data2, 'Cumulative_Earnings_Rate')
|
1821 |
+
#stock_index = calculate_stock_index(stock_data1, 'close')
|
1822 |
+
#stock_index2 = calculate_stock_index(tech_df, 'macd')
|
1823 |
+
#stock_index1 = calculate_stock_index(stock_data, 'candle_K')
|
1824 |
+
#stock_index2 = calculate_stock_index(df_daily, 'pe_ttm')
|
1825 |
+
#merge_df = merge_data(stock_index1, stock_index2)
|
1826 |
+
#res_earning = loop_rank(stock_data, 'stock_name', calculate_earning_between_two_time, start, end)
|
1827 |
+
# index_profit_yoy = loop_rank(stock_data, 'stock_name', get_Financial_data, start, end, 'profit_dedt')
|
1828 |
+
# index_profit_yoy = loop_rank(stock_data, 'stock_name', get_Financial_data, start, end, 'netprofit_yoy')
|
1829 |
+
|
1830 |
+
#res_earning_top_n = rank_index_cross_section(stock_index, 10, False)
|
1831 |
+
#index_profit_yoy_last = loop_rank(stock_data, 'stock_name', get_Financial_data, start_last_year, end_last_year, 'profit_dedt')
|
1832 |
+
# profit_yoy = calculate_stock_index(stock_technical_data, 'dt_netprofit_yoy')
|
1833 |
+
# accumulate_north_month = calculate_stock_index(money, 'accumulate_south_money')
|
1834 |
+
# accumulate_north_month = calculate_stock_index(res_earning, 'accumulate_south_money')
|
1835 |
+
# stock_code = get_stock_code(stock_name)
|
1836 |
+
# fin_df1 = get_Financial_data_from_time_range(stock_code, '20150101', '20230526', 'roa')
|
1837 |
+
# fin_df2 = get_Financial_data_from_time_range(stock_code, '20150101', '20230526', 'roa')
|
1838 |
+
# ax = plot_stock_data(fin_df1, ax, 'line', title_name)
|
1839 |
+
# ax = plot_stock_data(fin_df2, ax, 'line', title_name)
|
1840 |
+
#stock_data = get_index_constituent('上证50','20220105', '20230505')
|
1841 |
+
# stock_data = get_index_constituent('申万二级行业城商行Ⅱ','20220105', '20220505')
|
1842 |
+
# #stock_list = select_value_by_column(stock_data, 'stock_name', -1)
|
1843 |
+
#
|
1844 |
+
# index_profit_yoy = loop_rank(stock_list, get_Financial_data, start, 'netprofit_yoy')
|
1845 |
+
# median = output_median_col(index_profit_yoy, 'new_feature')
|
1846 |
+
# ax = plot_stock_data(index_profit_yoy, ax, 'bar', '上证50的最近季度归母净利润同比增长率')
|
1847 |
+
|
1848 |
+
|
1849 |
+
|
1850 |
+
|
1851 |
+
|
1852 |
+
|
1853 |
+
# ----------------------------------step3 可视化层:文字,图片,表格等多种模态数据输出-------------------------------------------------------
|
1854 |
+
#ax = plot_stock_data(stock_index, ax, 'line', title_name)
|
1855 |
+
#ax = plot_stock_data(stock_index_each_day, ax, 'bar', title_name)
|
1856 |
+
#print_save_table(fund_info, title_name)
|
1857 |
+
|
1858 |
+
#_, sum_new = output_mean_sum_col(index_profit_yoy,'new_feature')
|
1859 |
+
#_, sum_old = output_mean_sum_col(index_profit_yoy_last,'new_feature')
|
1860 |
+
|
1861 |
+
|
1862 |
+
#print('科创50成分股的最近季度归母净利润同比增长率中位数%:', median)
|
1863 |
+
#dt = cal_dt(sum_new, sum_old)
|
1864 |
+
#print('上证50成分股的最近季度归母净利润同比增长率:',dt)
|
1865 |
+
|
1866 |
+
#plot_k_line(merge_df, title_name)
|
1867 |
+
# ax = plot_stock_data(index_profit_yoy, ax, 'bar', '上证50成分股的最近季度归母净利润同比增长率')
|
1868 |
+
#ax = plot_stock_data(accumulate_north_month, ax, 'line', '2023年1月至4月南向资金累计流向')
|
1869 |
+
|
1870 |
+
# ax2 = plot_stock_data(stock_index2, ax1, 'line', '贵州茅台VS五粮液近十年收益率对比图')
|
1871 |
+
# ax = plot_stock_data(stock_index, ax,'line', title_name)
|
1872 |
+
# ax = plot_stock_data(stock_index2, ax,'line', title_name)
|
1873 |
+
# ax = plot_stock_data(stock_index3, ax,'line', title_name)
|
1874 |
+
# ax = plot_stock_data(stock_index4, ax,'line', title_name)
|
1875 |
+
|
1876 |
+
#ax = plot_stock_data(df_gdp, ax, 'line','2010-2022年国内每季度gdp增速同比')
|
1877 |
+
print_save_table(df_gdp,'GDP预测',True)
|
1878 |
+
|
1879 |
+
# show_dynamic_table(dynamic_new)
|
1880 |
+
|
1881 |
+
|
1882 |
+
# ax = plot_stock_data(res_earning, None, 'bar', '张坤管理各个基金收益率')
|
1883 |
+
# stock_data = get_index_constituent('上证50', '20230101', '20230508')
|
1884 |
+
# stock_list = select_value_by_column(stock_data, 'stock_name', -1)
|
1885 |
+
# res_earning = loop_rank(stock_list, calculate_earning_between_two_time, start, end)
|
1886 |
+
# res_earning_top_n = rank_index_cross_section(res_earnng, 10, False)
|
1887 |
+
# ax = plot_stock_data(res_earning_top_n, ax, 'bar', title_name)
|
1888 |
+
|
1889 |
+
# stock_data = get_index_constituent('上证50', '20230101', '20230508')
|
1890 |
+
# stock_list = select_value_by_column(stock_data, 'stock_name', -1)
|
1891 |
+
# res_earning = loop_rank(stock_list, calculate_earning_between_two_time, '20230101', '20230508')
|
1892 |
+
# res_earning_top_n = rank_index_cross_section(res_earning, 10, False)
|
1893 |
+
# ax = plot_stock_data(res_earning_top_n, ax, 'bar', title_name)
|
1894 |
+
|
1895 |
+
# fund_code = query_fund_name_or_code(fund_name, '')
|
1896 |
+
# fund_data = query_fund_data(fund_code, start, end)
|
1897 |
+
# fund_index = calculate_stock_index(fund_data, 'adj_nav')
|
1898 |
+
# ax = plot_stock_data(fund_index, ax, 'line', title_name)
|
1899 |
+
# fund_df = query_fund_Manager('张坤')
|
1900 |
+
# fund_code = select_value_by_column(fund_df, 'fund_code', -1)
|
1901 |
+
# res_earning = loop_rank(fund_code, calculate_earning_between_two_time, start, end, 'adj_nav')
|
1902 |
+
# ax = plot_stock_data(res_earning, None, 'bar', '张坤管理各个基金收益率')
|
1903 |
+
# company_df = get_company_info('贵州茅台')
|
1904 |
+
# print_save_table(company_df,'gzmt', False)
|
1905 |
+
|
1906 |
+
|
1907 |
+
|
1908 |
+
|
1909 |
+
if ax is not None:
|
1910 |
+
plt.grid()
|
1911 |
+
plt.show()
|
1912 |
+
|
1913 |
+
|
1914 |
+
|
1915 |
+
# xxx基金经理管理的几只基金中,收益率最高的那只基金的规模是多少----找基金经理search,按收益率排序rank,找到收益率最高的那个select,显示基金信息 show
|
1916 |
+
# 食品饮料行业中所有股票近十年涨幅最大的股票的信息----找行业search(行业分类--找到行业代码,根据行业代码找到股票成分), 收益率排序rank,找到涨幅最大的那个select,显示股票信息show
|
1917 |
+
|
1918 |
+
|
1919 |
+
|
1920 |
+
|
1921 |
+
|
1922 |
+
|
1923 |
+
|
1924 |
+
|
1925 |
+
|
1926 |
+
|
1927 |
+
|
1928 |
+
|
tool_lib/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
tool_lib/atomic_api.json
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"pro.daily": {
|
3 |
+
"func_desc": null,
|
4 |
+
"example_input": "pro.daily(ts_code='000001.SZ', start_date='20180701', end_date='20180718')",
|
5 |
+
"example_output": " ts_code trade_date open high low close pre_close change pct_chg vol amount\n0 000001.SZ 20180718 8.75 8.85 8.69 8.70 8.72 -0.02 -0.23 525152.77 460697.377\n1 000001.SZ 20180717 8.74 8.75 8.66 8.72 8.73 -0.01 -0.11 375356.33 326396.994\n12 000001.SZ 20180702 9.05 9.05 8.55 8.61 9.09 -0.48 -5.28 1315520.13 1158545.868",
|
6 |
+
"api_name": "pro.daily()",
|
7 |
+
"desc": "获取股票每日的行情数据"
|
8 |
+
}
|
9 |
+
}
|
tool_lib/tool_backup.json
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"Function: get_stock_code": "def get_stock_code(stock_name: str) -> str:\n # 读取本地文件,获取股票代码\n # 输入是股票的名字str格式\n # 输出是股票的代码str格式\n # 例如输入'贵州茅台',输出'600519.SH'\n # '600519.SH' = get_stock_code('贵州茅台')\n\n \n Reads a local file to retrieve the stock code of a given stock name.\n Args:\n - stock_name (str): The name of the stock.\n Returns:\n - str: The stock code of the given stock name.\n \n\n # The input of the function is a stock name in string format, and the output is the corresponding stock code in string format.\n # For example, if we call get_stock_code('贵州茅台'), it will return '600519.SH'.\n",
|
3 |
+
"Function: get_stock_prices": "def get_stock_prices(stock_symbol: str, start_date: str, end_date: str) -> pd.DataFrame:\n # 通过tushare pro获取对应的股票的价格信息的日频数据\n # 输入是股票的名字,开始日期和结束日期,均是str格式\n # 输出是股票的价格数据dataframe格式,包括日期,开盘价,收盘价,最高价,最低价,成交量,成交额\n # 例如stock_symbol='600519.SH',start_date='20200120',end_start='20220222'\n # 输出是dataframe格式的股票价格数据,每行是一天的股票信息数据\n '''\n ts_code trade_date open ... pct_chg vol amount\n 800 600519.SH 20200102 1128.00 ... -4.4801 148099.16 1.669684e+07\n 799 600519.SH 20200103 1117.00 ... -4.5522 130318.78 1.426638e+07\n 798 600519.SH 20200106 1070.86 ... -0.0528 63414.78 6.853918e+06\n '''\n\n \n Retrieves the daily price data for a given stock symbol during a specific time period from tushare pro API.\n\n Args:\n - stock_symbol (str): The stock symbol code, e.g. '600519.SH'.\n - start_date (str): The start date of the time period in the format 'YYYYMMDD'.\n - end_date (str): The end date of the time period in the format 'YYYYMMDD'.\n\n Returns:\n - pd.DataFrame: A dataframe that contains the daily data for the given stock symbol during the specified time period.\n \n # The input of the function is a stock symbol code in string format, a start date and an end date, both in the format 'YYYYMMDD'.\n # The output of the function is a pandas dataframe\n # The dataframe contains the daily price data for the given stock symbol during the specified time period.\n # The dataframe includes the following columns: ts_code, trade_date, open, high, low, close, pre_close, change, pct_chg, vol, and amount.\n # Each row in the dataframe represents the price data for a single trading day.",
|
4 |
+
"Function: plot_stock_prices": "def plot_stock_prices(stock_prices: pd.DataFrame, stock_name: str, index: str) -> None:\\n # 这个函数是用于绘制股票价格的折线图, 输入是股票的信息的dataframe以及股票的名字,以及需要绘制的指标\\n # 输出是绘制的该股票该段时间内对应指标的走势图\\n # index可以是'open','close','high','low','pct_chg','vol','amount'\\n # 例如plot_stock_prices(stock_prices, '贵州茅台', 'close')\\n \"\"\"\\n Plots the stock prices of a given stock symbol during a specific time period for a specified index.\\n\\n Args:\\n - stock_prices (pd.DataFrame): A pandas dataframe that contains the daily price data for the given stock symbol during the specified time period.\\n - stock_name (str): The name of the stock.\\n - index (str): The name of the index that we want to plot. It can be one of 'open', 'close', 'high', 'low', 'pct_chg', 'vol', or 'amount'.\\n\\n Returns:\\n - None: The function only plots the stock price data for the specified index.\\n \"\"\"\\n # The input of the function is a pandas dataframe that contains the daily data for a given stock symbol during a specific time period and the name of the stock in string format, and the index name of the price data that we want to plot in string format.\\n # The output of the function is a plot that shows the stock price data for the specified index during the specified time period.\\n # The function plots the specified index against the trade_date column of the dataframe, sets the x-axis label to 'Date', and sets the y-axis label to the 'index' of the stock.\\n # The function also rotates the x-axis labels by 45 degrees to improve readability, sets the grid, and shows the plot.",
|
5 |
+
"Function: save_stock_prices_to_csv": "def save_stock_prices_to_csv(stock_prices: pd.DataFrame, stock_name: str, file_path: str) -> None:\\n # 保存特定股票的数据到本地csv文件\\n # 输入是股票的价格信息dataframe格式,股票的名字,以及保存的文件夹路径\\n # 输出是保存到本地的csv文件\\n # 例如stock_prices=dataframe, stock_name='贵州茅台', file_path='./data/'\\n # 如果文件夹不存在则创建\\n\\n \"\"\"\\n Saves the price data of a specific stock symbol during a specific time period to a local CSV file.\\n\\n Args:\\n - stock_prices (pd.DataFrame): A pandas dataframe that contains the daily price data for the given stock symbol during the specified time period.\\n - stock_name (str): The name of the stock.\\n - file_path (str): The file path where the CSV file will be saved.\\n\\n Returns:\\n - None: The function only saves the CSV file to the specified file path.\\n \"\"\"\\n # The function checks if the directory to save the CSV file exists and creates it if it does not exist.\\n # The function then saves the price data of the specified stock symbol during the specified time period to a local CSV file with the name {stock_name}_price_data.csv in the specified file path.\n\n\n\n\n",
|
6 |
+
"Function: compare_stocks_Earnings_Rate": "def compare_stocks_Earnings_Rate(stock_pool: dict, start_date: str, end_date: str) -> None:\n # 比较给定时间里多只股票的累计的收益率\n # 输入是股票的名字和代码的组成的字典,dict格式key是股票代码str,value是股票名称str,以及开始日期str和结束日期str\n # 输出是绘制的每只股票该段时间累计收益率的走势图\n # 例如stock_pool = {'600519.SH':'贵州茅台', '000858.SZ':'五粮液', '000568.SZ':'泸州老窖'}, start_date='20200120', end_start='20220222'\n \n Compares the cumulative earnings rate of multiple stocks during a given time period.\n\n Args:\n - stock_pool (dict): A dictionary of stock codes and names. Key is the stock code (str) and value is the stock name (str).\n - start_date (str): The start date of the time period in the format 'YYYYMMDD'.\n - end_date (str): The end date of the time period in the format 'YYYYMMDD'.\n\n Returns:\n - None: The function only plots the cumulative earnings rate trends of the given stocks during the given time period.\n \n # The input of the function is a dictionary of stock codes and names in string format, the start date of the time period in string format, and the end date of the time period in string format.\n # The output of the function is a plot that shows the cumulative earnings rate trends of the given stocks during the given time period.\n # The function iterates over the dictionary of stock codes and names, calls the get_stock_prices function to retrieve the earning rate of each stock during the specified time period, computes the cumulative earnings rate for each stock using the price data,\n # and plots the cumulative earnings rate trends of the given stocks during the given time period.\n # The function sets the x-axis label to 'Date', the y-axis label to 'Cumulative Earnings Rate (%).'",
|
7 |
+
"Function: get_company_info": "def get_company_info(stock_code: str, stock_name:str) -> None:\n # 获取公司信息, 包括ts_code: str 股票代码, exchange:str 交易所代码SSE上交所 SZSE深交所, chairman:str 法人代表, manager:str 总经理, secretary:str 董秘\n # reg_capital:float 注册资本, setup_date:str 注册日期, province:str 所在省份 ,city:str 所在城市\n # introduction:str 公司介绍, website:str 公司主页 , email:str 电子邮件, office:str 办公室\n # ann_date: str 公告日期, business_scope:str 经营范围, employees:int 员工人数, main_business:str 主要业务及产品."
|
8 |
+
}
|
tool_lib/tool_economic.json
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"get_GDP_data(start_quarter:str='', end_quarter:str='', index:str='gdp_yoy') -> pd.DataFrame:\n": "# 可以查询的指标包括以下9类: # gdp GDP累计值(亿元)# gdp_yoy 当季同比增速(%)# pi 第一产业累计值(亿元)# pi_yoy 第一产业同比增速(%)# si 第二产业累计值(亿元)# si_yoy 第二产业同比增速(%)# ti 第三产业累计值(亿元) # ti_yoy 第三产业同比增速(%)\n \"\"\"\n Retrieves GDP data for the chosen index and specified time period.\n\n Args:\n - start_quarter (str): The start quarter of the query, in YYYYMMDD format.\n - end_quarter (str): The end quarter, in YYYYMMDD format.\n - index (str): The specific GDP index to retrieve. Default is `gdp_yoy`.\n\n Returns:\n - pd.DataFrame: A pandas DataFrame with three columns: `quarter`, `country`, and the selected `index`.\n \"\"\"",
|
3 |
+
"get_cpi_ppi_currency_supply_data(start_month: str = '', end_month: str = '', type: str = 'cpi', index: str = '') -> pd.DataFrame:\n": "# 查询的类型type包括cpi,ppi,currency_supply三种类型,每种类型对应不同的index,cpi有12个index,ppi有30个index,currency_supply有9个index\n # 输出是dataframe表格,包括三列 第一列月份,第二列国家,第三列是index\n\n # type='cpi',即月度cpi数据,可以查询的指标index包括以下12类:\n # nt_val\t全国当月值 # nt_yoy\t全国同比(%)# nt_mom\t全国环比(%)# nt_accu\t全国累计值# town_val\t城市当月值# town_yoy\t城市同比(%)# town_mom\t城市环比(%)# town_accu\t城市累计值# cnt_val\t农村当月值# cnt_yoy\t农村同比(%)# cnt_mom\t农村环比(%)# cnt_accu\t农村累计值\n\n # type = 'ppi',即月度ppi数据,月度ppi,可以查询的具体指标index为如下30类\n # ppi_yoy\tPPI:全部工业品:当月同比\n # ppi_mp_yoy PPI:生产资料:当月同比\n # ppi_mp_qm_yoy\tPPI:生产资料:采掘业:当月同比\n # ppi_mp_rm_yoy\tPPI:生产资料:原料业:当月同比\n # ppi_mp_p_yoy\tPPI:生产资料:加工业:当月同比\n # ppi_cg_yoy\tPPI:生活资料:当月同比\n # ppi_cg_f_yoy\tPPI:生活资料:食品类:当月同比\n # ppi_cg_c_yoy\tPPI:生活资料:衣着类:当月同比\n # ppi_cg_adu_yoy\tPPI:生活资料:一般日用品类:当月同比\n # ppi_cg_dcg_yoy\tPPI:生活资料:耐用消费品类:当月同比\n # ppi_mom\tPPI:全部工业品:环比\n # ppi_mp_mom\tPPI:生产资料:环比\n # ppi_mp_qm_mom\tPPI:生产资料:采掘业:环比\n # ppi_mp_rm_mom\tPPI:生产资料:原料业:环比\n # ppi_mp_p_mom\tPPI:生产资料:加工业:环比\n # ppi_cg_mom\tPPI:生活资料:环比\n # ppi_cg_f_mom\tPPI:生活资料:食品类:环比\n # ppi_cg_c_mom\tPPI:生活资料:衣着类:环比\n # ppi_cg_adu_mom\tPPI:生活资料:一般日用品类:环比\n # ppi_cg_dcg_mom\t\tPPI:生活资料:耐用消费品类:环比\n # ppi_accu\t\tPPI:全部工业品:累计同比\n # ppi_mp_accu\t\tPPI:生产资料:累计同比\n # ppi_mp_qm_accu\t\tPPI:生产资料:采掘业:累计同比\n # ppi_mp_rm_accu\t\tPPI:生产资料:原料业:累计同比\n # ppi_mp_p_accu\t PPI:生产资料:加工业:累计同比\n # ppi_cg_accu\tPPI:生活资料:累计同比\n # ppi_cg_f_accu\t\tPPI:生活资料:食品类:累计同比\n # ppi_cg_c_accu\t\tPPI:生活资料:衣着类:累计同比\n # ppi_cg_adu_accu\tPPI:生活资料:一般日用品类:累计同比\n # ppi_cg_dcg_accu\tPPI:生活资料:耐用消费品类:累计同比\n\n # type = 'currency_supply',即月度货币供应量数据,月度货币供应量,可以查询的具体指标index为如下9类:\n # m0 M0(亿元)# m0_yoy M0同比(%)# m0_mom M0环比(%)# m1 M1(亿元)# m1_yoy M1同比(%)# m1_mom M1环比(%)# m2 M2(亿元)# m2_yoy M2同比(%)# m2_mom M2环比(%)\n\n \"\"\"\n This function is used to retrieve China's monthly CPI (Consumer Price Index), PPI (Producer Price Index),\n and monetary supply data published by the National Bureau of Statistics,\n and return a DataFrame table containing month, country, and index values.\n The function parameters include start month, end month, query type, and query index.\n For query indexes that are not within the query range, the default index for the corresponding type is returned.\n\n Args:\n - start_month (str): start month of the query, in the format of YYYYMMDD.\n - end_month (str):end month in YYYYMMDD\n - type (str): required parameter, query type, including three types: cpi, ppi, and currency_supply.\n - index (str): optional parameter, query index, the specific index depends on the query type.\n If the query index is not within the range, the default index for the corresponding type is returned.\n\n Returns:\n - pd.DataFrame: DataFrame type, including three columns: month, country, and index value.\n \"\"\"",
|
4 |
+
"get_latest_new_from_web(src: str = 'sina') -> pd.DataFrame:\n": "# 获取主流新闻网站的快讯最新的新闻数据,其中包括新浪财经、同花顺、东方财富、云财经四个新闻网站,src = 'sina' 代表新浪财经\n# 新浪财经\tsina\t获取新浪财经实时资讯\n# 同花顺\t 10jqka\t同花顺财经新闻\n# 东方财富\teastmoney\t东方财富财经新闻\n# 云财经\t yuncaijing\t云财经新闻\n \"\"\"\n Retrieves the latest news data from major news websites, including Sina Finance, 10jqka, Eastmoney, and Yuncaijing.\n\n Args:\n src (str): The name of the news website. Default is 'sina'. Optional parameters include: 'sina' for Sina Finance,\n '10jqka' for 10jqka, 'eastmoney' for Eastmoney, and 'yuncaijing' for Yuncaijing.\n\n Returns:\n pd.DataFrame: A DataFrame containing the news data, including two columns for date/time and content.\n \"\"\"",
|
5 |
+
"get_company_info(stock_name: str='') -> pd.DataFrame:\n": "# 获取公司信息,包括ts_code: str\t股票代码, exchange:str\t交易所代码SSE上交所 SZSE深交所, chairman:str 法人代表, manager:str 总经理, secretary:str\t董秘 # reg_capital:float\t注册资本, setup_date:str 注册日期, province:str 所在省份 ,city:str 所在城市\n # introduction:str 公司介绍, website:str 公司主页 , email:str\t电子邮件, office:str 办公室 # ann_date: str 公告日期, business_scope:str 经营范围, employees:int\t员工人数, main_business:str 主要业务及产品\n \"\"\"\n This function retrieves company information including stock code, exchange, chairman, manager, secretary,\n registered capital, setup date, province, city, website, email, employees, business scope, main business,\n introduction, office, and announcement date.\n\n Args:\n - stock_name (str): The name of the stock.\n\n Returns:\n - pd.DataFrame: A DataFrame that contains the company information.\n \"\"\"",
|
6 |
+
"calculate_stock_index(stock_data: pd.DataFrame, index:str='close') -> pd.DataFrame:\n": "\"\"\"\n Calculates the rate of return for a specified stock/fund between two dates.\n\n Args:\n ts_code\n start_date\n end_date\n index (str): The index used to calculate the stock return, including 'open' and 'close'.\n\n Returns:\n float: The rate of return for the specified stock between the two dates.\n \"\"\"",
|
7 |
+
"get_north_south_money(start_date: str = '', end_date: str = '', trade_date: str = '') -> pd.DataFrame:\n": "# 获取北向资金和南向资金的每日的数据,即获取沪股通、深股通、港股通每日资金流向数据, 输出包含以下几列\n # trade_date: 交易日期\n # ggt_ss:\t港股通(上海)\n # ggt_sz:\t港股通(深圳)\n # hgt:\t沪股通(亿元)\n # sgt:\t深股通(亿元)\n # north_money:\t北向资金(亿元)= hgt + sgt\n # south_money:\t南向资金(亿元)= ggt_ss + ggt_sz\n # name: 固定为'A-H',代表A股和H股\n # accumulate_north_money: 累计北向资金流入,即从开始到结束的累计北向资金流入\n # accumulate_south_money: 累计南向资金流入",
|
8 |
+
"predict_next_value(df: pd.DataFrame, pred_index: str = 'nt_yoy', pred_num:int = 1. ) -> pd.DataFrame:": "Predict the next n values of a specific column in the DataFrame using linear regression. Parameters:df (pandas.DataFrame): The input DataFrame.pred_index (str): The name of the column to predict.pred_num (int): The number of future values to predict.Returns:pandas.DataFrame: The DataFrame with the predicted values appended to the specified column and other columns filled as pred+index"
|
9 |
+
}
|
tool_lib/tool_financial.json
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"get_Financial_data_from_time_range(stock_code:str, start_date:str, end_date:str, financial_index:str='') -> pd.DataFrame:\n": "# 例如 输入start_date='20190101',end_date='20221231',financial_index='roe', 则返回的是2019年到2022整三年的roe数据\n # 查询某个时刻的季报年报数据则 \"yyyy0331\"为一季报,\"yyyy0630\"为半年报,\"yyyy0930\"为三季报,\"yyyy1231\"为年报,例如get_Financial_data_from_time_range(\"600519.SH\", \"20190331\", \"20190331\", \"roe\").代表查询2019年一季报中roe数据\n # index包含: # current_ratio\t流动比率 # quick_ratio\t速动比率 # netprofit_margin\t销售净利率 # grossprofit_margin\t销售毛利率 # roe\t净资产收益率 # roe_dt\t净资产收益率(扣除非经常损益)\n # roa\t总资产报酬率 # debt_to_assets 资产负债率 # roa_yearly\t年化总资产净利率 # q_dtprofit\t扣除非经常损益后的单季度净利润 # q_eps\t每股收益(单季度)\n # q_netprofit_margin\t销售净利率(单季度) # q_gsprofit_margin\t销售毛利率(单季度) # basic_eps_yoy 基本每股收益同比增长率(%) # netprofit_yoy\t归属母公司股东的净利润同比增长率(%) # q_netprofit_yoy\t归属母公司股东的净利润同比增长率(%)(单季度) # q_netprofit_qoq\t归属母公司股东的净利润环比增长率(%)(单季度) # equity_yoy\t净资产同比增长率\n \"\"\"\n Retrieves the financial data for a given stock within a specified date range.\n\n Args:\n stock_code (str): The stock code.\n start_date (str): The start date of the data range in the format \"YYYYMMDD\".\n end_date (str): The end date of the data range in the format \"YYYYMMDD\".\n financial_index (str, optional): The financial indicator to be queried.\n\n Returns:\n pd.DataFrame: A DataFrame containin financial data for the specified stock and date range.\n\n\"\"\"",
|
3 |
+
"get_company_info(stock_name: str='') -> pd.DataFrame:\n": "# 获取公司信息,包括ts_code: str\t股票代码, exchange:str\t交易所代码SSE上交所 SZSE深交所, chairman:str 法人代表, manager:str 总经理, secretary:str\t董秘 # reg_capital:float\t注册资本, setup_date:str 注册日期, province:str 所在省份 ,city:str 所在城市\n # introduction:str 公司介绍, website:str 公司主页 , email:str\t电子邮件, office:str 办公室 # ann_date: str 公告日期, business_scope:str 经营范围, employees:int\t员工人数, main_business:str 主要业务及产品\n \"\"\"\n This function retrieves company information including stock code, exchange, chairman, manager, secretary,\n registered capital, setup date, province, city, website, email, employees, business scope, main business,\n introduction, office, and announcement date.\n\n Args:\n - stock_name (str): The name of the stock.\n\n Returns:\n - pd.DataFrame: A DataFrame that contains the company information.\n \"\"\"",
|
4 |
+
"get_stock_code(stock_name: str) -> str:\n": "\"\"\"\n # Retrieve the stock code of a given stock name. '600519.SH'=get_stock_code('贵州茅台')",
|
5 |
+
"loop_rank(df: pd.DataFrame, func: callable, *args, **kwargs) -> pd.DataFrame:\n": "\"\"\"\n It iteratively applies the given function to each row and get a result using function. It then stores the calculated result in 'new_feature' column.\n\n Args:\n df: DataFrame with a single column\n func : The function to be applied to each row: func(row, *args, **kwargs)\n *args: Additional positional arguments for `func` function.\n **kwargs: Additional keyword arguments for `func` function.\n\n Returns:\n pd.DataFrame: A output DataFrame with three columns: the constant column, input column, and new_feature column.\n The DataFrame is sorted based on the new_feature column in descending order.\n\n \"\"\"",
|
6 |
+
"get_index_constituent(index_name:str='',start_date:str ='',end_date:str ='') -> pd.DataFrame:\n": "\"\"\"\n Get the constituents and weights of a stock index within a given time period.\n\n args:\n index_name: the name of the index.\n start_date: the start date in \"YYYYMMDD\".\n end_date: the end date in \"YYYYMMDD\".\n\n return:\n A pandas DataFrame containing the following columns:\n index_code\n weight: the weight of the constituent stock.\n index_name\n stock_code: the code of the constituent stock.\n stock_name: the name of the constituent stock.\n industry: the industry of the constituent stock.\n market: 上交所或证交所\n \"\"\"",
|
7 |
+
"select_value_by_column(df1:pd.DataFrame, col_name: str = '', row_index: int = -1) -> Union[pd.DataFrame, Any]:\n": "\"\"\"\n Selects a specific column or a specific value within a DataFrame.\n\n Args:\n df1: The input DataFrame.\n col_name: The name of the column to be selected.\n row_index: The index of the row to be selected.\n\n Returns:\n Union[pd.DataFrame, Any]. row_index=-1: df1[col_name].to_frame() or df1[col_name][row_index]\n \"\"\""
|
8 |
+
}
|
tool_lib/tool_fund.json
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"query_fund_Manager(Manager_name: str) -> pd.DataFrame:\n": "# 输出包括基金经理管理的每只基金的代码fund_code,公告日期ann_date,基金经理名字name,性别gender,出生年份birth_year,学历edu,国籍nationality,开始管理日期begin_date,结束日期end_date,简历resume\n \"\"\"\n Retrieves information about a fund manager.\n\n Args:\n Manager_name (str): The name of the fund manager.\n\n Returns:\n df (DataFrame): A DataFrame containing the fund manager's information, including the fund codes, announcement dates,\n manager's name, gender, birth year, education, nationality, start and end dates of managing funds,\n and the manager's resume. \n \"\"\"",
|
3 |
+
"query_fund_name_or_code(fund_name: str = '', fund_code: str = '') -> str:\n": "# 输入是基金名称或者基金代码, 输出是基金名称或者基金代码,两者不可同时输入\n \"\"\"\n Retrieves the fund code based on the fund name or Retrieves the fund name based on the fund code.\n\n Args:\n fund_name (str, optional): Fund name. Defaults to ''.\n fund_code (str, optional): Fund code. Defaults to ''.\n\n Returns:\n code or name: Fund code if fund_name is provided and fund_code is empty. Fund name if fund_code is provided and fund_name is empty.\n \"\"\"",
|
4 |
+
"query_fund_data(fund_code: str = '', start_date: str = '', end_date: str = '') -> pd.DataFrame:\n": "# 输出是一份基金数据的dataframe,包括以下信息:\n # ts_code\tstr\tY\tTS代码 # ann_date\tstr\tY\t公告日期 # nav_date\tstr\tY\t净值日期 # unit_nav\tfloat\tY\t单位净值 # accum_nav\tfloat\tY\t累计净值\n # accum_div\tfloat\tY\t累计分红 # net_asset\tfloat\tY\t资产净值 # total_netasset\tfloat\tY\t合计资产净值 # adj_nav\tfloat\tY\t复权单位净值\n \"\"\"\n Retrieves fund data based on the fund code, start date, and end date.\n\n Args:\n fund_code (str, optional): Fund code. Defaults to ''.\n start_date (str, optional): Start date in YYYYMMDD format. Defaults to ''.\n end_date (str, optional): End date in YYYYMMDD format. Defaults to ''.\n\n Returns:\n df (DataFrame): A DataFrame containing fund data, including TS code, announcement date, net asset value date,\n unit net asset value, accumulated net asset value, accumulated dividends, net asset value,\n total net asset value, adjusted unit net asset value, and fund name. The 'ts_code' column is renamed\n to 'fund_code', 'nav_date' is renamed to 'trade_date', and the DataFrame is sorted by the trade date\n in ascending order. If the fund code does not exist, None is returned.\n \"\"\"",
|
5 |
+
|
6 |
+
"calculate_earning_between_two_time(stock_name: str = '', start_date: str = '', end_date: str = '', index: str = 'close') -> float:\n": "\"\"\"\n Calculates the rate of return for a specified stock between two dates.\n\n Args:\n index: The index used to calculate the stock return. The default is 'close' for stock and 'adj_nav' for fund.\n\n \"\"\"",
|
7 |
+
"calculate_stock_index(stock_data: pd.DataFrame, index:str='close') -> pd.DataFrame:\n": "\"\"\"\n Calculates the rate of return for a specified stock/fund between two dates.\n\n Args:\n ts_code\n start_date\n end_date\n index (str): The index used to calculate the stock return, including 'open' and 'close'.\n\n Returns:\n float: The rate of return for the specified stock between the two dates.\n \"\"\"",
|
8 |
+
"loop_rank(df: pd.DataFrame, func: callable, *args, **kwargs) -> pd.DataFrame:\n": "\"\"\"\n It iteratively applies the given function to each row and get a result using function. It then stores the calculated result in 'new_feature' column.\n\n Args:\n df: DataFrame with a single column\n func : The function to be applied to each row: func(row, *args, **kwargs)\n *args: Additional positional arguments for `func` function.\n **kwargs: Additional keyword arguments for `func` function.\n\n Returns:\n pd.DataFrame: A output DataFrame with three columns: the constant column, input column, and new_feature column.\n The DataFrame is sorted based on the new_feature column in descending order.\n\n \"\"\"",
|
9 |
+
"select_value_by_column(df1:pd.DataFrame, col_name: str = '', row_index: int = -1) -> Union[pd.DataFrame, Any]:\n": "\"\"\"\n Selects a specific column or a specific value within a DataFrame.\n\n Args:\n df1: The input DataFrame.\n col_name: The name of the column to be selected.\n row_index: The index of the row to be selected.\n\n Returns:\n Union[pd.DataFrame, Any]. row_index=-1: df1[col_name].to_frame() or df1[col_name][row_index]\n \"\"\""
|
10 |
+
}
|
tool_lib/tool_stock.json
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"get_stock_prices_data(stock_name: str='', start_date: str='', end_date: str='',freq:str='daily') -> pd.DataFrame:\n": "\"\"\"\n Retrieves the daily/weekly/monthly price data for a given stock name during a specific time period.\n Args:\n - start_date(end_date): The start and end date in format 'YYYYMMDD'.\n - freq: The frequency: 'daily', 'weekly', or 'monthly'.\n\n Returns:\n - pd.DataFrame: A dataframe that contains the many price columns including stock_code, trade_date, open, high, low, close, pre_close(昨天收盘价), change(涨跌额), pct_chg(涨跌幅),vol(成交量),amount(成交额)\n \"\"\"",
|
3 |
+
"get_stock_technical_data(stock_name: str, start_date: str, end_date: str) -> pd.DataFrame:\n": "\"\"\"\n Retrieves the daily technical indicator of a stock including macd turnover rate, volume, pe-ttm, etc. Those technical indicators are usually plotted as subplots in a k-line chart.\n\n Returns:\n pd.DataFrame: A DataFrame containing the columns: ts_code, trade_date, close, and many technical indicators\n. \"\"\"",
|
4 |
+
"get_index_data(index_name: str = '', start_date: str = '', end_date: str = '', freq: str = 'daily') -> pd.DataFrame:\n": "\"\"\"\n This function retrieves daily, weekly, or monthly data for a given stock index.\n\n Arguments:\n - index_name: Name of the index\n - freq: Frequency 'daily', 'weekly', or 'monthly'\n\n \"\"\"",
|
5 |
+
"get_index_constituent(index_name:str='',start_date:str ='',end_date:str ='') -> pd.DataFrame:\n": "\"\"\"\n Query the constituent stocks of basic index (中证500) or a specified SW (申万) industry index\n\n return:\n A pandas DataFrame containing the following columns:\n index_code\n index_name\n stock_code: the code of the constituent stock.\n stock_name: \n weight: the weight of the constituent stock.\n \"\"\"",
|
6 |
+
"get_Financial_data_from_time_range(stock_name:str, start_date:str, end_date:str, financial_index:str='') -> pd.DataFrame:\n": "#查询财报数据,每年有四个财报\"yyyy0331\"为一季报,\"yyyy0630\"为半年报,\"yyyy0930\"为三季报,\"yyyy1231\"为年报,不能获取pe-ttm,pb等技术指标. 例如查询某段时间内所有财报:get_Financial_data_from_time_range('20190101','20221231',roe')返回2019年到2022整三年12份财报里的roe数据\n 例如查询某一期财报:get_Financial_data_from_time_range(\"600519.SH\", \"20190331\", \"20190331\", \"roe\").代表查询2019年一季报中roe数据\n financial_index: The financial indicator:ROE, netprofit_yoy .\n\n\"\"\"",
|
7 |
+
|
8 |
+
"calculate_stock_index(stock_data: pd.DataFrame, index:str='close') -> pd.DataFrame:\n": "\"\"\"\n Select or Calculate a index for a stock from source dataframe.\n\n Index : The index could be price indicator: \"close\", \"pct_chg\" or technical indicator: \"pe-ttm\" or \"Cumulative_Earnings_Rate\" and \"candle_K\" \n \"\"\"",
|
9 |
+
"calculate_earning_between_two_time(stock_name: str = '', start_date: str = '', end_date: str = '', index: str = 'close') -> float:\n": "\"\"\"\n Calculates the rate of return for a specified stock between two dates.\n\n Args:\n index: The index used to calculate the stock return. The default is 'close' for stock and 'adj_nav' for fund.\n\n \"\"\"",
|
10 |
+
"loop_rank(df: pd.DataFrame, func: callable, *args, **kwargs) -> pd.DataFrame:\n": "\"\"\"\n It iteratively applies the given function to each row and get a result using function. It then stores the calculated result in 'new_feature' column and sorted the 'new_feature' column in descending order.\n\n args: The input DataFrame only have a single column\n func : The function to be applied to each row: func(row, *args, **kwargs)\n *args and **kwargs: arguments for `func` function.\n Returns:\n A output DataFrame with three columns: the constant column, input column, and new_feature column.\n\n \"\"\"",
|
11 |
+
"merge_indicator_for_same_stock(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:\n": "\"\"\"\n Merges two DataFrames (two indicators of the same stock). Data of two different stocks cannot be merged. \n\n Args:\n df1: DataFrame contains some indicators for stock A.\n df2: DataFrame contains other indicators for stock A.\n\n Returns:\n pd.DataFrame: The merged DataFrame contains two different indicators.\n \"\"\"",
|
12 |
+
"select_value_by_column(df1:pd.DataFrame, col_name: str = '', row_index: int = -1) -> Union[pd.DataFrame, Any]:\n": "\"\"\"\n Selects a specific column or a specific value within a DataFrame.\n\n Args:\n col_name: The name of the column to be selected.\n row_index: The index of the row to be selected.\n\n row_index=-1: df1[col_name].to_frame() or df1[col_name][row_index]\n \"\"\""
|
13 |
+
}
|
tool_lib/tool_visualization.json
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"plot_stock_data(stock_data: pd.DataFrame, ax: Optional[plt.Axes] = None, figure_type: str = 'line', title_name: str ='') -> plt.Axes:\n": "\"\"\"\n This function plots stock data.\n\n Args:\n - stock_data: the stock data to plot. The DataFrame should contain three columns:\n - Column 1: trade date in 'YYYYMMDD'\n - Column 2: Stock name or code (string format)\n - Column 3: Index value (numeric format)\n The DataFrame can be time series data or cross-sectional data. If it is time-series data, the first column represents different trade time, the second column represents the same name. For cross-sectional data, the first column is the same, the second column contains different stocks.\n\n - ax: matplotlib Axes object, the axes to plot the data on\n - figure_type: either 'line' or 'bar'\n - title_name\n\n Returns:\n - matplotlib Axes object, the axes containing the plot\n \"\"\"",
|
3 |
+
"plot_k_line(stock_data: pd.DataFrame, title: str = '') -> None:\n": "\"\"\"\n Plots a K-line chart of stock price and volume.\n\n Args:\n stock_data : A pandas DataFrame containing the stock price information, in which each row\n represents a daily record. The DataFrame must contain the 'trade_date','open', 'close', 'high', 'low','volume', 'name' columns, which is used for k-line and volume.\n 如果dataframe中还含有'macd','kdj', 'rsi', 'cci', 'boll','pe_ttm','turnover_rate'等列,则在k线图下方绘制这些指标的子图.\n \"\"\"",
|
4 |
+
"print_save_table(df: pd.DataFrame, title_name: str, save:bool = False ,file_path: str = './output/') -> None:": "\"\"\"\n It prints the dataframe and saves it to a CSV file at the specified file path.\n\n Args:\n - df: the dataframe to be printed and saved to a CSV file\n - title_name: the name to be printed and saved\n - save: whether to save as a CSV file\n - file_path \n\n \"\"\"",
|
5 |
+
"output_mean_median_col(data: pd.DataFrame, col: str = 'new_feature') -> float:\n": "# It calculates the mean and median value for the specified column."
|
6 |
+
}
|
tushare_daily_20230421211129.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tushare_fund_basic_20230508193747.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tushare_fund_basic_20230516041211.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tushare_fund_basic_20230605184116.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tushare_fund_basic_20230605184607.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tushare_fund_basic_all.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tushare_index_basic_20230427223903.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tushare_stock_basic_20230421210721.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|