Pine Script Overbought & Oversold Lesson

This short lesson breaks down how to detect overbought and oversold conditions in Pine Script - including how to generate alerts and trading signals.

Check out my other free lessons!

Source Code

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ZenAndTheArtOfTrading
// @version=5
indicator("RSI OB/OS", overlay=true)

// Get user input
i_RsiLength     = input.int(14, "RSI Length")
i_RsiOB         = input.float(70, "RSI Overbought")
i_RsiOS         = input.float(30, "RSI Oversold")

// Get RSI value
rsiValue        = ta.rsi(close, i_RsiLength)

// Check overbought and oversold conditions
rsiOverbought   = rsiValue > i_RsiOB //and close < low[1]
rsiOversold     = rsiValue < i_RsiOS //and close > high[1]

// Fire alerts 
if rsiOverbought
    alert("Market overbought - " + syminfo.ticker, alert.freq_once_per_bar_close)
if rsiOversold
    alert("Market oversold - " + syminfo.ticker, alert.freq_once_per_bar_close)

// Draw signals
plotshape(rsiOverbought, "Overbought", shape.triangledown, location.abovebar, color.red,   size=size.small)
plotshape(rsiOversold,   "Oversold",   shape.triangleup,   location.belowbar, color.green, size=size.small)