Indicator · Pine Script v4 · On the chart

Total Divergence+

by krosenfeld7

Total Divergence+ is a price-chart indicator that looks for divergences between price and several oscillators.

View on GitHub

GitHub stars1on the repository
Pine versionv4older, still runs
Lines of code903
Settings31inputs
LicenceApache-2.0
Last update3 years ago12 May 2023

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

903 lines of Pine Script v4 from Total_Divergence+.txt, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

Total_Divergence+.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © krosenfeld7

//@version=4
study("Total Divergence+", overlay = true, max_labels_count = 500, max_lines_count = 500)

displayRsiDiv    = input(title = "Display RSI Div",       type = input.bool,    defval = true)
rsiLookback      = input(title = "RSI Lookback",          type = input.integer, defval = 15,  minval = 1)
priceRsiLookback = input(title = "RSI Price Lookback",    type = input.integer, defval = 15,  minval = 1)
rsiTolerance     = input(title = "RSI Epsilon (%)",       type = input.float,   defval = 1.0, minval = 0.0)
priceRsiTolerance= input(title = "RSI Price Epsilon (%)", type = input.float,   defval = 1.0, minval = 0.0)

displayStochRsiDiv    = input(title = "Display Stoch RSI Div",       type = input.bool,    defval = true)
stochRsiLookback      = input(title = "Stoch RSI Lookback",          type = input.integer, defval = 15,  minval = 1)
priceStochRsiLookback = input(title = "Stoch RSI Price Lookback",    type = input.integer, defval = 15,  minval = 1)
stochRsiTolerance     = input(title = "Stoch Rsi Epsilon (%)",       type = input.float,   defval = 1.0, minval = 0.0)
priceStochRsiTolerance= input(title = "Stoch RSI Price Epsilon (%)", type = input.float,   defval = 1.0, minval = 0.0)

displayStochDiv    = input(title = "Display Stoch Div",       type = input.bool,    defval = false)
stochLookback      = input(title = "Stoch Lookback",          type = input.integer, defval = 15,  minval = 1)
priceStochLookback = input(title = "Stoch Price Lookback",    type = input.integer, defval = 15,  minval = 1)
stochTolerance     = input(title = "Stoch Epsilon (%)",       type = input.float,   defval = 1.0, minval = 0.0)
priceStochTolerance= input(title = "Stoch Price Epsilon (%)", type = input.float,   defval = 1.0, minval = 0.0)

displayAdxDiv    = input(title = "Display ADX Div",       type = input.bool,    defval = false)
adxLookback      = input(title = "ADX Lookback",          type = input.integer, defval = 15,  minval = 1)
priceAdxLookback = input(title = "ADX Price Lookback",    type = input.integer, defval = 15,  minval = 1)
adxTolerance     = input(title = "ADX Epsilon (%)",       type = input.float,   defval = 1.0, minval = 0.0)
priceAdxTolerance= input(title = "ADX Price Epsilon (%)", type = input.float,   defval = 1.0, minval = 0.0)

displayMacdDiv    = input(title = "Display MACD Div",       type = input.bool,    defval = true)
macdLookback      = input(title = "MACD Lookback",          type = input.integer, defval = 15,  minval = 1)
priceMacdLookback = input(title = "MACD Price Lookback",    type = input.integer, defval = 15,  minval = 1)
macdTolerance     = input(title = "MACD Epsilon (%)",       type = input.float,   defval = 1.0, minval = 0.0)
priceMacdTolerance= input(title = "MACD Price Epsilon (%)", type = input.float,   defval = 1.0, minval = 0.0)

displayPvtDiv    = input(title = "Display PVT Div",       type = input.bool,    defval = false)
pvtLookback      = input(title = "PVT Lookback",          type = input.integer, defval = 15,  minval = 1)
pricePvtLookback = input(title = "PVT Price Lookback",    type = input.integer, defval = 15,  minval = 1)
pvtTolerance     = input(title = "PVT Epsilon (%)",       type = input.float,   defval = 1.0, minval = 0.0)
pricePvtTolerance= input(title = "PVT Price Epsilon (%)", type = input.float,   defval = 1.0, minval = 0.0)

labelLookback = input(title = "Label Lookback", type=input.integer, defval = 10, minval = 1)

// RSI
up   = rma(max(change(close), 0), 14)
down = rma(-min(change(close), 0), 14)
rsi  = ((down == 0) ? 100 : ((up == 0) ? 0 : (100 - (100 / (1 + up / down)))))

// Stochastic RSI
rsi1 = rsi(close, 14)
k = sma(stoch(rsi1, rsi1, rsi1, 14), 3)
d = sma(k, 3)

// Stochastic
stochK = sma(stoch(close, high, low, 14), 1)
stochD = sma(stochK, 3)

// ADX / DMI
upAdx = change(high)
downAdx = -change(low)
plusDM = na(upAdx) ? na : (upAdx > downAdx and upAdx > 0 ? upAdx : 0)
minusDM = na(downAdx) ? na : (downAdx > upAdx and downAdx > 0 ? downAdx : 0)
trur = rma(tr, 14)
plus = fixnan(100 * rma(plusDM, 14) / trur)
minus = fixnan(100 * rma(minusDM, 14) / trur)
sum = plus + minus
adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), 14)

// MACD
macd = ema(close, 12) - ema(close, 26)
signal = ema(macd, 9)
hist = macd - signal

var bool priceRsiHighSet = na
var bool priceRsiLowSet  = na

var bool priceStochRsiHighSet = na
var bool priceStochRsiLowSet  = na

var bool priceStochHighSet = na
var bool priceStochLowSet  = na

var bool priceAdxHighSet = na
var bool priceAdxLowSet  = na

var bool priceMacdHighSet = na
var bool priceMacdLowSet  = na

var bool pricePvtHighSet = na
var bool pricePvtLowSet  = na

// RSI SECTION
currRsiHigh = pivothigh(rsi, rsiLookback, rsiLookback)
currRsiLow  = pivotlow(rsi,  rsiLookback, rsiLookback)

lastRsiHigh = valuewhen(not na(currRsiHigh), currRsiHigh, 1)
lastRsiLow  = valuewhen(not na(currRsiLow),  currRsiLow,  1)

lowerRsiHigh  = not na(currRsiHigh) ? lastRsiHigh > (currRsiHigh * (1 + (rsiTolerance / 100))) : false
higherRsiHigh = not na(currRsiHigh) ? lastRsiHigh < (currRsiHigh * (1 - (rsiTolerance / 100))) : false
lowerRsiLow   = not na(currRsiLow)  ? lastRsiLow  > (currRsiLow  * (1 + (rsiTolerance / 100))) : false
higherRsiLow  = not na(currRsiLow)  ? lastRsiLow  < (currRsiLow  * (1 - (rsiTolerance / 100))) : false

// STOCHASTIC RSI SECTION
currStochRsiHighK = pivothigh(k, stochRsiLookback, stochRsiLookback)
currStochRsiHighD = pivothigh(d, stochRsiLookback, stochRsiLookback)
currStochRsiLowK  = pivotlow(k,  stochRsiLookback, stochRsiLookback)
currStochRsiLowD  = pivotlow(d,  stochRsiLookback, stochRsiLookback)
currStochRsiHigh  = currStochRsiHighK > currStochRsiHighD ? currStochRsiHighK : currStochRsiHighD
currStochRsiLow   = currStochRsiLowK  > currStochRsiLowD  ? currStochRsiLowD  : currStochRsiLowK

lastStochRsiHigh = valuewhen(not na(currStochRsiHigh), currStochRsiHigh, 1)
lastStochRsiLow  = valuewhen(not na(currStochRsiLow),  currStochRsiLow,  1)

