siat 3.10.132__py3-none-any.whl → 3.10.133__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. siat/__init__.py +0 -0
  2. siat/allin.py +0 -0
  3. siat/assets_liquidity.py +0 -0
  4. siat/beta_adjustment.py +0 -0
  5. siat/beta_adjustment_china.py +0 -0
  6. siat/blockchain.py +0 -0
  7. siat/bond.py +0 -0
  8. siat/bond_base.py +0 -0
  9. siat/bond_china.py +0 -0
  10. siat/bond_zh_sina.py +0 -0
  11. siat/capm_beta.py +0 -0
  12. siat/capm_beta2.py +0 -0
  13. siat/compare_cross.py +0 -0
  14. siat/copyrights.py +0 -0
  15. siat/cryptocurrency.py +0 -0
  16. siat/economy.py +0 -0
  17. siat/economy2.py +0 -0
  18. siat/esg.py +0 -0
  19. siat/event_study.py +0 -0
  20. siat/exchange_bond_china.pickle +0 -0
  21. siat/fama_french.py +0 -0
  22. siat/fin_stmt2_yahoo.py +0 -0
  23. siat/financial_base.py +0 -0
  24. siat/financial_statements.py +0 -0
  25. siat/financials.py +0 -0
  26. siat/financials2.py +0 -0
  27. siat/financials_china.py +0 -0
  28. siat/financials_china2.py +0 -0
  29. siat/fund.py +0 -0
  30. siat/fund_china.pickle +0 -0
  31. siat/fund_china.py +0 -0
  32. siat/future_china.py +0 -0
  33. siat/google_authenticator.py +0 -0
  34. siat/grafix.py +0 -0
  35. siat/holding_risk.py +0 -0
  36. siat/luchy_draw.py +0 -0
  37. siat/market_china.py +0 -0
  38. siat/markowitz.py +0 -0
  39. siat/markowitz2.py +0 -0
  40. siat/markowitz2_20250704.py +0 -0
  41. siat/markowitz2_20250705.py +0 -0
  42. siat/markowitz_simple.py +0 -0
  43. siat/ml_cases.py +0 -0
  44. siat/ml_cases_example.py +0 -0
  45. siat/option_china.py +0 -0
  46. siat/option_pricing.py +0 -0
  47. siat/other_indexes.py +0 -0
  48. siat/risk_adjusted_return.py +0 -0
  49. siat/risk_adjusted_return2.py +0 -0
  50. siat/risk_evaluation.py +0 -0
  51. siat/risk_free_rate.py +0 -0
  52. siat/sector_china.py +0 -0
  53. siat/security_price2.py +0 -0
  54. siat/security_prices.py +40 -2
  55. siat/security_trend.py +0 -0
  56. siat/security_trend2.py +0 -0
  57. siat/stock.py +0 -0
  58. siat/stock_advice_linear.py +0 -0
  59. siat/stock_base.py +0 -0
  60. siat/stock_china.py +0 -0
  61. siat/stock_info.pickle +0 -0
  62. siat/stock_prices_kneighbors.py +0 -0
  63. siat/stock_prices_linear.py +0 -0
  64. siat/stock_profile.py +0 -0
  65. siat/stock_technical.py +0 -0
  66. siat/stooq.py +0 -0
  67. siat/transaction.py +0 -0
  68. siat/translate.py +0 -0
  69. siat/valuation.py +0 -0
  70. siat/valuation_china.py +0 -0
  71. siat/var_model_validation.py +0 -0
  72. siat/yf_name.py +0 -0
  73. {siat-3.10.132.dist-info/licenses → siat-3.10.133.dist-info}/LICENSE +0 -0
  74. {siat-3.10.132.dist-info → siat-3.10.133.dist-info}/METADATA +232 -235
  75. siat-3.10.133.dist-info/RECORD +78 -0
  76. {siat-3.10.132.dist-info → siat-3.10.133.dist-info}/WHEEL +1 -1
  77. {siat-3.10.132.dist-info → siat-3.10.133.dist-info}/top_level.txt +0 -1
  78. build/lib/build/lib/siat/__init__.py +0 -75
  79. build/lib/build/lib/siat/allin.py +0 -137
  80. build/lib/build/lib/siat/assets_liquidity.py +0 -915
  81. build/lib/build/lib/siat/beta_adjustment.py +0 -1058
  82. build/lib/build/lib/siat/beta_adjustment_china.py +0 -548
  83. build/lib/build/lib/siat/blockchain.py +0 -143
  84. build/lib/build/lib/siat/bond.py +0 -2900
  85. build/lib/build/lib/siat/bond_base.py +0 -992
  86. build/lib/build/lib/siat/bond_china.py +0 -100
  87. build/lib/build/lib/siat/bond_zh_sina.py +0 -143
  88. build/lib/build/lib/siat/capm_beta.py +0 -783
  89. build/lib/build/lib/siat/capm_beta2.py +0 -887
  90. build/lib/build/lib/siat/common.py +0 -5360
  91. build/lib/build/lib/siat/compare_cross.py +0 -642
  92. build/lib/build/lib/siat/copyrights.py +0 -18
  93. build/lib/build/lib/siat/cryptocurrency.py +0 -667
  94. build/lib/build/lib/siat/economy.py +0 -1471
  95. build/lib/build/lib/siat/economy2.py +0 -1853
  96. build/lib/build/lib/siat/esg.py +0 -536
  97. build/lib/build/lib/siat/event_study.py +0 -815
  98. build/lib/build/lib/siat/fama_french.py +0 -1521
  99. build/lib/build/lib/siat/fin_stmt2_yahoo.py +0 -982
  100. build/lib/build/lib/siat/financial_base.py +0 -1160
  101. build/lib/build/lib/siat/financial_statements.py +0 -598
  102. build/lib/build/lib/siat/financials.py +0 -2339
  103. build/lib/build/lib/siat/financials2.py +0 -1278
  104. build/lib/build/lib/siat/financials_china.py +0 -4433
  105. build/lib/build/lib/siat/financials_china2.py +0 -2212
  106. build/lib/build/lib/siat/fund.py +0 -629
  107. build/lib/build/lib/siat/fund_china.py +0 -3307
  108. build/lib/build/lib/siat/future_china.py +0 -551
  109. build/lib/build/lib/siat/google_authenticator.py +0 -47
  110. build/lib/build/lib/siat/grafix.py +0 -3636
  111. build/lib/build/lib/siat/holding_risk.py +0 -867
  112. build/lib/build/lib/siat/luchy_draw.py +0 -638
  113. build/lib/build/lib/siat/market_china.py +0 -1168
  114. build/lib/build/lib/siat/markowitz.py +0 -2363
  115. build/lib/build/lib/siat/markowitz2.py +0 -3150
  116. build/lib/build/lib/siat/markowitz2_20250704.py +0 -2969
  117. build/lib/build/lib/siat/markowitz2_20250705.py +0 -3158
  118. build/lib/build/lib/siat/markowitz_simple.py +0 -373
  119. build/lib/build/lib/siat/ml_cases.py +0 -2291
  120. build/lib/build/lib/siat/ml_cases_example.py +0 -60
  121. build/lib/build/lib/siat/option_china.py +0 -3069
  122. build/lib/build/lib/siat/option_pricing.py +0 -1925
  123. build/lib/build/lib/siat/other_indexes.py +0 -409
  124. build/lib/build/lib/siat/risk_adjusted_return.py +0 -1576
  125. build/lib/build/lib/siat/risk_adjusted_return2.py +0 -1900
  126. build/lib/build/lib/siat/risk_evaluation.py +0 -2218
  127. build/lib/build/lib/siat/risk_free_rate.py +0 -351
  128. build/lib/build/lib/siat/sector_china.py +0 -4140
  129. build/lib/build/lib/siat/security_price2.py +0 -727
  130. build/lib/build/lib/siat/security_prices.py +0 -3408
  131. build/lib/build/lib/siat/security_trend.py +0 -402
  132. build/lib/build/lib/siat/security_trend2.py +0 -646
  133. build/lib/build/lib/siat/stock.py +0 -4284
  134. build/lib/build/lib/siat/stock_advice_linear.py +0 -934
  135. build/lib/build/lib/siat/stock_base.py +0 -26
  136. build/lib/build/lib/siat/stock_china.py +0 -2095
  137. build/lib/build/lib/siat/stock_prices_kneighbors.py +0 -910
  138. build/lib/build/lib/siat/stock_prices_linear.py +0 -386
  139. build/lib/build/lib/siat/stock_profile.py +0 -707
  140. build/lib/build/lib/siat/stock_technical.py +0 -3305
  141. build/lib/build/lib/siat/stooq.py +0 -74
  142. build/lib/build/lib/siat/transaction.py +0 -347
  143. build/lib/build/lib/siat/translate.py +0 -5183
  144. build/lib/build/lib/siat/valuation.py +0 -1378
  145. build/lib/build/lib/siat/valuation_china.py +0 -2076
  146. build/lib/build/lib/siat/var_model_validation.py +0 -444
  147. build/lib/build/lib/siat/yf_name.py +0 -811
  148. build/lib/siat/__init__.py +0 -75
  149. build/lib/siat/allin.py +0 -137
  150. build/lib/siat/assets_liquidity.py +0 -915
  151. build/lib/siat/beta_adjustment.py +0 -1058
  152. build/lib/siat/beta_adjustment_china.py +0 -548
  153. build/lib/siat/blockchain.py +0 -143
  154. build/lib/siat/bond.py +0 -2900
  155. build/lib/siat/bond_base.py +0 -992
  156. build/lib/siat/bond_china.py +0 -100
  157. build/lib/siat/bond_zh_sina.py +0 -143
  158. build/lib/siat/capm_beta.py +0 -783
  159. build/lib/siat/capm_beta2.py +0 -887
  160. build/lib/siat/common.py +0 -5360
  161. build/lib/siat/compare_cross.py +0 -642
  162. build/lib/siat/copyrights.py +0 -18
  163. build/lib/siat/cryptocurrency.py +0 -667
  164. build/lib/siat/economy.py +0 -1471
  165. build/lib/siat/economy2.py +0 -1853
  166. build/lib/siat/esg.py +0 -536
  167. build/lib/siat/event_study.py +0 -815
  168. build/lib/siat/fama_french.py +0 -1521
  169. build/lib/siat/fin_stmt2_yahoo.py +0 -982
  170. build/lib/siat/financial_base.py +0 -1160
  171. build/lib/siat/financial_statements.py +0 -598
  172. build/lib/siat/financials.py +0 -2339
  173. build/lib/siat/financials2.py +0 -1278
  174. build/lib/siat/financials_china.py +0 -4433
  175. build/lib/siat/financials_china2.py +0 -2212
  176. build/lib/siat/fund.py +0 -629
  177. build/lib/siat/fund_china.py +0 -3307
  178. build/lib/siat/future_china.py +0 -551
  179. build/lib/siat/google_authenticator.py +0 -47
  180. build/lib/siat/grafix.py +0 -3636
  181. build/lib/siat/holding_risk.py +0 -867
  182. build/lib/siat/luchy_draw.py +0 -638
  183. build/lib/siat/market_china.py +0 -1168
  184. build/lib/siat/markowitz.py +0 -2363
  185. build/lib/siat/markowitz2.py +0 -3150
  186. build/lib/siat/markowitz2_20250704.py +0 -2969
  187. build/lib/siat/markowitz2_20250705.py +0 -3158
  188. build/lib/siat/markowitz_simple.py +0 -373
  189. build/lib/siat/ml_cases.py +0 -2291
  190. build/lib/siat/ml_cases_example.py +0 -60
  191. build/lib/siat/option_china.py +0 -3069
  192. build/lib/siat/option_pricing.py +0 -1925
  193. build/lib/siat/other_indexes.py +0 -409
  194. build/lib/siat/risk_adjusted_return.py +0 -1576
  195. build/lib/siat/risk_adjusted_return2.py +0 -1900
  196. build/lib/siat/risk_evaluation.py +0 -2218
  197. build/lib/siat/risk_free_rate.py +0 -351
  198. build/lib/siat/sector_china.py +0 -4140
  199. build/lib/siat/security_price2.py +0 -727
  200. build/lib/siat/security_prices.py +0 -3408
  201. build/lib/siat/security_trend.py +0 -402
  202. build/lib/siat/security_trend2.py +0 -646
  203. build/lib/siat/stock.py +0 -4284
  204. build/lib/siat/stock_advice_linear.py +0 -934
  205. build/lib/siat/stock_base.py +0 -26
  206. build/lib/siat/stock_china.py +0 -2095
  207. build/lib/siat/stock_prices_kneighbors.py +0 -910
  208. build/lib/siat/stock_prices_linear.py +0 -386
  209. build/lib/siat/stock_profile.py +0 -707
  210. build/lib/siat/stock_technical.py +0 -3305
  211. build/lib/siat/stooq.py +0 -74
  212. build/lib/siat/transaction.py +0 -347
  213. build/lib/siat/translate.py +0 -5183
  214. build/lib/siat/valuation.py +0 -1378
  215. build/lib/siat/valuation_china.py +0 -2076
  216. build/lib/siat/var_model_validation.py +0 -444
  217. build/lib/siat/yf_name.py +0 -811
  218. siat-3.10.132.dist-info/RECORD +0 -218
