pinescript
//@version=5
strategy("Zig Zag Mean Reversion Strategy (ZZ-MRS)", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=1)
// === INPUTS ===
zigzag_perc = input.float(2.0, "Zig Zag % Threshold", step=0.1) / 100
ema_short_len = input.int(20, "Short EMA Length")
ema_long_len = input.int(50, "Long EMA Length")
volume_mult = input.float(1.5, "Volume Multiplier for Confirmation")
atr_len = input.int(14, "ATR Length")
atr_tp_mult = input.float(2.0, "Take Profit ATR Multiplier")
atr_sl_mult = input.float(1.0, "Stop Loss ATR Multiplier")
// === INDICATORS ===
// EMAs
ema_short = ta.ema(close, ema_short_len)
ema_long = ta.ema(close, ema_long_len)
// ATR for volatility and TP/SL
atr = ta.atr(atr_len)
// Volume average
vol_avg = ta.sma(volume, 20)
// === ZIG ZAG IMPLEMENTATION ===
// Zig Zag pivots based on % price movement from last pivot
var float zz_last_pivot_price = na
var int zz_last_pivot_bar = na
var int zz_dir = 0 // 1 = up, -1 = down
// Detect Zig Zag pivots
zigzag_pivot = false
zigzag_pivot_type = 0 // 1 = low pivot, -1 = high pivot
if na(zz_last_pivot_price)
zz_last_pivot_price := close
zz_last_pivot_bar := bar_index
zigzag_pivot := true
zigzag_pivot_type := 1 // start with low pivot
else
price_change = (close - zz_last_pivot_price) / zz_last_pivot_price
if zz_dir <= 0 and price_change >= zigzag_perc
// New high pivot
zz_last_pivot_price := close
zz_last_pivot_bar := bar_index
zz_dir := 1
zigzag_pivot := true
zigzag_pivot_type := -1
else if zz_dir >= 0 and price_change <= -zigzag_perc
// New low pivot
zz_last_pivot_price := close
zz_last_pivot_bar := bar_index
zz_dir := -1
zigzag_pivot := true
zigzag_pivot_type := 1
// Store pivots for plotting
var float[] zz_pivot_prices = array.new_float()
var int[] zz_pivot_bars = array.new_int()
var int[] zz_pivot_types = array.new_int()
if zigzag_pivot
array.push(zz_pivot_prices, zz_last_pivot_price)
array.push(zz_pivot_bars, zz_last_pivot_bar)
array.push(zz_pivot_types, zigzag_pivot_type)
// Plot Zig Zag lines connecting pivots
for i = 1 to array.size(zz_pivot_prices) - 1
p1 = array.get(zz_pivot_prices, i - 1)
p2 = array.get(zz_pivot_prices, i)
b1 = array.get(zz_pivot_bars, i - 1)
b2 = array.get(zz_pivot_bars, i)
line.new(b1, p1, b2, p2, color=color.new(color.blue, 50), width=2)
// === CANDLESTICK PATTERN DETECTION ===
// Hammer: small body near top, long lower wick
isHammer() =>
body = math.abs(close - open)
lower_wick = open < close ? open - low : close - low
upper_wick = high - math.max(open, close)
body < (high - low) * 0.3 and lower_wick > body * 2 and upper_wick < body
// Bullish Engulfing: current candle engulfs previous candle body upward
isBullishEngulfing() =>
prev_open = open[1]
prev_close = close[1]
close > open and prev_close < prev_open and close > prev_open and open < prev_close
// Bearish Engulfing: current candle engulfs previous candle body downward
isBearishEngulfing() =>
prev_open = open[1]
prev_close = close[1]
close < open and prev_close > prev_open and close < prev_open and open > prev_close
// Shooting Star: small body near bottom, long upper wick
isShootingStar() =>
body = math.abs(close - open)
upper_wick = high - math.max(open, close)
lower_wick = math.min(open, close) - low
body < (high - low) * 0.3 and upper_wick > body * 2 and lower_wick < body
// === MOVING AVERAGE CROSSOVERS ===
bull_cross = ta.crossover(ema_short, ema_long)
bear_cross = ta.crossunder(ema_short, ema_long)
// === VOLUME CONFIRMATION ===
vol_confirm = volume > vol_avg * volume_mult
// === SIGNAL LOGIC ===
// Find last Zig Zag pivot price and type
last_pivot_price = array.size(zz_pivot_prices) > 0 ? array.get(zz_pivot_prices, array.size(zz_pivot_prices) - 1) : na
last_pivot_type = array.size(zz_pivot_types) > 0 ? array.get(zz_pivot_types, array.size(zz_pivot_types) - 1) : na
// Define proximity threshold for price to pivot (e.g., 0.3% of price)
pivot_proximity = 0.003
price_near_pivot = math.abs(close - last_pivot_price) / last_pivot_price < pivot_proximity
// Entry conditions
long_entry_cond = price_near_pivot and last_pivot_type == 1 and bull_cross and vol_confirm and (isHammer() or isBullishEngulfing())
short_entry_cond = price_near_pivot and last_pivot_type == -1 and bear_cross and vol_confirm and (isShootingStar() or isBearishEngulfing())
// Probabilistic scoring system
score = 0
score += price_near_pivot ? 20 : 0
score += bull_cross or bear_cross ? 20 : 0
score += (isHammer() or isBullishEngulfing() or isShootingStar() or isBearishEngulfing()) ? 20 : 0
score += vol_confirm ? 15 : 0
// Multi-timeframe and volatility filters can add more points here if implemented
// For now, assume 75 as threshold
score_threshold = 75
long_signal = long_entry_cond and score >= score_threshold
short_signal = short_entry_cond and score >= score_threshold
// === STRATEGY EXECUTION ===
if (long_signal)
strategy.entry("Long", strategy.long)
// TP and SL based on ATR
strategy.exit("Long Exit", "Long", stop=close - atr * atr_sl_mult, limit=close + atr * atr_tp_mult)
if (short_signal)
strategy.entry("Short", strategy.short)
strategy.exit("Short Exit", "Short", stop=close + atr * atr_sl_mult, limit=close - atr * atr_tp_mult)
// === EXIT SIGNALS ===
// Exit on opposite candlestick pattern + MA crossover + volume drop
exit_long_cond = strategy.position_size > 0 and (isBearishEngulfing() or isShootingStar()) and bear_cross and volume < vol_avg
exit_short_cond = strategy.position_size < 0 and (isHammer() or isBullishEngulfing()) and bull_cross and volume < vol_avg
if exit_long_cond
strategy.close("Long")
if exit_short_cond
strategy.close("Short")
// === PLOT ===
plot(ema_short, color=color.orange, title="EMA 20")
plot(ema_long, color=color.blue, title="EMA 50")
// Plot buy/sell arrows
plotshape(long_signal, title="Long Entry", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY", size=size.small)
plotshape(short_signal, title="Short Entry", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL", size=size.small)
// Plot last Zig Zag pivot as circle
plotshape(zigzag_pivot and last_pivot_type == 1, title="ZZ Support", location=location.belowbar, color=color.lime, style=shape.circle, size=size.tiny)
plotshape(zigzag_pivot and last_pivot_type == -1, title="ZZ Resistance", location=location.abovebar, color=color.red, style=shape.circle, size=size.tiny)
// Display score on chart
var label score_label = na
if barstate.islast
label.delete(score_label)
score_label := label.new(bar_index, high, "Score: " + str.tostring(score), color=color.new(color.gray, 80), style=label.style_label_down, yloc=yloc.abovebar)
// === END OF SCRIPT ===
If you want me to help with any of these enhancements or port the strategy to another platform (e.g., MetaTrader, Python), just ask!