lowerStochRsiHigh  = not na(currStochRsiHigh) ? lastStochRsiHigh > (currStochRsiHigh * (1 + (stochRsiTolerance / 100))) : false
higherStochRsiHigh = not na(currStochRsiHigh) ? lastStochRsiHigh < (currStochRsiHigh * (1 - (stochRsiTolerance / 100))) : false
lowerStochRsiLow   = not na(currStochRsiLow)  ? lastStochRsiLow  > (currStochRsiLow *  (1 + (stochRsiTolerance / 100))) : false
higherStochRsiLow  = not na(currStochRsiLow)  ? lastStochRsiLow  < (currStochRsiLow *  (1 - (stochRsiTolerance / 100))) : false

// STOCHASTIC SECTION
currStochHighK = pivothigh(stochK, stochLookback, stochLookback)
currStochHighD = pivothigh(stochD, stochLookback, stochLookback)
currStochLowK  = pivotlow(stochK,  stochLookback, stochLookback)
currStochLowD  = pivotlow(stochD,  stochLookback, stochLookback)

currStochHigh = currStochHighK > currStochHighD ? currStochHighK : currStochHighD
currStochLow  = currStochLowK  > currStochLowD  ? currStochLowD  : currStochLowK

lastStochHigh = valuewhen(not na(currStochHigh), currStochHigh, 1)
lastStochLow  = valuewhen(not na(currStochLow),  currStochLow,  1)

lowerStochHigh  = not na(currStochHigh) ? lastStochHigh > (currStochHigh * (1 + (stochTolerance / 100))) : false
higherStochHigh = not na(currStochHigh) ? lastStochHigh < (currStochHigh * (1 - (stochTolerance / 100))) : false
lowerStochLow   = not na(currStochLow)  ? lastStochLow  > (currStochLow *  (1 + (stochTolerance / 100))) : false
higherStochLow  = not na(currStochLow)  ? lastStochLow  < (currStochLow *  (1 - (stochTolerance / 100))) : false

// ADX / DMI SECTION
currAdxHigh = pivothigh(adx, adxLookback, adxLookback)
currAdxLow  = pivotlow(adx,  adxLookback, adxLookback)

lastAdxHigh = valuewhen(not na(currAdxHigh), currAdxHigh, 1)
lastAdxLow  = valuewhen(not na(currAdxLow),  currAdxLow,  1)

lowerAdxHigh  = not na(currAdxHigh) ? lastAdxHigh > (currAdxHigh * (1 + (adxTolerance / 100))) : false
higherAdxHigh = not na(currAdxHigh) ? lastAdxHigh < (currAdxHigh * (1 - (adxTolerance / 100))) : false
lowerAdxLow   = not na(currAdxLow)  ? lastAdxLow  > (currAdxLow *  (1 + (adxTolerance / 100))) : false
higherAdxLow  = not na(currAdxLow)  ? lastAdxLow  < (currAdxLow *  (1 - (adxTolerance / 100))) : false

// MACD SECTION
currMacdHigh = pivothigh(macd, macdLookback, macdLookback)
currMacdLow  = pivotlow(macd,  macdLookback, macdLookback)

lastMacdHigh = valuewhen(not na(currMacdHigh), currMacdHigh, 1)
lastMacdLow  = valuewhen(not na(currMacdLow),  currMacdLow,  1)

lowerMacdHigh  = not na(currMacdHigh) ? lastMacdHigh > (currMacdHigh * (1 + (macdTolerance / 100))) : false
higherMacdHigh = not na(currMacdHigh) ? lastMacdHigh < (currMacdHigh * (1 - (macdTolerance / 100))) : false
lowerMacdLow   = not na(currMacdLow)  ? lastMacdLow  > (currMacdLow *  (1 + (macdTolerance / 100))) : false
higherMacdLow  = not na(currMacdLow)  ? lastMacdLow  < (currMacdLow *  (1 - (macdTolerance / 100))) : false

// VOLUME / PVT SECTION
currPvtHigh = pivothigh(pvt, pvtLookback, pvtLookback)
currPvtLow  = pivotlow(pvt,  pvtLookback, pvtLookback)

lastPvtHigh = valuewhen(not na(currPvtHigh), currPvtHigh, 1)
lastPvtLow  = valuewhen(not na(currPvtLow),  currPvtLow,  1)

lowerPvtHigh  = not na(currPvtHigh) ? lastPvtHigh > (currPvtHigh * (1 + (pvtTolerance / 100))) : false
higherPvtHigh = not na(currPvtHigh) ? lastPvtHigh < (currPvtHigh * (1 - (pvtTolerance / 100))) : false
lowerPvtLow   = not na(currPvtLow)  ? lastPvtLow  > (currPvtLow *  (1 + (pvtTolerance / 100))) : false
higherPvtLow  = not na(currPvtLow)  ? lastPvtLow  < (currPvtLow *  (1 - (pvtTolerance / 100))) : false

// PRICE RSI SECTION
currPriceRsiHigh = pivothigh(high, priceRsiLookback, priceRsiLookback)
currPriceRsiLow  = pivotlow(low,   priceRsiLookback, priceRsiLookback)

lastPriceRsiHigh = valuewhen(not na(currPriceRsiHigh), currPriceRsiHigh, 1)
lastPriceRsiLow  = valuewhen(not na(currPriceRsiLow),  currPriceRsiLow,  1)

lowerPriceRsiHigh  = not na(currPriceRsiHigh) ? lastPriceRsiHigh > (currPriceRsiHigh * (1 + (priceRsiTolerance / 100))) : false
higherPriceRsiHigh = not na(currPriceRsiHigh) ? lastPriceRsiHigh < (currPriceRsiHigh * (1 - (priceRsiTolerance / 100))) : false
lowerPriceRsiLow   = not na(currPriceRsiLow)  ? lastPriceRsiLow  > (currPriceRsiLow  * (1 + (priceRsiTolerance / 100))) : false
higherPriceRsiLow  = not na(currPriceRsiLow)  ? lastPriceRsiLow  < (currPriceRsiLow  * (1 - (priceRsiTolerance / 100))) : false

// PRICE STOCHASTIC RSI SECTION
currPriceStochRsiHigh = pivothigh(high, priceStochRsiLookback, priceStochRsiLookback)
currPriceStochRsiLow  = pivotlow(low,   priceStochRsiLookback, priceStochRsiLookback)

lastPriceStochRsiHigh = valuewhen(not na(currPriceStochRsiHigh), currPriceStochRsiHigh, 1)
lastPriceStochRsiLow  = valuewhen(not na(currPriceStochRsiLow),  currPriceStochRsiLow,  1)

lowerPriceStochRsiHigh  = not na(currPriceStochRsiHigh) ? lastPriceStochRsiHigh > (currPriceStochRsiHigh * (1 + (priceStochRsiTolerance / 100))) : false
higherPriceStochRsiHigh = not na(currPriceStochRsiHigh) ? lastPriceStochRsiHigh < (currPriceStochRsiHigh * (1 - (priceStochRsiTolerance / 100))) : false
lowerPriceStochRsiLow   = not na(currPriceStochRsiLow)  ? lastPriceStochRsiLow  > (currPriceStochRsiLow  * (1 + (priceStochRsiTolerance / 100))) : false
higherPriceStochRsiLow  = not na(currPriceStochRsiLow)  ? lastPriceStochRsiLow  < (currPriceStochRsiLow  * (1 - (priceStochRsiTolerance / 100))) : false

// PRICE STOCHASTIC SECTION
currPriceStochHigh = pivothigh(high, priceStochLookback, priceStochLookback)
currPriceStochLow  = pivotlow(low,   priceStochLookback, priceStochLookback)

lastPriceStochHigh = valuewhen(not na(currPriceStochHigh), currPriceStochHigh, 1)
lastPriceStochLow  = valuewhen(not na(currPriceStochLow),  currPriceStochLow,  1)

