Indicator · Pine Script v6 · Own pane

Comparative Relative Strength - SJ

by theonetrade

The Comparative Relative Strength - SJ indicator measures an asset's performance relative to a chosen benchmark index, such as NASDAQ:NDX.

View on GitHub

GitHub stars34on the repository
Pine versionv6current
Lines of code60
Settings6inputs
LicenceMIT
Last update2 weeks ago8 Nov 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

60 lines of Pine Script v6 from assets/indicator/bit-exact/comparative_relative_strength_sj_wO9tQFSs.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

comparative_relative_strength_sj_wO9tQFSs.pine
//@version=6
// Comparative Relative Strength - SJ
// Compares the MOMENTUM of the stock against the index of the market.
// Two display modes:
//   RATIO mode - stock / benchmark. Good for READING one chart.
//   ROC mode   - rate of change of that ratio. Scale-free, so it is the one
//                that can be RANKED ACROSS SYMBOLS in a scanner.
indicator("Comparative Relative Strength - SJ", shorttitle = "CRS-SJ", overlay = false)

// ---------------- inputs ----------------
benchSym = input.symbol("NASDAQ:NDX", "Benchmark index")
maLen    = input.int(22, "Smoothing length", minval = 1, tooltip = "22 = one trading month on daily")
maType   = input.string("EMA", "Smoothing type", options = ["EMA", "SMA"])
mode     = input.string("Ratio", "Mode", options = ["Ratio", "ROC"])
rocLen   = input.int(22, "ROC length", minval = 1)
showTurn = input.bool(true, "Mark the turns")

// ---------------- data ----------------
benchRaw = request.security(benchSym, timeframe.period, close, ignore_invalid_symbol = true)

// a halted or missing benchmark print must not blow the series up
benchOk = not na(benchRaw) and benchRaw != 0
bench   = benchOk ? benchRaw : na

crs = close / bench

crsMa = maType == "EMA" ? ta.ema(crs, maLen) : ta.sma(crs, maLen)

// scale-free form - comparable between different symbols
crsRoc   = ta.roc(crs, rocLen)
crsRocMa = maType == "EMA" ? ta.ema(crsRoc, maLen) : ta.sma(crsRoc, maLen)

isRoc = mode == "ROC"

lineRaw = isRoc ? crsRoc : crs
lineMa  = isRoc ? crsRocMa : crsMa

// ---------------- plots ----------------
pRaw = plot(lineRaw, "CRS", color = color.new(color.gray, 0))
pMa  = plot(lineMa, "CRS smoothed", color = color.navy, linewidth = 2)

plot(isRoc ? 0.0 : na, "Zero", color = color.new(color.gray, 40))

fill(pRaw, pMa, color = lineRaw > lineMa ? color.new(color.green, 88) : color.new(color.red, 88))

// ---------------- state ----------------
outperforming = lineRaw > lineMa
turnUp = ta.crossover(lineRaw, lineMa)
turnDn = ta.crossunder(lineRaw, lineMa)

plotshape(showTurn and turnUp, "CRS turns up", shape.circle, location.bottom, color.new(color.green, 0), size = size.tiny)
plotshape(showTurn and turnDn, "CRS turns down", shape.circle, location.top, color.new(color.red, 0), size = size.tiny)

alertcondition(turnUp, "CRS turn up", "CRS crossed above its average - stock beginning to beat the index")
alertcondition(turnDn, "CRS turn down", "CRS crossed below its average - stock beginning to lag the index")

// ---------------- scanner value ----------------
// Plot this (ROC mode) invisibly so a screener column can read one number
// that means the same thing on every symbol.
plot(crsRoc, "SCAN: CRS ROC %", color = color.new(color.gray, 100), display = display.data_window)

Overview

Comparative Relative Strength - SJ calculates either a direct ratio of the asset's close to the benchmark or a scale-free rate of change of that ratio. An EMA or SMA moving average smooths the output. The script plots the relative strength line alongside its moving average in a separate pane, shading the space between them to show outperformance or underperformance, and optionally placing turn markers on moving average crossovers.

Details

Published by

theonetrade on GitHub

Draws

Separate pane below the chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

algorithmic-tradingbacktestingbitcoinccxtcrypto-tradingllm-agentoffline-backtestingopen-source-tradingpine-scriptpine-to-pythonpinescriptpynecorepythonquantquantitative-financestrategy-backtestingtechnical-analysistrading-indicatorstrading-strategiestradingview

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Benchmark indexsymbolNASDAQ:NDXbenchSym
Smoothing lengthint22maLen
Smoothing typestringEMAmaType
ModestringRatiomode
ROC lengthint22rocLen
Mark the turnsbooltrueshowTurn
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Schaff Trend Cycle

by everget
947 stars

This indicator plots the Schaff Trend Cycle in a separate pane. It subtracts a slow exponential moving average from a fast one, applies a stochastic cycle twice, and smooths each result to form a 0–100 oscillator. Line color follows the slope, or the upper and

Indicator v4 Oscillators & momentumTrend
Updated 5 weeks ago · GPL-3.0 GitHub repository of Schaff Trend Cycle

Beep Boop

by magic8bot
408 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop

[RS]Standardized Trend Map

by ricardosantos79
261 stars

Standardized Trend Map calculates series momentum relative to active price range across 40 distinct lookback periods ranging from 2 to 80 bars on an exponential moving average source. Each standardized calculation rounds to an integer values of -1, 0, or 1 to

Indicator v4 TrendOscillators & momentum
Updated 5 years ago · None stated GitHub repository of [RS]Standardized Trend Map
228 stars

Multi-Timeframe Analysis Suite calculates trend direction and momentum conditions across five user-selected timeframes using moving averages, RSI, and Stochastic oscillators. It evaluates moving average slope and oscillator levels to compute trend alignment, m

Indicator v6 Screeners & dashboardsTrend
Updated 4 months ago · None stated GitHub repository of Multi-Timeframe Analysis Suite

ADX Histogram

by harryguiacorn
220 stars

This indicator calculates and displays the Average Directional Index (ADX) as a column histogram in a separate pane. It evaluates market trend strength by calculating directional movement metrics based on specified smoothing and DI length inputs. When the ADX

Indicator v5 TrendOscillators & momentum
Updated 1 year ago · None stated GitHub repository of ADX Histogram

More Oscillators & momentum scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers