Exact saved source
Pine Script
//@version=6
indicator("XAUUSD 1H Confirmed Trend", overlay=true)
// Trend filter
trendLength = input.int(50, "Trend EMA Length", minval=5)
trendEma = ta.ema(close, trendLength)
trendSlope = trendEma - trendEma[3]
isUptrend = trendSlope > 0
isDowntrend = trendSlope < 0
plot(trendEma, "Trend EMA", color=isUptrend ? color.new(color.teal, 0) : color.new(color.maroon, 0), linewidth=2)
// Momentum confirmation
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)
momentumBullish = macdLine > signalLine and macdLine > 0
momentumBearish = macdLine < signalLine and macdLine < 0
// Volatility filter
atrLength = input.int(14, "ATR Length", minval=1)
atrValue = ta.atr(atrLength)
minVolatility = input.float(0.5, "Minimum ATR Multiple to Trade", minval=0.1)
volatilityOk = atrValue > ta.sma(atrValue, 50) * minVolatility
// Edge-triggered entry events prevent repeated markers on every trend bar
longSetup = isUptrend and momentumBullish and volatilityOk
shortSetup = isDowntrend and momentumBearish and volatilityOk
longCondition = longSetup and not longSetup[1]
shortCondition = shortSetup and not shortSetup[1]
plotshape(longCondition, title="Long Signal", location=location.belowbar, style=shape.triangleup, color=color.new(color.teal, 0), size=size.small)
plotshape(shortCondition, title="Short Signal", location=location.abovebar, style=shape.triangledown, color=color.new(color.maroon, 0), size=size.small)
// Invalidation levels
stopAtrMultiple = input.float(1.5, "Stop Distance (ATR multiples)", minval=0.5)
var float longStopLevel = na
var float shortStopLevel = na
if longCondition
longStopLevel := close - (atrValue * stopAtrMultiple)
shortStopLevel := na
if shortCondition
shortStopLevel := close + (atrValue * stopAtrMultiple)
longStopLevel := na
plot(longStopLevel, "Long Invalidation", color=color.new(color.red, 40), style=plot.style_linebr)
plot(shortStopLevel, "Short Invalidation", color=color.new(color.red, 40), style=plot.style_linebr)
// Alerts
alertcondition(longCondition, title="Long Signal", message="Long signal on {{ticker}} at {{close}}")
alertcondition(shortCondition, title="Short Signal", message="Short signal on {{ticker}} at {{close}}")
ResultStatic checks passed
Errors0
Warnings0
Policy2026-07-26.2
SHA-256
25b1e7cda76c6de7ef9c7627e44e419e64004ac395e0bc58b0aaab23b5a08b0dWhat this result means
The source passed the Lab's bounded local checks for Pine version, declaration count, structural balance, signal conditions, and lookahead use. Only TradingView can compile Pine. Compile and test it there before any use.