lowerPriceStochHigh  = not na(currPriceStochHigh) ? lastPriceStochHigh > (currPriceStochHigh * (1 + (priceStochTolerance / 100))) : false
higherPriceStochHigh = not na(currPriceStochHigh) ? lastPriceStochHigh < (currPriceStochHigh * (1 - (priceStochTolerance / 100))) : false
lowerPriceStochLow   = not na(currPriceStochLow)  ? lastPriceStochLow  > (currPriceStochLow  * (1 + (priceStochTolerance / 100))) : false
higherPriceStochLow  = not na(currPriceStochLow)  ? lastPriceStochLow  < (currPriceStochLow  * (1 - (priceStochTolerance / 100))) : false

// PRICE ADX SECTION
currPriceAdxHigh = pivothigh(high, priceAdxLookback, priceAdxLookback)
currPriceAdxLow  = pivotlow(low,   priceAdxLookback, priceAdxLookback)

lastPriceAdxHigh = valuewhen(not na(currPriceAdxHigh), currPriceAdxHigh, 1)
lastPriceAdxLow  = valuewhen(not na(currPriceAdxLow),  currPriceAdxLow,  1)

lowerPriceAdxHigh  = not na(currPriceAdxHigh) ? lastPriceAdxHigh > (currPriceAdxHigh * (1 + (priceAdxTolerance / 100))) : false
higherPriceAdxHigh = not na(currPriceAdxHigh) ? lastPriceAdxHigh < (currPriceAdxHigh * (1 - (priceAdxTolerance / 100))) : false
lowerPriceAdxLow   = not na(currPriceAdxLow)  ? lastPriceAdxLow  > (currPriceAdxLow  * (1 + (priceAdxTolerance / 100))) : false
higherPriceAdxLow  = not na(currPriceAdxLow)  ? lastPriceAdxLow  < (currPriceAdxLow  * (1 - (priceAdxTolerance / 100))) : false

// PRICE MACD SECTION
currPriceMacdHigh = pivothigh(high, priceMacdLookback, priceMacdLookback)
currPriceMacdLow  = pivotlow(low,   priceMacdLookback, priceMacdLookback)

lastPriceMacdHigh = valuewhen(not na(currPriceMacdHigh), currPriceMacdHigh, 1)
lastPriceMacdLow  = valuewhen(not na(currPriceMacdLow),  currPriceMacdLow,  1)

lowerPriceMacdHigh  = not na(currPriceMacdHigh) ? lastPriceMacdHigh > (currPriceMacdHigh * (1 + (priceMacdTolerance / 100))) : false
higherPriceMacdHigh = not na(currPriceMacdHigh) ? lastPriceMacdHigh < (currPriceMacdHigh * (1 - (priceMacdTolerance / 100))) : false
lowerPriceMacdLow   = not na(currPriceMacdLow)  ? lastPriceMacdLow  > (currPriceMacdLow  * (1 + (priceMacdTolerance / 100))) : false
higherPriceMacdLow  = not na(currPriceMacdLow)  ? lastPriceMacdLow  < (currPriceMacdLow  * (1 - (priceMacdTolerance / 100))) : false

// PRICE PVT SECTION
currPricePvtHigh = pivothigh(high, pricePvtLookback, pricePvtLookback)
currPricePvtLow  = pivotlow(low,   pricePvtLookback, pricePvtLookback)

lastPricePvtHigh = valuewhen(not na(currPricePvtHigh), currPricePvtHigh, 1)
lastPricePvtLow  = valuewhen(not na(currPricePvtLow),  currPricePvtLow,  1)

lowerPricePvtHigh  = not na(currPricePvtHigh) ? lastPricePvtHigh > (currPricePvtHigh * (1 + (pricePvtTolerance / 100))) : false
higherPricePvtHigh = not na(currPricePvtHigh) ? lastPricePvtHigh < (currPricePvtHigh * (1 - (pricePvtTolerance / 100))) : false
lowerPricePvtLow   = not na(currPricePvtLow)  ? lastPricePvtLow  > (currPricePvtLow  * (1 + (pricePvtTolerance / 100))) : false
higherPricePvtLow  = not na(currPricePvtLow)  ? lastPricePvtLow  < (currPricePvtLow  * (1 - (pricePvtTolerance / 100))) : false

// END PRICE

// COMPUTATION OF BEARISH / BULLISH
lowerRsiLowRecent   = barssince(lowerRsiLow)   < rsiLookback
lowerRsiHighRecent  = barssince(lowerRsiHigh)  < rsiLookback
higherRsiLowRecent  = barssince(higherRsiLow)  < rsiLookback
higherRsiHighRecent = barssince(higherRsiHigh) < rsiLookback

lowerStochRsiLowRecent   = barssince(lowerStochRsiLow)   < stochRsiLookback
lowerStochRsiHighRecent  = barssince(lowerStochRsiHigh)  < stochRsiLookback
higherStochRsiLowRecent  = barssince(higherStochRsiLow)  < stochRsiLookback
higherStochRsiHighRecent = barssince(higherStochRsiHigh) < stochRsiLookback

lowerStochLowRecent   = barssince(lowerStochLow)   < stochLookback
lowerStochHighRecent  = barssince(lowerStochHigh)  < stochLookback
higherStochLowRecent  = barssince(higherStochLow)  < stochLookback
higherStochHighRecent = barssince(higherStochHigh) < stochLookback

lowerAdxLowRecent   = barssince(lowerAdxLow)   < adxLookback
lowerAdxHighRecent  = barssince(lowerAdxHigh)  < adxLookback
higherAdxLowRecent  = barssince(higherAdxLow)  < adxLookback
higherAdxHighRecent = barssince(higherAdxHigh) < adxLookback
    
lowerMacdLowRecent   = barssince(lowerMacdLow)   < macdLookback
lowerMacdHighRecent  = barssince(lowerMacdHigh)  < macdLookback
higherMacdLowRecent  = barssince(higherMacdLow)  < macdLookback
higherMacdHighRecent = barssince(higherMacdHigh) < macdLookback

lowerPvtLowRecent   = barssince(lowerPvtLow)   < pvtLookback
lowerPvtHighRecent  = barssince(lowerPvtHigh)  < pvtLookback
higherPvtLowRecent  = barssince(higherPvtLow)  < pvtLookback
higherPvtHighRecent = barssince(higherPvtHigh) < pvtLookback

// PRICE RSI COMPUTATION
lowerPriceRsiLowRecent   = barssince(lowerPriceRsiLow)   < priceRsiLookback
lowerPriceRsiHighRecent  = barssince(lowerPriceRsiHigh)  < priceRsiLookback
higherPriceRsiLowRecent  = barssince(higherPriceRsiLow)  < priceRsiLookback
higherPriceRsiHighRecent = barssince(higherPriceRsiHigh) < priceRsiLookback

// PRICE STOCHASTIC RSI COMPUTATION
lowerPriceStochRsiLowRecent   = barssince(lowerPriceStochRsiLow)   < priceStochRsiLookback
lowerPriceStochRsiHighRecent  = barssince(lowerPriceStochRsiHigh)  < priceStochRsiLookback
higherPriceStochRsiLowRecent  = barssince(higherPriceStochRsiLow)  < priceStochRsiLookback
higherPriceStochRsiHighRecent = barssince(higherPriceStochRsiHigh) < priceStochRsiLookback

// PRICE STOCHASTIC COMPUTATION
lowerPriceStochLowRecent   = barssince(lowerPriceStochLow)   < priceStochLookback
lowerPriceStochHighRecent  = barssince(lowerPriceStochHigh)  < priceStochLookback
higherPriceStochLowRecent  = barssince(higherPriceStochLow)  < priceStochLookback
higherPriceStochHighRecent = barssince(higherPriceStochHigh) < priceStochLookback

// PRICE ADX COMPUTATION
lowerPriceAdxLowRecent   = barssince(lowerPriceAdxLow)   < priceAdxLookback
lowerPriceAdxHighRecent  = barssince(lowerPriceAdxHigh)  < priceAdxLookback
higherPriceAdxLowRecent  = barssince(higherPriceAdxLow)  < priceAdxLookback
higherPriceAdxHighRecent = barssince(higherPriceAdxHigh) < priceAdxLookback