@@ -1,727 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
- 版权:王德宏,北京外国语大学国际商学院
4
- 功能:
5
- 1、获取证券价格,多种方法
6
- 2、首先获取单一证券的价格,其后证券列表的价格
7
- 3、支持股票、基金、债券、申万行业指数
8
- 版本:0.1,2024-4-3,经过多方测试
9
- """
10
-
11
- #==============================================================================
12
- #关闭所有警告
13
- import warnings; warnings.filterwarnings('ignore')
14
- from siat.common import *
15
- from siat.translate import *
16
- from siat.security_prices import *
17
- from siat.other_indexes import *
18
- #==============================================================================
19
- import pandas as pd
20
- #==============================================================================
21
- SUFFIX_LIST_CN=['SS','SZ','BJ','SW','SH']
22
- SUFFIX_LIST_HK=['HK']
23
- #==============================================================================
24
-
25
- #==============================================================================
26
- #==============================================================================
27
- if __name__=='__main__':
28
- ticker='AAPL' #股票
29
- ticker='600519.SS'
30
- ticker='00700.HK'
31
- ticker='OPN.PL'
32
-
33
- ticker='000001.SS' #指数
34
- ticker='000300.SS'
35
-
36
- ticker='sh018003' #债券
37
- ticker='sz149808'
38
- ticker='sz159998' #基金
39
-
40
- ticker='801010.SW' #申万
41
- ticker='851242.SW' #申万
42
- ticker='807110.SW'
43
-
44
- ticker='AAPL'
45
-
46
- ticker='GC=F'
47
-
48
- ticker="006257"
49
- ticker_type='auto'
50
-
51
- fromdate='2024-5-1'; todate='2024-5-20'
52
-
53
- ticker_type='auto'
54
- ticker_type='bond'
55
-
56
- ticker='000418'
57
- ticker='180202.SZ'
58
- ticker_type='fund'
59
-
60
- source='auto'
61
- #source='yahoo'
62
-
63
- adjust=''
64
- adjust='qfq'
65
-
66
- fill=False
67
- fill=True
68
-
69
- # 新测试组
70
- ticker="XAUUSD"
71
- ticker="^NSEI"
72
- fromdate='2024-5-1'; todate='2024-5-20'
73
-
74
- ticker="BMW.DE"
75
- fromdate='2022-6-1'; todate='2025-6-15'
76
-
77
- ticker_type='auto';source='auto'
78
- adjust='';fill=False
79
-
80
-
81
- price,found=get_price_1ticker(ticker=ticker,fromdate=fromdate,todate=todate, \
82
- ticker_type=ticker_type)
83
-
84
- def get_price_1ticker(ticker,fromdate,todate, \
85
- ticker_type='auto',source='auto', \
86
- adjust='',fill=False):
87
- """
88
- 功能:抓取一只证券的价格序列,不处理列表,不处理投资组合
89
- 类型优先顺序:ticker_type,auto-自动,stock-指定股票,fund-指定基金,bond-指定债券
90
- 数据源优先顺序:1-source,2-ticker属地,3-ticker_type
91
- adjust:""-未复权,qfq-前复权,hfq-后复权,qfq-factor:前复权因子和调整,hfq-factor: 后复权因子和调整
92
- 返回值:
93
- df: None-未找到ticker,空df-找到ticker但规定时间段内无数据
94
- found: 字符串,Found-找到且有数据,Empty-找到但规定时间段内无数据,None-未找到ticker。
95
- 简化使用者判断
96
- fill:为现有数据开始结束日期之间的工作日填充=True
97
- """
98
- #设置雅虎yfinance开关
99
- YF=False
100
- #设置雅虎pandas_datareader开关,避免在此花费时间
101
- PDR_yahoo=False
102
-
103
- #返回值初始状态,确保各种情况下都有返回值
104
- df=None
105
- found='None'
106
-
107
- #只处理字符串
108
- if not isinstance(ticker,str):
109
- print(" #Warning(get_price_1ticker):",ticker,"is not a single security code")
110
- return df,found
111
-
112
- #检查日期期间合理性
113
- valid_period,fromdatepd,todatepd=check_period(fromdate,todate)
114
- if not valid_period:
115
- pass
116
- #print(" #Warning(get_price_1ticker): invalid date period from",fromdate,"to",todate)
117
- return df,found
118
-
119
- #检查复权选项合理性:adj_only特别指最高最低价开盘收盘价全为复权价
120
- ak_fq_list=['','qfq','hfq','qfq-factor','hfq-factor','adj_only']
121
- if adjust not in ak_fq_list:
122
- adjust='qfq'
123
-
124
- #变换ticker为内部格式(yahoo格式)
125
- ticker1=ticker1_cvt2yahoo(ticker)
126
- _,prefix,suffix=split_prefix_suffix(ticker1)
127
-
128
- #预处理ticker_type
129
- ticker_type=ticker_type_preprocess_1str(ticker,ticker_type)
130
-
131
- #数据源情形1:akshare
132
- if source in ['auto','sina','em']:
133
- #中国的证券
134
- if suffix in SUFFIX_LIST_CN:
135
- #含处理证券类型优先级
136
- dft=get_price_ak_cn(ticker1,fromdate,todate,adjust=adjust,ticker_type=ticker_type)
137
- found=df_have_data(dft)
138
-
139
- #香港的证券
140
- if suffix in SUFFIX_LIST_HK and found not in ['Found','Empty']:
141
- dft=get_price_ak_hk(ticker1,fromdate,todate,adjust=adjust)
142
- found=df_have_data(dft)
143
-
144
- #是否美股
145
- if found not in ['Found','Empty']:
146
- adjust='qfq' if adjust != '' else ''
147
- dft=get_price_ak_us(ticker1,fromdate,todate,adjust=adjust)
148
- found=df_have_data(dft)
149
- """
150
- if ticker_type in ['fund']:
151
- #变换代码格式
152
- ticker2=tickers_cvt2ak(ticker1)
153
- try:
154
- #优先抓取开放式基金单位净值
155
- dft =get_price_oef_china(ticker2,fromdate,todate)
156
- dft['Date']=dft.index
157
- except:
158
- dft=None
159
- found=df_have_data(dft)
160
-
161
- if ticker_type in ['bond']:
162
- #变换代码格式
163
- ticker2=tickers_cvt2ak(ticker1)
164
- try:
165
- #最后抓取交易所债券行情
166
- dft = exchange_bond_price(ticker2,fromdate,todate,graph=False,data_crop=False)
167
- dft['Date']=dft.index
168
- except:
169
- #print(" #Error(get_price_ak_cn): failed to find prices for",ticker)
170
- return None
171
- found=df_have_data(dft)
172
- """
173
- #数据源情形2:stooq
174
- if source in ['auto','stooq'] and found not in ['Found','Empty']:
175
- dft=get_price_stooq(ticker1,fromdate,todate)
176
- found=df_have_data(dft)
177
-
178
- #访问雅虎财经
179
- if source in ['auto','yahoo'] and found not in ['Found','Empty']:
180
- dft=None
181
- if test_yahoo_finance():
182
- #数据源情形3a:yahoo, yahooquery, 需要访问yahoo
183
- dft=get_price_yq(ticker1,fromdate,todate)
184
- found=df_have_data(dft)
185
-
186
- #数据源情形3b:yahoo, yfinance, 需要访问yahoo,直接为复权价
187
- if found not in ['Found','Empty']:
188
- if YF:
189
- dft=get_price_yf(ticker1,fromdate,todate)
190
- found=df_have_data(dft)
191
-
192
- #数据源情形4:yahoo, pandas_datareader,需要访问yahoo,似乎不工作了!
193
- if found not in ['Found','Empty']:
194
- if PDR_yahoo:
195
- dft=get_prices_yahoo(ticker1,fromdate,todate)
196
- found=df_have_data(dft)
197
- else:
198
- if source in ['yahoo']:
199
- print(" #Warning(get_price_1ticker): sorry, yahoo is currently inaccessible")
200
-
201
- #数据源情形5:FRED, 仅用于几个常用指数,备用
202
- if source in ['auto'] and found not in ['Found','Empty']:
203
- dft=get_index_fred(ticker1,fromdate,todate)
204
- found=df_have_data(dft)
205
-
206
- #数据源情形6:仅用于几个另类非常用指数,例如胡志明指数/卡拉奇指数/埃及指数等,新浪/东方财富
207
- if source in ['auto','sina','em'] and found not in ['Found','Empty']:
208
- dft=get_other_index_ak(ticker1,fromdate,todate)
209
- found=df_have_data(dft)
210
-
211
- #数据源情形7:Tiingo,每日限1000次调用
212
- if source in ['auto','ti'] and found not in ['Found','Empty']:
213
- dft=get_price_tiingo(ticker1,fromdate,todate)
214
- found=df_have_data(dft)
215
-
216
- #数据源情形8:alpha_vantage,每日限25次调用
217
- if source in ['auto','av'] and found not in ['Found','Empty']:
218
- dft=get_prices_av(ticker1,fromdate,todate)
219
- found=df_have_data(dft)
220
-
221
- #数据源情形9:Alpha Vantage, pandas_datareader, 可进行模糊匹配,但匹配准确度不确定!
222
- if source in ['auto','av'] and found not in ['Found','Empty']:
223
- dft=get_prices_av_pdr(ticker1,fromdate,todate)
224
- found=df_have_data(dft)
225
-
226
- """
227
- IEX:获得投资交易信息,需要API_KEY
228
- Econdb:提供超过90家官方统计机构提供的经济数据,免费
229
- Enigma:交易数据,需要API
230
- Quandl:股价和基金交易数据,需要API_KEY
231
- FRED:来自FRED的金融研究数据,编码特殊,需要转换
232
- Fama/French:来自Fama/French数据实验室的数据
233
- World Bank:世行数据
234
- OECD:经合组织数据
235
- Eurostat:欧洲统计局数据
236
- TSP Fund Data:TSP(Thrift Savings Plan) 基金数据
237
- Nasdaq Trader Symbol Definitions:Nasdaq 股票代码定义文档 (包含公司名,上市状态等一些数据)
238
- MOEX Data:莫斯科交易所数据
239
- Bank of Canada:加拿大银行数据
240
- """
241
-
242
- #整理字段
243
- if found in ['Found','Empty']:
244
- dft1_cols=['Open','High','Low','Close','Volume','Adj Close','source','ticker']
245
- dft1=dft[dft1_cols]
246
-
247
- dft1['name']=ticker_name(ticker1,ticker_type=ticker_type)
248
- dft1['Amount']=dft1.apply(lambda x: x['Close'] * x['Volume'],axis=1)
249
- else:
250
- dft1=dft
251
-
252
- if found == 'None':
253
- print(" Sorry, tried all means,",ticker,'info not found or inaccessible')
254
- if found == 'Empty':
255
- print(" Pity, zero record available for",ticker,'from',fromdate,'to',todate)
256
-
257
- if found in ['Found'] and fill:
258
- #需要产生连续工作日日期,以便对缺失值填充
259
- dft1=df_fill_extend(dft1,colname='Close',extend_business_date=True)
260
- dft1['Volume']=dft1.apply(lambda x: 0 if x['filled']==True else x['Volume'],axis=1)
261
- dft1['Amount']=dft1.apply(lambda x: 0 if x['filled']==True else x['Amount'],axis=1)
262
-
263
- return dft1,found
264
-
265
- #==============================================================================
266
- if __name__=='__main__':
267
- ticker=["430047.BJ","600519.SS","000858.SZ"] #全股票
268
- ticker_type='auto'
269
- ticker_type=['auto','stock']
270
-
271
- ticker=["sz169107","sh510050","sh010504","000858.SZ"] #LOF基金,ETF基金,国债,股票
272
- ticker_type='bond'
273
-
274
- ticker=['801002.SW',"600519.SS","sh510050","sh010504"] #申万指数,股票,ETF基金,国债
275
- ticker_type='bond'
276
-
277
- ticker=["180801.SZ","180101.SZ"]
278
- fromdate="2024-3-1"
279
- todate="2024-4-1"
280
- ticker_type='fund'
281
-
282
- adjust=''
283
- adjust=['','qfq']
284
-
285
- source='auto'
286
- fill=True
287
-
288
- #测试复权价
289
- ticker=['300750.SZ','300014.SZ']
290
- fromdate="2023-4-20"
291
- todate="2023-4-30"
292
- ticker_type='fund'
293
-
294
- adjust='qfq'
295
- source='auto'
296
- fill=False
297
-
298
-
299
- prices,found=get_price_mticker(ticker,fromdate,todate,adjust,source,ticker_type,fill)
300
-
301
- def get_price_mticker(ticker,fromdate,todate, \
302
- adjust='',source='auto',ticker_type='auto',fill=False):
303
- """
304
- 功能:多个证券(股票,指数,基金,债券),不含投资组合(否则容易引起递归调用)
305
- ticker_type:若为'auto'则基金优先于债券(代码重合时),亦可为列表分别指定优先抓取类型。
306
- 'stock', 'fund', 'bond',不足部分自动补充为最后项
307
- 其中,'auto'/'stock'/'fund'优先抓取指数、股票和基金;'bond'优先抓取债券;
308
-
309
- 注意:adjust,source,ticker_type既可以指定单个值,也可以使用列表分别指定各个证券
310
- 不足部分由最后1个值补全
311
- """
312
- DEBUG=False
313
-
314
- df=None; found='None'
315
-
316
- #将证券代码列表化
317
- if isinstance(ticker,list): ticker_list=ticker
318
- else: ticker_list=[ticker]
319
- ticker_num=len(ticker_list)
320
-
321
- #将adjust列表化:不足部分由列表中最后1个值补全
322
- if isinstance(adjust,list): adjust_list=adjust
323
- else: adjust_list=[adjust]
324
- adjust_len=len(adjust_list)
325
-
326
- if ticker_num > adjust_len:
327
- adjust1=adjust_list[-1] #延续最后项
328
- adjust_list=adjust_list + [adjust1]*(ticker_num - adjust_len)
329
-
330
- #将source列表化
331
- if isinstance(source,list): source_list=source
332
- else: source_list=[source]
333
- source_len=len(source_list)
334
-
335
- if ticker_num > source_len:
336
- source1=source_list[-1] #延续最后项
337
- source_list=source_list + [source1]*(ticker_num - source_len)
338
-
339
- #预处理ticker_type
340
- ticker_type_list=ticker_type_preprocess_mstr(ticker,ticker_type)
341
-
342
- #单个普通证券的特殊处理,不产生MultiIndex,避免后续程序识别出错
343
- if ticker_num == 1:
344
- df,found=get_price_1ticker(ticker=ticker_list[0],fromdate=fromdate,todate=todate, \
345
- adjust=adjust_list[0],source=source_list[0], \
346
- ticker_type=ticker_type_list[0],fill=fill)
347
-
348
- #多个普通证券
349
- if ticker_num > 1:
350
- for t in ticker_list:
351
- pos=ticker_list.index(t)
352
- at=adjust_list[pos]
353
- st=source_list[pos]
354
- tt=ticker_type_list[pos]
355
-
356
- #普通单个证券
357
- dft,found=get_price_1ticker(t,fromdate,todate,adjust=at,source=st, \
358
- ticker_type=tt,fill=fill)
359
- if found=='Found':
360
- columns=create_tuple_for_columns(dft,t)
361
- dft.columns=pd.MultiIndex.from_tuples(columns)
362
- else: continue
363
-
364
- if df is None: df=dft
365
- else: #合并
366
- df=pd.merge(df,dft,how='outer',left_index=True,right_index=True)
367
-
368
- found=df_have_data(df)
369
-
370
- return df,found
371
-
372
- #==============================================================================
373
- if __name__=='__main__':
374
- #股票组合
375
- ticker={'Market':('China','000001.SS','白酒组合'),'600519.SS':0.4,'000858.SZ':0.6}
376
- ticker_type='auto'
377
-
378
- #股债/股基组合
379
- ticker={'Market':('China','000001.SS','股债组合'),'600519.SS':0.4,'sh010504':0.6}
380
- ticker_type='bond' #股债组合
381
- ticker_type='auto' #股基组合
382
-
383
- #股债基组合:分别指定每个成份股的品种(股票,债券,基金),份额自动换算
384
- ticker={'Market':('China','000001.SS','股债基组合'),'600519.SS':50,'sh018003':150,'sh010504':300}
385
- ticker_type=['stock','bond','fund']
386
-
387
- fromdate='2024-1-1'
388
- todate='2024-4-1'
389
- adjust=''
390
- source='auto'
391
- fill=True
392
-
393
- pf,found=get_price_1portfolio(ticker=ticker,fromdate=fromdate,todate=todate, \
394
- adjust=adjust,source=source,ticker_type=ticker_type, \
395
- fill=fill)
396
-
397
- def get_price_1portfolio(ticker,fromdate,todate, \
398
- adjust='',source='auto',ticker_type='bond',fill=True):
399
- """
400
- 功能:抓取1个投资组合的信息,不能处理单个证券或证券列表
401
- 注意:
402
- 投资组合采用字典格式,但各个成份股份额之和不一定为1
403
- fill默认为True,否则由于成份股股价为nan时投资组合价格会受影响
404
- """
405
-
406
- #返回值初始状态,确保各种情况下都有返回值
407
- df=None
408
- found='None'
409
-
410
- #只处理投资组合
411
- if not isinstance(ticker,dict):
412
- print(" #Warning(get_price_1portfolio): not in dict format for",ticker)
413
- return df,found
414
-
415
- #检查日期期间合理性
416
- valid_period,fromdatepd,todatepd=check_period(fromdate,todate)
417
- if not valid_period:
418
- print(" #Warning(v): invalid date period from",fromdate,"to",todate)
419
- return df,found
420
-
421
- #拆分投资组合为成份股列表和份额列表
422
- _,_,tickerlist,sharelist,ticker_type=decompose_portfolio(ticker)
423
-
424
- #处理份额列表,确保其和为1
425
- share_sum=sum(sharelist)
426
- sharelist1=[x / share_sum for x in sharelist]
427
-
428
- #预处理ticker_type,是否还需要?
429
- ticker_type=ticker_type_preprocess_1portfolio(ticker,ticker_type)
430
-
431
- #抓取各个成份股的价格信息
432
- prices,found=get_price_mticker(ticker=tickerlist, \
433
- fromdate=fromdate,todate=todate, \
434
- adjust=adjust,source=source, \
435
- ticker_type=ticker_type,fill=fill)
436
-
437
- if found not in ['Found','Empty']:
438
- return df,found
439
-
440
- #加权平均,矩阵乘法
441
- df=pd.DataFrame() #为None时无法对其赋值
442
- collist=['Open','High','Low','Close','Adj Close','Amount']
443
- for c in collist:
444
- dfc=prices[c]
445
- df[c]=dfc.dot(sharelist1)
446
-
447
- df['Volume']=df.apply(lambda x: (x['Amount'] / x['Close']),axis=1)
448
- df['ticker']=portfolio_name(ticker)
449
- df['name']=df['ticker']
450
- df['source']=source
451
- df['component']=str(list(prices['name'].tail(1).values[0]))
452
- df['portion']=str(sharelist1)
453
-
454
- return df,found
455
-
456
- #==============================================================================
457
- if __name__=='__main__':
458
- #股票组合
459
- pf1={'Market':('China','000001.SS','白酒组合'),'600519.SS':0.4,'000858.SZ':0.6}
460
- pf2={'Market':('China','000001.SS','股债组合'),'600519.SS':0.4,'sh010504':0.6}
461
- pf3={'Market':('China','000001.SS','股债基组合'),'600519.SS':50,'sh018003':150,'sh010504':300}
462
-
463
- pflist=[pf1,pf2]
464
- pflist=[pf1,pf2,pf3]
465
-
466
- ticker_type='bond'
467
- ticker_type=['stock','bond','bond']
468
-
469
- fromdate='2024-1-1'
470
- todate='2024-4-1'
471
- adjust=''
472
- source='auto'
473
- fill=True
474
-
475
- pfs,found=get_price_mportfolio(ticker=pflist,fromdate=fromdate,todate=todate, \
476
- adjust=adjust,source=source, \
477
- ticker_type=ticker_type,fill=fill)
478
-
479
-
480
- def get_price_mportfolio(ticker,fromdate,todate, \
481
- adjust='',source='auto',ticker_type='bond',fill=True):
482
- """
483
- 功能:多个投资组合(股票,指数,基金,债券),不含非投资组合(否则容易引起递归调用)
484
- ticker_type:可为单个、列表或列表中含有列表
485
-
486
- 注意:adjust,source,ticker_type既可以指定单个值,也可以使用列表分别指定各个投资组合
487
- 不足部分由最后1个值补全
488
- """
489
- DEBUG=False
490
-
491
- df=None; found='None'
492
-
493
- #将投资组合列表化
494
- if isinstance(ticker,list): ticker_list=ticker
495
- elif isinstance(ticker,dict): ticker_list=[ticker]
496
- else: return df,found
497
- ticker_num=len(ticker_list)
498
-
499
- #将adjust列表化:不足部分由列表中最后1个值补全
500
- if isinstance(adjust,list): adjust_list=adjust
501
- else: adjust_list=[adjust]
502
- adjust_len=len(adjust_list)
503
-
504
- if ticker_num > adjust_len:
505
- adjust1=adjust_list[-1] #延续最后项
506
- adjust_list=adjust_list + [adjust1]*(ticker_num - adjust_len)
507
-
508
- #将source列表化和补全
509
- if isinstance(source,list): source_list=source
510
- else: source_list=[source]
511
- source_len=len(source_list)
512
-
513
- if ticker_num > source_len:
514
- source1=source_list[-1] #延续最后项
515
- source_list=source_list + [source1]*(ticker_num - source_len)
516
-
517
- #预处理ticker_type
518
- ticker_type_list=ticker_type_preprocess_mticker_mixed(ticker,ticker_type)
519
-
520
- #单个投资组合的特殊处理,不产生MultiIndex,避免后续程序识别出错
521
- if ticker_num == 1:
522
- df,found=get_price_1portfolio(ticker=ticker_list[0],fromdate=fromdate,todate=todate, \
523
- adjust=adjust_list[0],source=source_list[0], \
524
- ticker_type=ticker_type_list[0],fill=fill)
525
-
526
- #多个投资组合
527
- if ticker_num > 1:
528
- for t in ticker_list:
529
- pos=ticker_list.index(t)
530
- at=adjust_list[pos]
531
- st=source_list[pos]
532
- tt=ticker_type_list[pos]
533
-
534
- #单个投资组合
535
- dft,found=get_price_1portfolio(ticker=t,fromdate=fromdate,todate=todate, \
536
- adjust=at,source=st,ticker_type=tt,fill=fill)
537
- if found=='Found':
538
- tn=ticker_name(t)
539
- columns=create_tuple_for_columns(dft,tn)
540
- dft.columns=pd.MultiIndex.from_tuples(columns)
541
- else: continue
542
-
543
- if df is None: df=dft
544
- else: #合并
545
- df=pd.merge(df,dft,how='outer',left_index=True,right_index=True)
546
-
547
- found=df_have_data(df)
548
-
549
- return df,found
550
-
551
- #==============================================================================
552
- if __name__=='__main__':
553
- #股票/债券/基金+投资组合
554
- pf={'Market':('China','000001.SS','白酒组合'),'600519.SS':0.4,'000858.SZ':0.6}
555
- ticker=[pf,'000002.SZ','002594.SZ','sh018003','sh010504']
556
- ticker_type=[['auto','stock'],'auto','auto','bond'] #不足部分自动延续最后一个类型
557
-
558
- ticker=["180801.SZ","180101.SZ"]
559
- ticker_type='fund'
560
-
561
- fromdate='2024-1-1'
562
- todate='2024-4-1'
563
- adjust=''
564
- source='auto'
565
- fill=True
566
-
567
- mix,found=get_price_mticker_mixed(ticker=ticker,fromdate=fromdate,todate=todate,ticker_type=ticker_type)
568
-
569
- def get_price_mticker_mixed(ticker,fromdate,todate, \
570
- adjust='',source='auto',ticker_type='auto',fill=False):
571
- """
572
- 功能:混合抓取证券列表的价格信息,列表中的元素可为多个股票、基金、债券或投资组合
573
- 注意:
574
- 可为列表中的各个元素分别指定证券类型ticker_type
575
- 若元素为投资组合可为其指定类型列表(即列表中的列表)
576
- 某个元素为债券时证券类型需要选择'bond'(即优先寻找债券),余者指定为'auto'即可
577
-
578
- """
579
-
580
- #返回值初始状态,确保各种情况下都有返回值
581
- df=None; found='None'
582
-
583
- #将ticker列表化
584
- if isinstance(ticker,list): ticker_list=ticker
585
- else: ticker_list=[ticker]
586
- ticker_num=len(ticker_list)
587
-
588
- #将adjust列表化和补全:不足部分由列表中最后1个值补全
589
- if isinstance(adjust,list): adjust_list=adjust
590
- else: adjust_list=[adjust]
591
- adjust_len=len(adjust_list)
592
-
593
- if ticker_num > adjust_len:
594
- adjust1=adjust_list[-1] #延续最后项
595
- adjust_list=adjust_list + [adjust1]*(ticker_num - adjust_len)
596
-
597
- #将source列表化和补全
598
- if isinstance(source,list): source_list=source
599
- else: source_list=[source]
600
- source_len=len(source_list)
601
-
602
- if ticker_num > source_len:
603
- source1=source_list[-1] #延续最后项
604
- source_list=source_list + [source1]*(ticker_num - source_len)
605
-
606
- #预处理ticker_type
607
- ticker_type_list=ticker_type_preprocess_mticker_mixed(ticker,ticker_type)
608
-
609
- #若列表中仅有1个元素,不需要产生MultiIndex
610
- if ticker_num == 1:
611
- #单个证券
612
- if isinstance(ticker_list[0],str):
613
- df,found=get_price_1ticker(ticker=ticker_list[0],fromdate=fromdate,todate=todate, \
614
- ticker_type=ticker_type_list[0],source=source_list[0], \
615
- adjust=adjust_list[0],fill=fill)
616
-
617
- #单个投资组合
618
- if isinstance(ticker_list[0],dict):
619
- df,found=get_price_1portfolio(ticker=ticker_list[0],fromdate=fromdate,todate=todate, \
620
- ticker_type=ticker_type_list[0],source=source_list[0], \
621
- adjust=adjust_list[0],fill=fill)
622
-
623
- return df,found
624
-
625
- #若ticker是列表************************************************************
626
- if ticker_num > 1:
627
- for t in ticker_list:
628
- pos=ticker_list.index(t)
629
- at=adjust_list[pos]
630
- st=source_list[pos]
631
- tt=ticker_type_list[pos]
632
-
633
- #单个普通证券
634
- if isinstance(t,str):
635
- dft,found=get_price_1ticker(ticker=t,fromdate=fromdate,todate=todate, \
636
- adjust=at,source=st,ticker_type=tt,fill=fill)
637
- tn=t
638
-
639
- #单个投资组合
640
- if isinstance(t,dict):
641
- dft,found=get_price_1portfolio(ticker=t,fromdate=fromdate,todate=todate, \
642
- adjust=at,source=st,ticker_type=tt,fill=fill)
643
- tn=ticker_name(t)
644
-
645
- if found=='Found':
646
- columns=create_tuple_for_columns(dft,tn)
647
- dft.columns=pd.MultiIndex.from_tuples(columns)
648
- else: continue
649
-
650
- if df is None: df=dft
651
- else: #合并
652
- df=pd.merge(df,dft,how='outer',left_index=True,right_index=True)
653
-
654
- found=df_have_data(df)
655
-
656
- return df,found
657
-
658
- #==============================================================================
659
- if __name__=='__main__':
660
- #股票/债券/基金+投资组合
661
- ticker={'Market':('China','000001.SS','白酒组合'),'600519.SS':0.4,'000858.SZ':0.6}
662
- ticker='sh018003'
663
- ticker_type='bond'
664
-
665
- ticker='180202.SZ'
666
- ticker_type='fund'
667
-
668
- ticker='851242.SW'
669
- ticker_type='auto'
670
-
671
- fromdate='2021-1-1'
672
- todate='2024-5-30'
673
-
674
- ticker='BMW.DE'
675
- fromdate='2024-6-2'
676
- todate='2025-6-15'
677
-
678
- adjust=''
679
- source='auto'
680
- fill=True
681
-
682
- mixed,found=get_price_1ticker_mixed(ticker=ticker,fromdate=fromdate,todate=todate, \
683
- ticker_type=ticker_type)
684
-
685
- def get_price_1ticker_mixed(ticker,fromdate,todate, \
686
- adjust='',source='auto',ticker_type='auto',fill=False):
687
- """
688
- 功能:混合抓取证券列表的价格信息,列表中的元素需为单个股票、基金、债券或投资组合
689
- 方便仅需一只证券的调用
690
- """
691
-
692
- #返回值初始状态,确保各种情况下都有返回值
693
- df=None; found='None'
694
-
695
- #若ticker为列表则取其第1个元素
696
- if isinstance(ticker,list): ticker=ticker[0]
697
-
698
- #若adjust为列表则取其第1个元素
699
- if isinstance(adjust,list): adjust=adjust[0]
700
-
701
- #若source为列表则取其第1个元素
702
- if isinstance(source,list): source=source[0]
703
-
704
- #若ticker_type为列表则取其第1个元素
705
- if isinstance(ticker_type,list): ticker_type=ticker_type[0]
706
-
707
- #单个证券
708
- if isinstance(ticker,str):
709
- df,found=get_price_1ticker(ticker=ticker,fromdate=fromdate,todate=todate, \
710
- ticker_type=ticker_type,source=source, \
711
- adjust=adjust,fill=fill)
712
-
713
- #单个投资组合
714
- if isinstance(ticker,dict):
715
- df,found=get_price_1portfolio(ticker=ticker,fromdate=fromdate,todate=todate, \
716
- ticker_type=ticker_type,source=source, \
717
- adjust=adjust,fill=fill)
718
- #对空缺值填充,以免影响后续计算
719
- df=df.fillna(method='ffill')
720
- df=df.fillna(method='bfill')
721
-
722
- return df,found
723
-
724
-
725
- #==============================================================================
726
- #==============================================================================
727
- #==============================================================================