//@version=6
strategy("XAUUSD MTF Pullback Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// === Inputs ===
ema200_len = input.int(200, title="EMA 200")
ema50_len = input.int(50, title="EMA 50")
rsi_len = input.int(14, title="RSI Length")
macd_fast = input.int(12, title="MACD Fast")
macd_slow = input.int(26, title="MACD Slow")
macd_signal = input.int(9, title="MACD Signal")
atr_len = input.int(14, title="ATR Length")
// === Helper functions ===
get_macd_hist() =>
[macdLine, signalLine, histLine] = ta.macd(close, macd_fast, macd_slow, macd_signal)
histLine
get_rsi() =>
ta.rsi(close, rsi_len)
// === Higher Timeframe (1H) Trend Filter ===
htf_close = request.security(syminfo.tickerid, "60", close)
htf_ema200 = request.security(syminfo.tickerid, "60", ta.ema(close, ema200_len))
htf_trend_up = htf_close > htf_ema200
// === Mid Timeframe (5M) Confirmation ===
mtf_macd_hist = request.security(syminfo.tickerid, "5", get_macd_hist())
mtf_rsi = request.security(syminfo.tickerid, "5", get_rsi())
// === Current Timeframe (1M) Indicators ===
ema200 = ta.ema(close, ema200_len)
ema50 = ta.ema(close, ema50_len)
[macdLine, signalLine, histLine] = ta.macd(close, macd_fast, macd_slow, macd_signal)
rsi = ta.rsi(close, rsi_len)
atr = ta.atr(atr_len)
// === Long Condition ===
longCond = htf_trend_up and close > ema200 and close <= ema50 and mtf_macd_hist > 0 and mtf_rsi < 50 and histLine > 0 and rsi < 50
// === Short Condition ===
shortCond = not htf_trend_up and close < ema200 and close >= ema50 and mtf_macd_hist < 0 and mtf_rsi > 50 and histLine < 0 and rsi > 50
// === ATR-based SL and Fibonacci TP Levels ===
var float sl_long = na
var float tp1_long = na
var float tp2_long = na
var float tp3_long = na
var float sl_short = na
var float tp1_short = na
var float tp2_short = na
var float tp3_short = na
if longCond
sl_long := close - atr
tp1_long := close + atr
tp2_long := close + (atr * 1.618)
tp3_long := close + (atr * 2.618)
if shortCond
sl_short := close + atr
tp1_short := close - atr
tp2_short := close - (atr * 1.618)
tp3_short := close - (atr * 2.618)
// === Orders ===
if longCond
strategy.entry("Long", strategy.long)
strategy.exit("Long Exit", from_entry="Long", stop=sl_long, limit=tp1_long)
if shortCond
strategy.entry("Short", strategy.short)
strategy.exit("Short Exit", from_entry="Short", stop=sl_short, limit=tp1_short)
// === Plotting ===
plot(ema200, title="EMA 200", color=color.orange, linewidth=2)
plot(ema50, title="EMA 50", color=color.blue, linewidth=2)
plotshape(longCond, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY", size=size.small)
plotshape(shortCond, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL", size=size.small)
// Plot support and resistance levels when in position
plot(strategy.position_size > 0 ? sl_long : na, title="Long Stop Loss", color=color.red, style=plot.style_linebr, linewidth=1)
plot(strategy.position_size > 0 ? tp1_long : na, title="Long Take Profit 1", color=color.green, style=plot.style_linebr, linewidth=1)
plot(strategy.position_size > 0 ? tp2_long : na, title="Long Take Profit 2", color=color.lime, style=plot.style_linebr, linewidth=1)
plot(strategy.position_size > 0 ? tp3_long : na, title="Long Take Profit 3", color=color.aqua, style=plot.style_linebr, linewidth=1)
plot(strategy.position_size < 0 ? sl_short : na, title="Short Stop Loss", color=color.red, style=plot.style_linebr, linewidth=1)
plot(strategy.position_size < 0 ? tp1_short : na, title="Short Take Profit 1", color=color.green, style=plot.style_linebr, linewidth=1)
plot(strategy.position_size < 0 ? tp2_short : na, title="Short Take Profit 2", color=color.lime, style=plot.style_linebr, linewidth=1)
plot(strategy.position_size < 0 ? tp3_short : na, title="Short Take Profit 3", color=color.aqua, style=plot.style_linebr, linewidth=1)
// === Alerts ===
alertcondition(longCond, title="Buy Alert", message="XAUUSD Buy Signal (MTF)")
alertcondition(shortCond, title="Sell Alert", message="XAUUSD Sell Signal (MTF)")