通过 http 调取 Tushare 数据 & 在 Stata 中使用 Tushare 财经数据接口

通过 http 调取 Tushare 数据 & 在 Stata 中使用 Tushare 财经数据接口

一直非常喜欢的 Tushare 财经数据接口最近又推出了不少新东西,这篇文章学习了如何通过 http 请求财经数据。

输入下面的参数通过 post 请求就可以请求到数据了:

  • api_name:接口名称,比如daily(请求日线数据的接口);
  • token:密钥(需要注册获取,注册网址:Tushare 注册);
  • params:接口参数,如 daily 接口中 start_date 和 end_date;
  • fields:字段列表,用于接口获取指定的字段,以逗号分隔,如”open,high,low,close”;

接口名称、接口参数和 Python SDK 的都是一样的,fields 参数一般可以不用指定,默认请求所有变量。例如请求日线行情数据,该数据的接口为:daily

Shell
1
2
3
4
5
6
7
curl -X POST -d '{"api_name": "daily", \
"token": "xxx···xxx", \
"params": {"ts_code":"000001.SZ", \
"start_date":"20181027", \
"end_date":"20181101"}, \
"fields": ""}' \
http://api.tushare.pro -o temp.json

返回结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
{
"request_id" : "0a2da3a0ddf311e8a123d00dd36710041541089655236465",
"code" : "0",
"msg" : "null",
"data" :
{
"fields" :
[
"ts_code",
"trade_date",
"open",
"high",
"low",
"close",
"pre_close",
"change",
"pct_change",
"vol",
"amount"
],
"items" :
[
[
"000001.SZ",
"20181101",
"10.9900",
"11.0500",
"10.7600",
"10.8300",
"10.9100",
"-0.0800",
"-0.7333",
"1542776.320000",
"1679443.449000"
],
[
"000001.SZ",
"20181031",
"10.9500",
"10.9900",
"10.7500",
"10.9100",
"10.9000",
"0.0100",
"0.0917",
"1152683.790000",
"1252156.775000"
],
[
"000001.SZ",
"20181030",
"10.7800",
"11.0800",
"10.7300",
"10.9000",
"10.7500",
"0.1500",
"1.3953",
"1501827.860000",
"1641371.787000"
],
[
"000001.SZ",
"20181029",
"11.2000",
"11.2400",
"10.6200",
"10.7500",
"11.1800",
"-0.4300",
"-3.8462",
"1591628.780000",
"1725932.497000"
]
]
}

为了能在 Stata 中方便的使用这种方式,写了一个 Python 的脚本辅助 Stata 调用数据:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
!curl -X POST -d '{"api_name": "daily", "token": "xxxxxx", "params": {"ts_code":"000001.SZ", "start_date":"20180101", "end_date":"20181101"}, "fields": ""}' http:\/\/api.tushare.pro -o jsonfile.json
!rm jsonhandle.py
!touch jsonhandle.py
!echo "import json" >> jsonhandle.py
!echo "import pandas as pd" >> jsonhandle.py
!echo "file = open('jsonfile.json', 'rb')" >> jsonhandle.py
!echo "json = json.load(file)" >> jsonhandle.py
!echo "fields = json['data']['fields']" >> jsonhandle.py
!echo "items = json['data']['items']" >> jsonhandle.py
!echo "df = pd.DataFrame(items, columns = fields)" >> jsonhandle.py
!echo "df.to_csv('jsonfile.csv')" >> jsonhandle.py
!/Users/mr.cheng/anaconda3/bin/python3 jsonhandle.py
insheet using jsonfile.csv, clear
drop v1

运行一下上面的这段代码(注意你的电脑上的 python3 的位置应该与我不同!)就会发现数据被读入 Stata 了:

下面我在把这段代码封装成一个 Stata 命令:

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
*! 在Stata中使用Tushare财经数据接口
*! 程振兴 2018年11月2日
*! 【示例】
*! local token "8085d30d4cf1f0734be35387f719c6896be333ab1cb5f0a5ec8b1dba"
*! * 日线行情
*! tushare daily, t(`token') p(`""ts_code":"000001.SZ""')
*! tushare daily, t(`token') p(`""ts_code":"000001.SZ", "start_date":"20180101", "end_date":"20181101""')
*! tushare daily, t(`token') p(`""ts_code":"000001.SZ", "start_date":"20180101", "end_date":"20181101""') f("ts_code,trade_date,close")*!
*! * 交易日历
*! tushare trade_cal, t(`token')
*! tushare trade_cal, t(`token') p(`""exchange_id":"SSE""')
cap prog drop tushare
prog def tushare
version 15.0
syntax anything(name = apiname) [, Token(string) Params(string) Fields(string)]
di in yellow "正在接入数据···"
qui{
!curl -X POST -d '{"api_name": "`apiname'", "token": "`token'", "params": {`params'}, "fields": "`fields'"}' http:\/\/api.tushare.pro -o jsonfile.json
cap erase jsonhandle.py
!touch jsonhandle.py
!echo "import json" >> jsonhandle.py
!echo "import pandas as pd" >> jsonhandle.py
!echo "file = open('jsonfile.json', 'rb')" >> jsonhandle.py
!echo "json = json.load(file)" >> jsonhandle.py
!echo "fields = json['data']['fields']" >> jsonhandle.py
!echo "items = json['data']['items']" >> jsonhandle.py
!echo "df = pd.DataFrame(items, columns = fields)" >> jsonhandle.py
!echo "df.to_csv('jsonfile.csv')" >> jsonhandle.py
!/anaconda3/bin/python jsonhandle.py
insheet using jsonfile.csv, clear
}
if _rc != 0{
di in green "接入成功!"
}
qui{
drop v1
cap erase jsonhandle.py
cap erase jsonfile.csv
cap erase jsonfile.json
}
end

例如,获取平安银行的日线数据:

Stata
1
2
3
4
5
6
7
8
9
local token "xxx···xxx"
* 默认下载全部数据
tushare daily, t(`token') p(`""ts_code":"000001.SZ""')

* 指定起止日期
tushare daily, t(`token') p(`""ts_code":"000001.SZ", "start_date":"20180101", "end_date":"20181101""')

* 指定字段列表
tushare daily, t(`token') p(`""ts_code":"000001.SZ", "start_date":"20180101", "end_date":"20181101""') f("ts_code,trade_date,close")

再例如获取交易日历:

Stata
1
2
3
4
5
6
7
8
* 获取上交所和深交所的交易日历
tushare trade_cal, t(`token')

* 只获取上交所的
tushare trade_cal, t(`token') p(`""exchange_id":"SSE""')

* 只获取深交所的
tushare trade_cal, t(`token') p(`""exchange_id":"SZSE""')

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×