俗话说,“巧媳妇难为无米之炊”。做金融数据研究,首先要有可用的数据源。
Python
下,pandas_datareader
模块可以用于获取研究数据。例子如下:
>>> from pandas_datareader.data import DataReader
>>>
>>> datas = DataReader(name='AAPL', data_source='yahoo', start='2018-01-01')
>>>
>>> type(datas)
<class 'pandas.core.frame.DataFrame'>
>>> datas
Open High Low Close Adj Close \
Date
2018-01-02 170.160004 172.300003 169.259995 172.259995 172.259995
2018-01-03 172.529999 174.550003 171.960007 172.229996 172.229996
2018-01-04 172.539993 173.470001 172.080002 173.029999 173.029999
2018-01-05 173.440002 175.369995 173.050003 175.000000 175.000000
2018-01-08 174.350006 175.610001 173.929993 174.350006 174.350006
2018-01-09 174.550003 175.059998 173.410004 174.330002 174.330002
2018-01-10 173.160004 174.300003 173.000000 174.289993 174.289993
2018-01-11 174.589996 175.490005 174.490005 175.279999 175.279999
2018-01-12 176.179993 177.360001 175.649994 177.089996 177.089996
Volume
Date
2018-01-02 25555900
2018-01-03 29517900
2018-01-04 22434600
2018-01-05 23660000
2018-01-08 20567800
2018-01-09 21584000
2018-01-10 23959900
2018-01-11 18667700
2018-01-12 25226000
>>>
>>> print(datas.to_csv())
Date,Open,High,Low,Close,Adj Close,Volume
2018-01-02,170.160004,172.300003,169.259995,172.259995,172.259995,25555900
2018-01-03,172.529999,174.550003,171.960007,172.229996,172.229996,29517900
2018-01-04,172.539993,173.470001,172.080002,173.029999,173.029999,22434600
2018-01-05,173.440002,175.369995,173.050003,175.0,175.0,23660000
2018-01-08,174.350006,175.610001,173.929993,174.350006,174.350006,20567800
2018-01-09,174.550003,175.059998,173.410004,174.330002,174.330002,21584000
2018-01-10,173.160004,174.300003,173.0,174.289993,174.289993,23959900
2018-01-11,174.589996,175.490005,174.490005,175.279999,175.279999,18667700
2018-01-12,176.179993,177.360001,175.649994,177.089996,177.089996,25226000
>>>
首先,引入DataReader
方法;
然后,以股票代码(name
)、数据源(data_source
)以及开始日期(start
)为参数,调用DataReader
获取数据;
获取到的数据(datas
)是一个DataFrame
,以日期(Date
)为索引,包含Open
、High
、Low
、Close
等多列指标。
DataFrame
数据也可以非常方便地转换成csv
格式并输出。
注意到,pandas_datareader
还可以使用其他数据源,比如Google Finance
。
更多关于pandas_datareader
使用方式,请参考:pandas-datareader文档。