Indicator · Pine Script v4 · Own pane

RSI Divergence

by digasic

A separate-pane indicator that calculates an RSI with adjustable source, length, overbought and oversold levels, then looks for bullish and bearish divergence between price and RSI.

View on GitHub

GitHub stars1on the repository
Pine versionv4older, still runs
Lines of code75
Settings5inputs
LicenceNone statedfile: MPL-2.0
Last update6 years ago21 May 2020

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

75 lines of Pine Script v4 from RSIDiv.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

RSIDiv.pine

//  ||------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\  ||
//  || This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/                                                             \\  ||
//  ||------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\  ||
//  || Published by © Eric Thies on Tradingview.com --> Released on 2/6/2020                                                                                                      ||
//  ||------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\  ||
//  || RSI Divergence - by KingThies                                                                                                                                           \\  ||
//  ||------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\  ||
//<Start Source> 
//@version=4
study("RSI Divergence", shorttitle="RSI Div", precision=1, overlay=false)

//1. Calculate RSI
src = input(close, 'RSI Source')
len = input(14, minval=1, title="RSI Length")
ob = input(70, "Overbought", input.integer, minval=0, maxval=100)
os = input(30, "Oversold", input.integer, minval=0, maxval=100)
RSI = rsi(src, len)

//2. Divergence Framework
xbars = input(90, "Div lookback period (bars)?", input.integer, minval=1)
hb = abs(highestbars(RSI, xbars))  // Finds bar with highest value in last X bars
lb = abs(lowestbars(RSI, xbars))  // Finds bar with lowest value in last X bars

// (2a. Defining variable values, mandatory in Pine 4)
max = float(na),        min = float(na),        pivoth = bool(na),      divbear = bool(na)  
max_rsi = float(na),    min_rsi = float(na),    pivotl = bool(na),      divbull = bool(na)

//2b. Identify Divergences --> "If bar with lowest / highest is current bar, use it's value"
max := hb == 0 ? close : na(max[1]) ? close : max[1]
max_rsi := hb == 0 ? RSI : na(max_rsi[1]) ? RSI : max_rsi[1]
min := lb == 0 ? close : na(min[1]) ? close : min[1]
min_rsi := lb == 0 ? RSI : na(min_rsi[1]) ? RSI : min_rsi[1]

//3. Compare high of current bar being examined with previous bar's high --> "If curr bar high is higher than the max bar high in the lookback window range"
if close > max  // "if  we have a new high"
    max := close  // "change variable "max" to use current bar's high value"
    max
if RSI > max_rsi  // we have a new high
    max_rsi := RSI  // change variable "max_rsi" to use current bar's RSI value
    max_rsi
if close < min  // we have a new low
    min := close  // change variable "min" to use current bar's low value
    min
if RSI < min_rsi  // we have a new low
    min_rsi := RSI  // change variable "min_rsi" to use current bar's RSI value
    min_rsi

//4. Finds pivot point with at least 2 right candles with lower value
pivoth := max_rsi == max_rsi[2] and max_rsi[2] != max_rsi[3] ? true : na,       pivotl := min_rsi == min_rsi[2] and min_rsi[2] != min_rsi[3] ? true : na

if max[1] > max[2] and RSI[1] < max_rsi and RSI <= RSI[1]
    divbear := true
    divbear
if min[1] < min[2] and RSI[1] > min_rsi and RSI >= RSI[1]
    divbull := true
    divbull

//5. Plot
plot(RSI, "RSI Bear", color=RSI < 70 ? na : color.red, linewidth=3)
plot(RSI, "RSI Bull", color=RSI > 30 ? na : color.green, linewidth=3)
plot(RSI, "RSI", color=color.gray, linewidth=1)
hline(30)
hline(70)

//Add: Alerts
alertcondition(divbear==true, title='Bearish RSI Divergence', message='Bearish RSI Divergence Spotted on {{exchange}}:{{ticker}}')
alertcondition(divbull==true, title='Bullish RSI Divergence', message='Bearish RSI Divergence Spotted on {{exchange}}:{{ticker}}')

// </End Source> 
//  ||----------------------------------------------------------------------------------------------------------------------------------------------------------------------------||
//  || RSI Divergence Oscillator - by KingThies                                                                                                                                            ||
//  ||--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ||
//  || Published by © Eric Thies on Tradingview.com --> Released on 2/6/2020                                                                                                      ||
//  ||--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ||

Overview

RSI Divergence tracks the highest and lowest price and RSI values inside a lookback window, 90 bars by default, and flags a divergence when price makes a new extreme that RSI does not confirm. Pivot checks need a couple of bars after the turn, so signals appear with a delay.

Details

Published by

digasic on GitHub

Type

Indicator, Pine Script v4

Draws

Separate pane below the chart

Category

Oscillators & momentum, Divergence

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.

Settings

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

SettingTypeDefaultVariable
RSI Sourcesourceclosesrc
RSI Lengthint14len
Overboughtint70ob
Oversoldint30os
Div lookback period (bars)?int90xbars
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

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator
330 stars

This indicator calculates the MACD oscillator from a configurable source with fast and slow lengths, then plots the histogram, MACD line, and signal line in a separate pane. It detects regular bullish divergences (price lower lows with oscillator higher lows),

Indicator v5 Oscillators & momentumDivergence
Updated 2 months ago · None stated GitHub repository of MACD + Divergences [Alorse]
218 stars

This indicator calculates the Relative Strength Index (RSI) across four user-selected timeframes and plots the results in a separate pane. It identifies regular bullish and bearish divergences between price pivot points and the primary timeframe RSI value. Add

Indicator v6 Oscillators & momentumDivergence
Updated 3 months ago · None stated GitHub repository of Multi-Timeframe RSI with Divergence

Bubbles Indicator

by capissimo
114 stars

The Bubbles Indicator measures volatility squeezes and expansion bursts using Bollinger Bands combined with a sinusoid oscillator and RSI divergence. It processes price data through scaled Bollinger Bands to highlight volatility changes while confirming trend

Indicator v4 Volatility & bandsOscillators & momentum Repaints
Updated 1 year ago · None stated GitHub repository of Bubbles Indicator

RSI [SystemAlpha]

by iamc1oud
56 stars

This indicator plots a standard Relative Strength Index (RSI) in a separate pane alongside customizable overbought and oversold threshold levels with shaded fills. It identifies pivot highs and lows on the oscillator to detect regular and hidden bullish and be

Indicator v4 Oscillators & momentumDivergence Repaints
Updated 2 months ago · None stated GitHub repository of RSI [SystemAlpha]
55 stars

This indicator calculates a 14-period RSI from close and plots it in a separate pane with dotted overbought and dashed oversold lines at 70 and 30. It marks bullish divergence when a five-bar pivot low in price is lower than the price twenty bars earlier while

Indicator v6 Oscillators & momentumDivergence
Updated 6 months ago · Apache-2.0 GitHub repository of RSI Divergence Detector

More indicators

Trade Your Charts With a Regulated Broker

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

Compare brokers