[Python 与 炒股] TuShare 使用篇之三

2016年新年第一贴,大年夜搞这个只能说明春晚实在是有点无聊。


在之前的blog里写了一个最简单的例子:

http://blog.csdn.net/robertsong2004/article/details/50642655

现在试一下简单的分析,即设定一个策略:以20日线为标准,当前股价低于20日线的时候就卖出,高于20日线的时候就买入。

然后计算一下这个策略的效果。

主要用 TuShare  里的 get_hist_data 这个接口,用于获取到目前为止3年的历史数据。

主要的用法参照如下:

获取个股历史交易数据(包括均线数据),可以通过参数设置获取日k线、周k线、月k线,以及5分钟、15分钟、30分钟和60分钟k线数据。本接口只能获取近3年的日线数据,适合搭配均线数据进行选股和分析,如果需要全部历史数据,请调用下一个接口get_h_data()。

参数说明:

  • code:股票代码,即6位数字代码,或者指数代码(sh=上证指数 sz=深圳成指 hs300=沪深300指数 sz50=上证50 zxb=中小板 cyb=创业板)
  • start:开始日期,格式YYYY-MM-DD
  • end:结束日期,格式YYYY-MM-DD
  • ktype:数据类型,D=日k线 W=周 M=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟,默认为D
  • retry_count:当网络异常后重试次数,默认为3
  • pause:重试时停顿秒数,默认为0

返回值说明:

  • 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:换手率[注:指数无此项]
那我们只要比较 v_ma20 和 close 这两个值就可以了。

OK,写了一个很简单的程序:

#!/usr/bin/python
# coding: UTF-8

"""This script parse stock info"""

import tushare as ts

def parse(code_list):
    '''process stock'''
    is_buy    = 0
    buy_val   = []
    buy_date  = []
    sell_val  = []
    sell_date = []
    df = ts.get_hist_data(STOCK)
    ma20 = df[u'ma20']
    close = df[u'close']
    rate = 1.0
    idx = len(ma20)

    while idx > 0:
        idx -= 1
        close_val = close[idx]
        ma20_val = ma20[idx]
        if close_val > ma20_val:
                if is_buy == 0:
                        is_buy = 1
                        buy_val.append(close_val)
                        buy_date.append(close.keys()[idx])
        elif close_val < ma20_val:
                if is_buy == 1:
                        is_buy = 0
                        sell_val.append(close_val)
                        sell_date.append(close.keys()[idx])

    print "stock number: %s" %STOCK
    print "buy count   : %d" %len(buy_val)
    print "sell count  : %d" %len(sell_val)

    for i in range(len(sell_val)):
        rate = rate * (sell_val[i] * (1 - 0.002) / buy_val[i])
        print "buy date : %s, buy price : %.2f" %(buy_date[i], buy_val[i])
        print "sell date: %s, sell price: %.2f" %(sell_date[i], sell_val[i])

    print "rate: %.2f" % rate

if __name__ == '__main__':
    STOCK = '600000'       ##浦发银行
    parse(STOCK)

主要就是调 parse 这个函数,这个函数就会从数据中读 20 日线的值和当日收盘价做对比,如果当日收盘价比20日线值高且之前没有买入,那就买入。

如果当日收盘价比20日线值低且之前已经买入,那就卖出。

然后加了一个 rate 参数,这是用于统计每次买卖后的收益。这里有个0.002的值,表示两块一个是印花税卖出后有1%的印花税要抽,一个是券商的佣金,按万5来算,

一来一回也是千分之一。


跑一下结果如下,

