get_hist_data接口
tushare的get_hist_data()个股历史数据中,默认支持以下返回值,包含的均线数据是ma5, ma10和ma20:
- date:日期
- open:开盘价
- high:最高价
- close:收盘价
- low:最低价
- volume:成交量
- price_change:价格变动
- p_change:涨跌幅
- ma5:5日均价
- ma10:10日均价
- ma20:20日均价
- v_ma5:5日均量
- v_ma10:10日均量
- v_ma20:20日均量
- turnover:换手率[注:指数无此项]
调用方式:
import tushare as ts
ts.get_hist_data('600848')
ts.get_k_data接口
而最新的接口get_k_data(),干脆只返回了基本数据:
- date 日期和时间。 低频数据时为:YYYY-MM-DD; 高频数为:YYYY-MM-DD HH:MM
- open 开盘价
- close 收盘价
- high 最高价
- low 最低价
- volume 成交量
- code 证券代码
自定义自己需要的MA值
在用软件定义自己的均线策略的时候,时常需要直到各个移动平均线的值。虽然tushare没有提供这样的功能,但自己做起来确实非常但简单。
下面的例子先用get_hist_data()举例,目的是为了对比值是否正确,在将来的操作中,作者也说了,最好是用get_k_data()的鹅厂数据。
取出数据
import tushare as ts
sqjt = ts.get_hist_data('600104')
sqjt.head()
Out[3]:
open high close low volume price_change p_change \
date
2017-10-16 32.17 32.35 32.01 31.46 215734.89 0.04 0.12
2017-10-13 32.10 32.60 31.97 31.87 205029.53 -0.14 -0.44
2017-10-12 31.90 32.28 32.11 31.54 256751.47 0.37 1.17
2017-10-11 30.70 31.84 31.74 30.65 547914.75 1.25 4.10
2017-10-10 31.30 31.45 30.49 29.94 566905.44 -0.78 -2.49
ma5 ma10 ma20 v_ma5 v_ma10 v_ma20 turnover
date
2017-10-16 31.664 31.109 30.348 358467.22 296958.31 274748.68 0.20
2017-10-13 31.516 30.958 30.218 410394.98 306801.60 273160.20 0.19
2017-10-12 31.160 30.792 30.080 400913.36 328966.87 269473.18 0.23
2017-10-11 30.822 30.533 29.938 382196.72 319873.73 263040.20 0.50
2017-10-10 30.560 30.282 29.813 315010.56 290065.85 246347.60 0.51
使用pandas的rolling接口
Signature: sqjt.rolling(window, min_periods=None, freq=None, center=False, win_type=None, on=None, axis=0, closed=None)
Docstring:
Provides rolling window calculcations.
.. versionadded:: 0.18.0
parameters:
window : int, or offset
Size of the moving window. This is the number of observations used for
calculating the statistic. Each window will be a fixed size.
这里,关心window就够了,window就是移动窗口的大小。我们设置一个移动窗口,再在这个窗口里的数据用mean()方法取平均值。
sqjt['close'].rolling(5).mean()
Out[7]:
date
2017-10-16 NaN
2017-10-13 NaN
2017-10-12 NaN
2017-10-11 NaN
2017-10-10 31.664
2017-10<