ETF28择时交易法,回测结果,18年之前还可以,18年以后到现在,亏损40%,惨啊,什么原因,还不知道,先记下来,回头再搞。以下是代码:
#设置参数和回测
def initialize(context):
set_params()
set_backtest()
#设置参数,定义交易时间和股票
def set_params():
#设置基准收益
set_benchmark('000300.XSHG')
g.lag = 20 #回溯期
g.hour = 14 #小时
g.minute = 53 #分钟
g.sz = '000016.XSHG' #计算标的,上证50指数,超级大盘股
g.hs = '000300.XSHG' #计算标的,沪深300指数,价值股
g.zz = '000905.XSHG' #计算标的,中证500指数,成长股
g.ETF50 = '510050.XSHG' #交易标的50
g.ETF300 = '510300.XSHG' #交易标的300
g.ETF500 = '510500.XSHG' #交易标的500
#设置回测条件
def set_backtest():
set_option('use_real_price', True) #用真实价格交易
log.set_level('order', 'error') #过滤日志
#每天开盘前要做的事情,输入context,输出none
def before_trading_start(context):
#将滑点设置为交易额的千分之2
set_slippage(PriceRelatedSlippage(0.002))
#设置手续费
dt = context.current_dt
set_order_cost(OrderCost(open_tax=0,close_tax=0.001,open_commission=0.0003,\
close_commission=0.0003,close_today_commission=0,min_commission=5),type='stock')
#买入时印花税0,卖出时印花税千1,买入佣金万3,卖出佣金万3,最低佣金5元
#定义函数,取得股票在某个期间的收盘价
def getStockPrice(stock, interval):
h = attribute_history(stock, interval, unit='1d', fields=('close'),skip_paused=True)
return (h['close'].values[0], h['close'].values[-1])
#计算得到信号,是一个string
def get_signal(context):
#取出价格
interval50,Yesterday50 = getStockPrice(g.sz, g.lag)
interval300,Yesterday300 = getStockPrice(g.hs, g.lag)
interval500,Yesterday500 = getStockPrice(g.zz, g.lag)
#计算20日动量
sz50increase = (Yesterday50-interval50) / interval50
hs300increase = (Yesterday300-interval300) / interval300
zz500increase = (Yesterday500-interval500) /interval500
#计算3个指数基金的持有金额
hold50 = context.portfolio.positions[g.ETF50].total_amount
hold300 = context.portfolio.positions[g.ETF300].total_amount
hold500 = context.portfolio.positions[g.ETF500].total_amount
#对3个标的的止损
if(sz50increase<=0 and hold50>0) or(hs300increase<=0 and hold300>0) or(zz500increase<=0 and hold500>0):
print('sell')
return 'sell_the_stocks' #返回string给get_signal函数
#如果50增长率大于300和500,且幅度大于0.01,且50增长率>0,且50、300、500无仓位,买入50
elif sz50increase-hs300increase>0.01 and sz50increase-zz500increase>0.01 and sz50increase>0 and (hold50==0 and hold300==0 and hold500==0):
print('buy 50')
return 'ETF50'
#如果300增长率大于50和500,且幅度大于0.01,且300增长率>0,且50、300、500无仓位,买入300
elif hs300increase-zz500increase>0.01 and hs300increase-sz50increase>0.01 and hs300increase>0 and (hold50==0 and hold300==0 and hold500==0):
print('buy 300')
return 'ETF300'
#as same as above
elif zz500increase-sz50increase>0.01 and zz500increase-hs300increase>0.01 and zz500increase>0 and (hold50==0 and hold300==0 and hold500==0):
print('buy 500')
return 'ETF500'
#卖出指令
def sell_the_stocks(context):
for i in context.portfolio.positions.keys():
return (log.info("Selling %s" %i), order_target_value(i,0))
#买入股票
def buy_the_stocks(context,signal):
return (log.info("Buying %s"%signal),order_value(eval('g.%s'%signal),context.portfolio.available_cash))
def handle_data(context, data):
hour=context.current_dt.hour
minute = context.current_dt.minute
#if hour==g.hour and minute==g.minute:
signal=get_signal(context)
print('trade signal is %s'%signal)
if signal=='sell_the_stocks':
sell_the_stocks(context)
print('sell sell sell')
elif signal == 'ETF50' or signal=='ETF300' or signal=='ETF500':
buy_the_stocks(context, signal)
print('buy buy buy')
def after_trading_end(context):
log.info(context.portfolio.available_cash+context.portfolio.positions_value)
return