
This strategy is a quantitative trading system based on zero-lag moving averages and trend strength scoring. It eliminates the lag of traditional moving averages, combining volatility channels and trend strength scoring to identify market trends and capture medium to short-term price movements. The strategy employs bi-directional trading, going long in uptrends and short in downtrends, with built-in stop-loss and take-profit mechanisms for risk control.
The core of the strategy lies in eliminating the lag effect of traditional moving averages through zero-lag calculation. This is achieved by first calculating the difference between current and lagged prices, adding this difference to the current price, and then applying moving average calculations to the result. The strategy also incorporates a trend strength scoring system that quantifies trend strength by comparing price levels across different timeframes. Additionally, it implements an ATR-based dynamic volatility channel to filter trading signals. Trade signals are only triggered when price breaks through the channel and trend score reaches the threshold.
This strategy effectively addresses the lag issues in traditional trend-following strategies through innovative zero-lag calculation methods and trend strength scoring system. By incorporating dynamic volatility channels and comprehensive risk control mechanisms, it enhances strategy stability and reliability. While there is room for improvement in parameter optimization and market adaptability, the overall design is clear and shows good practical application value. Traders are advised to adjust parameters according to specific market characteristics and trading instruments when implementing the strategy in live trading.
/*backtest
start: 2024-11-14 00:00:00
end: 2025-02-19 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"DOGE_USDT"}]
*/
// 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 --
timeframe = input.timeframe("10", "Timeframe")
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(8, "Signal Percentage")
stoppct = input.float(0, "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")