// PRICE MACD COMPUTATION
lowerPriceMacdLowRecent   = barssince(lowerPriceMacdLow)   < priceMacdLookback
lowerPriceMacdHighRecent  = barssince(lowerPriceMacdHigh)  < priceMacdLookback
higherPriceMacdLowRecent  = barssince(higherPriceMacdLow)  < priceMacdLookback
higherPriceMacdHighRecent = barssince(higherPriceMacdHigh) < priceMacdLookback

// PRICE PVT COMPUTATION
lowerPricePvtLowRecent   = barssince(lowerPricePvtLow)   < pricePvtLookback
lowerPricePvtHighRecent  = barssince(lowerPricePvtHigh)  < pricePvtLookback
higherPricePvtLowRecent  = barssince(higherPricePvtLow)  < pricePvtLookback
higherPricePvtHighRecent = barssince(higherPricePvtHigh) < pricePvtLookback

// END PRICE

regRsiBullish = lowerPriceRsiLowRecent   and higherRsiLowRecent
hidRsiBullish = higherPriceRsiLowRecent  and lowerRsiLowRecent
regRsiBearish = higherPriceRsiHighRecent and lowerRsiHighRecent
hidRsiBearish = lowerPriceRsiHighRecent  and higherRsiHighRecent

regStochRsiBullish = lowerPriceStochRsiLowRecent   and higherStochRsiLowRecent
hidStochRsiBullish = higherPriceStochRsiLowRecent  and lowerStochRsiLowRecent
regStochRsiBearish = higherPriceStochRsiHighRecent and lowerStochRsiHighRecent
hidStochRsiBearish = lowerPriceStochRsiHighRecent  and higherStochRsiHighRecent

regStochBullish = lowerPriceStochLowRecent   and higherStochLowRecent
hidStochBullish = higherPriceStochLowRecent  and lowerStochLowRecent
regStochBearish = higherPriceStochHighRecent and lowerStochHighRecent
hidStochBearish = lowerPriceStochHighRecent  and higherStochHighRecent

regAdxBullish = lowerPriceAdxLowRecent   and higherAdxLowRecent
hidAdxBullish = higherPriceAdxLowRecent  and lowerAdxLowRecent
regAdxBearish = higherPriceAdxHighRecent and lowerAdxHighRecent
hidAdxBearish = lowerPriceAdxHighRecent  and higherAdxHighRecent

regMacdBullish = lowerPriceMacdLowRecent   and higherMacdLowRecent
hidMacdBullish = higherPriceMacdLowRecent  and lowerMacdLowRecent
regMacdBearish = higherPriceMacdHighRecent and lowerMacdHighRecent
hidMacdBearish = lowerPriceMacdHighRecent  and higherMacdHighRecent

regPvtBullish = lowerPricePvtLowRecent   and higherPvtLowRecent
hidPvtBullish = higherPricePvtLowRecent  and lowerPvtLowRecent
regPvtBearish = higherPricePvtHighRecent and lowerPvtHighRecent
hidPvtBearish = lowerPricePvtHighRecent  and higherPvtHighRecent

// PRICE RSI
priceRsiHighIndex = barssince(not na(priceRsiHighSet))
priceRsiLowIndex  = barssince(not na(priceRsiLowSet))

priceRsiHighSet := not na(currPriceRsiHigh) ? true : na
priceRsiLowSet  := not na(currPriceRsiLow)  ? true : na

currPriceRsiHigh := na(currPriceRsiHigh) ? currPriceRsiHigh[1] : currPriceRsiHigh
currPriceRsiLow  := na(currPriceRsiLow)  ? currPriceRsiLow[1]  : currPriceRsiLow

// PRICE STOCHASTIC RSI
priceStochRsiHighIndex = barssince(not na(priceStochRsiHighSet))
priceStochRsiLowIndex  = barssince(not na(priceStochRsiLowSet))

priceStochRsiHighSet := not na(currPriceStochRsiHigh) ? true : na
priceStochRsiLowSet  := not na(currPriceStochRsiLow)  ? true : na

currPriceStochRsiHigh := na(currPriceStochRsiHigh) ? currPriceStochRsiHigh[1] : currPriceStochRsiHigh
currPriceStochRsiLow  := na(currPriceStochRsiLow)  ? currPriceStochRsiLow[1]  : currPriceStochRsiLow

// PRICE STOCHASTIC
priceStochHighIndex = barssince(not na(priceStochHighSet))
priceStochLowIndex  = barssince(not na(priceStochLowSet))

priceStochHighSet := not na(currPriceStochHigh) ? true : na
priceStochLowSet  := not na(currPriceStochLow)  ? true : na

currPriceStochHigh := na(currPriceStochHigh) ? currPriceStochHigh[1] : currPriceStochHigh
currPriceStochLow  := na(currPriceStochLow)  ? currPriceStochLow[1]  : currPriceStochLow

// PRICE ADX
priceAdxHighIndex = barssince(not na(priceAdxHighSet))
priceAdxLowIndex  = barssince(not na(priceAdxLowSet))

priceAdxHighSet := not na(currPriceAdxHigh) ? true : na
priceAdxLowSet  := not na(currPriceAdxLow)  ? true : na

currPriceAdxHigh := na(currPriceAdxHigh) ? currPriceAdxHigh[1] : currPriceAdxHigh
currPriceAdxLow  := na(currPriceAdxLow)  ? currPriceAdxLow[1]  : currPriceAdxLow

// PRICE MACD
priceMacdHighIndex = barssince(not na(priceMacdHighSet))
priceMacdLowIndex  = barssince(not na(priceMacdLowSet))

priceMacdHighSet := not na(currPriceMacdHigh) ? true : na
priceMacdLowSet  := not na(currPriceMacdLow)  ? true : na

currPriceMacdHigh := na(currPriceMacdHigh) ? currPriceMacdHigh[1] : currPriceMacdHigh
currPriceMacdLow  := na(currPriceMacdLow)  ? currPriceMacdLow[1]  : currPriceMacdLow

// PRICE PVT
pricePvtHighIndex = barssince(not na(pricePvtHighSet))
pricePvtLowIndex  = barssince(not na(pricePvtLowSet))

pricePvtHighSet := not na(currPricePvtHigh) ? true : na
pricePvtLowSet  := not na(currPricePvtLow)  ? true : na

currPricePvtHigh := na(currPricePvtHigh) ? currPricePvtHigh[1] : currPricePvtHigh
currPricePvtLow  := na(currPricePvtLow)  ? currPricePvtLow[1]  : currPricePvtLow

// END PRICE

var line highPriceLine = na
var line lowPriceLine  = na

labelColor1 = color.new(color.black, transp=25)

int indicatorBearishOrder = 0

currPriceHigh = pivothigh(high, labelLookback, labelLookback)

distBackBear = 0

for i = 0 to labelLookback
    if indicatorBearishOrder[i] > indicatorBearishOrder
        indicatorBearishOrder := indicatorBearishOrder[i]
    if not na(currPriceHigh[i])
        break

rsiBullishFound = false
rsiBearishFound = false

stochRsiBullishFound = false
stochRsiBearishFound = false

stochBullishFound = false
stochBearishFound = false

adxBullishFound = false
adxBearishFound = false

macdBullishFound = false
macdBearishFound = false

pvtBullishFound = false
pvtBearishFound = false

string bullishMessage = ""
string bearishMessage = ""

