前一篇博客讲到如何筛选合适股票对。鉴于笔者决定从2015.1.1至今做回测,最终选取了2015.1.1开始可以获得股票数据,且SSD最小的股票对?,?来进行单配对。
笔者不愿意透露的核心数据将使用‘?’表示。
基本思想:
分别计算两只股票?日收盘价标准差std1,std2。
分别计算年化数值hv1,hv2。
计算相关系数,因股票高度相关,可设为?。
计算开仓上下轨vb_up,vb_down。
计算最新价涨跌幅度的价差 s。
根据s和vb的比较结果做多做空,并止盈止损。
策略中需要记录的标志较多,实现初始化
#标志是否做多做空
g.flag_long=0
g.flag_short=0
#标志从何处回到正常趋势
g.flag_sd=0
g.flag_su=0
#标志做多做空股数
g.num_long=0
g.num_short=0
#标志是否突破上下轨
g.vb_up=0
g.vb_down=0
监测到突破上下轨,并且未曾操作,则做多做空
def open1(context,last_price1,last_price2):
#做多1,做空2
if g.flag_long==0:
g.flag_long=1
g.num_long=(g.cash*1.0/last_price1)//100*100
order(g.pair[0], g.num_long,pindex=0)
if g.flag_short==0:
g.num_short=(g.cash*1.0/last_price2)//100*100
if marginsec_open(g.pair[1], g.num_short,pindex=1)!=None:
g.flag_short=1
做多2,做空1与之类似。
监测到回到正常趋势,检测从何方向回归,对应平仓
def close1(context):
#从open1平仓
if g.flag_long==1:
if order_target(g.pair[0], 0, pindex=0) != None:
g.flag_long=0
g.num_long=0
if g.flag_short==1:
marginsec_close(g.pair[1], g.num_short, pindex=1)
g.flag_short=0
g.num_short=0
从另一方向平仓与之类似。
监测股票运行
if s<g.vb_down:
if g.flag_sd==0:
open1(context,last_price1,last_price2)
g.flag_sd=1
elif s>g.vb_up:
if g.flag_su==0:
open2(context,last_price1,last_price2)
g.flag_su=1
else:
if g.flag_sd==1:
close1(context)
g.flag_sd=0
elif g.flag_su==1:
close2(context)
g.flag_su=0
上下轨、最新价涨跌幅度的价差计算
????????????
????????????
????????????
止盈、止损
????????????
????????????
????????????
2015.1.1~2018.6.29回测结果