siat 3.10.131__py3-none-any.whl → 3.10.132__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 (220) hide show
  1. build/lib/build/lib/siat/__init__.py +75 -0
  2. build/lib/build/lib/siat/allin.py +137 -0
  3. build/lib/build/lib/siat/assets_liquidity.py +915 -0
  4. build/lib/build/lib/siat/beta_adjustment.py +1058 -0
  5. build/lib/build/lib/siat/beta_adjustment_china.py +548 -0
  6. build/lib/build/lib/siat/blockchain.py +143 -0
  7. build/lib/build/lib/siat/bond.py +2900 -0
  8. build/lib/build/lib/siat/bond_base.py +992 -0
  9. build/lib/build/lib/siat/bond_china.py +100 -0
  10. build/lib/build/lib/siat/bond_zh_sina.py +143 -0
  11. build/lib/build/lib/siat/capm_beta.py +783 -0
  12. build/lib/build/lib/siat/capm_beta2.py +887 -0
  13. build/lib/build/lib/siat/common.py +5360 -0
  14. build/lib/build/lib/siat/compare_cross.py +642 -0
  15. build/lib/build/lib/siat/copyrights.py +18 -0
  16. build/lib/build/lib/siat/cryptocurrency.py +667 -0
  17. build/lib/build/lib/siat/economy.py +1471 -0
  18. build/lib/build/lib/siat/economy2.py +1853 -0
  19. build/lib/build/lib/siat/esg.py +536 -0
  20. build/lib/build/lib/siat/event_study.py +815 -0
  21. build/lib/build/lib/siat/fama_french.py +1521 -0
  22. build/lib/build/lib/siat/fin_stmt2_yahoo.py +982 -0
  23. build/lib/build/lib/siat/financial_base.py +1160 -0
  24. build/lib/build/lib/siat/financial_statements.py +598 -0
  25. build/lib/build/lib/siat/financials.py +2339 -0
  26. build/lib/build/lib/siat/financials2.py +1278 -0
  27. build/lib/build/lib/siat/financials_china.py +4433 -0
  28. build/lib/build/lib/siat/financials_china2.py +2212 -0
  29. build/lib/build/lib/siat/fund.py +629 -0
  30. build/lib/build/lib/siat/fund_china.py +3307 -0
  31. build/lib/build/lib/siat/future_china.py +551 -0
  32. build/lib/build/lib/siat/google_authenticator.py +47 -0
  33. build/lib/build/lib/siat/grafix.py +3636 -0
  34. build/lib/build/lib/siat/holding_risk.py +867 -0
  35. build/lib/build/lib/siat/luchy_draw.py +638 -0
  36. build/lib/build/lib/siat/market_china.py +1168 -0
  37. build/lib/build/lib/siat/markowitz.py +2363 -0
  38. build/lib/build/lib/siat/markowitz2.py +3150 -0
  39. build/lib/build/lib/siat/markowitz2_20250704.py +2969 -0
  40. build/lib/build/lib/siat/markowitz2_20250705.py +3158 -0
  41. build/lib/build/lib/siat/markowitz_simple.py +373 -0
  42. build/lib/build/lib/siat/ml_cases.py +2291 -0
  43. build/lib/build/lib/siat/ml_cases_example.py +60 -0
  44. build/lib/build/lib/siat/option_china.py +3069 -0
  45. build/lib/build/lib/siat/option_pricing.py +1925 -0
  46. build/lib/build/lib/siat/other_indexes.py +409 -0
  47. build/lib/build/lib/siat/risk_adjusted_return.py +1576 -0
  48. build/lib/build/lib/siat/risk_adjusted_return2.py +1900 -0
  49. build/lib/build/lib/siat/risk_evaluation.py +2218 -0
  50. build/lib/build/lib/siat/risk_free_rate.py +351 -0
  51. build/lib/build/lib/siat/sector_china.py +4140 -0
  52. build/lib/build/lib/siat/security_price2.py +727 -0
  53. build/lib/build/lib/siat/security_prices.py +3408 -0
  54. build/lib/build/lib/siat/security_trend.py +402 -0
  55. build/lib/build/lib/siat/security_trend2.py +646 -0
  56. build/lib/build/lib/siat/stock.py +4284 -0
  57. build/lib/build/lib/siat/stock_advice_linear.py +934 -0
  58. build/lib/build/lib/siat/stock_base.py +26 -0
  59. build/lib/build/lib/siat/stock_china.py +2095 -0
  60. build/lib/build/lib/siat/stock_prices_kneighbors.py +910 -0
  61. build/lib/build/lib/siat/stock_prices_linear.py +386 -0
  62. build/lib/build/lib/siat/stock_profile.py +707 -0
  63. build/lib/build/lib/siat/stock_technical.py +3305 -0
  64. build/lib/build/lib/siat/stooq.py +74 -0
  65. build/lib/build/lib/siat/transaction.py +347 -0
  66. build/lib/build/lib/siat/translate.py +5183 -0
  67. build/lib/build/lib/siat/valuation.py +1378 -0
  68. build/lib/build/lib/siat/valuation_china.py +2076 -0
  69. build/lib/build/lib/siat/var_model_validation.py +444 -0
  70. build/lib/build/lib/siat/yf_name.py +811 -0
  71. build/lib/siat/__init__.py +75 -0
  72. build/lib/siat/allin.py +137 -0
  73. build/lib/siat/assets_liquidity.py +915 -0
  74. build/lib/siat/beta_adjustment.py +1058 -0
  75. build/lib/siat/beta_adjustment_china.py +548 -0
  76. build/lib/siat/blockchain.py +143 -0
  77. build/lib/siat/bond.py +2900 -0
  78. build/lib/siat/bond_base.py +992 -0
  79. build/lib/siat/bond_china.py +100 -0
  80. build/lib/siat/bond_zh_sina.py +143 -0
  81. build/lib/siat/capm_beta.py +783 -0
  82. build/lib/siat/capm_beta2.py +887 -0
  83. build/lib/siat/common.py +5360 -0
  84. build/lib/siat/compare_cross.py +642 -0
  85. build/lib/siat/copyrights.py +18 -0
  86. build/lib/siat/cryptocurrency.py +667 -0
  87. build/lib/siat/economy.py +1471 -0
  88. build/lib/siat/economy2.py +1853 -0
  89. build/lib/siat/esg.py +536 -0
  90. build/lib/siat/event_study.py +815 -0
  91. build/lib/siat/fama_french.py +1521 -0
  92. build/lib/siat/fin_stmt2_yahoo.py +982 -0
  93. build/lib/siat/financial_base.py +1160 -0
  94. build/lib/siat/financial_statements.py +598 -0
  95. build/lib/siat/financials.py +2339 -0
  96. build/lib/siat/financials2.py +1278 -0
  97. build/lib/siat/financials_china.py +4433 -0
  98. build/lib/siat/financials_china2.py +2212 -0
  99. build/lib/siat/fund.py +629 -0
  100. build/lib/siat/fund_china.py +3307 -0
  101. build/lib/siat/future_china.py +551 -0
  102. build/lib/siat/google_authenticator.py +47 -0
  103. build/lib/siat/grafix.py +3636 -0
  104. build/lib/siat/holding_risk.py +867 -0
  105. build/lib/siat/luchy_draw.py +638 -0
  106. build/lib/siat/market_china.py +1168 -0
  107. build/lib/siat/markowitz.py +2363 -0
  108. build/lib/siat/markowitz2.py +3150 -0
  109. build/lib/siat/markowitz2_20250704.py +2969 -0
  110. build/lib/siat/markowitz2_20250705.py +3158 -0
  111. build/lib/siat/markowitz_simple.py +373 -0
  112. build/lib/siat/ml_cases.py +2291 -0
  113. build/lib/siat/ml_cases_example.py +60 -0
  114. build/lib/siat/option_china.py +3069 -0
  115. build/lib/siat/option_pricing.py +1925 -0
  116. build/lib/siat/other_indexes.py +409 -0
  117. build/lib/siat/risk_adjusted_return.py +1576 -0
  118. build/lib/siat/risk_adjusted_return2.py +1900 -0
  119. build/lib/siat/risk_evaluation.py +2218 -0
  120. build/lib/siat/risk_free_rate.py +351 -0
  121. build/lib/siat/sector_china.py +4140 -0
  122. build/lib/siat/security_price2.py +727 -0
  123. build/lib/siat/security_prices.py +3408 -0
  124. build/lib/siat/security_trend.py +402 -0
  125. build/lib/siat/security_trend2.py +646 -0
  126. build/lib/siat/stock.py +4284 -0
  127. build/lib/siat/stock_advice_linear.py +934 -0
  128. build/lib/siat/stock_base.py +26 -0
  129. build/lib/siat/stock_china.py +2095 -0
  130. build/lib/siat/stock_prices_kneighbors.py +910 -0
  131. build/lib/siat/stock_prices_linear.py +386 -0
  132. build/lib/siat/stock_profile.py +707 -0
  133. build/lib/siat/stock_technical.py +3305 -0
  134. build/lib/siat/stooq.py +74 -0
  135. build/lib/siat/transaction.py +347 -0
  136. build/lib/siat/translate.py +5183 -0
  137. build/lib/siat/valuation.py +1378 -0
  138. build/lib/siat/valuation_china.py +2076 -0
  139. build/lib/siat/var_model_validation.py +444 -0
  140. build/lib/siat/yf_name.py +811 -0
  141. siat/__init__.py +0 -0
  142. siat/allin.py +0 -0
  143. siat/assets_liquidity.py +0 -0
  144. siat/beta_adjustment.py +0 -0
  145. siat/beta_adjustment_china.py +0 -0
  146. siat/blockchain.py +0 -0
  147. siat/bond.py +0 -0
  148. siat/bond_base.py +0 -0
  149. siat/bond_china.py +0 -0
  150. siat/bond_zh_sina.py +0 -0
  151. siat/capm_beta.py +0 -0
  152. siat/capm_beta2.py +0 -0
  153. siat/common.py +136 -3
  154. siat/compare_cross.py +0 -0
  155. siat/copyrights.py +0 -0
  156. siat/cryptocurrency.py +0 -0
  157. siat/economy.py +0 -0
  158. siat/economy2.py +0 -0
  159. siat/esg.py +0 -0
  160. siat/event_study.py +0 -0
  161. siat/exchange_bond_china.pickle +0 -0
  162. siat/fama_french.py +0 -0
  163. siat/fin_stmt2_yahoo.py +0 -0
  164. siat/financial_base.py +0 -0
  165. siat/financial_statements.py +0 -0
  166. siat/financials.py +0 -0
  167. siat/financials2.py +0 -0
  168. siat/financials_china.py +0 -0
  169. siat/financials_china2.py +0 -0
  170. siat/fund.py +0 -0
  171. siat/fund_china.pickle +0 -0
  172. siat/fund_china.py +0 -0
  173. siat/future_china.py +0 -0
  174. siat/google_authenticator.py +0 -0
  175. siat/grafix.py +1 -1
  176. siat/holding_risk.py +0 -0
  177. siat/luchy_draw.py +0 -0
  178. siat/market_china.py +1 -1
  179. siat/markowitz.py +0 -0
  180. siat/markowitz2.py +240 -39
  181. siat/markowitz2_20250704.py +2969 -0
  182. siat/markowitz2_20250705.py +3158 -0
  183. siat/markowitz_simple.py +0 -0
  184. siat/ml_cases.py +0 -0
  185. siat/ml_cases_example.py +0 -0
  186. siat/option_china.py +0 -0
  187. siat/option_pricing.py +0 -0
  188. siat/other_indexes.py +0 -0
  189. siat/risk_adjusted_return.py +0 -0
  190. siat/risk_adjusted_return2.py +0 -0
  191. siat/risk_evaluation.py +0 -0
  192. siat/risk_free_rate.py +0 -0
  193. siat/sector_china.py +0 -0
  194. siat/security_price2.py +0 -0
  195. siat/security_prices.py +3 -1
  196. siat/security_trend.py +0 -0
  197. siat/security_trend2.py +1 -1
  198. siat/stock.py +4 -2
  199. siat/stock_advice_linear.py +0 -0
  200. siat/stock_base.py +0 -0
  201. siat/stock_china.py +0 -0
  202. siat/stock_info.pickle +0 -0
  203. siat/stock_prices_kneighbors.py +0 -0
  204. siat/stock_prices_linear.py +0 -0
  205. siat/stock_profile.py +0 -0
  206. siat/stock_technical.py +0 -0
  207. siat/stooq.py +0 -0
  208. siat/transaction.py +0 -0
  209. siat/translate.py +11 -11
  210. siat/valuation.py +0 -0
  211. siat/valuation_china.py +0 -0
  212. siat/var_model_validation.py +0 -0
  213. siat/yf_name.py +0 -0
  214. {siat-3.10.131.dist-info → siat-3.10.132.dist-info}/METADATA +235 -227
  215. siat-3.10.132.dist-info/RECORD +218 -0
  216. {siat-3.10.131.dist-info → siat-3.10.132.dist-info}/WHEEL +1 -1
  217. {siat-3.10.131.dist-info → siat-3.10.132.dist-info/licenses}/LICENSE +0 -0
  218. siat-3.10.132.dist-info/top_level.txt +4 -0
  219. siat-3.10.131.dist-info/RECORD +0 -76
  220. siat-3.10.131.dist-info/top_level.txt +0 -1