if displayRsiDiv and ((regRsiBearish and not regRsiBearish[1]) or (hidRsiBearish and not hidRsiBearish[1]))
    newIndexPriceHigh = priceRsiHighIndex
    newIndexPriceHigh2 = bar_index - priceRsiLookback
    if priceRsiHighIndex < priceRsiLookback
        for i = 0 to priceRsiLookback
            if priceRsiHighIndex[i] == 0
                newIndexPriceHigh += priceRsiHighIndex[i + 1] + 1
                newIndexPriceHigh2 -= i + 1
                break
    
    x_price_high     = bar_index - priceRsiLookback - newIndexPriceHigh - 1
    x_price_high_mid = bar_index - priceRsiLookback - (newIndexPriceHigh / 2)

    highPriceLine := line.new(0, 0, 0, 0)

    line.set_x1(highPriceLine, x_price_high)
    line.set_y1(highPriceLine, lastPriceRsiHigh)
    line.set_x2(highPriceLine, newIndexPriceHigh2)
    line.set_y2(highPriceLine, currPriceRsiHigh)
    line.set_style(highPriceLine, line.style_dotted)
    line.set_width(highPriceLine, 3)
    line.set_color(highPriceLine, color=regRsiBearish ? color.red : color.yellow)
    
    float indicatorOffsetPricePos = (1.0 + (0.05 * indicatorBearishOrder))
    float multiple = (currPriceRsiHigh + lastPriceRsiHigh) / 2
    
    if regRsiBearish
        label.new(x_price_high_mid - distBackBear, multiple * indicatorOffsetPricePos, "Reg RSI Bear Div", color=labelColor1, textcolor=color.red, size=size.normal)
        indicatorBearishOrder += 1
    else if hidRsiBearish
        label.new(x_price_high_mid - distBackBear, multiple * indicatorOffsetPricePos, "Hid RSI Bear Div", color=labelColor1, textcolor=color.yellow, size=size.normal)
        indicatorBearishOrder += 1
    
    rsiBearishFound := true

if displayStochRsiDiv and ((regStochRsiBearish and not regStochRsiBearish[1]) or (hidStochRsiBearish and not hidStochRsiBearish[1]))
    newIndexPriceHigh = priceStochRsiHighIndex
    newIndexPriceHigh2 = bar_index - priceStochRsiLookback
    if priceStochRsiHighIndex < priceStochRsiLookback
        for i = 0 to priceStochRsiLookback
            if priceStochRsiHighIndex[i] == 0
                newIndexPriceHigh += priceStochRsiHighIndex[i + 1] + 1
                newIndexPriceHigh2 -= i + 1
                break
    
    x_price_high     = bar_index - priceStochRsiLookback - newIndexPriceHigh - 1
    x_price_high_mid = bar_index - priceStochRsiLookback - (newIndexPriceHigh / 2)

    highPriceLine := line.new(0, 0, 0, 0)

    line.set_x1(highPriceLine, x_price_high)
    line.set_y1(highPriceLine, lastPriceStochRsiHigh)
    line.set_x2(highPriceLine, newIndexPriceHigh2)
    line.set_y2(highPriceLine, currPriceStochRsiHigh)
    line.set_style(highPriceLine, line.style_dotted)
    line.set_width(highPriceLine, 3)
    line.set_color(highPriceLine, color=regStochRsiBearish ? color.red : color.yellow)
    
    float indicatorOffsetPricePos = (1.0 + (0.05 * indicatorBearishOrder))
    float multiple = (currPriceStochRsiHigh + lastPriceStochRsiHigh) / 2
    
    if regStochRsiBearish
        label.new(x_price_high_mid - distBackBear, multiple  * indicatorOffsetPricePos, "Reg Stoch RSI Bear Div", color=labelColor1, textcolor=color.red, size=size.normal)
        indicatorBearishOrder += 1
    else if hidStochRsiBearish
        label.new(x_price_high_mid - distBackBear, multiple  * indicatorOffsetPricePos, "Hid Stoch RSI Bear Div", color=labelColor1, textcolor=color.yellow, size=size.normal)
        indicatorBearishOrder += 1
    
    stochRsiBearishFound := true

if displayStochDiv and ((regStochBearish and not regStochBearish[1]) or (hidStochBearish and not hidStochBearish[1]))
    newIndexPriceHigh = priceStochHighIndex
    newIndexPriceHigh2 = bar_index - priceStochLookback
    if priceStochHighIndex < priceStochLookback
        for i = 0 to priceStochLookback
            if priceStochHighIndex[i] == 0
                newIndexPriceHigh += priceStochHighIndex[i + 1] + 1
                newIndexPriceHigh2 -= i + 1
                break
    
    x_price_high     = bar_index - priceStochLookback - newIndexPriceHigh - 1
    x_price_high_mid = bar_index - priceStochLookback - (newIndexPriceHigh / 2)

    highPriceLine := line.new(0, 0, 0, 0)

    line.set_x1(highPriceLine, x_price_high)
    line.set_y1(highPriceLine, lastPriceStochHigh)
    line.set_x2(highPriceLine, newIndexPriceHigh2)
    line.set_y2(highPriceLine, currPriceStochHigh)
    line.set_style(highPriceLine, line.style_dotted)
    line.set_width(highPriceLine, 3)
    line.set_color(highPriceLine, color=regStochBearish ? color.red : color.yellow)
    
    float indicatorOffsetPricePos = (1.0 + (0.05 * indicatorBearishOrder))
    float multiple = (currPriceStochHigh + lastPriceStochHigh) / 2

    if regStochBearish
        label.new(x_price_high_mid - distBackBear, multiple  * indicatorOffsetPricePos, "Reg Stoch Bear Div", color=labelColor1, textcolor=color.red, size=size.normal)
        indicatorBearishOrder += 1
    else if hidStochBearish
        label.new(x_price_high_mid - distBackBear, multiple  * indicatorOffsetPricePos, "Hid Stoch Bear Div", color=labelColor1, textcolor=color.yellow, size=size.normal)
        indicatorBearishOrder += 1
    
    stochBearishFound := true

if displayAdxDiv and ((regAdxBearish and not regAdxBearish[1]) or (hidAdxBearish and not hidAdxBearish[1]))
    newIndexPriceHigh = priceAdxHighIndex
    newIndexPriceHigh2 = bar_index - priceAdxLookback
    if priceAdxHighIndex < priceAdxLookback
        for i = 0 to priceAdxLookback
            if priceAdxHighIndex[i] == 0
                newIndexPriceHigh += priceAdxHighIndex[i + 1] + 1
                newIndexPriceHigh2 -= i + 1
                break
    
    x_price_high     = bar_index - priceAdxLookback - newIndexPriceHigh - 1
    x_price_high_mid = bar_index - priceAdxLookback - (newIndexPriceHigh / 2)

    highPriceLine := line.new(0, 0, 0, 0)

    line.set_x1(highPriceLine, x_price_high)
    line.set_y1(highPriceLine, lastPriceAdxHigh)
    line.set_x2(highPriceLine, newIndexPriceHigh2)
    line.set_y2(highPriceLine, currPriceAdxHigh)
    line.set_style(highPriceLine, line.style_dotted)
    line.set_width(highPriceLine, 3)
    line.set_color(highPriceLine, color=regAdxBearish ? color.red : color.yellow)
    
    float indicatorOffsetPricePos = (1.0 + (0.05 * indicatorBearishOrder))
    float multiple = (currPriceAdxHigh + lastPriceAdxHigh) / 2
    
    if regAdxBearish
        label.new(x_price_high_mid - distBackBear, multiple * indicatorOffsetPricePos, "Reg ADX Bear Div", color=labelColor1, textcolor=color.red, size=size.normal)
        indicatorBearishOrder += 1
    else if hidAdxBearish
        label.new(x_price_high_mid - distBackBear, multiple * indicatorOffsetPricePos, "Hid ADX Bear Div", color=labelColor1, textcolor=color.yellow, size=size.normal)
        indicatorBearishOrder += 1
    
    adxBearishFound := true

