siat 3.10.130__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.
- build/lib/build/lib/siat/__init__.py +75 -0
- build/lib/build/lib/siat/allin.py +137 -0
- build/lib/build/lib/siat/assets_liquidity.py +915 -0
- build/lib/build/lib/siat/beta_adjustment.py +1058 -0
- build/lib/build/lib/siat/beta_adjustment_china.py +548 -0
- build/lib/build/lib/siat/blockchain.py +143 -0
- build/lib/build/lib/siat/bond.py +2900 -0
- build/lib/build/lib/siat/bond_base.py +992 -0
- build/lib/build/lib/siat/bond_china.py +100 -0
- build/lib/build/lib/siat/bond_zh_sina.py +143 -0
- build/lib/build/lib/siat/capm_beta.py +783 -0
- build/lib/build/lib/siat/capm_beta2.py +887 -0
- build/lib/build/lib/siat/common.py +5360 -0
- build/lib/build/lib/siat/compare_cross.py +642 -0
- build/lib/build/lib/siat/copyrights.py +18 -0
- build/lib/build/lib/siat/cryptocurrency.py +667 -0
- build/lib/build/lib/siat/economy.py +1471 -0
- build/lib/build/lib/siat/economy2.py +1853 -0
- build/lib/build/lib/siat/esg.py +536 -0
- build/lib/build/lib/siat/event_study.py +815 -0
- build/lib/build/lib/siat/fama_french.py +1521 -0
- build/lib/build/lib/siat/fin_stmt2_yahoo.py +982 -0
- build/lib/build/lib/siat/financial_base.py +1160 -0
- build/lib/build/lib/siat/financial_statements.py +598 -0
- build/lib/build/lib/siat/financials.py +2339 -0
- build/lib/build/lib/siat/financials2.py +1278 -0
- build/lib/build/lib/siat/financials_china.py +4433 -0
- build/lib/build/lib/siat/financials_china2.py +2212 -0
- build/lib/build/lib/siat/fund.py +629 -0
- build/lib/build/lib/siat/fund_china.py +3307 -0
- build/lib/build/lib/siat/future_china.py +551 -0
- build/lib/build/lib/siat/google_authenticator.py +47 -0
- build/lib/build/lib/siat/grafix.py +3636 -0
- build/lib/build/lib/siat/holding_risk.py +867 -0
- build/lib/build/lib/siat/luchy_draw.py +638 -0
- build/lib/build/lib/siat/market_china.py +1168 -0
- build/lib/build/lib/siat/markowitz.py +2363 -0
- build/lib/build/lib/siat/markowitz2.py +3150 -0
- build/lib/build/lib/siat/markowitz2_20250704.py +2969 -0
- build/lib/build/lib/siat/markowitz2_20250705.py +3158 -0
- build/lib/build/lib/siat/markowitz_simple.py +373 -0
- build/lib/build/lib/siat/ml_cases.py +2291 -0
- build/lib/build/lib/siat/ml_cases_example.py +60 -0
- build/lib/build/lib/siat/option_china.py +3069 -0
- build/lib/build/lib/siat/option_pricing.py +1925 -0
- build/lib/build/lib/siat/other_indexes.py +409 -0
- build/lib/build/lib/siat/risk_adjusted_return.py +1576 -0
- build/lib/build/lib/siat/risk_adjusted_return2.py +1900 -0
- build/lib/build/lib/siat/risk_evaluation.py +2218 -0
- build/lib/build/lib/siat/risk_free_rate.py +351 -0
- build/lib/build/lib/siat/sector_china.py +4140 -0
- build/lib/build/lib/siat/security_price2.py +727 -0
- build/lib/build/lib/siat/security_prices.py +3408 -0
- build/lib/build/lib/siat/security_trend.py +402 -0
- build/lib/build/lib/siat/security_trend2.py +646 -0
- build/lib/build/lib/siat/stock.py +4284 -0
- build/lib/build/lib/siat/stock_advice_linear.py +934 -0
- build/lib/build/lib/siat/stock_base.py +26 -0
- build/lib/build/lib/siat/stock_china.py +2095 -0
- build/lib/build/lib/siat/stock_prices_kneighbors.py +910 -0
- build/lib/build/lib/siat/stock_prices_linear.py +386 -0
- build/lib/build/lib/siat/stock_profile.py +707 -0
- build/lib/build/lib/siat/stock_technical.py +3305 -0
- build/lib/build/lib/siat/stooq.py +74 -0
- build/lib/build/lib/siat/transaction.py +347 -0
- build/lib/build/lib/siat/translate.py +5183 -0
- build/lib/build/lib/siat/valuation.py +1378 -0
- build/lib/build/lib/siat/valuation_china.py +2076 -0
- build/lib/build/lib/siat/var_model_validation.py +444 -0
- build/lib/build/lib/siat/yf_name.py +811 -0
- build/lib/siat/__init__.py +75 -0
- build/lib/siat/allin.py +137 -0
- build/lib/siat/assets_liquidity.py +915 -0
- build/lib/siat/beta_adjustment.py +1058 -0
- build/lib/siat/beta_adjustment_china.py +548 -0
- build/lib/siat/blockchain.py +143 -0
- build/lib/siat/bond.py +2900 -0
- build/lib/siat/bond_base.py +992 -0
- build/lib/siat/bond_china.py +100 -0
- build/lib/siat/bond_zh_sina.py +143 -0
- build/lib/siat/capm_beta.py +783 -0
- build/lib/siat/capm_beta2.py +887 -0
- build/lib/siat/common.py +5360 -0
- build/lib/siat/compare_cross.py +642 -0
- build/lib/siat/copyrights.py +18 -0
- build/lib/siat/cryptocurrency.py +667 -0
- build/lib/siat/economy.py +1471 -0
- build/lib/siat/economy2.py +1853 -0
- build/lib/siat/esg.py +536 -0
- build/lib/siat/event_study.py +815 -0
- build/lib/siat/fama_french.py +1521 -0
- build/lib/siat/fin_stmt2_yahoo.py +982 -0
- build/lib/siat/financial_base.py +1160 -0
- build/lib/siat/financial_statements.py +598 -0
- build/lib/siat/financials.py +2339 -0
- build/lib/siat/financials2.py +1278 -0
- build/lib/siat/financials_china.py +4433 -0
- build/lib/siat/financials_china2.py +2212 -0
- build/lib/siat/fund.py +629 -0
- build/lib/siat/fund_china.py +3307 -0
- build/lib/siat/future_china.py +551 -0
- build/lib/siat/google_authenticator.py +47 -0
- build/lib/siat/grafix.py +3636 -0
- build/lib/siat/holding_risk.py +867 -0
- build/lib/siat/luchy_draw.py +638 -0
- build/lib/siat/market_china.py +1168 -0
- build/lib/siat/markowitz.py +2363 -0
- build/lib/siat/markowitz2.py +3150 -0
- build/lib/siat/markowitz2_20250704.py +2969 -0
- build/lib/siat/markowitz2_20250705.py +3158 -0
- build/lib/siat/markowitz_simple.py +373 -0
- build/lib/siat/ml_cases.py +2291 -0
- build/lib/siat/ml_cases_example.py +60 -0
- build/lib/siat/option_china.py +3069 -0
- build/lib/siat/option_pricing.py +1925 -0
- build/lib/siat/other_indexes.py +409 -0
- build/lib/siat/risk_adjusted_return.py +1576 -0
- build/lib/siat/risk_adjusted_return2.py +1900 -0
- build/lib/siat/risk_evaluation.py +2218 -0
- build/lib/siat/risk_free_rate.py +351 -0
- build/lib/siat/sector_china.py +4140 -0
- build/lib/siat/security_price2.py +727 -0
- build/lib/siat/security_prices.py +3408 -0
- build/lib/siat/security_trend.py +402 -0
- build/lib/siat/security_trend2.py +646 -0
- build/lib/siat/stock.py +4284 -0
- build/lib/siat/stock_advice_linear.py +934 -0
- build/lib/siat/stock_base.py +26 -0
- build/lib/siat/stock_china.py +2095 -0
- build/lib/siat/stock_prices_kneighbors.py +910 -0
- build/lib/siat/stock_prices_linear.py +386 -0
- build/lib/siat/stock_profile.py +707 -0
- build/lib/siat/stock_technical.py +3305 -0
- build/lib/siat/stooq.py +74 -0
- build/lib/siat/transaction.py +347 -0
- build/lib/siat/translate.py +5183 -0
- build/lib/siat/valuation.py +1378 -0
- build/lib/siat/valuation_china.py +2076 -0
- build/lib/siat/var_model_validation.py +444 -0
- build/lib/siat/yf_name.py +811 -0
- siat/__init__.py +0 -0
- siat/allin.py +0 -0
- siat/assets_liquidity.py +0 -0
- siat/beta_adjustment.py +0 -0
- siat/beta_adjustment_china.py +0 -0
- siat/blockchain.py +0 -0
- siat/bond.py +0 -0
- siat/bond_base.py +0 -0
- siat/bond_china.py +0 -0
- siat/bond_zh_sina.py +0 -0
- siat/capm_beta.py +0 -0
- siat/capm_beta2.py +0 -0
- siat/common.py +94 -30
- siat/compare_cross.py +0 -0
- siat/copyrights.py +0 -0
- siat/cryptocurrency.py +0 -0
- siat/economy.py +0 -0
- siat/economy2.py +0 -0
- siat/esg.py +0 -0
- siat/event_study.py +0 -0
- siat/fama_french.py +0 -0
- siat/fin_stmt2_yahoo.py +0 -0
- siat/financial_base.py +0 -0
- siat/financial_statements.py +0 -0
- siat/financials.py +0 -0
- siat/financials2.py +0 -0
- siat/financials_china.py +0 -0
- siat/financials_china2.py +0 -0
- siat/fund.py +0 -0
- siat/fund_china.py +0 -0
- siat/future_china.py +0 -0
- siat/google_authenticator.py +0 -0
- siat/grafix.py +1 -1
- siat/holding_risk.py +0 -0
- siat/luchy_draw.py +0 -0
- siat/market_china.py +7 -1
- siat/markowitz.py +0 -0
- siat/markowitz2.py +240 -39
- siat/markowitz2_20250704.py +2969 -0
- siat/markowitz2_20250705.py +3158 -0
- siat/markowitz_simple.py +0 -0
- siat/ml_cases.py +0 -0
- siat/ml_cases_example.py +0 -0
- siat/option_china.py +0 -0
- siat/option_pricing.py +0 -0
- siat/other_indexes.py +0 -0
- siat/risk_adjusted_return.py +0 -0
- siat/risk_adjusted_return2.py +0 -0
- siat/risk_evaluation.py +0 -0
- siat/risk_free_rate.py +0 -0
- siat/sector_china.py +0 -0
- siat/security_price2.py +0 -0
- siat/security_prices.py +3 -1
- siat/security_trend.py +0 -0
- siat/security_trend2.py +1 -1
- siat/stock.py +4 -2
- siat/stock_advice_linear.py +0 -0
- siat/stock_base.py +0 -0
- siat/stock_china.py +0 -0
- siat/stock_prices_kneighbors.py +0 -0
- siat/stock_prices_linear.py +0 -0
- siat/stock_profile.py +0 -0
- siat/stock_technical.py +0 -0
- siat/stooq.py +0 -0
- siat/transaction.py +0 -0
- siat/translate.py +11 -11
- siat/valuation.py +0 -0
- siat/valuation_china.py +0 -0
- siat/var_model_validation.py +0 -0
- siat/yf_name.py +0 -0
- {siat-3.10.130.dist-info → siat-3.10.132.dist-info}/METADATA +11 -11
- siat-3.10.132.dist-info/RECORD +218 -0
- siat-3.10.132.dist-info/top_level.txt +4 -0
- siat-3.10.130.dist-info/RECORD +0 -76
- siat-3.10.130.dist-info/top_level.txt +0 -1
- {siat-3.10.130.dist-info → siat-3.10.132.dist-info}/WHEEL +0 -0
- {siat-3.10.130.dist-info → siat-3.10.132.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,409 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
"""
|
3
|
+
本模块功能:另类证券市场指数
|
4
|
+
所属工具包:证券投资分析工具SIAT
|
5
|
+
SIAT:Security Investment Analysis Tool
|
6
|
+
创建日期:2025年5月8日
|
7
|
+
最新修订日期:
|
8
|
+
作者:王德宏 (WANG Dehong, Peter)
|
9
|
+
作者单位:北京外国语大学国际商学院
|
10
|
+
作者邮件:wdehong2000@163.com
|
11
|
+
版权所有:王德宏
|
12
|
+
用途限制:仅限研究与教学使用,不可商用!商用需要额外授权。
|
13
|
+
特别声明:作者不对使用本工具进行证券投资导致的任何损益负责!
|
14
|
+
"""
|
15
|
+
#==============================================================================
|
16
|
+
#关闭所有警告
|
17
|
+
import warnings; warnings.filterwarnings('ignore')
|
18
|
+
|
19
|
+
from siat.common import *
|
20
|
+
#==============================================================================
|
21
|
+
|
22
|
+
|
23
|
+
def other_index_translate(index_code):
|
24
|
+
"""
|
25
|
+
===========================================================================
|
26
|
+
功能:另类证券市场指数代码
|
27
|
+
参数:
|
28
|
+
index_code: 指数代码,非标准,来自东方财富和新浪。
|
29
|
+
返回值:是否找到,基于语言环境为中文或英文解释。
|
30
|
+
语言环境判断为check_language()
|
31
|
+
|
32
|
+
数据结构:['指数代码','指数符号','指数名称中文','指数名称英文','数据来源']
|
33
|
+
"""
|
34
|
+
|
35
|
+
import pandas as pd
|
36
|
+
trans_dict=pd.DataFrame([
|
37
|
+
|
38
|
+
['INDEXCF','俄罗斯MICEX指数','俄罗斯MICEX指数','MICEX Index','sina'],
|
39
|
+
['RTS','俄罗斯RTS指数','俄罗斯RTS指数','RTS Index','em'],
|
40
|
+
['CASE','埃及CASE 30指数','埃及CASE30指数','CASE30 Index','sina'],
|
41
|
+
['VNINDEX','越南胡志明','越南胡志明指数','Ho Chi-Ming Index','em'],
|
42
|
+
['HSCEI','国企指数','港股国企指数','HK H-share Index','em'],
|
43
|
+
['HSCCI','红筹指数','港股红筹指数','HK Red-share Index','em'],
|
44
|
+
['CSEALL','斯里兰卡科伦坡','斯里兰卡科伦坡全指','Colombo Index','em'],
|
45
|
+
['UDI','美元指数','美元指数','US Dollar Index','em'],
|
46
|
+
['CRB','路透CRB商品指数','路透CRB商品指数','Reuters CRB Index','em'],
|
47
|
+
['BDI','波罗的海BDI指数','波罗的海BDI指数','Baltic Dry Index','em'],
|
48
|
+
['KSE100','巴基斯坦卡拉奇','巴基斯坦卡拉奇指数','KSE100 Index','em'],
|
49
|
+
|
50
|
+
|
51
|
+
], columns=['code','symbol','name_cn','name_en','source'])
|
52
|
+
|
53
|
+
found=False; symbol=index_code
|
54
|
+
try:
|
55
|
+
dict_word=trans_dict[trans_dict['code']==index_code]
|
56
|
+
found=True
|
57
|
+
except:
|
58
|
+
#未查到翻译词汇,返回原词
|
59
|
+
pass
|
60
|
+
|
61
|
+
if dict_word is None:
|
62
|
+
found=False
|
63
|
+
elif len(dict_word) == 0:
|
64
|
+
found=False
|
65
|
+
|
66
|
+
source=''; name=''
|
67
|
+
if found:
|
68
|
+
symbol=dict_word['symbol'].values[0]
|
69
|
+
|
70
|
+
lang=check_language()
|
71
|
+
if lang == 'Chinese':
|
72
|
+
name=dict_word['name_cn'].values[0]
|
73
|
+
else:
|
74
|
+
name=dict_word['name_en'].values[0]
|
75
|
+
|
76
|
+
source=dict_word['source'].values[0]
|
77
|
+
|
78
|
+
return symbol,name,source
|
79
|
+
|
80
|
+
if __name__=='__main__':
|
81
|
+
index_code='KSE100'
|
82
|
+
index_code='CASE'
|
83
|
+
index_code='XYZ'
|
84
|
+
|
85
|
+
set_language('Chinese')
|
86
|
+
set_language('English')
|
87
|
+
other_index_translate(index_code)
|
88
|
+
|
89
|
+
#==============================================================================
|
90
|
+
def get_other_index_em(index_code,start,end):
|
91
|
+
"""
|
92
|
+
功能:获取另类指数历史行情,东方财富
|
93
|
+
参数:
|
94
|
+
index_code:指数代码
|
95
|
+
start,end:开始/结束日期
|
96
|
+
"""
|
97
|
+
symbol,name,source=other_index_translate(index_code)
|
98
|
+
if symbol == index_code:
|
99
|
+
return None
|
100
|
+
|
101
|
+
import akshare as ak
|
102
|
+
try:
|
103
|
+
dft = ak.index_global_hist_em(symbol=symbol)
|
104
|
+
except:
|
105
|
+
return None
|
106
|
+
|
107
|
+
dft.rename(columns={'日期':'Date','代码':'ticker','名称':'Name','今开':'Open', \
|
108
|
+
'最新价':'Close','最高':'High','最低':'Low','振幅':'Change'}, \
|
109
|
+
inplace=True)
|
110
|
+
dft['Change']=dft['Change']/100.00
|
111
|
+
dft['Adj Close']=dft['Close']
|
112
|
+
dft['source']=source
|
113
|
+
dft['Volume']=0
|
114
|
+
dft['Name']=name
|
115
|
+
|
116
|
+
import pandas as pd
|
117
|
+
dft['date']=dft['Date'].apply(lambda x: pd.to_datetime(x))
|
118
|
+
dft.set_index('date',inplace=True)
|
119
|
+
|
120
|
+
startpd=pd.to_datetime(start); endpd=pd.to_datetime(end)
|
121
|
+
df=dft[(dft.index >= startpd) & (dft.index <= endpd)]
|
122
|
+
|
123
|
+
return df
|
124
|
+
|
125
|
+
if __name__=='__main__':
|
126
|
+
index_code='KSE100'
|
127
|
+
start='2025-2-1'; end='2025-3-31'
|
128
|
+
get_other_index_em(index_code,start,end)
|
129
|
+
#==============================================================================
|
130
|
+
if __name__=='__main__':
|
131
|
+
index_code='RTS'
|
132
|
+
start='2025-2-1'; end='2025-3-31'
|
133
|
+
get_other_index_em(index_code,start,end)
|
134
|
+
|
135
|
+
def get_other_index_sina(index_code,start,end):
|
136
|
+
"""
|
137
|
+
功能:获取另类指数历史行情,新浪财经
|
138
|
+
参数:
|
139
|
+
index_code:指数代码
|
140
|
+
start,end:开始/结束日期
|
141
|
+
"""
|
142
|
+
symbol,name,source=other_index_translate(index_code)
|
143
|
+
if symbol == index_code:
|
144
|
+
return None
|
145
|
+
|
146
|
+
import akshare as ak
|
147
|
+
try:
|
148
|
+
dft = ak.index_global_hist_sina(symbol=symbol)
|
149
|
+
except:
|
150
|
+
return None
|
151
|
+
|
152
|
+
dft.rename(columns={'open':'Open','high':'High','low':'Low','close':'Close', \
|
153
|
+
'volume':'Volume'},inplace=True)
|
154
|
+
dft['ticker']=index_code; dft['Name']=name; dft['Date']=dft['date']
|
155
|
+
dft['Adj Close']=dft['Close']
|
156
|
+
dft['source']=source
|
157
|
+
|
158
|
+
import pandas as pd
|
159
|
+
dft['date']=dft['Date'].apply(lambda x: pd.to_datetime(x))
|
160
|
+
dft.set_index('date',inplace=True)
|
161
|
+
|
162
|
+
startpd=pd.to_datetime(start); endpd=pd.to_datetime(end)
|
163
|
+
df=dft[(dft.index >= startpd) & (dft.index <= endpd)]
|
164
|
+
|
165
|
+
return df
|
166
|
+
|
167
|
+
if __name__=='__main__':
|
168
|
+
index_code='CASE'
|
169
|
+
start='2025-2-1'; end='2025-3-31'
|
170
|
+
get_other_index_sina(index_code,start,end)
|
171
|
+
#==============================================================================
|
172
|
+
def get_other_index_ak(index_code,start,end):
|
173
|
+
"""
|
174
|
+
功能:获取另类指数历史行情,新浪财经或东方财富
|
175
|
+
参数:
|
176
|
+
index_code:指数代码
|
177
|
+
start,end:开始/结束日期
|
178
|
+
"""
|
179
|
+
symbol,name,source=other_index_translate(index_code)
|
180
|
+
if symbol == index_code:
|
181
|
+
return None
|
182
|
+
|
183
|
+
if source == 'em':
|
184
|
+
df=get_other_index_em(index_code,start,end)
|
185
|
+
elif source == 'sina':
|
186
|
+
df=get_other_index_sina(index_code,start,end)
|
187
|
+
else:
|
188
|
+
df=None
|
189
|
+
|
190
|
+
return df
|
191
|
+
|
192
|
+
if __name__=='__main__':
|
193
|
+
index_code='CASE'
|
194
|
+
index_code='KSE100'
|
195
|
+
index_code='VNINDEX'
|
196
|
+
start='2025-2-1'; end='2025-3-31'
|
197
|
+
get_other_index(index_code,start,end)
|
198
|
+
#==============================================================================
|
199
|
+
if __name__=='__main__':
|
200
|
+
ticker='AAPL'
|
201
|
+
ticker='^TVX'
|
202
|
+
ticker='Apple'
|
203
|
+
start='2025-4-1'; end='2025-4-30'
|
204
|
+
|
205
|
+
get_prices_av(ticker,start,end)
|
206
|
+
|
207
|
+
def get_prices_av(ticker,start,end):
|
208
|
+
"""
|
209
|
+
功能:从Alpha Vantage获取美股股价历史行情,使用Alpha Vantage
|
210
|
+
参数:
|
211
|
+
ticker:AV股票代码(假设与雅虎财经的股票代码相同),如不同可通过search_av获得准确代码
|
212
|
+
start:起始日期
|
213
|
+
end:结束日期
|
214
|
+
"""
|
215
|
+
# 免费注册:wangdehong@bfsu.edu.cn,每日25次。
|
216
|
+
api_key='VTRR3TA7L9O2DIX6'
|
217
|
+
|
218
|
+
from alpha_vantage.timeseries import TimeSeries
|
219
|
+
ts = TimeSeries(key=api_key, output_format="pandas")
|
220
|
+
try:
|
221
|
+
dft, _ = ts.get_daily(symbol=ticker, outputsize="full")
|
222
|
+
except:
|
223
|
+
pass
|
224
|
+
return None
|
225
|
+
|
226
|
+
dft.sort_index(ascending=True,inplace=True)
|
227
|
+
dft.rename(columns={'1. open':'Open','2. high':'High','3. low':'Low', \
|
228
|
+
'4. close':'Close','5. volume':'Volume'},inplace=True)
|
229
|
+
dft['Adj Close']=dft['Close']
|
230
|
+
dft['source']='Alpha Vantage'
|
231
|
+
dft['ticker']=ticker
|
232
|
+
dft['Name']=ticker
|
233
|
+
|
234
|
+
import pandas as pd
|
235
|
+
startpd=pd.to_datetime(start); endpd=pd.to_datetime(end)
|
236
|
+
df=dft[(dft.index >= startpd) & (dft.index <= endpd)]
|
237
|
+
|
238
|
+
return df
|
239
|
+
|
240
|
+
|
241
|
+
#==============================================================================
|
242
|
+
if __name__=='__main__':
|
243
|
+
ticker='AAPL'
|
244
|
+
ticker='Apple'
|
245
|
+
start='2025-4-1'; end='2025-4-30'
|
246
|
+
|
247
|
+
get_prices_av_pdr(ticker,start,end)
|
248
|
+
|
249
|
+
def get_prices_av_pdr(ticker,start,end):
|
250
|
+
"""
|
251
|
+
功能:从Alpha Vantage获取美股股价历史行情,使用pandas_datareader
|
252
|
+
参数:
|
253
|
+
ticker:AV股票代码(可能与雅虎财经的股票代码不同),可以通过search_av获得准确代码
|
254
|
+
start:起始日期
|
255
|
+
end:结束日期
|
256
|
+
"""
|
257
|
+
# 免费注册:wangdehong@bfsu.edu.cn,限每日25次。
|
258
|
+
api_key='VTRR3TA7L9O2DIX6'
|
259
|
+
|
260
|
+
import pandas_datareader.data as pdr
|
261
|
+
try:
|
262
|
+
dft = pdr.DataReader(ticker, "av-daily", api_key=api_key,start=start,end=end)
|
263
|
+
dft['ticker']=ticker
|
264
|
+
dft['Name']=ticker
|
265
|
+
except: # 拯救一次,查找字符串匹配的股票代码
|
266
|
+
firstcode,firstname,_=search_av(ticker,api_key)
|
267
|
+
if firstcode is None:
|
268
|
+
pass
|
269
|
+
return None
|
270
|
+
print(f" Notice: matching keyword {ticker} to stock code {firstcode}({firstname})")
|
271
|
+
try:
|
272
|
+
dft = pdr.DataReader(firstcode, "av-daily", api_key=api_key,start=start,end=end)
|
273
|
+
dft['ticker']=firstcode
|
274
|
+
dft['Name']=firstname
|
275
|
+
except:
|
276
|
+
pass
|
277
|
+
return None
|
278
|
+
|
279
|
+
if dft is None:
|
280
|
+
pass
|
281
|
+
return None
|
282
|
+
if len(dft) == 0:
|
283
|
+
pass
|
284
|
+
return None
|
285
|
+
|
286
|
+
dft.rename(columns={'open':'Open','high':'High','low':'Low','close':'Close', \
|
287
|
+
'volume':'Volume'},inplace=True)
|
288
|
+
dft['Adj Close']=dft['Close']
|
289
|
+
dft['source']='Alpha Vantage'
|
290
|
+
|
291
|
+
import pandas as pd
|
292
|
+
dft['Date']=dft['date']=dft.index
|
293
|
+
dft['date']=dft['date'].apply(lambda x: pd.to_datetime(x))
|
294
|
+
dft.set_index('date',inplace=True)
|
295
|
+
|
296
|
+
#startpd=pd.to_datetime(start); endpd=pd.to_datetime(end)
|
297
|
+
#df=dft[(dft.index >= startpd) & (dft.index <= endpd)]
|
298
|
+
df=dft
|
299
|
+
|
300
|
+
return df
|
301
|
+
|
302
|
+
|
303
|
+
#==============================================================================
|
304
|
+
if __name__=='__main__':
|
305
|
+
api_key='VTRR3TA7L9O2DIX6'
|
306
|
+
keyword='AAPL'
|
307
|
+
keyword='Apple'
|
308
|
+
keyword='^TYX'
|
309
|
+
|
310
|
+
search_av("microsoft")
|
311
|
+
search_av("Apple")
|
312
|
+
|
313
|
+
def search_av(keyword,api_key='VTRR3TA7L9O2DIX6'):
|
314
|
+
"""
|
315
|
+
过程:给定上市公司关键名称或不带后缀的股票代码,找出Alpha Vantage的股票代码。
|
316
|
+
"""
|
317
|
+
DEBUG=False
|
318
|
+
|
319
|
+
import requests
|
320
|
+
url = f"https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords={keyword}&apikey={api_key}"
|
321
|
+
response = requests.get(url)
|
322
|
+
if response.status_code != 200:
|
323
|
+
pass
|
324
|
+
return None,None,None
|
325
|
+
|
326
|
+
data = response.json()
|
327
|
+
if "bestMatches" in data:
|
328
|
+
try:
|
329
|
+
firstcode=data["bestMatches"][0]['1. symbol']
|
330
|
+
firstname=data["bestMatches"][0]['2. name']
|
331
|
+
except:
|
332
|
+
if DEBUG:
|
333
|
+
print(f" #Warning(search_av): no contents found for {keyword} in Alpha Vantage")
|
334
|
+
#未找到可匹配的股票代码
|
335
|
+
return None,None,None
|
336
|
+
|
337
|
+
else:
|
338
|
+
if DEBUG:
|
339
|
+
if "Information" in data:
|
340
|
+
print(f" #Warning(search_av): exceeded limit of requests per day in Alpha Vantage")
|
341
|
+
else:
|
342
|
+
print(f" #Warning(search_av): keyword {keyword} not found in Alpha Vantage")
|
343
|
+
#未找到可匹配的股票代码
|
344
|
+
return None,None,None
|
345
|
+
|
346
|
+
return firstcode,firstname,data
|
347
|
+
|
348
|
+
#==============================================================================
|
349
|
+
if __name__=='__main__':
|
350
|
+
ticker='AAPL'
|
351
|
+
ticker='^TVX'
|
352
|
+
start='2025-4-1'; end='2025-4-30'
|
353
|
+
|
354
|
+
get_price_tiingo(ticker,start,end)
|
355
|
+
|
356
|
+
def get_price_tiingo(ticker,start,end):
|
357
|
+
"""
|
358
|
+
功能:获取美股历史行情信息,基于TIINGO
|
359
|
+
"""
|
360
|
+
DEBUG=False
|
361
|
+
|
362
|
+
# 每日限1000次调用,基于wdehong2000@163.com
|
363
|
+
api_token='0892bdb0533f8114535f354db596e6c244f5618d'
|
364
|
+
|
365
|
+
from tiingo import TiingoClient
|
366
|
+
# 通过配置字典
|
367
|
+
config = {
|
368
|
+
'api_key': api_token, # 替换为实际密钥
|
369
|
+
'session': True # 启用HTTP会话复用,提升性能
|
370
|
+
}
|
371
|
+
client = TiingoClient(config)
|
372
|
+
|
373
|
+
|
374
|
+
# 获取历史行情(默认返回DataFrame)
|
375
|
+
try:
|
376
|
+
dft = client.get_dataframe(
|
377
|
+
ticker,
|
378
|
+
startDate=start,
|
379
|
+
endDate=end,
|
380
|
+
frequency='daily'
|
381
|
+
)
|
382
|
+
except Exception as e:
|
383
|
+
if DEBUG:
|
384
|
+
print(f" #Error(get_price_tiingo): {e}")
|
385
|
+
print(f" #Error(get_price_tiingo): {ticker} not found or exceeded max requests per day")
|
386
|
+
return None
|
387
|
+
|
388
|
+
# 去掉时区
|
389
|
+
dft.index = dft.index.tz_localize(None)
|
390
|
+
|
391
|
+
# 整理数据项
|
392
|
+
dft.rename(columns={'open':'Open','high':'High','low':'Low','close':'Close', \
|
393
|
+
'volume':'Volume'},inplace=True)
|
394
|
+
|
395
|
+
dft.rename(columns={'adjOpen':'Adj Open','adjHigh':'Adj High','adjLow':'Adj Low', \
|
396
|
+
'adjClose':'Adj Close', \
|
397
|
+
'adjVolume':'Adj Volume'},inplace=True)
|
398
|
+
|
399
|
+
dft['source']='Tiingo'
|
400
|
+
dft['ticker']=ticker; dft['name']=ticker
|
401
|
+
|
402
|
+
return dft
|
403
|
+
|
404
|
+
|
405
|
+
#==============================================================================
|
406
|
+
#==============================================================================
|
407
|
+
#==============================================================================
|
408
|
+
|
409
|
+
|