然后计算一下这个策略的效果。
主要用 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-DDend:结束日期,格式YYYY-MM-DDktype:数据类型,D=日k线 W=周 M=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟,默认为Dretry_count:当网络异常后重试次数,默认为3pause:重试时停顿秒数,默认为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 这两个值就可以了。
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)
stock number: 600000
buy count : 41
sell count : 41
buy date : 2014-03-06, buy price : 8.80
sell date: 2014-03-07, sell price: 8.73
buy date : 2014-03-13, buy price : 8.84
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
buy date : 2016-02-04, buy price : 17.26
sell date: 2016-03-11, sell price: 16.99
buy date : 2016-03-16, buy price : 18.03
sell date: 2016-03-28, sell price: 17.46
buy date : 2016-03-30, buy price : 17.88
sell date: 2016-04-07, sell price: 17.69
buy date : 2016-04-15, buy price : 17.89
sell date: 2016-04-18, sell price: 17.82
buy date : 2016-04-19, buy price : 17.90
sell date: 2016-04-27, sell price: 17.86
buy date : 2016-04-28, buy price : 17.93
sell date: 2016-04-29, sell price: 17.84
buy date : 2016-05-03, buy price : 18.14
sell date: 2016-05-06, sell price: 17.74
buy date : 2016-05-25, buy price : 17.67
sell date: 2016-06-15, sell price: 17.76
buy date : 2016-06-20, buy price : 17.86
sell date: 2016-06-21, sell price: 17.81
buy date : 2016-07-20, buy price : 15.75
sell date: 2016-07-22, sell price: 15.61
buy date : 2016-07-25, buy price : 15.69
sell date: 2016-07-29, sell price: 15.69
buy date : 2016-08-01, buy price : 15.80
sell date: 2016-08-03, sell price: 15.67
buy date : 2016-08-05, buy price : 15.72
sell date: 2016-09-12, sell price: 16.40
buy date : 2016-09-13, buy price : 16.45
sell date: 2016-09-14, sell price: 16.40
buy date : 2016-09-19, buy price : 16.47
sell date: 2016-10-13, sell price: 16.43
buy date : 2016-10-24, buy price : 16.47
sell date: 2016-10-25, sell price: 16.42
buy date : 2016-11-04, buy price : 16.39
sell date: 2016-12-15, sell price: 16.70
buy date : 2017-01-13, buy price : 16.27
sell date: 2017-02-17, sell price: 16.64
buy date : 2017-02-20, buy price : 16.91
sell date: 2017-02-23, sell price: 16.69
rate: 1.49