Written by TradeSage Team
May 16, 2025
4 mins read

Build Powerful RSI Indicators in Pine Script™: A Step-by-Step Guide

Learn how to code, visualize, and set alerts for RSI in Pine Script v6. Includes color-coded plots, debugging tips, and free TradeSage script generator.

The Relative Strength Index (RSI) is one of the most popular momentum indicators in technical analysis. In this comprehensive guide, we'll show you how to implement, customize, and create alerts for RSI using Pine Script™ v6. Whether you're a beginner or advanced trader, you'll learn practical techniques to enhance your trading strategy.

What is RSI & Why Traders Love It

The Relative Strength Index (RSI) is a momentum oscillator that measures the speed and magnitude of recent price changes. Developed by J. Welles Wilder Jr., it helps traders identify:

  • Overbought conditions (RSI > 70)
  • Oversold conditions (RSI < 30)
  • Bullish/bearish divergences
  • Price momentum trends

Quick-start Code: 14-period RSI

Here's a simple implementation of the standard 14-period RSI:

//@version=6
indicator("Basic RSI", overlay=false)

// Input for RSI length
rsiLength = input.int(14, "RSI Length", minval=1)

// Calculate RSI
rsiValue = ta.rsi(close, rsiLength)

// Plot RSI line
plot(rsiValue, "RSI", color=color.rgb(136, 76, 146), linewidth=2)

// Plot overbought/oversold levels
hline(70, "Overbought", color=color.red, linestyle=hline.style_dashed)
hline(30, "Oversold", color=color.green, linestyle=hline.style_dashed)

Color-coding Bullish vs Bearish Zones

Let's enhance our RSI visualization by color-coding different zones:

//@version=6
indicator("RSI with Zones", overlay=false)

// Inputs
rsiLength = input.int(14, "RSI Length", minval=1)
rsiValue = ta.rsi(close, rsiLength)

// Dynamic color based on RSI zones
rsiColor = rsiValue > 70 ? color.red :
           rsiValue < 30 ? color.green :
           color.rgb(136, 76, 146)

// Plot RSI with dynamic color
plot(rsiValue, "RSI", color=rsiColor, linewidth=2)

// Background coloring for zones
bgcolor(rsiValue > 70 ? color.new(color.red, 90) :
        rsiValue < 30 ? color.new(color.green, 90) :
        na)

Plotting RSI + Median for Trend Bias

Adding a median line helps identify the overall trend bias:

//@version=6
indicator("RSI with Trend Bias", overlay=false)

// Inputs
rsiLength = input.int(14, "RSI Length", minval=1)
rsiValue = ta.rsi(close, rsiLength)

// Plot RSI
plot(rsiValue, "RSI", color=color.rgb(136, 76, 146), linewidth=2)

// Plot median line (50 level)
hline(50, "Median", color=color.gray, linestyle=hline.style_dotted)

// Plot trend bias zones
fill(hline(70, "Overbought"), hline(50, "Upper Zone"),
     color.new(color.red, 90))
fill(hline(30, "Oversold"), hline(50, "Lower Zone"),
     color.new(color.green, 90))

Debugging Compound RSI Conditions

When working with multiple RSI conditions, proper debugging is crucial. Here's how to validate your conditions:

//@version=6
indicator("RSI Debug", overlay=false)

// Basic RSI setup
rsiLength = input.int(14, "RSI Length", minval=1)
rsiValue = ta.rsi(close, rsiLength)

// Debug variables
var table debugTable = table.new(position.top_right, 3, 4)
table.cell(debugTable, 0, 0, "Condition", bgcolor=color.gray)
table.cell(debugTable, 1, 0, "Value", bgcolor=color.gray)
table.cell(debugTable, 2, 0, "Status", bgcolor=color.gray)

// Check conditions
isOverbought = rsiValue > 70
isOversold = rsiValue < 30
isCrossing50 = ta.cross(rsiValue, 50)

// Update debug table
table.cell(debugTable, 0, 1, "Overbought")
table.cell(debugTable, 1, 1, str.tostring(rsiValue, "#.##"))
table.cell(debugTable, 2, 1, isOverbought ? "✓" : "×")

From Script to Alert (Cross Below 30 Example)

Creating alerts for RSI signals is straightforward:

//@version=6
indicator("RSI Alerts", overlay=false)

// RSI setup
rsiLength = input.int(14, "RSI Length", minval=1)
rsiValue = ta.rsi(close, rsiLength)

// Define alert conditions
crossBelow = ta.crossunder(rsiValue, 30)
crossAbove = ta.crossover(rsiValue, 70)

// Plot signals
plotshape(crossBelow, "Oversold Signal", shape.circle,
         location.bottom, color.green, size=size.small)
plotshape(crossAbove, "Overbought Signal", shape.circle,
         location.top, color.red, size=size.small)

// Alert conditions
alertcondition(crossBelow, "RSI Oversold", "RSI crossed below 30")
alertcondition(crossAbove, "RSI Overbought", "RSI crossed above 70")

TradeSage One-click Generator Demo

Prefer a no-code approach? TradeSage can auto-generate and back-test this RSI strategy in 30 seconds. Simply:

  1. Select RSI from our indicator library
  2. Customize your parameters
  3. Generate and test your strategy instantly

Frequently Asked Questions

Why is my RSI flatlined at 100/0?

If your RSI is stuck at extreme values, check:

  • Data feed quality
  • Calculation period (too short?)
  • Price source (using appropriate OHLC values?)

Does RSI repaint on lower time frames?

RSI calculations are based on completed candles by default. To prevent repainting:

  • Use barstate.isconfirmed
  • Apply appropriate look-back periods
  • Consider using previous candle's close

How to combine RSI with moving averages?

Here's a simple example combining RSI with a 200 EMA:

//@version=6
indicator("RSI + EMA Strategy", overlay=true)

// Inputs
rsiLength = input.int(14, "RSI Length")
emaLength = input.int(200, "EMA Length")

// Calculations
rsiValue = ta.rsi(close, rsiLength)
emaValue = ta.ema(close, emaLength)

// Combined signals
bullish = rsiValue < 30 and close > emaValue
bearish = rsiValue > 70 and close < emaValue

// Debug: plot RSI and EMA for visual cross check
plot(emaValue, title="200 EMA", color=color.blue)
plot(rsiValue, title="RSI", color=color.orange, display=display.none)

// Plot shapes—ensure offset=0
plotshape(bullish, title="Buy Signal", style=shape.triangleup,
         location=location.belowbar, color=color.green, size=size.small, offset=0)
plotshape(bearish, title="Sell Signal", style=shape.triangledown,
         location=location.abovebar, color=color.red, size=size.small, offset=0)

Remember to always test your strategies thoroughly before using them in live trading. TradeSage's back-testing tools can help you validate your RSI-based strategies across different market conditions.

Elevate your trading today!

Join TradeSage and get access to AI-powered Pinescript generator, intelligent trading assistant, and strategy optimizer. Start your journey to smarter trading today.

Advanced Trading Tools
Pinescript V6 Generator
Strategy Optimizer

Share this article