数据分析小白入门之从Tushare获取财经数据

2019-07-31 10:52:13
tushare简介

下面是官网对其的介绍,详细参考http://tushare.org/

1
2
3
4
5
6
7
8
9
10
Tushare是一个免费、开源的python财经数据接口包。
主要实现对股票等金融数据从数据采集、清洗加工 到 数据存储的过程,能够为金融分析人员提供快速、整洁、
和多样的便于分析的数据,为他们在数据获取方面极大地减轻工作量,
使他们更加专注于策略和模型的研究与实现上。
考虑到Python pandas包在金融量化分析中体现出的优势,Tushare返回的绝大部分的数据格式都是
pandas DataFrame类型,非常便于用pandas/NumPy/Matplotlib进行数据分析和可视化。
当然,如果您习惯了用Excel或者关系型数据库做分析,您也可以通过Tushare的数据存储功能,
将数据全部保存到本地后进行分析。
应一些用户的请求,从0.2.5版本开始,Tushare同时兼容Python 2.x和Python 3.x,
对部分代码进行了重构,并优化了一些算法,确保数据获取的高效和稳定。

tushare安装

不论是linux 还是window系统,在命令行里直接执行相关的pip命令即可

1
2
3
4
5
pip install tushare
pip install matplotlib
pip install openpyxl

pip install jupyter

一切静待执行完毕

1
jupyter notebook

在浏览器打开的页面中选择“new” 创建一个新的python2文件,执行下面这行语句:

1
2
3
import tushare as ts

print(ts.__version__)

正常输出的话就可以使用了

tushare简单使用

最新版本需要token来进行验证,而这个只要注册一个账户即可,下面就是注册链接
https://tushare.pro/register?reg=289223
根据提示,根据我们日常的经验,轻松就可以注册完成

我们可以在我们刚才注册过网站的右上角点击个人主页,在接口TOKEN中我们就可以复制到token
获取到token之后,就是设置token了,直接上代码

1
2
3
4
5
6
7
8
9
10
import tushare as ts

#方式一
ts.set_token('复制的token填在这里')
#这种方式设置会保存token到本地,所以只需设置一次,失效之后,可以替换为新的token

#方式二
pro = ts.pro_api()
pro = ts.pro_api('复制的token填在这里')
这种在初始化接口的时候设置token

设置过token之后,就是使用tushare获取数据

基础功能
  • get_hist_data
    获取股票历史数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    import tushare as ts
    ts.get_hist_data('601006')

    date open high close low volume price_change p_change ma5 ma10 ma20 v_ma5 v_ma10 v_ma20

    2019-07-30 7.90 7.95 7.92 7.89 185306.34 0.03 0.38 7.882 7.847 7.906 206837.68 205383.95 253866.89
    2019-07-29 7.84 7.90 7.89 7.84 174947.23 0.04 0.51 7.862 7.839 7.924 207086.78 211223.57 264796.92
    2019-07-26 7.86 7.88 7.85 7.83 183398.98 -0.02 -0.25 7.840 7.838 7.939 218223.60 216809.15 275667.15
    2019-07-25 7.89 7.90 7.87 7.83 195916.06 -0.01 -0.13 7.828 7.838 7.951 218596.59 220304.86 285239.61
    2019-07-24 7.82 7.91 7.88 7.81 294619.81 0.06 0.77 7.812 7.838 7.965 227321.78 222894.04 308046.28
    2019-07-23 7.76 7.83 7.82 7.75 186551.83 0.04 0.51 7.812 7.837 8.009 203930.21 215005.02 323511.71
    2019-07-22 7.80 7.82 7.78 7.72 230631.33 -0.01 -0.13 7.816 7.849 8.056 215360.36 224992.43 333106.69
    2019-07-19 7.79 7.85 7.79 7.78 185263.91 0.00 0.00 7.836 7.870 8.107 215394.70 254050.48 341493.82
    2019-07-18 7.85 7.86 7.79 7.77 239542.00 -0.09 -1.14 7.848 7.902 8.156 222013.13 268792.96 354813.18
    2019-07-17 7.84 7.88 7.88 7.81 177661.97 0.04 0.51 7.864 7.933 8.204 218466.30 284275.44 370840.09
    2019-07-16 7.88 7.88 7.84 7.82 243702.58 -0.04 -0.51 7.862 7.965 8.241 226079.84 302349.83 395746.08
    2019-07-15 7.85 7.89 7.88 7.77 230803.05 0.03 0.38 7.882 8.008 8.272 234624.51 318370.28 394510.21
    2019-07-12 7.89 7.90 7.85 7.83 218356.06 -0.02 -0.25 7.904 8.040 8.298 292706.26 334525.14 403332.35
    2019-07-11 7.91 7.92 7.87 7.87 221807.84 0.00 0.00 7.956 8.064 8.329 315572.78 350174.36 422860.53
    2019-07-10 7.95 7.96 7.87 7.86 215729.67 -0.07 -0.88 8.002 8.092 8.356 350084.58 393198.52 427026.92
    ......
  • get_realtime_quotes
    获取股票实时行情

    1
    2
    3
    4
    5
    import tushare as ts
    ts.get_realtime_quotes('601006')

    name open pre_close price high low bid ask volume amount ... a2_p a3_v a3_p a4_v a4_p a5_v a5_p date time code
    0 大秦铁路 7.910 7.920 7.920 7.940 7.900 7.920 7.930 8712132 69009119.000 ... 7.940 6582 7.950 4530 7.960 1752 7.970 2019-07-31 11:16:02 601006
  • get_deposit_rate
    除了股票,TuShare 还提供了多种数据,比如宏观经济数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import tushare as ts
    ts.get_deposit_rate()

    date deposit_type rate
    0 2015-10-24 定活两便(定期) --
    1 2015-10-24 定期存款整存整取(半年) 1.30
    2 2015-10-24 定期存款整存整取(二年) 2.10
    3 2015-10-24 定期存款整存整取(三个月) 1.10
    4 2015-10-24 定期存款整存整取(三年) 2.75
    5 2015-10-24 定期存款整存整取(五年) --
    6 2015-10-24 定期存款整存整取(一年) 1.50
    7 2015-10-24 活期存款(不定期) 0.35
  • realtime_boxoffice
    电影票房

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    import tushare as ts
    ts.realtime_boxoffice()

    BoxOffice Irank MovieName boxPer default_url larger_url moblie_url movieDay sumBoxOffice time
    0 4143.16 1 哪吒之魔童降世 81.56 http://images.entgroup.cn/group2/M00/02/8A/wKg... http://images.entgroup.cn/group1/M00/05/13/wKg... http://images.entgroup.cn/group2/M00/02/8A/wKg... 6 112864.35 2019-07-31 11:24:57
    1 428.82 2 银河补习班 8.44 http://images.entgroup.cn/group1/M00/05/0E/wKg... http://images.entgroup.cn/group2/M00/02/8A/wKg... http://images.entgroup.cn/group1/M00/05/0E/wKg... 14 78734.09 2019-07-31 11:24:57
    2 173.57 3 狮子王 3.42 http://images.entgroup.cn/group1/M00/05/0C/wKg... http://images.entgroup.cn/group2/M00/02/8A/wKg... http://images.entgroup.cn/group1/M00/05/0C/wKg... 20 80458.95 2019-07-31 11:24:57
    3 83.24 4 扫毒2:天地对决 1.64 http://images.entgroup.cn/group2/M00/00/59/wKg... http://images.entgroup.cn/group1/M00/05/10/wKg... http://images.entgroup.cn/group2/M00/00/9B/wKg... 27 128413.41 2019-07-31 11:24:57
    4 55.68 5 跳舞吧!大象 1.10 http://images.entgroup.cn/group2/M00/02/8A/wKg... http://images.entgroup.cn/group1/M00/05/13/wKg... 6 3723.65 2019-07-31 11:24:57
    5 34.51 6 回到过去拥抱你 0.68 http://images.entgroup.cn/group1/M00/05/10/wKg... http://images.entgroup.cn/group1/M00/05/10/wKg... 6 1703.80 2019-07-31 11:24:57
    6 28.01 7 烈火英雄 0.55 http://images.entgroup.cn/group1/M00/00/CA/wKg... http://images.entgroup.cn/group1/M00/01/46/wKg... 0 5422.26 2019-07-31 11:24:57
    7 22.08 8 蜘蛛侠:英雄远征 0.43 http://images.entgroup.cn/group1/M00/05/0D/wKg... http://images.entgroup.cn/group1/M00/05/0D/wKg... http://images.entgroup.cn/group1/M00/05/0D/wKg... 34 140925.20 2019-07-31 11:24:57
    8 17.48 9 爱宠大机密2 0.34 http://images.entgroup.cn/group2/M00/00/4D/wKg... http://images.entgroup.cn/group1/M00/05/10/wKg... http://images.entgroup.cn/group1/M00/01/0A/wKg... 27 15257.41 2019-07-31 11:24:57
    9 15.14 10 猪八戒·传说 0.30 http://images.entgroup.cn/group1/M00/05/08/wKg... http://images.entgroup.cn/group1/M00/05/08/wKg... 13 5802.09 2019-07-31 11:24:57
    10 78.23 11 其它 1.00 0 0.00 2019-07-31 11:24:57
  • 演示
    下面的代码来演示下 TuShare 的使用。这里我将获取今年上证指数的日K信息,然后保存成 excel 文件,再画出每日的收盘指数的折线图

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    import tushare as ts
    import matplotlib.pyplot as plt

    df=ts.get_hist_data('sh', start='2019-01-01')
    df.to_excel('stock_sh.xlsx')
    df.close.plot()
    ax = plt.gca()
    ax.invert_xaxis()
    plt.figure()
    plt.show()

image

  • 后续的思考
    通过接口我们看到的是冷冰冰数据,所以,接下来数据的可视化才是重头戏,将这些数据之间的联系结合起来更是具有挑战的事情,对所涉及到的金融概念及他们的意义需要领会,可以根据这些数据实现一些自己的想法。