最近参加了上海计算机应用大赛,做一个关于ETF基金的题目
写了一个使用tushare接口获取ETF基金数据的代码:
代码如下:
导入包
# 导入tushare
import tushare as ts
# 初始化pro接口
pro = ts.pro_api('tokey')#tushare的TOKEY码
import numpy as np
下面时获取数据和对数据处理的代码:
def get_etf_code():
df = pro.fund_basic(**{
"ts_code": "",
"market": "E",
"update_flag": "",
"offset": "",
"limit": "",
"status": "L"
}, fields=[
"ts_code",
"name",
"management",
"custodian",
"fund_type",
"found_date",
"due_date",
"list_date",
"issue_date",
"delist_date",
"issue_amount",
"m_fee",
"c_fee",
"duration_year",
"p_value",
"min_amount",
"exp_return",
"benchmark",
"status",
"invest_type",
"type",
"trustee",
"purc_startdate",
"redm_startdate",
"market"
])
#对数据进行切片,重塑
fund=df[['ts_code','name','fund_type','invest_type']]
data=np.array(fund)
data=data.reshape(-1,4)
#获取只有ETF基金的code
funds = []
for idx in range(data.shape[0]):
if 'ETF' not in data[idx,[1]] and data[idx,[2]] in ['股票型'] and data[idx,[3]] in ['被动指数型']:
funds.append(data[idx])
return funds
然后利用code去获取基金历史数据
s
# 拉取数据
def get_funds_data(code):
df = pro.fund_daily(**{
"trade_date": "",
"start_date": "",
"end_date": "",
"ts_code": code,
"limit": "",
"offset": ""
}, fields=[
"ts_code",
"trade_date",
"pre_close",
"open",
"high",
"low",
"close",
"change",
"pct_chg",
"vol",
"amount"
])
return df
def get_funds_allhistory():
code=get_etf_code()
funds_alldata=[]
for i in range(20):
his=get_funds_data(funds[i][0])
funds_alldata.append(his)
return funds_alldata
直接调用 get_funds_allhistory()就能得到所有ETF数据
介于积分权限问题:一分钟只能访问80次,一个只能27行数据。
解决办法:去社区努力获得积分 !!