@@ -0,0 +1,811 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ 本模块功能:SIAT公共转换函数,获取雅虎证券代码英文名称
4
+ 所属工具包:证券投资分析工具SIAT
5
+ SIAT:Security Investment Analysis Tool
6
+ 创建日期:2024年7月12日
7
+ 最新修订日期:
8
+ 作者:王德宏 (WANG Dehong, Peter)
9
+ 作者单位:北京外国语大学国际商学院
10
+ 作者邮件:wdehong2000@163.com
11
+ 版权所有:王德宏
12
+ 用途限制:仅限研究与教学使用,不可商用!商用需要额外授权。
13
+ 特别声明:作者不对使用本工具进行证券投资导致的任何损益负责!
14
+ """
15
+ #==============================================================================
16
+ #关闭所有警告
17
+ import warnings; warnings.filterwarnings('ignore')
18
+
19
+ #==============================================================================
20
+ if __name__=='__main__':
21
+ test_yahoo_access()
22
+
23
+ def test_yahoo_access():
24
+ """
25
+ 功能:测试雅虎财经是否可达
26
+ """
27
+ url="https://finance.yahoo.com/"
28
+ result=test_website(url)
29
+
30
+ return result
31
+
32
+ if __name__=='__main__':
33
+ url="https://finance.yahoo.com"
34
+ test_website(url)
35
+
36
+ def test_website(url):
37
+ import requests
38
+ headers = {
39
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
40
+ }
41
+ try:
42
+ response = requests.get(url,headers=headers)
43
+ if response.status_code == 200:
44
+ #print(f"Website {url} is accessible")
45
+ return True
46
+ else:
47
+ #print(f"Website {url} access failed,Code:{response.status_code}")
48
+ return False
49
+ except requests.exceptions.RequestException:
50
+ print(f" #Warning: website {url} is inaccessible")
51
+ return False
52
+
53
+ if __name__=='__main__':
54
+ s = "Hello, world. Python is fun!"
55
+ split_string(s)
56
+
57
+ def split_string(s):
58
+ import re
59
+ # 使用正则表达式匹配空格、逗号或句点
60
+ return re.split(r'[ ,.]', s)
61
+
62
+ if __name__=='__main__':
63
+ s = "Hello, world. Python is fun!"
64
+ filter_string(s)
65
+
66
+ def filter_string(s):
67
+ #排除证券名称中的多余空格、逗号和句号
68
+ slist=split_string(s)
69
+ s1=''
70
+ for sl in slist:
71
+ if sl != '':
72
+ if s1=='':
73
+ s1=sl
74
+ else:
75
+ s1=s1+' '+sl
76
+
77
+ return s1
78
+ #==============================================================================
79
+ if __name__=='__main__':
80
+ ticker='1155.KL'
81
+ ticker='MSFT'
82
+ ticker='G13.SI'
83
+ ticker='S63.SI'
84
+ ticker='SUS.ST'
85
+ ticker='600519.SS'
86
+ ticker='U11.SI'
87
+ ticker='1295.KL'
88
+ ticker='BMW.DE'
89
+ ticker='MBG.DE'
90
+ ticker='005930.KS'
91
+ ticker='LI'
92
+ ticker='600599.SS'
93
+ ticker='600123.SS'
94
+ ticker='600123.ss'
95
+ ticker='600999.ss'
96
+ ticker='600111.ss'
97
+ ticker='600333.ss'
98
+ ticker='600444.ss'
99
+ ticker='600777.ss'
100
+ ticker='GC=F'
101
+
102
+ yahoo_name1(ticker)
103
+
104
+ #极端测试
105
+ inamelist=[]
106
+ for i in range(100,150+1):
107
+ icode=str(600000+i)+'.SS'
108
+ iname=yahoo_name1(icode)
109
+ print(icode+':',iname)
110
+ inamelist=inamelist+[iname]
111
+
112
+ #发现问题后单独测试
113
+ ticker='600087.SS'
114
+ yahoo_name1(ticker)
115
+
116
+ yahoo_name1(ticker,short_name=True)
117
+
118
+ ticker_name(ticker)
119
+
120
+ def yahoo_name1x(ticker,short_name=False,add_suffix=True,maxlen=80):
121
+ """
122
+ 功能:从雅虎财经取得全球证券名称,仅限英文。需要去掉常用词,如Corporation
123
+ 优点:对未定义的证券代码也可给出英文名称,即使在中文语言环境中
124
+ 现存问题:需要访问雅虎,且耗时稍长
125
+ 废弃!!!
126
+ """
127
+ #测试雅虎
128
+ if not test_yahoo_access():
129
+ return ticker
130
+
131
+ #需要去掉的单词,注意顺序不要轻易颠倒!子串包含的,要长文在前!
132
+ remove_list=['Corporation','Berhad','Bhd','PLC','plc','Plc', \
133
+ ', Inc.','Inc.', \
134
+ 'AG ST','AG','NA O.N.', \
135
+ 'Aktiengesellschaft','(publ)', \
136
+ ', LLC','LLC', \
137
+ 'Co., Ltd.','Co., Ltd','Co.,Ltd.','Co.,Ltd','Co,.Ltd','co.,ltd', \
138
+ 'Co. LTD','CO.,LTD','Co., Limited', \
139
+ 'Ltd.','Ltd', \
140
+ 'Company', \
141
+ 'Incorporated', \
142
+ 'Corp., Ltd.','Corp.','Corp','AB', \
143
+ 'Limited', \
144
+
145
+ #强行缩短名称长度,去掉不影响名称的花哨词语
146
+ '(Group)','Group', \
147
+ 'Science & Technology','High-Tech','High Technology', \
148
+
149
+ #扫尾漏网之逗号句点
150
+ '.',',']
151
+
152
+ """
153
+ remove_list=['Corporation','Berhad','Bhd','PLC','plc','Limited', \
154
+ 'Inc', \
155
+ 'AG ST','AG','NA O.N.', \
156
+ 'Aktiengesellschaft','(publ)', \
157
+ 'LLC', \
158
+ 'Co., Ltd.','Ltd.','Ltd', \
159
+ 'Company', \
160
+ 'Incorporated','Corp.','AB']
161
+ """
162
+ #去掉ticker中的.US后缀
163
+ ticker=ticker.upper()
164
+ ticker1=ticker.replace('.US', "")
165
+
166
+ import yfinance as yf
167
+ ticker_info = yf.Ticker(ticker1)
168
+
169
+ try:
170
+ t_info=ticker_info.info
171
+ except:
172
+ pass
173
+ return ticker
174
+
175
+ try:
176
+ if short_name:
177
+ t_name0=t_info['shortName']
178
+ else:
179
+ t_name0=t_info['longName']
180
+ if len(t_name0) > maxlen:
181
+ t_name0=t_info['shortName']
182
+ except:
183
+ pass
184
+ return ticker #未找到ticker
185
+
186
+ #过滤逗号句点?过滤也可能带来更多复杂性!
187
+ #t_name1=filter_string(t_name0)
188
+ t_name1=t_name0
189
+
190
+ for r in remove_list:
191
+ t_name1=t_name1.replace(r, "")
192
+
193
+ #排除前后空格
194
+ t_name=t_name1.strip()
195
+
196
+ #增加交易所后缀
197
+ if add_suffix:
198
+ tlist=ticker.split('.')
199
+ if len(tlist)==2:
200
+ sid=tlist[1]
201
+ if sid not in ['SS','SZ','BJ']:
202
+ t_name=t_name+'('+sid+')'
203
+
204
+ return t_name
205
+
206
+ #==============================================================================
207
+ def replace_multiple_spaces(s):
208
+ import re
209
+ return re.sub(r'\s+', ' ', s)
210
+
211
+ #==============================================================================
212
+ if __name__=='__main__':
213
+ ticker='1155.KL'
214
+ ticker='MSFT'
215
+ ticker='G13.SI'
216
+ ticker='S63.SI'
217
+ ticker='SUS.ST'
218
+ ticker='600519.SS'
219
+ ticker='U11.SI'
220
+ ticker='1295.KL'
221
+ ticker='BMW.DE'
222
+ ticker='MBG.DE'
223
+ ticker='005930.KS'
224
+ ticker='LI'
225
+ ticker='600599.SS'
226
+ ticker='600123.SS'
227
+ ticker='600123.ss'
228
+ ticker='600999.ss'
229
+ ticker='600111.ss'
230
+ ticker='600333.ss'
231
+ ticker='600444.ss'
232
+ ticker='600777.ss'
233
+ ticker='CPL.WA'
234
+
235
+ ticker='SWMCX'
236
+
237
+ yahoo_name1(ticker)
238
+
239
+ #极端测试
240
+ inamelist=[]
241
+ for i in range(0,50+1):
242
+ icode=str(600000+i)+'.SS'
243
+ iname=yahoo_name2(icode)
244
+ print(icode+':',iname)
245
+ inamelist=inamelist+[iname]
246
+
247
+ #发现问题后单独测试
248
+ ticker='600088.SS'
249
+ ticker="ALI=F"
250
+ ticker="ZS=F"
251
+ ticker="ES=F"
252
+
253
+ ticker_info(ticker)
254
+
255
+ yahoo_name1(ticker)
256
+ yahoo_name2(ticker)
257
+
258
+ yahoo_name2(ticker,short_name=True)
259
+
260
+ ticker_name(ticker)
261
+
262
+ numeric_to_date(1734652800)
263
+
264
+ def numeric_to_date(numeric):
265
+ # 数值转日期
266
+ from datetime import datetime, timedelta
267
+ epoch = datetime(1970, 1, 1)
268
+ return (epoch + timedelta(seconds=numeric)).strftime('%Y-%m-%d')
269
+
270
+ if __name__=='__main__':
271
+ ticker="ES=F" #期货
272
+ ticker="VIX241120C00035000" #期权
273
+
274
+ ticker_info(ticker, info="interest")
275
+ ticker_info(ticker, info="open interest")
276
+ ticker_info(ticker, info="volume")
277
+ ticker_info(ticker, info="average volume")
278
+ ticker_info(ticker, info="REGULAR CLOSE")
279
+ ticker_info(ticker, info="day average")
280
+
281
+ ticker_info(ticker, info=["regular close","fifty day average","two hundred day average"])
282
+
283
+ def ticker_info(ticker,info="all"):
284
+ """
285
+
286
+ 功能:显示yahoo证券代码的信息,可多个信息类型
287
+ """
288
+ if isinstance(info,str):
289
+ infos=[info]
290
+ elif isinstance(info,list):
291
+ infos=info
292
+ else:
293
+ print(" Sorry, unsupported info type:",info)
294
+ return
295
+
296
+ first_time=True
297
+ for i in infos:
298
+ if first_time:
299
+ ticker_info1(ticker,info=i,test_access=True,print_title=True)
300
+ first_time=False
301
+ else:
302
+ ticker_info1(ticker,info=i,test_access=False,print_title=False)
303
+
304
+ return
305
+
306
+ if __name__=='__main__':
307
+ ticker="TSLA260618C00330000"
308
+
309
+ ticker_info1(ticker)
310
+ ticker_info1(ticker, info="open interest")
311
+
312
+ def ticker_info1(ticker,info="all",test_access=True,print_title=True):
313
+ """
314
+
315
+ 功能:显示yahoo证券代码的信息,1个信息类型
316
+ """
317
+ #测试雅虎
318
+ if test_access:
319
+ if not test_yahoo_access():
320
+ print(" Sorry, data source Yahoo is currently not reachable")
321
+ return
322
+
323
+ #去掉ticker中的.US后缀
324
+ ticker=ticker.upper()
325
+ ticker1=ticker.replace('.US', "")
326
+
327
+ import yfinance as yf
328
+ ticker_info = yf.Ticker(ticker1)
329
+
330
+ import datetime
331
+ stoday = datetime.date.today().strftime("%Y-%m-%d")
332
+
333
+ info_list=info.split(); found=False
334
+ """
335
+ info_yahoo=(info_list[0]).lower()
336
+ for i in info_list[1:]:
337
+ info_yahoo=info_yahoo+(i.lower()).capitalize()
338
+ """
339
+
340
+ try:
341
+ t_info=ticker_info.info
342
+
343
+ if print_title:
344
+ print("*** Ticker",ticker,'Information @'+stoday)
345
+
346
+ if ('all' in info) or ('All' in info) or ('ALL' in info):
347
+ for k in t_info.keys():
348
+ if not 'Date' in k:
349
+ print(' '+k+':',t_info[k])
350
+ else:
351
+ print(' '+k+':',numeric_to_date(t_info[k]))
352
+ #display(t_info)
353
+ else:
354
+ for k in t_info.keys():
355
+ for i in info_list:
356
+ if (not i.lower() in k) and (not (i.lower()).capitalize() in k):
357
+ found=False; break
358
+ else:
359
+ found=True
360
+ if not found: continue
361
+
362
+ if not 'Date' in k:
363
+ print(' '+k+':',t_info[k])
364
+ else:
365
+ print(' '+k+':',numeric_to_date(t_info[k]))
366
+
367
+ except:
368
+ print(" Sorry, ticker",ticker,"is not found in data source Yahoo")
369
+
370
+ return
371
+
372
+
373
+ if __name__=='__main__':
374
+ ticker="SGC=F"
375
+ ticker="XAUUSD"
376
+ short_name=False;add_suffix=True;maxlen=80
377
+
378
+ yahoo_name1y(ticker)
379
+
380
+ def yahoo_name1y(ticker,short_name=False,add_suffix=True,maxlen=80):
381
+ """
382
+ 功能:从雅虎财经取得全球证券名称,仅限英文。需要去掉常用词,如Corporation
383
+ 优点:对未定义的证券代码也可给出英文名称,即使在中文语言环境中
384
+ 现存问题:需要访问雅虎,且耗时稍长;当ticker不存在时会提示一大堆错误信息
385
+ 仅作备用
386
+ """
387
+
388
+ #测试雅虎
389
+ if not test_yahoo_access():
390
+ return ticker
391
+
392
+ #去掉ticker中的.US后缀
393
+ ticker=ticker.upper()
394
+ ticker1=ticker.replace('.US', "")
395
+
396
+ import yfinance as yf
397
+ ticker_info = yf.Ticker(ticker1)
398
+
399
+ import os, sys
400
+ class HiddenPrints:
401
+ def __enter__(self):
402
+ self._original_stdout = sys.stdout
403
+ sys.stdout = open(os.devnull, 'w')
404
+
405
+ def __exit__(self, exc_type, exc_val, exc_tb):
406
+ sys.stdout.close()
407
+ sys.stdout = self._original_stdout
408
+
409
+ try:
410
+ with HiddenPrints():
411
+ t_info=ticker_info.info
412
+ except:
413
+ pass
414
+ return ticker
415
+
416
+ try:
417
+ if short_name:
418
+ try:
419
+ t_name0=t_info['shortName']
420
+ except:
421
+ t_name0=t_info['longName']
422
+ else:
423
+ try:
424
+ t_name0=t_info['longName']
425
+ except:
426
+ try:
427
+ t_name0=t_info['shortName']
428
+ except:
429
+ pass
430
+ return ticker
431
+
432
+
433
+ if len(t_name0) > maxlen:
434
+ t_name0=t_info['shortName']
435
+ except:
436
+ pass
437
+ return ticker #未找到ticker
438
+
439
+ #过滤名称中多余的尾部词汇
440
+ t_name=filter_stock_name(t_name0)
441
+
442
+ #增加交易所后缀
443
+ if add_suffix:
444
+ tlist=ticker.split('.')
445
+ if len(tlist)==2:
446
+ sid=tlist[1]
447
+ if sid not in ['SS','SZ','BJ']:
448
+ t_name=t_name+'('+sid+')'
449
+
450
+ return t_name
451
+
452
+ #==============================================================================
453
+ if __name__ == '__main__':
454
+ stock_code='KSL.AX'
455
+ stock_code='AAPL'
456
+ stock_code='600519.SS'
457
+ stock_code='6758.T'
458
+ stock_code='6758.JP'
459
+ stock_code='ULVR.L'
460
+ stock_code='ULVR.UK'
461
+
462
+ stock_code='1155.KL'
463
+ stock_code='MSFT'
464
+
465
+ stock_code='SWMCX'
466
+ stock_code='SGC=F'
467
+
468
+ yahoo_name1_direct(stock_code)
469
+
470
+
471
+ def yahoo_name1_direct(stock_code,add_suffix=True):
472
+ """
473
+ 功能:网页直接抓取,有的带后缀的股票代码可能失败,原因未知
474
+ """
475
+ import requests
476
+ from bs4 import BeautifulSoup
477
+
478
+ stock_code1=stock_code.upper()
479
+
480
+ #抓取证券名称
481
+ headers = {
482
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
483
+ }
484
+ # https://finance.yahoo.com/quote/SWMCX/
485
+ url = f"https://finance.yahoo.com/quote/{stock_code1}/"
486
+ response = requests.get(url,headers=headers)
487
+ if response.status_code == 200:
488
+ soup = BeautifulSoup(response.text, 'html.parser')
489
+ soup_title=soup.title
490
+ soup_text=soup_title.text
491
+ soup_text_list=soup_text.split('(')
492
+
493
+ t_name = soup_text_list[0].strip()
494
+ else:
495
+ #未找到证券代码
496
+ pass
497
+ return stock_code
498
+
499
+ #过滤名称中多余的尾部词汇
500
+ t_name=filter_stock_name(t_name)
501
+
502
+
503
+ return t_name
504
+ #==============================================================================
505
+ if __name__=='__main__':
506
+ original_name='Oponeo.pl SA'
507
+ original_name='Apple Inc'
508
+ original_name='Schwab US Mid-Cap Index'
509
+
510
+ original_name='Shanghai Gold (CNH) Futures,Apr'
511
+
512
+ filter_stock_name(original_name)
513
+
514
+ def filter_stock_name(original_name):
515
+ """
516
+ 功能:过滤从网站上抓取到的证券名称,去掉尾部的公司类别词汇,缩短长度,便于显示
517
+ """
518
+
519
+ #将字符串中的多个空格变为单个空格
520
+ original_name=replace_multiple_spaces(original_name)
521
+
522
+ #定义需要去掉的单词,注意顺序不要轻易颠倒!子串包含的,要长文在前!前置留空格的为避免误删
523
+ remove_list=[' CORPORATION',' BERHAD',' BHD',' PLC',' INC',' AG ST',' NA O N', \
524
+ ' AKTIENGESELLSCHAFT','(PUBL)',' LLC', \
525
+ ' CO LTD',' CO LIMITED',' LTD',' LIMITED',' COMPANY',' INCORPORATED', \
526
+ ' CORP LTD',' CORP',' AB',' CO', \
527
+ ' GROUP CO','(GROUP)',' GROUP', \
528
+ ' PL S A',' PL SA',' AG', \
529
+ ' SCIENCE & TECHNOLOGY',' HIGH-TECH',' HIGH TECHNOLOGY', \
530
+
531
+ ' APR',' MAY',' JUN',' JUL',' AUG',' SEP',' SEPT',' OCT',' NOV',' DEC',
532
+ ' JAN',' FEB',' MAR',
533
+
534
+ ]
535
+
536
+ #去掉逗号和句点
537
+ name1=original_name.replace(',',' ')
538
+ name1=name1.replace(' ',' ')
539
+ name2=name1.replace('.',' ')
540
+
541
+ #将字符串字母全部大写
542
+ name4=name2.upper()
543
+
544
+ name5=name4
545
+ for ss in remove_list:
546
+ name5=name5.replace(ss,'')
547
+
548
+ name6=name5.strip()
549
+
550
+ name7=original_name[:len(name6)+1]
551
+ name7=name7.replace(',','').replace('.','')
552
+
553
+ shorter_name=name7
554
+ return shorter_name
555
+
556
+
557
+ #==============================================================================
558
+ if __name__ == '__main__':
559
+ stock_code='OPN.PL'
560
+ stock_code='AAPL'
561
+ stock_code='600519.SS'
562
+ stock_code='6758.T'
563
+ stock_code='6758.JP'
564
+ stock_code='ULVR.L'
565
+ stock_code='ULVR.UK'
566
+
567
+ stock_code='1155.KL'
568
+ stock_code='MSFT'
569
+
570
+ stock_code='GC.F'
571
+ stock_code='XAUUSD'
572
+
573
+ stooq_name1(stock_code)
574
+
575
+
576
+ def stooq_name1(stock_code,add_suffix=True):
577
+
578
+ import requests
579
+ from bs4 import BeautifulSoup
580
+
581
+ stock_code1=stock_code.lower()
582
+
583
+ #美股:尾部增加.us
584
+ stock_code_list=stock_code1.split('.')
585
+ if len(stock_code_list)==1:
586
+ stock_code1=stock_code1+'.us'
587
+
588
+ #其他国家股票
589
+ if len(stock_code_list)==2:
590
+ code=stock_code_list[0]
591
+ sid=stock_code_list[1]
592
+
593
+ #中国股票:尾部变为.cn
594
+ if sid in ['ss','sz','bj']:
595
+ stock_code1=code+'.cn'
596
+
597
+ #日本股票:尾部变为.jp
598
+ if sid in ['t']:
599
+ stock_code1=code+'.jp'
600
+
601
+ #英国股票:尾部变为.uk
602
+ if sid in ['l']:
603
+ stock_code1=code+'.uk'
604
+
605
+ #XX国股票
606
+
607
+ #波兰股票:去掉尾部.PL
608
+ stock_code1=stock_code1.replace('.pl','')
609
+
610
+ #抓取证券名称
611
+ headers = {
612
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
613
+ }
614
+ url = f"https://stooq.com/q/?s={stock_code1}"
615
+ response = requests.get(url,headers=headers)
616
+ if response.status_code == 200:
617
+ soup = BeautifulSoup(response.text, 'html.parser')
618
+ soup_title=soup.title
619
+ soup_text=soup_title.text
620
+ soup_text_list=soup_text.split(' - ')
621
+
622
+ t_name = soup_text_list[1].strip()
623
+ else:
624
+ pass
625
+ return stock_code
626
+
627
+ #未找到证券代码
628
+ if t_name == 'Stooq':
629
+
630
+ #尝试不加后缀'.us'
631
+ url = f"https://stooq.com/q/?s={stock_code}"
632
+ response = requests.get(url,headers=headers)
633
+ if response.status_code == 200:
634
+ soup = BeautifulSoup(response.text, 'html.parser')
635
+ soup_title=soup.title
636
+ soup_text=soup_title.text
637
+ soup_text_list=soup_text.split(' - ')
638
+
639
+ t_name = soup_text_list[1].strip()
640
+ else:
641
+ pass
642
+ return stock_code
643
+
644
+ if t_name == 'Stooq':
645
+ return stock_code
646
+
647
+ #过滤名称中多余的尾部词汇
648
+ t_name=filter_stock_name(t_name)
649
+
650
+ #增加交易所后缀
651
+ if add_suffix:
652
+ tlist=stock_code1.split('.')
653
+ if len(tlist)==1: sid='PL'
654
+ if len(tlist)==2:
655
+ sid=tlist[1].upper()
656
+
657
+ if sid not in ['CN','US']:
658
+ t_name=t_name+'('+sid+')'
659
+
660
+ t_name=t_name.strip()
661
+
662
+ return t_name
663
+
664
+ #==============================================================================
665
+ #==============================================================================
666
+ if __name__ == '__main__':
667
+ stock_code='AAPL'
668
+ stock_code='600519.SS'
669
+ stock_code='6758.T'
670
+ stock_code='6758.JP'
671
+ stock_code='ULVR.L'
672
+ stock_code='1155.KL'
673
+ stock_code='MSFT'
674
+
675
+ stock_code='GC=F'
676
+ stock_code='XAUUSD'
677
+
678
+ yahoo_name1(stock_code)
679
+
680
+
681
+ def yahoo_name1(stock_code):
682
+
683
+ import requests
684
+ from bs4 import BeautifulSoup
685
+
686
+ stock_code1=stock_code.lower()
687
+
688
+ #抓取证券名称
689
+ headers = {
690
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
691
+ }
692
+ url = f"https://finance.yahoo.com/quote/{stock_code1}"
693
+ response = requests.get(url,headers=headers)
694
+ if response.status_code == 200:
695
+ soup = BeautifulSoup(response.text, 'html.parser')
696
+ soup_title=soup.title
697
+ soup_text=soup_title.text
698
+
699
+ stock_code2=stock_code1.upper()
700
+ try:
701
+ pos=soup_text.index(stock_code2)
702
+ except:
703
+ pass
704
+ return stock_code
705
+ t_name = soup_text[:pos-1].strip()
706
+ else:
707
+ pass
708
+ return stock_code
709
+
710
+ #过滤名称中多余的尾部词汇
711
+ t_name=filter_stock_name(t_name).strip()
712
+ t_name=filter_stock_name(t_name).strip()
713
+
714
+ return t_name
715
+
716
+ #==============================================================================
717
+
718
+
719
+
720
+
721
+ if __name__=='__main__':
722
+ ticker='1155.KL'
723
+ ticker='MSFT'
724
+ ticker='G13.SI'
725
+ ticker='S63.SI'
726
+ ticker='SUS.ST'
727
+ ticker='SUN.UK'
728
+ ticker='IUI1.DE'
729
+
730
+ ticker='600519.SS'
731
+ ticker='U11.SI'
732
+ ticker='1295.KL'
733
+ ticker='BMW.DE'
734
+ ticker='MBG.DE'
735
+ ticker='005930.KS'
736
+ ticker='LI'
737
+ ticker='ULVR.L'
738
+ ticker='KSL.AX'
739
+
740
+ ticker='SGC=F'
741
+
742
+ ticker='IBM'
743
+
744
+ ticker='XAUUSD'
745
+ short_name=False;add_suffix=True;maxlen=80
746
+
747
+ get_stock_name1_en(ticker)
748
+
749
+ def get_stock_name1_en(ticker,short_name=False,add_suffix=True,maxlen=80):
750
+ """
751
+ 功能:分别从stooq和yahoo网站获取证券名称,优先stooq(因为不需要vpn)
752
+ """
753
+ sname=ticker
754
+
755
+ try:
756
+ sname=stooq_name1(ticker,add_suffix=add_suffix)
757
+ except:
758
+ pass
759
+
760
+ if sname==ticker:
761
+ try:
762
+ sname=yahoo_name1(ticker,short_name=short_name,add_suffix=add_suffix,maxlen=maxlen)
763
+ except:
764
+ pass
765
+
766
+ return sname
767
+
768
+ #==============================================================================
769
+ #==============================================================================
770
+ if __name__ == '__main__':
771
+ ticker='600519.SS'
772
+ ticker='600305.SS'
773
+ ticker='200725.SZ'
774
+ ticker='000725.SZ'
775
+
776
+ get_stock_name_china_sina(ticker)
777
+
778
+ def get_stock_name_china_sina(ticker):
779
+ """
780
+ 功能:抓取股票的申万行业分类名称
781
+ """
782
+
783
+ import requests
784
+ from bs4 import BeautifulSoup
785
+
786
+ ticker6=ticker[:6]
787
+ headers = {
788
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
789
+ }
790
+ url=f"https://vip.stock.finance.sina.com.cn/corp/go.php/vCI_CorpOtherInfo/stockid/{ticker6}/menu_num/2.phtml"
791
+ response = requests.get(url,headers=headers)
792
+
793
+ if response.status_code == 200:
794
+ soup = BeautifulSoup(response.text, 'html.parser')
795
+ soup_text=soup.find(id="stockName").text
796
+
797
+ soup_text_list=soup_text.split('(')
798
+
799
+ t_name = soup_text_list[0].strip()
800
+
801
+ return t_name
802
+ else:
803
+ return ticker
804
+
805
+ #==============================================================================
806
+ #==============================================================================
807
+ #==============================================================================
808
+ #==============================================================================
809
+ #==============================================================================
810
+ #==============================================================================
811
+ #==============================================================================