来自于两年前用户公开的一个简单的波动率策略
无意翻到, 用PINE重写了下代码量减少了 90% 给量化的朋友们学习参考
回测测试
/*backtest start: 2021-09-01 09:00:00 end: 2022-06-06 15:00:00 period: 1d basePeriod: 1h exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}] args: [["ContractType","rb888",360008]] */ N = input(50, '指数计算周期') vix = math.log(close) / math.log(close[N-1]) - 1 vix_ma = ta.ema(vix, N) vix_ma_up = ta.highest(vix_ma, N) vix_ma_down = ta.lowest(vix_ma, N) plot(vix, 'vix') plot(vix_ma, 'vix_ma') upline = plot(vix_ma_up, 'vix_ma_up') downline = plot(vix_ma_down, 'vix_ma_down') fill(upline, downline, color=color.new(color.gray, 50)) qty = 1 // math.round(strategy.equity / close, 2) if strategy.position_size == 0 if vix > vix_ma_up and vix[1] <= vix_ma_up[1] strategy.entry("LONG", strategy.long, qty=qty) else if vix < vix_ma_down and vix[1] >= vix_ma_down[1] strategy.entry("SHORT", strategy.short, qty=qty) else if strategy.position_size > 0 if vix < vix_ma and vix[1] >= vix_ma[1] strategy.close_all() else if strategy.position_size < 0 if vix > vix_ma and vix[1] <= vix_ma[1] strategy.close_all()template: strategy.tpl:40:21: executing "strategy.tpl" at <.api.GetStrategyListByName>: wrong number of args for GetStrategyListByName: want 7 got 6