stock number: 600000
buy count   : 39
sell count  : 38
buy date : 2013-02-28, buy price : 11.06
sell date: 2013-03-04, sell price: 10.39
buy date : 2013-03-05, buy price : 11.07
sell date: 2013-03-07, sell price: 10.76
buy date : 2013-03-19, buy price : 10.65
sell date: 2013-03-26, sell price: 10.70
buy date : 2013-04-22, buy price : 10.22
sell date: 2013-04-23, sell price: 9.84
buy date : 2013-05-03, buy price : 10.01
sell date: 2013-06-03, sell price: 9.84
buy date : 2013-07-11, buy price : 8.86
sell date: 2013-07-19, sell price: 8.03
buy date : 2013-08-12, buy price : 8.31
sell date: 2013-09-26, sell price: 10.17
buy date : 2013-10-29, buy price : 10.48
sell date: 2013-11-06, sell price: 10.03
buy date : 2013-11-12, buy price : 10.12
sell date: 2013-11-13, sell price: 9.64
buy date : 2013-11-18, buy price : 10.10
sell date: 2013-11-19, sell price: 9.97
buy date : 2013-11-29, buy price : 9.99
sell date: 2013-12-11, sell price: 9.93
buy date : 2014-01-10, buy price : 9.41
sell date: 2014-01-15, sell price: 9.23
buy date : 2014-01-22, buy price : 9.34
sell date: 2014-01-23, sell price: 9.22
buy date : 2014-01-29, buy price : 9.29
sell date: 2014-01-30, sell price: 9.17
buy date : 2014-02-10, buy price : 9.29
sell date: 2014-02-18, sell price: 9.26
buy date : 2014-02-19, buy price : 9.49
sell date: 2014-02-21, sell price: 9.29
buy date : 2014-03-14, buy price : 8.99
sell date: 2014-03-18, sell price: 8.89
buy date : 2014-03-19, buy price : 9.05
sell date: 2014-04-17, sell price: 9.76
buy date : 2014-04-22, buy price : 9.90
sell date: 2014-04-24, sell price: 9.87
buy date : 2014-05-12, buy price : 9.93
sell date: 2014-05-15, sell price: 9.73
buy date : 2014-05-26, buy price : 9.74
sell date: 2014-05-27, sell price: 9.66
buy date : 2014-06-13, buy price : 9.73
sell date: 2014-06-24, sell price: 9.04
buy date : 2014-07-22, buy price : 9.06
sell date: 2014-08-20, sell price: 9.64
buy date : 2014-09-02, buy price : 9.60
sell date: 2014-10-23, sell price: 9.75
buy date : 2014-10-30, buy price : 9.81
sell date: 2015-01-19, sell price: 14.82
buy date : 2015-01-21, buy price : 15.74
sell date: 2015-01-26, sell price: 15.43
buy date : 2015-02-26, buy price : 14.62
sell date: 2015-03-03, sell price: 14.00
buy date : 2015-03-09, buy price : 14.51
sell date: 2015-03-10, sell price: 14.10
buy date : 2015-03-11, buy price : 14.25
sell date: 2015-05-04, sell price: 17.81
buy date : 2015-05-22, buy price : 17.76
sell date: 2015-05-28, sell price: 17.04
buy date : 2015-06-01, buy price : 17.93
sell date: 2015-06-19, sell price: 17.06
buy date : 2015-07-06, buy price : 17.22
sell date: 2015-07-08, sell price: 15.98
buy date : 2015-07-10, buy price : 17.31
sell date: 2015-07-14, sell price: 16.74
buy date : 2015-07-15, buy price : 17.07
sell date: 2015-07-20, sell price: 16.68
buy date : 2015-08-31, buy price : 14.97
sell date: 2015-09-07, sell price: 14.41
buy date : 2015-09-08, buy price : 15.10
sell date: 2015-12-11, sell price: 18.61
buy date : 2015-12-21, buy price : 19.16
sell date: 2015-12-22, sell price: 18.89
buy date : 2015-12-24, buy price : 19.03
sell date: 2015-12-28, sell price: 18.72
rate: 1.23

最终收益率为23%,这样就是一个最简单的量化工具了。

这里还有一些问题,上述的接口是没有复权的,这个如果除权或分红比较多的话就有比较大的偏差,要加上前复权的处理。

另外就是有时候非必是买卖都能完成的,比如无量涨停和无量跌停以及之前的熔断机制,这种情况可以往后推直到涨跌停破了为止。

你可能感兴趣的:([Python 与 炒股] TuShare 使用篇之三)