if displayMacdDiv and ((regMacdBearish and not regMacdBearish[1]) or (hidMacdBearish and not hidMacdBearish[1]))
    newIndexPriceHigh = priceMacdHighIndex
    newIndexPriceHigh2 = bar_index - priceMacdLookback
    if priceMacdHighIndex < priceMacdLookback
        for i = 0 to priceMacdLookback
            if priceMacdHighIndex[i] == 0
                newIndexPriceHigh += priceMacdHighIndex[i + 1] + 1
                newIndexPriceHigh2 -= i + 1
                break
    
    x_price_high     = bar_index - priceMacdLookback - newIndexPriceHigh - 1
    x_price_high_mid = bar_index - priceMacdLookback - (newIndexPriceHigh / 2)

    highPriceLine := line.new(0, 0, 0, 0)

    line.set_x1(highPriceLine, x_price_high)
    line.set_y1(highPriceLine, lastPriceMacdHigh)
    line.set_x2(highPriceLine, newIndexPriceHigh2)
    line.set_y2(highPriceLine, currPriceMacdHigh)
    line.set_style(highPriceLine, line.style_dotted)
    line.set_width(highPriceLine, 3)
    line.set_color(highPriceLine, color=regMacdBearish ? color.red : color.yellow)
    
    float indicatorOffsetPricePos = (1.0 + (0.05 * indicatorBearishOrder))
    float multiple = (currPriceMacdHigh + lastPriceMacdHigh) / 2
    
    if regMacdBearish
        label.new(x_price_high_mid - distBackBear, multiple  * indicatorOffsetPricePos, "Reg MACD Bear Div", color=labelColor1, textcolor=color.red, size=size.normal)
        indicatorBearishOrder += 1
    else if hidMacdBearish
        label.new(x_price_high_mid - distBackBear, multiple  * indicatorOffsetPricePos, "Hid MACD Bear Div", color=labelColor1, textcolor=color.yellow, size=size.normal)
        indicatorBearishOrder += 1
    
    macdBearishFound := true

if displayPvtDiv and ((regPvtBearish and not regPvtBearish[1]) or (hidPvtBearish and not hidPvtBearish[1]))
    newIndexPriceHigh = pricePvtHighIndex
    newIndexPriceHigh2 = bar_index - pricePvtLookback
    if pricePvtHighIndex < pricePvtLookback
        for i = 0 to pricePvtLookback
            if pricePvtHighIndex[i] == 0
                newIndexPriceHigh += pricePvtHighIndex[i + 1] + 1
                newIndexPriceHigh2 -= i + 1
                break
    
    x_price_high     = bar_index - pricePvtLookback - newIndexPriceHigh - 1
    x_price_high_mid = bar_index - pricePvtLookback - (newIndexPriceHigh / 2)

    highPriceLine := line.new(0, 0, 0, 0)

    line.set_x1(highPriceLine, x_price_high)
    line.set_y1(highPriceLine, lastPricePvtHigh)
    line.set_x2(highPriceLine, newIndexPriceHigh2)
    line.set_y2(highPriceLine, currPricePvtHigh)
    line.set_style(highPriceLine, line.style_dotted)
    line.set_width(highPriceLine, 3)
    line.set_color(highPriceLine, color=regPvtBearish ? color.red : color.yellow)
    
    float indicatorOffsetPricePos = (1.0 + (0.05 * indicatorBearishOrder))
    float multiple = (currPricePvtHigh + lastPricePvtHigh) / 2

    if regPvtBearish
        label.new(x_price_high_mid - distBackBear, multiple * indicatorOffsetPricePos, "Reg Vol Bear Div", color=labelColor1, textcolor=color.red, size=size.normal)
        indicatorBearishOrder += 1
    else if hidPvtBearish
        label.new(x_price_high_mid - distBackBear, multiple * indicatorOffsetPricePos, "Hid Vol Bear Div", color=labelColor1, textcolor=color.yellow, size=size.normal)
        indicatorBearishOrder += 1
    
    pvtBearishFound := true

int indicatorBullishOrder = 0

currPriceLow = pivothigh(low, labelLookback, labelLookback)

distBackBull = 0

for i = 0 to labelLookback
    if indicatorBullishOrder[i] > indicatorBullishOrder
        indicatorBullishOrder := indicatorBullishOrder[i]
    if not na(currPriceLow[i])
        break

if displayRsiDiv and ((regRsiBullish and not regRsiBullish[1]) or (hidRsiBullish and not hidRsiBullish[1]))
    newIndexPriceLow = priceRsiLowIndex
    newIndexPriceLow2 = bar_index - priceRsiLookback
    if priceRsiLowIndex < priceRsiLookback
        for i = 0 to priceRsiLookback
            if priceRsiLowIndex[i] == 0
                newIndexPriceLow := priceRsiLowIndex[i + 1] + (i + 1)
                newIndexPriceLow2 -= i + 1 
                break

    x_price_low      = bar_index - priceRsiLookback - newIndexPriceLow - 1
    x_price_low_mid  = bar_index - priceRsiLookback - (newIndexPriceLow / 2)

    lowPriceLine := line.new(0, 0, 0, 0)

    line.set_x1(lowPriceLine, x_price_low)
    line.set_y1(lowPriceLine, lastPriceRsiLow)
    line.set_x2(lowPriceLine, newIndexPriceLow2)
    line.set_y2(lowPriceLine, currPriceRsiLow)
    line.set_style(lowPriceLine, line.style_dotted)
    line.set_width(lowPriceLine, 3)
    line.set_color(lowPriceLine, color=regRsiBullish ? color.green : color.blue)

    float indicatorOffsetPriceNeg = (1.0 - (0.05 * indicatorBullishOrder))
    float multiple = (currPriceRsiLow + lastPriceRsiLow) / 2
    
    if regRsiBullish
        label.new(x_price_low_mid - distBackBull, multiple * indicatorOffsetPriceNeg, "Reg RSI Bull Div", color=labelColor1, style=label.style_label_up, textcolor=color.green, size=size.normal)
        indicatorBullishOrder += 1
    else if hidRsiBullish
        label.new(x_price_low_mid - distBackBull, multiple * indicatorOffsetPriceNeg, "Hid RSI Bull Div", color=labelColor1, style=label.style_label_up, textcolor=color.blue, size=size.normal)
        indicatorBullishOrder += 1
    
    rsiBullishFound := true

if displayStochRsiDiv and ((regStochRsiBullish and not regStochRsiBullish[1]) or (hidStochRsiBullish and not hidStochRsiBullish[1]))
    newIndexPriceLow = priceStochRsiLowIndex
    newIndexPriceLow2 = bar_index - priceStochRsiLookback
    if priceStochRsiLowIndex < priceStochRsiLookback
        for i = 0 to priceStochRsiLookback
            if priceStochRsiLowIndex[i] == 0
                newIndexPriceLow := priceStochRsiLowIndex[i + 1] + (i + 1)
                newIndexPriceLow2 -= i + 1 
                break

    x_price_low      = bar_index - priceStochRsiLookback - newIndexPriceLow - 1
    x_price_low_mid  = bar_index - priceStochRsiLookback - (newIndexPriceLow / 2)

    lowPriceLine := line.new(0, 0, 0, 0)

    line.set_x1(lowPriceLine, x_price_low)
    line.set_y1(lowPriceLine, lastPriceStochRsiLow)
    line.set_x2(lowPriceLine, newIndexPriceLow2)
    line.set_y2(lowPriceLine, currPriceStochRsiLow)
    line.set_style(lowPriceLine, line.style_dotted)
    line.set_width(lowPriceLine, 3)
    line.set_color(lowPriceLine, color=regStochRsiBullish ? color.green : color.blue)

    float indicatorOffsetPriceNeg = (1.0 - (0.05 * indicatorBullishOrder))
    float multiple = (currPriceStochRsiLow + lastPriceStochRsiLow) / 2

    if regStochRsiBullish
        label.new(x_price_low_mid - distBackBull, multiple * indicatorOffsetPriceNeg, "Reg Stoch RSI Bull Div", color=labelColor1, style=label.style_label_up, textcolor=color.green, size=size.normal)
        indicatorBullishOrder += 1
    else if hidStochRsiBullish
        label.new(x_price_low_mid - distBackBull, multiple * indicatorOffsetPriceNeg, "Hid Stoch RSI Bull Div", color=labelColor1, style=label.style_label_up, textcolor=color.blue, size=size.normal)
        indicatorBullishOrder += 1
    
    stochRsiBullishFound := true
    
