Certainly! Below is a Pine Script v5 implementation for TradingView that encapsulates the Zig Zag Mean Reversion Strategy (ZZ-MRS) as described. This script is designed for 15-minute charts and works on both forex and crypto pairs. It includes:
- Zig Zag indicator with percentage threshold
- EMA crossovers (20/50)
- Candlestick pattern detection (Hammer, Engulfing, Shooting Star, etc.)
- Volume confirmation
- ATR-based volatility filter
- Probabilistic scoring system
- Visual and numerical signal display
Note:
- Pine Script cannot access higher timeframes’ volume or candlestick patterns directly, so multi-timeframe analysis is simplified.
- For best results, use on 15-minute charts.
- This script is for educational purposes; always test before live trading.
//@version=5
strategy("ZZ-MRS: Zig Zag Mean Reversion Strategy", overlay=true, max_bars_back=500, default_qty_type=strategy.percent_of_equity, default_qty_value=1)
// === INPUTS ===
zigzag_percent = input.float(2.0, "Zig Zag % Threshold", minval=0.1, step=0.1)
ema_fast_len = input.int(20, "Fast EMA", minval=1)
ema_slow_len = input.int(50, "Slow EMA", minval=1)
atr_len = input.int(14, "ATR Length", minval=1)
atr_mult = input.float(1.5, "ATR Multiplier for TP/SL", minval=0.1)
vol_mult = input.float(1.5, "Volume Multiplier", minval=0.1)
score_threshold = input.int(70, "Signal Score Threshold", minval=0, maxval=100)
// === ZIG ZAG ===
var float zigzag_high = na
var float zigzag_low = na
var float last_pivot_high = na
var float last_pivot_low = na
var int last_pivot_dir = 0 // 1=high, -1=low
zigzag_move = close * zigzag_percent / 100
if na(zigzag_high) or high > zigzag_high
zigzag_high := high
if na(zigzag_low) or low < zigzag_low
zigzag_low := low
// Detect new pivot high
if high >= zigzag_high and (high - zigzag_low) > zigzag_move
last_pivot_high := high
last_pivot_dir := 1
zigzag_low := low
// Detect new pivot low
if low <= zigzag_low and (zigzag_high - low) > zigzag_move
last_pivot_low := low
last_pivot_dir := -1
zigzag_high := high
plotshape(last_pivot_dir == 1, title="Zig Zag High", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.tiny, offset=0)
plotshape(last_pivot_dir == -1, title="Zig Zag Low", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.tiny, offset=0)
// === MOVING AVERAGES ===
ema_fast = ta.ema(close, ema_fast_len)
ema_slow = ta.ema(close, ema_slow_len)
plot(ema_fast, color=color.blue, linewidth=1, title="Fast EMA")
plot(ema_slow, color=color.orange, linewidth=1, title="Slow EMA")
bull_cross = ta.crossover(ema_fast, ema_slow)
bear_cross = ta.crossunder(ema_fast, ema_slow)
// === ATR & VOLATILITY FILTER ===
atr = ta.atr(atr_len)
vol_ok = volume > ta.sma(volume, 20) * vol_mult
atr_ok = atr < close * 0.05 // 5% of price for crypto, adjust as needed
// === CANDLESTICK PATTERNS ===
is_hammer = ta.candlecolor(1) == color.green and (high - low) > 3 * (open - close) and (close - low) / (0.001 + high - low) > 0.6
is_bull_engulf = close > open and close[1] < open[1] and close > open[1] and open < close[1]
is_shooting_star = ta.candlecolor(1) == color.red and (high - low) > 3 * (close - open) and (high - close) / (0.001 + high - low) > 0.6
is_bear_engulf = close < open and close[1] > open[1] and close < open[1] and open > close[1]
// === SIGNAL LOGIC & SCORING ===
long_score = 0
short_score = 0
// Long: Zig Zag Low, Bullish EMA Cross, Bullish Candle, Volume, ATR
if last_pivot_dir == -1 and low == last_pivot_low
long_score += 20
if bull_cross
long_score += 20
if is_hammer or is_bull_engulf
long_score += 20
if vol_ok
long_score += 15
if atr_ok
long_score += 10
if ema_fast > ema_slow
long_score += 15
// Short: Zig Zag High, Bearish EMA Cross, Bearish Candle, Volume, ATR
if last_pivot_dir == 1 and high == last_pivot_high
short_score += 20
if bear_cross
short_score += 20
if is_shooting_star or is_bear_engulf
short_score += 20
if vol_ok
short_score += 15
if atr_ok
short_score += 10
if ema_fast < ema_slow
short_score += 15
// === ENTRY & EXIT ===
long_entry = long_score >= score_threshold
short_entry = short_score >= score_threshold
// Take Profit and Stop Loss
long_tp = close + atr * atr_mult
long_sl = close - atr * atr_mult
short_tp = close - atr * atr_mult
short_sl = close + atr * atr_mult
if long_entry
strategy.entry("Long", strategy.long, comment="Long Entry")
strategy.exit("Long Exit", "Long", stop=long_sl, limit=long_tp)
label.new(bar_index, low, "Long\nScore: " + str.tostring(long_score), color=color.green, style=label.style_label_up, size=size.small)
if short_entry
strategy.entry("Short", strategy.short, comment="Short Entry")
strategy.exit("Short Exit", "Short", stop=short_sl, limit=short_tp)
label.new(bar_index, high, "Short\nScore: " + str.tostring(short_score), color=color.red, style=label.style_label_down, size=size.small)
// === DASHBOARD ===
var table dash = table.new(position.top_right, 2, 2)
if bar_index % 5 == 0
table.cell(dash, 0, 0, "Long Score", bgcolor=color.new(color.green, 80))
table.cell(dash, 0, 1, str.tostring(long_score))
table.cell(dash, 1, 0, "Short Score", bgcolor=color.new(color.red, 80))
table.cell(dash, 1, 1, str.tostring(short_score))
How to Use
- Copy and paste the code into a new TradingView Pine Script strategy.
- Apply to a 15-minute chart of your chosen forex or crypto pair.
- Adjust parameters (Zig Zag %, ATR multiplier, etc.) for your market and risk tolerance.
- Observe the plotted signals, scores, and dashboard for systematic, data-driven trading.
Enhancements:
- For multi-timeframe confirmation, you can add
request.security() calls for higher timeframe EMAs.
- For more candlestick patterns, expand the pattern detection logic.
- For live trading, always paper trade and optimize parameters for your asset.
Let me know if you need a version for another platform (e.g., MetaTrader) or want to add more features!