OPEN-SOURCE SCRIPT

XAU/USD Swing Trading Strategy

//version=5
indicator("XAU/USD Swing Trading Strategy", overlay=true)

// Inputs
ema200_length = input.int(200, title="200 EMA Length")
ema50_length = input.int(50, title="50 EMA Length")
ema100_length = input.int(100, title="100 EMA Length")
rsi_length = input.int(14, title="RSI Length")
rsi_overbought = input.int(70, title="RSI Overbought Level")
rsi_oversold = input.int(30, title="RSI Oversold Level")
fib_levels = input.string("38.2, 50, 61.8", title="Fibonacci Levels")

// EMAs
ema200 = ta.ema(close, ema200_length)
ema50 = ta.ema(close, ema50_length)
ema100 = ta.ema(close, ema100_length)

// RSI
rsi = ta.rsi(close, rsi_length)

// Fibonacci Retracement Levels
fib_382 = 0.382
fib_500 = 0.5
fib_618 = 0.618

// Plot EMAs
plot(ema200, color=color.blue, title="200 EMA", linewidth=2)
plot(ema50, color=color.orange, title="50 EMA", linewidth=1)
plot(ema100, color=color.red, title="100 EMA", linewidth=1)

// Plot RSI
hline(rsi_overbought, "Overbought", color=color.red)
hline(rsi_oversold, "Oversold", color=color.green)

// Candlestick Patterns
bullish_engulfing = ta.crossover(close[1], open[1]) and close > open and close[1] < open[1]
bearish_engulfing = ta.crossover(open[1], close[1]) and close < open and close[1] > open[1]
pin_bar_bullish = (low < low[1] and close > (high + low) / 2)
pin_bar_bearish = (high > high[1] and close < (high + low) / 2)

// Trend Direction
uptrend = close > ema200
downtrend = close < ema200

// Entry Signals
long_signal = uptrend and bullish_engulfing and rsi > rsi_oversold
short_signal = downtrend and bearish_engulfing and rsi < rsi_overbought

// Plot Signals
plotshape(series=long_signal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=short_signal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")

// Fibonacci Levels
fib_high = ta.highest(high, 50)
fib_low = ta.lowest(low, 50)
fib_range = fib_high - fib_low

plot(fib_high - fib_range * fib_382, color=color.purple, title="38.2% Fib")
plot(fib_high - fib_range * fib_500, color=color.blue, title="50% Fib")
plot(fib_high - fib_range * fib_618, color=color.orange, title="61.8% Fib")

// Alerts
alertcondition(long_signal, title="Buy Signal", message="BUY XAU/USD")
alertcondition(short_signal, title="Sell Signal", message="SELL XAU/USD")

Disclaimer