if displayStochDiv and ((regStochBullish and not regStochBullish[1]) or (hidStochBullish and not hidStochBullish[1]))
    newIndexPriceLow = priceStochLowIndex
    newIndexPriceLow2 = bar_index - priceStochLookback
    if priceStochLowIndex < priceStochLookback
        for i = 0 to priceStochLookback
            if priceStochLowIndex[i] == 0
                newIndexPriceLow := priceStochLowIndex[i + 1] + (i + 1)
                newIndexPriceLow2 -= i + 1 
                break

    x_price_low      = bar_index - priceStochLookback - newIndexPriceLow - 1
    x_price_low_mid  = bar_index - priceStochLookback - (newIndexPriceLow / 2)

    lowPriceLine := line.new(0, 0, 0, 0)

    line.set_x1(lowPriceLine, x_price_low)
    line.set_y1(lowPriceLine, lastPriceStochLow)
    line.set_x2(lowPriceLine, newIndexPriceLow2)
    line.set_y2(lowPriceLine, currPriceStochLow)
    line.set_style(lowPriceLine, line.style_dotted)
    line.set_width(lowPriceLine, 3)
    line.set_color(lowPriceLine, color=regStochBullish ? color.green : color.blue)

    float indicatorOffsetPriceNeg = (1.0 - (0.05 * indicatorBullishOrder))
    float multiple = (currPriceStochLow + lastPriceStochLow) / 2

    if regStochBullish
        label.new(x_price_low_mid - distBackBull, multiple  * indicatorOffsetPriceNeg, "Reg Stoch Bull Div", color=labelColor1, style=label.style_label_up, textcolor=color.green, size=size.normal)
        indicatorBullishOrder += 1
    else if hidStochBullish
        label.new(x_price_low_mid - distBackBull, multiple  * indicatorOffsetPriceNeg, "Hid Stoch Bull Div", color=labelColor1, style=label.style_label_up, textcolor=color.blue, size=size.normal)
        indicatorBullishOrder += 1
    
    stochBullishFound := true

if displayAdxDiv and ((regAdxBullish and not regAdxBullish[1]) or (hidAdxBullish and not hidAdxBullish[1]))
    newIndexPriceLow = priceAdxLowIndex
    newIndexPriceLow2 = bar_index - priceAdxLookback
    if priceAdxLowIndex < priceAdxLookback
        for i = 0 to priceAdxLookback
            if priceAdxLowIndex[i] == 0
                newIndexPriceLow := priceAdxLowIndex[i + 1] + (i + 1)
                newIndexPriceLow2 -= i + 1 
                break

    x_price_low      = bar_index - priceAdxLookback - newIndexPriceLow - 1
    x_price_low_mid  = bar_index - priceAdxLookback - (newIndexPriceLow / 2)

    lowPriceLine := line.new(0, 0, 0, 0)

    line.set_x1(lowPriceLine, x_price_low)
    line.set_y1(lowPriceLine, lastPriceAdxLow)
    line.set_x2(lowPriceLine, newIndexPriceLow2)
    line.set_y2(lowPriceLine, currPriceAdxLow)
    line.set_style(lowPriceLine, line.style_dotted)
    line.set_width(lowPriceLine, 3)
    line.set_color(lowPriceLine, color=regAdxBullish ? color.green : color.blue)

    float indicatorOffsetPriceNeg = (1.0 - (0.05 * indicatorBullishOrder))
    float multiple = (currPriceAdxLow + lastPriceAdxLow) / 2
    
    if regAdxBullish
        label.new(x_price_low_mid - distBackBull, multiple * indicatorOffsetPriceNeg, "Reg ADX Bull Div", color=labelColor1, style=label.style_label_up, textcolor=color.green, size=size.normal)
        indicatorBullishOrder += 1
    else if hidAdxBullish
        label.new(x_price_low_mid - distBackBull, multiple * indicatorOffsetPriceNeg, "Hid ADX Bull Div", color=labelColor1, style=label.style_label_up, textcolor=color.blue, size=size.normal)
        indicatorBullishOrder += 1
    
    adxBullishFound := true
    
if displayMacdDiv and ((regMacdBullish and not regMacdBullish[1]) or (hidMacdBullish and not hidMacdBullish[1]))
    newIndexPriceLow = priceMacdLowIndex
    newIndexPriceLow2 = bar_index - priceMacdLookback
    if priceMacdLowIndex < priceMacdLookback
        for i = 0 to priceMacdLookback
            if priceMacdLowIndex[i] == 0
                newIndexPriceLow := priceMacdLowIndex[i + 1] + (i + 1)
                newIndexPriceLow2 -= i + 1 
                break

    x_price_low      = bar_index - priceMacdLookback - newIndexPriceLow - 1
    x_price_low_mid  = bar_index - priceMacdLookback - (newIndexPriceLow / 2)

    lowPriceLine := line.new(0, 0, 0, 0)

    line.set_x1(lowPriceLine, x_price_low)
    line.set_y1(lowPriceLine, lastPriceMacdLow)
    line.set_x2(lowPriceLine, newIndexPriceLow2)
    line.set_y2(lowPriceLine, currPriceMacdLow)
    line.set_style(lowPriceLine, line.style_dotted)
    line.set_width(lowPriceLine, 3)
    line.set_color(lowPriceLine, color=regMacdBullish ? color.green : color.blue)

    float indicatorOffsetPriceNeg = (1.0 - (0.05 * indicatorBullishOrder))
    float multiple = (currPriceMacdLow + lastPriceMacdLow) / 2
    
    if regMacdBullish
        label.new(x_price_low_mid - distBackBull, multiple * indicatorOffsetPriceNeg, "Reg MACD Bull Div", color=labelColor1, style=label.style_label_up, textcolor=color.green, size=size.normal)
        indicatorBullishOrder += 1
    else if hidMacdBullish
        label.new(x_price_low_mid - distBackBull, multiple * indicatorOffsetPriceNeg, "Hid MACD Bull Div", color=labelColor1, style=label.style_label_up, textcolor=color.blue, size=size.normal)
        indicatorBullishOrder += 1
    
    macdBullishFound := true
    
if displayPvtDiv and ((regPvtBullish and not regPvtBullish[1]) or (hidPvtBullish and not hidPvtBullish[1]))
    newIndexPriceLow = pricePvtLowIndex
    newIndexPriceLow2 = bar_index - pricePvtLookback
    if pricePvtLowIndex < pricePvtLookback
        for i = 0 to pricePvtLookback
            if pricePvtLowIndex[i] == 0
                newIndexPriceLow := pricePvtLowIndex[i + 1] + (i + 1)
                newIndexPriceLow2 -= i + 1 
                break

    x_price_low      = bar_index - pricePvtLookback - newIndexPriceLow - 1
    x_price_low_mid  = bar_index - pricePvtLookback - (newIndexPriceLow / 2)

    lowPriceLine := line.new(0, 0, 0, 0)

    line.set_x1(lowPriceLine, x_price_low)
    line.set_y1(lowPriceLine, lastPricePvtLow)
    line.set_x2(lowPriceLine, newIndexPriceLow2)
    line.set_y2(lowPriceLine, currPricePvtLow)
    line.set_style(lowPriceLine, line.style_dotted)
    line.set_width(lowPriceLine, 3)
    line.set_color(lowPriceLine, color=regPvtBullish ? color.green : color.blue)

    float indicatorOffsetPriceNeg = (1.0 - (0.05 * indicatorBullishOrder))
    float multiple = (currPricePvtLow + lastPricePvtLow) / 2
    
    if regPvtBullish
        label.new(x_price_low_mid - distBackBull, multiple * indicatorOffsetPriceNeg, "Reg Vol Bull Div", color=labelColor1, style=label.style_label_up, textcolor=color.green, size=size.normal)
        indicatorBullishOrder += 1
    else if hidPvtBullish
        label.new(x_price_low_mid - distBackBull, multiple * indicatorOffsetPriceNeg, "Hid Vol Bull Div", color=labelColor1, style=label.style_label_up, textcolor=color.blue, size=size.normal)
        indicatorBullishOrder += 1
    
    pvtBullishFound := true
    
