In this video tutorial you’ll learn how to create a fully functional RS Line indicator for TradingView in Pine Script.

RS Line features:

■ Choose the index (S&P 500 default)
■ Optional line color based on direction
■ Adjust line width and vertical offset
■ New high indicator

The video was put together rather quickly, so apologies up front on a few rather clunky transitions. Hopefully the content makes up for the lack of polish. Time for an equipment upgrade 🙂


RS Line Indicator Code

There is one step that was missed in the video as it relates to creating a new indicator. After you click on Pine Editor on the bottom of TradingView, follow these steps:

■ Tap on the Open menu in the upper right.
■ Choose New Blank Indicator.

At this point you can copy and paste the code below.

Version 1 - Basic RS Line indicator in a separate pane.

//@version=4
study(title = "RS Line", overlay = false)
//---------------------------------------------------------
// Calls to input() show in the Settings dialog
//---------------------------------------------------------
iIndex = input(title = "Index", type = input.symbol, defval = "SPX")
//---------------------------------------------------------
// Calculate the ratio of closing price vs selected index
//---------------------------------------------------------
spx = security(iIndex, "D", close)
ratio = (close / spx) * 100
//---------------------------------------------------------
// Plot the RS Line
//---------------------------------------------------------
plot(ratio, color = color.blue, linewidth = 1)

Version 2 - Complete RS Line indicator

//@version=4
study(title = "RS Line", overlay = true)
//---------------------------------------------------------
// Calls to input() show in the Settings dialog
//---------------------------------------------------------
iIndex = input(title="Index", type = input.symbol, defval="SPX")
iChangeColor = input(title = "Color Based on Direction", type = input.bool, defval = false, inline = "I1")
iColorUp = input(title = "Up", type = input.color, defval = color.green, inline = "I1")
iColorDown = input(title = "Down", type = input.color, defval = color.red, inline = "I1")
iWidth = input(title = "Line Width", type = input.integer, defval = 1, minval = 1, maxval = 5)
iOffset = input(title = "Vertical offset of RS Line", type = input.integer, defval = 25, minval = 1, maxval = 500, step = 1)
iLookbackCount = input(title = "New high look-back count", defval = 50, minval = 10, maxval = 200)
// Save the ratio of closing price vs index
var previousRatio = 0.0
// Save each ratio so we can look for RS Line new high
var arrayOfRatios = array.new_float(0)
//---------------------------------------------------------
// Calculate the ratio of closing price vs selected index
//---------------------------------------------------------
spx = security(iIndex, "D", close)
ratio = (close / spx) * 100
//---------------------------------------------------------
// Determine the color to display
//---------------------------------------------------------
lineColor = color.blue
if (iChangeColor and ratio > previousRatio)
    lineColor := iColorUp
else if (iChangeColor and ratio <= previousRatio)
    lineColor := iColorDown
//lineColor = iChangeColor ? (ratio > previousRatio ?  iColorUp : iColorDown) : color.blue
//---------------------------------------------------------
// Plot the RS Line and save the current ratio
//---------------------------------------------------------
plot(timeframe.isdwm ? ratio * iOffset : na, color = lineColor, linewidth = iWidth)
//---------------------------------------------------------
// Show new high dot if applicable
//---------------------------------------------------------
if (barstate.islast)
    newHigh = true
    for i = 0 to iLookbackCount - 1
        if (array.get(arrayOfRatios, i) > ratio)
            newHigh := false
            break
    if (newHigh)
        label.new(x = bar_index, y = (ratio * iOffset), color = color.new(color.green, transp = 50), style = label.style_circle, size = size.tiny)
//---------------------------------------------------------
// Save current ratio for comparison colored bars
// Push ratio into front of array
//---------------------------------------------------------
previousRatio := ratio
array.unshift(arrayOfRatios, ratio)

TradingView is my preferred tool for charting and technical analysis.

If you haven’t tried TradingView, I highly recommend you check out the free trial!