Abracadabra

Get financial data from Tushare

Introduction

TuShare is a famous free, open source python financial data interface package. Its official home page is: TuShare - financial data interface package. The interface package now provides a large amount of financial data covering a wide range of data such as stocks, fundamentals, macros, news, etc. (Please check the official website for details) and keep updating. At present, the length of the stock’s data is three years. Although it is a bit short, it can basically meet the needs of quantitative beginners for testing.

Tutorial

Install and Import

You need to install first:

  • Pandas
  • lxml

Two way to install tushare:

  1. pip install tushare
  2. visit https://pypi.python.org/pypi/Tushare/, download and install

How to update:

pip install tushare --upgrade

Import package and view package version:

1
2
3
import tushare
print(tushare.__version__)

Use some simple function

Stock data

update:Many of the quotes returned by the get_hist_data function are wrong, but both get_h_data and get_k_data can be used

We should still master how to use tushare to obtain stock market data, using the ts.get_hist_data() function whose input parameters are:

  • code: Stock code, ie 6-digit code, or index code (sh = Shanghai index sz = Shenzhen index hs300 = CSI 300 index sz50 = SSE 50 zxb = small and medium board cyb = board)
  • start: Start date, format YYYY-MM-DD
  • end: End date, format YYYY-MM-DD
  • ktype: Data type, D = day k line W = week M = month 5 = 5 minutes 15 = 15 minutes 30 = 30 minutes 60 = 60 minutes, the default is D
  • retry_count: The number of retries after the network is abnormal. The default is 3
  • pause: Pause seconds when retrying, default is 0

Return values:

  • date:date
  • open:Opening price
  • high:Highest price
  • close:Closing price
  • low:Lowest price
  • volume:Volume
  • price_change:price fluncuation
  • p_change:Quote change
  • ma5:5-day average price
  • ma10:10-day average price
  • ma20: 20-day average price
  • v_ma5: 5-day average volume
  • v_ma10: 10-day average volume
  • v_ma20: 20-day average volume
  • turnover: Change in hand rate [Note: Index does not have this item]

Specific examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
ts.get_hist_data('600848')
date open high close low volume p_change ma5
2012-01-11 6.880 7.380 7.060 6.880 14129.96 2.62 7.060
2012-01-12 7.050 7.100 6.980 6.900 7895.19 -1.13 7.020
2012-01-13 6.950 7.000 6.700 6.690 6611.87 -4.01 6.913
2012-01-16 6.680 6.750 6.510 6.480 2941.63 -2.84 6.813
2012-01-17 6.660 6.880 6.860 6.460 8642.57 5.38 6.822
2012-01-18 7.000 7.300 6.890 6.880 13075.40 0.44 6.788
2012-01-19 6.690 6.950 6.890 6.680 6117.32 0.00 6.770
2012-01-20 6.870 7.080 7.010 6.870 6813.09 1.74 6.832
date ma10 ma20 v_ma5 v_ma10 v_ma20 turnover
2012-01-11 7.060 7.060 14129.96 14129.96 14129.96 0.48
2012-01-12 7.020 7.020 11012.58 11012.58 11012.58 0.27
2012-01-13 6.913 6.913 9545.67 9545.67 9545.67 0.23
2012-01-16 6.813 6.813 7894.66 7894.66 7894.66 0.10
2012-01-17 6.822 6.822 8044.24 8044.24 8044.24 0.30
2012-01-18 6.833 6.833 7833.33 8882.77 8882.77 0.45
2012-01-19 6.841 6.841 7477.76 8487.71 8487.71 0.21
2012-01-20 6.863 6.863 7518.00 8278.38 8278.38 0.23

You can also set the start time and end time of historical data:

1
2
3
4
5
6
7
8
9
10
11
12
13
ts.get_hist_data('600848',start='2015-01-05',end='2015-01-09')
date open high close low volume p_change ma5 ma10
2015-01-05 11.160 11.390 11.260 10.890 46383.57 1.26 11.156 11.212
2015-01-06 11.130 11.660 11.610 11.030 59199.93 3.11 11.182 11.155
2015-01-07 11.580 11.990 11.920 11.480 86681.38 2.67 11.366 11.251
2015-01-08 11.700 11.920 11.670 11.640 56845.71 -2.10 11.516 11.349
2015-01-09 11.680 11.710 11.230 11.190 44851.56 -3.77 11.538 11.363
date ma20 v_ma5 v_ma10 v_ma20 turnover
2015-01-05 11.198 58648.75 68429.87 97141.81 1.59
2015-01-06 11.382 54854.38 63401.05 98686.98 2.03
2015-01-07 11.543 55049.74 61628.07 103010.58 2.97
2015-01-08 11.647 57268.99 61376.00 105823.50 1.95

Others:

1
2
3
4
5
6
7
8
9
10
11
12
ts.get_hist_data('600848', ktype='W') # Get weekly k-line data
ts.get_hist_data('600848', ktype='M') # Get monthly k-line data
ts.get_hist_data('600848', ktype='5') # Get 5 minutes k-line data
ts.get_hist_data('600848', ktype='15') # Get 15 minutes k-line data
ts.get_hist_data('600848', ktype='30') # Get 30 minutes k-line data
ts.get_hist_data('600848', ktype='60') # Get 60 minutes k-line data
ts.get_hist_data('sh'# Get data on the Shanghai index k-line, other parameters consistent with the stocks, the same below
ts.get_hist_data('sz'# Get Shenzhen Chengzhi k line data
ts.get_hist_data('hs300'# Get the CSI 300 k line data
ts.get_hist_data('sz50'# Get SSE 50 Index k-line data
ts.get_hist_data('zxb'# Get the k-line data of small and medium board indices
ts.get_hist_data('cyb'# Get GEM Index k-line data

Fundamental data

With tushare we can also get fundamental data through ts.get_stock_basics() (shown in the results section):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ts.get_stock_basics()
code name industry area pe outstanding totals totalAssets
300563 N神宇 通信设备 江苏 26.73 2000.00 8000.00 4.216000e+04
601882 海天精工 机床制造 浙江 26.83 5220.00 52200.00 1.877284e+05
601880 大连港 港口 辽宁 76.40 773582.00 1289453.63 3.263012e+06
300556 丝路视觉 软件服务 深圳 101.38 2780.00 11113.33 4.448248e+04
600528 中铁二局 建筑施工 四川 149.34 145920.00 145920.00 5.709568e+06
002495 佳隆股份 食品 广东 202.12 66611.13 93562.56 1.169174e+05
600917 重庆燃气 供气供热 重庆 76.87 15600.00 155600.00 8.444600e+05
002752 昇兴股份 广告包装 福建 75.14 12306.83 63000.00 2.387493e+05
002346 柘中股份 电气设备 上海 643.97 7980.00 44157.53 2.263010e+05
000680 山推股份 工程机械 山东 0.00 105694.97 124078.75 9.050701e+05
...

Macro data

We use the resident consumer index as an example, which can be obtained through the ts.get_cpi() function (it will get 322 items at a time, some of them will be displayed):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
print ts.get_cpi()
month cpi
0 2016.10 102.10
1 2016.9 101.90
2 2016.8 101.34
3 2016.7 101.77
4 2016.6 101.88
5 2016.5 102.04
6 2016.4 102.33
7 2016.3 102.30
8 2016.2 102.28
9 2016.1 101.75
10 2015.12 101.64
...

Recent news

The tushare package can use the ts.get_latest_news() function to view the latest news, and it will return 80. For reasons of space, we only show the first 15 here. We can see that it is all Sina Finance’s news data.

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
print ts.get_latest_news();
classify title time \
0 美股 “特朗普通胀”预期升温 美国国债下挫 11-14 23:10
1 美股 特朗普:脸书、推特等社交媒体助我入主白宫 11-14 23:10
2 证券 1114日晚增减持每日速览 11-14 22:54
3 美股 财经观察:日本为何急于推动TPP批准程序 11-14 22:54
4 美股 新总统谜题:特朗普会连续加息吗? 11-14 22:52
5 证券 神州专车财报遭质疑 增发100亿股东退出需5011-14 22:41
6 证券 恒大闪电杀回马枪锁仓半年 戒短炒了吗? 11-14 22:38
7 国内财经 楼继伟力推改革做派 或加快国有资本划拨社保 11-14 22:36
8 美股 开盘:美股周一小幅高开 延续上周涨势 11-14 22:32
9 美股 喜达屋创始人:当好总统就要走中庸之道 11-14 22:24
10 证券 北京高华:将乐视网评级下调至中性 11-14 22:09
11 美股 111422点交易员正关注要闻 11-14 22:02
12 美股 摩根大通:新兴市场股市、货币的前景悲观 11-14 21:55
13 国内财经 人民日报刊文谈全面深化改革这三年:啃下硬骨头 11-14 21:46
14 证券 泽平宏观:经济L型延续 地产销量回落投资超预期 11-14 21:43
15 证券 黄燕铭等五大券商大佬告诉你 2017年买点啥? 11-14 21:41
url
0 http://finance.sina.com.cn/stock/usstock/c/201...
1 http://finance.sina.com.cn/stock/usstock/c/201...
2 http://finance.sina.com.cn/stock/y/2016-11-14/...
3 http://finance.sina.com.cn/stock/usstock/c/201...
4 http://finance.sina.com.cn/stock/usstock/c/201...
5 http://finance.sina.com.cn/stock/marketresearc...
6 http://finance.sina.com.cn/stock/marketresearc...
7 http://finance.sina.com.cn/china/gncj/2016-11-...
8 http://finance.sina.com.cn/stock/usstock/c/201...
9 http://finance.sina.com.cn/stock/usstock/c/201...
10 http://finance.sina.com.cn/stock/s/2016-11-14/...
11 http://finance.sina.com.cn/stock/usstock/c/201...
12 http://finance.sina.com.cn/stock/usstock/c/201...
13 http://finance.sina.com.cn/china/gncj/2016-11-...
14 http://finance.sina.com.cn/stock/marketresearc...
15 http://finance.sina.com.cn/stock/marketresearc...