OPEN-SOURCE SCRIPT
XAUUSD Trendline Breakout + RSI + MA Signal

//version=5
indicator("XAUUSD Trendline Breakout + RSI + MA Signal", overlay=true)
// --- Inputs ---
lengthFast = input.int(10, title="Fast MA Length")
lengthSlow = input.int(30, title="Slow MA Length")
rsiLength = input.int(14, title="RSI Length")
rsiOverbought = input.int(70, title="RSI Overbought Level")
rsiOversold = input.int(30, title="RSI Oversold Level")
// --- Moving Averages ---
fastMA = ta.sma(close, lengthFast)
slowMA = ta.sma(close, lengthSlow)
// --- RSI ---
rsi = ta.rsi(close, rsiLength)
// --- Trendline (Manual input) ---
// Use fixed default price values here, change manually based on your chart.
trend_x1 = input.int(20, title="Trendline Point 1 X (bars ago)")
trend_y1 = input.float(3000.0, title="Trendline Point 1 Y (price)") // constant default
trend_x2 = input.int(2, title="Trendline Point 2 X (bars ago)")
trend_y2 = input.float(3100.0, title="Trendline Point 2 Y (price)") // constant default
bar_idx1 = bar_index - trend_x1
bar_idx2 = bar_index - trend_x2
trendline_y_value(bar) =>
m = (trend_y2 - trend_y1) / (bar_idx2 - bar_idx1)
c = trend_y1 - m * bar_idx1
m * bar + c
trend_y_curr = trendline_y_value(bar_index)
trend_y_prev = trendline_y_value(bar_index - 1)
// --- Signals ---
// Trendline breakout
buyBreakout = (close[1] < trend_y_prev) and (close > trend_y_curr)
sellBreakout = (close[1] > trend_y_prev) and (close < trend_y_curr)
// MA crossover
maBuy = ta.crossover(fastMA, slowMA)
maSell = ta.crossunder(fastMA, slowMA)
// RSI confirmation
rsiBuy = rsi < rsiOversold
rsiSell = rsi > rsiOverbought
// Final buy/sell signal with all conditions
buySignal = buyBreakout and maBuy and rsiBuy
sellSignal = sellBreakout and maSell and rsiSell
// --- Plot ---
plot(fastMA, color=color.blue, title="Fast MA")
plot(slowMA, color=color.orange, title="Slow MA")
plotshape(buySignal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(sellSignal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")
// Plot trendline
var line trendLine = na
if barstate.islast
if na(trendLine)
trendLine := line.new(bar_idx1, trend_y1, bar_idx2, trend_y2, color=color.yellow, width=2)
else
line.set_xy1(trendLine, bar_idx1, trend_y1)
line.set_xy2(trendLine, bar_idx2, trend_y2)
// --- Alerts ---
alertcondition(buySignal, title="Buy Alert", message="XAUUSD Buy Signal")
alertcondition(sellSignal, title="Sell Alert", message="XAUUSD Sell Signal")
indicator("XAUUSD Trendline Breakout + RSI + MA Signal", overlay=true)
// --- Inputs ---
lengthFast = input.int(10, title="Fast MA Length")
lengthSlow = input.int(30, title="Slow MA Length")
rsiLength = input.int(14, title="RSI Length")
rsiOverbought = input.int(70, title="RSI Overbought Level")
rsiOversold = input.int(30, title="RSI Oversold Level")
// --- Moving Averages ---
fastMA = ta.sma(close, lengthFast)
slowMA = ta.sma(close, lengthSlow)
// --- RSI ---
rsi = ta.rsi(close, rsiLength)
// --- Trendline (Manual input) ---
// Use fixed default price values here, change manually based on your chart.
trend_x1 = input.int(20, title="Trendline Point 1 X (bars ago)")
trend_y1 = input.float(3000.0, title="Trendline Point 1 Y (price)") // constant default
trend_x2 = input.int(2, title="Trendline Point 2 X (bars ago)")
trend_y2 = input.float(3100.0, title="Trendline Point 2 Y (price)") // constant default
bar_idx1 = bar_index - trend_x1
bar_idx2 = bar_index - trend_x2
trendline_y_value(bar) =>
m = (trend_y2 - trend_y1) / (bar_idx2 - bar_idx1)
c = trend_y1 - m * bar_idx1
m * bar + c
trend_y_curr = trendline_y_value(bar_index)
trend_y_prev = trendline_y_value(bar_index - 1)
// --- Signals ---
// Trendline breakout
buyBreakout = (close[1] < trend_y_prev) and (close > trend_y_curr)
sellBreakout = (close[1] > trend_y_prev) and (close < trend_y_curr)
// MA crossover
maBuy = ta.crossover(fastMA, slowMA)
maSell = ta.crossunder(fastMA, slowMA)
// RSI confirmation
rsiBuy = rsi < rsiOversold
rsiSell = rsi > rsiOverbought
// Final buy/sell signal with all conditions
buySignal = buyBreakout and maBuy and rsiBuy
sellSignal = sellBreakout and maSell and rsiSell
// --- Plot ---
plot(fastMA, color=color.blue, title="Fast MA")
plot(slowMA, color=color.orange, title="Slow MA")
plotshape(buySignal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(sellSignal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")
// Plot trendline
var line trendLine = na
if barstate.islast
if na(trendLine)
trendLine := line.new(bar_idx1, trend_y1, bar_idx2, trend_y2, color=color.yellow, width=2)
else
line.set_xy1(trendLine, bar_idx1, trend_y1)
line.set_xy2(trendLine, bar_idx2, trend_y2)
// --- Alerts ---
alertcondition(buySignal, title="Buy Alert", message="XAUUSD Buy Signal")
alertcondition(sellSignal, title="Sell Alert", message="XAUUSD Sell Signal")
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.