下面介绍两种常用的数据调取方式:
已经获取到tushare token凭证
下载并安装最新版tushare SDK
导入tushare
import xcsc_tushare as ts
设置token
ts.set_token('your token here')
以上方法只需要在第一次或者token失效后调用,完成调取tushare数据凭证的设置,正常情况下不需要重复设置。也可以忽略此步骤,直接用pro_api('your token')完成初始化
初始化pro接口
默认测试环境,提取测试数据
pro = ts.pro_api()
qa代表测试环境,是提取测试环境数据
pro = ts.pro_api(env='qa')
指定访问地址,提取生产环境数据
pro = ts.pro_api(server='http://116.128.206.39:7172')
如果上一步骤ts.set_token('your token')无效或不想保存token到本地,也可以在初始化接口里直接设置token:
pro = ts.pro_api('your token',server='http://116.128.206.39:7172')
#或
pro = ts.pro_api('your token',env='qa')
数据调取
以获取交易日历信息为例:
df = pro.trade_cal(exchange='', start_date='20180901', end_date='20181001')
或者
df = pro.query('trade_cal', exchange='', start_date='20180901', end_date='20181001')
调取结果:
trade_date exchange
0 20180903 SHN
1 20180904 SHN
2 20180905 SHN
3 20180906 SHN
4 20180907 SHN
5 20180910 SHN
6 20180911 SHN
7 20180912 SHN
8 20180913 SHN
9 20180914 SHN
http restful 采用post方式,通过json body传入接口参数,请求地址为http://116.128.206.39:7172
采用命令行工具curl的请求示例如下:
curl -X POST -d '{"api_name": "trade_cal", "token": "xxxxxxxx", "params": {"exchange":"", "start_date":"20180901", "end_date":"20181001", "is_open":"0"}, "fields": "exchange,cal_date,is_open,pretrade_date"}' http://116.128.206.39:7172
返回结果:
```json
{
"code":0,
"msg":null,
"data":{
"fields":[
"exchange",
"cal_date",
"is_open",
"pretrade_date"
],
"items":[
[
"SSE",
"20180901",
0,
"20180831"
],
[
"SSE",
"20180902",
0,
"20180831"
],
[
"SSE",
"20180908",
0,
"20180907"
],
...
[
"SSE",
"20180929",
0,
"20180928"
],
[
"SSE",
"20180930",
0,
"20180928"
],
[
"SSE",
"20181001",
0,
"20180928"
]
]
}
}```