该策略是一个基于零延迟移动平均线和趋势强度评分的量化交易系统。它通过消除传统移动平均线的滞后性,结合波动率通道和趋势强度评分来识别市场趋势,从而捕捉价格的中短期波动机会。该策略采用双向交易模式,在上升趋势中做多,下降趋势中做空,并设置了止盈止损来控制风险。
策略的核心是通过零延迟移动平均线来消除传统移动平均线的滞后效应。具体实现方法是:首先计算当前价格与滞后价格的差值,然后将该差值与当前价格相加,最后对结果进行移动平均计算。同时,策略引入了趋势强度评分系统,通过比较不同时间周期的价格高低来量化趋势强度。另外,策略还设置了基于ATR的动态波动率通道,用于过滤交易信号。当价格突破通道且趋势评分达到阈值时,才会触发交易信号。
该策略通过创新的零延迟计算方法和趋势强度评分系统,很好地解决了传统趋势跟踪策略中的滞后问题。同时,通过引入动态波动率通道和完善的风险控制机制,提高了策略的稳定性和可靠性。虽然策略在参数优化和市场适应性方面还有改进空间,但整体设计思路清晰,具有较好的实战应用价值。建议交易者在实盘使用时,需要根据具体市场特征和交易标的特性进行适当的参数调整。
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © josephdelvecchio
//@version=6
strategy("Zero Lag Trend Strategy", overlay=true)
// -- Input Parameters --
zeroLagMovAvg = input.string("ema", "Zero Lag Moving Average", options=["ema", "sma"])
length = input.int(50, "Lookback Period")
volatility_mult = input.float(1.5, "Volatility Multiplier")
loop_start = input.int(1, "Loop Start")
loop_end = input.int(50, "Loop End")
threshold_up = input.int(5, "Threshold Up")
threshold_down = input.int(-5, "Threshold Down")
signalpct = input.float(3, "Signal Percentage")
stoppct = input.float(1.5, "Stop Percentage")
// -- Helper Variables --
nATR = ta.atr(length)
lag = math.floor((length - 1) / 2)
zl_basis = zeroLagMovAvg == "ema" ? ta.ema(2 * close - close[lag], length) : ta.sma(2 * close - close[lag], length)
volatility = ta.highest(nATR, length * 3) * volatility_mult
// -- Trend Strength Scoring Function --
forloop_analysis(basis_price, loop_start, loop_end) =>
int sum = 0 // Use 'sum' as you did originally, for the +/- logic
for i = loop_start to loop_end
if basis_price > basis_price[i]
sum += 1
else if basis_price < basis_price[i] // Explicitly check for less than
sum -= 1
// If they are equal, do nothing (sum remains unchanged)
sum
score = forloop_analysis(zl_basis, loop_start, loop_end)
// -- Signal Generation --
long_signal = score > threshold_up and close > zl_basis + volatility
short_signal = score < threshold_down and close < zl_basis - volatility
// -- Trend Detection (Ensure One Trade Until Reversal) --
var int trend = na
trend := long_signal ? 1 : short_signal ? -1 : trend[1]
trend_changed = trend != trend[1]
// -- Stop-Loss & Take-Profit --
stop_loss = close * (1 - stoppct / 100)
take_profit = close * (1 + signalpct / 100)
// -- Strategy Orders (Enter Only When Trend Changes) --
if long_signal
strategy.entry("Long", strategy.long)
else if short_signal
strategy.entry("Short", strategy.short)
// -- Strategy Exits --
strategy.exit("Exit Long", from_entry="Long", stop=stop_loss, limit=take_profit)
strategy.exit("Exit Short", from_entry="Short", stop=take_profit, limit=stop_loss)
// -- Visualization --
p_basis = zl_basis
plot(p_basis, title="Zero Lag Line", color=color.blue, linewidth=2)
// -- Buy/Sell Arrows --
plotshape(series=trend_changed and trend == 1, location=location.belowbar, color=color.green, style=shape.triangleup, size=size.large, title="Buy Signal")
plotshape(series=trend_changed and trend == -1, location=location.abovebar, color=color.red, style=shape.triangledown, size=size.large, title="Sell Signal")