plot(rsiBullishFound ? 1 : 0, title = "RSI Bullish", color = rsiBullishFound ? color.new(color.white, 50) : color.new(color.black, 100))
plot(stochRsiBullishFound ? 1 : 0, title = "Stoch RSI Bullish", color = stochRsiBullishFound ? color.new(color.white, 50) : color.new(color.black, 100))
plot(macdBullishFound ? 1 : 0, title = "MACD Bullish", color = macdBullishFound ? color.new(color.white, 50) : color.new(color.black, 100))
plot(stochBullishFound ? 1 : 0, title = "Stoch Bullish", color = stochBullishFound ? color.new(color.white, 50) : color.new(color.black, 100))
plot(pvtBullishFound ? 1 : 0, title = "PVT Bullish", color = pvtBullishFound ? color.new(color.white, 50) : color.new(color.black, 100))
plot(adxBullishFound ? 1 : 0, title = "ADX Bullish", color = adxBullishFound ? color.new(color.white, 50) : color.new(color.black, 100))

plot(rsiBearishFound ? 1 : 0, title = "RSI Bearish", color = rsiBearishFound ? color.new(color.white, 50) : color.new(color.black, 100))
plot(stochRsiBearishFound ? 1 : 0, title = "Stoch RSI Bearish", color = stochRsiBearishFound ? color.new(color.white, 50) : color.new(color.black, 100))
plot(macdBearishFound ? 1 : 0, title = "MACD Bearish", color = macdBearishFound ? color.new(color.white, 50) : color.new(color.black, 100))
plot(stochBearishFound ? 1 : 0, title = "Stoch Bearish", color = stochBearishFound ? color.new(color.white, 50) : color.new(color.black, 100))
plot(pvtBearishFound ? 1 : 0, title = "PVT Bearish", color = pvtBearishFound ? color.new(color.white, 50) : color.new(color.black, 100))
plot(adxBearishFound ? 1 : 0, title = "ADX Bearish", color = adxBearishFound ? color.new(color.white, 50) : color.new(color.black, 100))

bullishFound = false
bearishFound = false

if rsiBullishFound or stochRsiBullishFound or macdBullishFound or stochBullishFound or pvtBullishFound or adxBullishFound
    bullishFound := true

if rsiBearishFound or stochRsiBearishFound or macdBearishFound or stochBearishFound or pvtBearishFound or adxBearishFound
    bearishFound := true

alertcondition(bullishFound, title = "Bullish Divergence Alert", message = 'Bullish Divergences:\nRSI Divergence: {{plot("RSI Bullish")}}\nStoch RSI Divergence: {{plot("Stoch RSI Bullish")}}\nMACD Divergence: {{plot("MACD Bullish")}}\nStoch Divergence: {{plot("Stoch Bullish")}}\nPVT Divergence: {{plot("PVT Bullish")}}\nADX Divergence: {{plot("ADX Bullish")}}')
alertcondition(bearishFound, title = "Bearish Divergence Alert", message = 'Bearish Divergences:\nRSI Divergence: {{plot("RSI Bearish")}}\nStoch RSI Divergence: {{plot("Stoch RSI Bearish")}}\nMACD Divergence: {{plot("MACD Bearish")}}\nStoch Divergence: {{plot("Stoch Bearish")}}\nPVT Divergence: {{plot("PVT Bearish")}}\nADX Divergence: {{plot("ADX Bearish")}}')

Overview

Total Divergence+ can show RSI, Stochastic RSI, Stochastic, ADX, MACD, and Price Volume Trend divergences, each with its own display switch. Lookback lengths set how far price and the oscillator are compared, and percentage epsilon settings set how close values must be to count. RSI, Stochastic RSI, and MACD are on by default; the others start off. It does not define entries, exits, or stops.

Details

Published by

krosenfeld7 on GitHub

Type

Indicator, Pine Script v4

Draws

On the price 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.

Settings

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

SettingTypeDefaultVariable
Display RSI DivbooltruedisplayRsiDiv
RSI Lookbackint15rsiLookback
RSI Price Lookbackint15priceRsiLookback
RSI Epsilon (%)float1.0rsiTolerance
RSI Price Epsilon (%)float1.0priceRsiTolerance
Display Stoch RSI DivbooltruedisplayStochRsiDiv
Stoch RSI Lookbackint15stochRsiLookback
Stoch RSI Price Lookbackint15priceStochRsiLookback
Stoch Rsi Epsilon (%)float1.0stochRsiTolerance
Stoch RSI Price Epsilon (%)float1.0priceStochRsiTolerance
Display Stoch DivboolfalsedisplayStochDiv
Stoch Lookbackint15stochLookback
Show all 31 settings
SettingTypeDefaultVariable
Stoch Price Lookbackint15priceStochLookback
Stoch Epsilon (%)float1.0stochTolerance
Stoch Price Epsilon (%)float1.0priceStochTolerance
Display ADX DivboolfalsedisplayAdxDiv
ADX Lookbackint15adxLookback
ADX Price Lookbackint15priceAdxLookback
ADX Epsilon (%)float1.0adxTolerance
ADX Price Epsilon (%)float1.0priceAdxTolerance
Display MACD DivbooltruedisplayMacdDiv
MACD Lookbackint15macdLookback
MACD Price Lookbackint15priceMacdLookback
MACD Epsilon (%)float1.0macdTolerance
MACD Price Epsilon (%)float1.0priceMacdTolerance
Display PVT DivboolfalsedisplayPvtDiv
PVT Lookbackint15pvtLookback
PVT Price Lookbackint15pricePvtLookback
PVT Epsilon (%)float1.0pvtTolerance
PVT Price Epsilon (%)float1.0pricePvtTolerance
Label Lookbackint10labelLookback
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

257 stars

This overlay indicator looks for regular price divergences against a set of oscillators, using pivot highs and lows. It compares those pivots with RSI, MACD and its histogram, stochastic, CCI, momentum, OBV, a directional oscillator, volume-weighted MACD, Chai

Indicator v5 DivergenceOscillators & momentum Repaints
Updated 7 weeks ago · None stated GitHub repository of Snag - Best Futures V3

RSI Divergence Scanner

by TradersPost
228 stars

The RSI Divergence Scanner calculates the Relative Strength Index and identifies regular and hidden bullish or bearish divergences against price using pivot high and low points. It plots divergence lines and customizable labels directly on the price chart. Inp

Indicator v6 Oscillators & momentumDivergence
Updated 3 months ago · None stated GitHub repository of RSI Divergence Scanner

Price Divergence

by capissimo
114 stars

This indicator detects regular and hidden divergences across multiple technical factors plotted directly on the price chart. It evaluates price highs and lows against peaks and troughs identified by a five-bar fractal mechanism on RSI, RSI Stochastic, MACD, re

Indicator v4 DivergenceOscillators & momentum
Updated 1 year ago · None stated GitHub repository of Price Divergence

RSI Div at dVWAP StDev

by cryptorife
75 stars

This indicator marks RSI divergences only when price trades beyond daily VWAP standard-deviation bands. It compares recent RSI extremes with price highs and lows over a lookback window, and it treats a divergence as valid only while RSI stays beyond overbought

Indicator v4 DivergenceOscillators & momentum Repaints
Updated 3 years ago · GPL-3.0 GitHub repository of RSI Div at dVWAP StDev

This indicator builds a momentum score from the rate of change of close, using a lookback that expands or contracts with ATR relative to its standard deviation. It divides that rate of change by price standard deviation, scales the result, and weights it by vo

Indicator v5 Oscillators & momentumVolatility & bands Repaints
Updated 8 weeks ago · None stated GitHub repository of Adaptive Volatility-Adjusted Momentum Score

More Divergence 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