该策略利用ZigZag指标判断趋势方向,在趋势确认后进行趋势追踪,属于趋势跟踪策略。
该策略主要通过ZigZag指标判断价格的趋势方向。ZigZag指标能够过滤市场噪音,判断出主要的价格波动方向。当价格创出新高或新低时,ZigZag就会发出交易信号。
具体来说,策略首先计算ZigZag值,当价格创出更高的高点时,ZigZag为该高点价格;当价格创出更低的低点时,ZigZag为该低点价格。这样,ZigZag就能清晰地反映价格的主要波动方向。
策略根据ZigZag值判断趋势方向。当ZigZag上涨时,表明处于上涨趋势;当ZigZag下跌时,表明处于下跌趋势。策略在ZigZag发生转折时开仓,以跟踪趋势方向。
具体来说,策略在ZigZag转折形成新高时开多单,在ZigZag转折形成新低时开空单。平仓条件为ZigZag再次转折反转。这样就实现了基于ZigZag判断趋势的自动交易策略。
可通过以下方法降低风险:
该策略可以从以下几个方面进行优化:
增加止损策略,以控制单笔亏损风险。可以设置移动止损或挂单止损。
增加趋势反转判断机制。可以加入MACD、移动平均线等指标,在判断到趋势反转时关闭仓位。
加入重入模块。在趋势延续时,可以适当加仓,以便获利更多。
增加机器学习模型。可以训练LSTM等深度学习模型,辅助ZigZag判断趋势方向。
优化资金管理机制。可以根据回撤或相关性理论优化仓位大小。
全面优化参数设定。可以通过历史回测和参考专业书籍优化参数,如ZigZag的周期长度等。
该策略利用ZigZag指标判断趋势方向,实现了基于趋势的交易策略。策略逻辑简单清晰,容易实现。但也存在单指标依赖、趋势反转风险等问题。我们可以从止损、辅助指标、重入模块、机器学习等方面进行多维度优化,使策略更稳健、合理。在参数优化和风险控制到位后,该策略可以成为中长线跟踪趋势的有效工具。
/*backtest start: 2022-10-23 00:00:00 end: 2023-04-14 00:00:00 period: 1d basePeriod: 1h exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //Noro //2018 //@version=2 strategy(title = "Noro's ZZ Strategy v1.0", shorttitle = "ZZ str 1.0", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, pyramiding = 0) //Settings needlong = input(true, defval = true, title = "Long") needshort = input(false, defval = false, title = "Short") capital = input(100, defval = 100, minval = 1, maxval = 10000, title = "Capital, %") tf = input('W', title='Timeframe for ZigZag') showzz = input(false, defval = false, title = "Show ZigZag") showbg = input(false, defval = false, title = "Show Background") fromyear = input(1900, defval = 1900, minval = 1900, maxval = 2100, title = "From Year") toyear = input(2100, defval = 2100, minval = 1900, maxval = 2100, title = "To Year") frommonth = input(01, defval = 01, minval = 01, maxval = 12, title = "From Month") tomonth = input(12, defval = 12, minval = 01, maxval = 12, title = "To Month") fromday = input(01, defval = 01, minval = 01, maxval = 31, title = "From day") today = input(31, defval = 31, minval = 01, maxval = 31, title = "To day") //ZigZag zigzag() => _isUp = close >= open _isDown = close <= open _direction = _isUp[1] and _isDown ? -1 : _isDown[1] and _isUp ? 1 : nz(_direction[1]) _zigzag = _isUp[1] and _isDown and _direction[1] != -1 ? highest(2) : _isDown[1] and _isUp and _direction[1] != 1 ? lowest(2) : na useAltTF = true zz = useAltTF ? (change(time(tf)) != 0 ? request.security(syminfo.tickerid, tf, zigzag()) : na) : zigzag() zzcolor = showzz ? black : na plot(zz, title = 'ZigZag', color = zzcolor, linewidth = 2) //Levels dot = zz > 0 ? zz : dot[1] uplevel = dot > dot[1] ? dot : uplevel[1] dnlevel = dot < dot[1] ? dot : dnlevel[1] colorup = close[1] < uplevel[1] and uplevel == uplevel[1] ? lime : na colordn = close[1] > dnlevel[1] and dnlevel == dnlevel[1] ? red : na plot(uplevel, color = colorup, linewidth = 3) plot(dnlevel, color = colordn, linewidth = 3) //Background size = strategy.position_size bgcol = showbg == false ? na : size != size[1] ? blue : na bgcolor(bgcol, transp = 50) //Trading lot = strategy.position_size != strategy.position_size[1] ? strategy.equity / close * capital / 100 : lot[1] if uplevel > 0 and dnlevel > 0 strategy.entry("Long", strategy.long, needlong == false ? 0 : lot, stop = uplevel + syminfo.mintick) strategy.entry("Short", strategy.short, needshort == false ? 0 : lot, stop = dnlevel - syminfo.mintick) if time > timestamp(toyear, tomonth, today, 23, 59) strategy.close_all()