The example divides the ETF close by SPY, then measures that ratio against its own 50-bar SMA. A positive value means the ratio is above that average. The absolute ratio level, including whether it is above 1, says nothing about outperformance. This is a price-series comparison, not a dividend-reinvested total-return calculation. Use an appropriate benchmark for the asset class and keep sessions and adjustment settings consistent.
Complete Pine Script v6 indicator; daily timeframe for the example workflowpine
//@version=6
indicator("ETF trend and relative strength review", overlay=false)
// Use a daily chart for daily screening. SPY is an example benchmark.
float benchmark = request.security("AMEX:SPY", timeframe.period, close, gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_off)
float ratio = not na(benchmark) and benchmark > 0 ? close / benchmark : na
float ratioAverage = ta.sma(ratio, 50)
float priceAverage = ta.sma(close, 200)
float rsDistance = not na(ratio) and ratioAverage > 0 ? 100 * (ratio / ratioAverage - 1) : na
float priceDistance = priceAverage > 0 ? 100 * (close / priceAverage - 1) : na
float momentum = ta.rsi(close, 14)
float atrValue = ta.atr(14)
float atrPercent = close > 0 ? 100 * atrValue / close : na
// Every output refers to the preceding completed chart-timeframe bar.
float closedRs = rsDistance[1]
float closedTrend = priceDistance[1]
float closedRsi = momentum[1]
float closedAtr = atrPercent[1]
bool ready = not na(closedRs) and not na(closedTrend) and not na(closedRsi) and not na(closedAtr)
bool candidate = ready and closedRs > 0 and closedTrend > 0 and closedRsi > 50
plot(closedRs, "Previous RS distance from 50-bar SMA (%)", color=color.blue)
plot(closedTrend, "Previous price distance from SMA 200 (%)", color=color.orange)
plot(closedRsi, "Previous RSI 14", color=color.purple)
plot(closedAtr, "Previous ATR 14 (%)", color=color.gray)
plot(ready ? (candidate ? 1 : 0) : na, "Previous-bar candidate (1=yes)", color=color.green)