RSI Potential Divergence - Fontiramisu
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Author : @Fontiramisu
// The divergence part is inspired from divergence indicator by @tradingView team.
// Indicator showing potential divergences on RSI momentum.
//@version=5
indicator(title="RSI Potential Divergence - Fontiramisu", shorttitle="Potential Div RSI - Fontiramisu", timeframe="", timeframe_gaps=true)


import Fontiramisu/fontilab/7 as fontilab
// import Fontiramisu/fontlib/60 as fontilab

// ] ------------------ RSI ------------------ [
hline(0, "Zero Line", color=color.new(#787B86, 50))

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="RSI Settings")
maLengthInput = input.int(14, title="MA Length", group="RSI Settings")

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"

plot(rsi, "RSI", color=#7E57C2)
plot(rsiMA, "RSI-based MA", color=color.yellow, display=display.none)

rsiUpperBand = hline(75, "RSI Upper Band", linestyle=hline.style_dotted, color=color.new(color.white, 75))
hline(50, "RSI Middle Band", linestyle=hline.style_dotted, color=color.new(color.white, 85))
rsiLowerBand = hline(25, "RSI Lower Band", linestyle=hline.style_dotted, color=color.new(color.white, 75))



// ] -------------- Divergence Handle -------------- [
showDiv = input.bool(title="Show Divergence", defval=true, group="div settings")
lbR = input(title="Pivot Lookback Right", defval=5, group="div settings")
lbL = input(title="Pivot Lookback Left", defval=5, group="div settings")
rangeUpper = input(title="Max of Lookback Range", defval=60, group="div settings")
rangeLower = input(title="Min of Lookback Range", defval=5, group="div settings")
plotBull = input(title="Plot Bullish", defval=true, group="div settings")
plotBullPot = input(title="Plot Bullish Potential", defval=true, group="div settings")
plotHiddenBull = input(title="Plot Hidden Bullish", defval=false, group="div settings")
plotHiddenBullPot = input(title="Plot Hidden Bullish", defval=false, group="div settings")
plotBear = input(title="Plot Bearish", defval=true, group="div settings")
plotBearPot = input(title="Plot Bearish Potential Potential", defval=true, group="div settings")
plotHiddenBear = input(title="Plot Hidden Bearish", defval=false, group="div settings")
plotHiddenBearPot = input(title="Plot Hidden Bearish Potential", defval=false, group="div settings")
bearColor = color.red
bearPotColor = color.new(color.red, 20)
bullColor = color.green
bullPotColor = color.new(color.green, 20)
hiddenBullColor = color.new(color.green, 80)
hiddenBearColor = color.new(color.red, 80)
textColor = color.white
textColorDivPot = color.new(color.white, 20)
noneColor = color.new(color.white, 100)

float osc = rsi 

// Get pivots.
[plFound, phFound, plFoundPot, phFoundPot] = fontilab.getOscPivots(osc, lbL, lbR)

// Div for curent ut.
[bullDiv, bullDivPot, hiddenBullDiv, hiddenBullDivPot, bearDiv, bearDivPot, hiddenBearDiv, hiddenBearDivPot] = 
     fontilab.plotDivergences(osc, lbR, plFound, phFound, plFoundPot, phFoundPot, rangeLower, rangeUpper)

//------
// Regular Bullish
plot(
     showDiv and plotBullPot and plFound ? osc[lbR] : na,
     offset=-lbR,
     title="Regular Bullish",
     linewidth=2,
     color=(bullDiv ? bullColor : noneColor)
     )

plotshape(
	 showDiv and plotBullPot and bullDivPot ? osc[1] : na,
	 offset= -1,
	 title="Regular Bullish Pot Label",
	 text="B",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullPotColor,
	 textcolor=textColorDivPot
	 )
	 
plotshape(
	 showDiv and plotBullPot and bullDiv ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bullish Label",
	 text=" Bull ",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullColor,
	 textcolor=textColor
	 )


//------
// Hidden Bullish
plot(
	 showDiv and plotHiddenBull and plFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bullish",
	 linewidth=2,
	 color=(hiddenBullDiv ? hiddenBullColor : noneColor)
	 )

plotshape(
	 showDiv and plotHiddenBullPot and hiddenBullDivPot ? osc[1] : na,
	 offset=-1,
	 title="Hidden Bullish Pot Label",
	 text="H",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullPotColor,
	 textcolor=textColorDivPot
	 )

plotshape(
	 showDiv and plotHiddenBull and hiddenBullDiv ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bullish Label",
	 text=" H Bull ",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullColor,
	 textcolor=textColor
	 )

//------
// Regular Bearish
plot(
	 showDiv and plotBearPot and phFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bearish",
	 linewidth=2,
	 color=(bearDiv ? bearColor : noneColor)
	 )

plotshape(
	 showDiv and plotBearPot and bearDivPot ? osc[1] : na,
	 offset=-1,
	 title="Regular Bearish Pot Label",
	 text="B",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearPotColor,
	 textcolor=textColorDivPot
	 )
	 
plotshape(
	 showDiv and plotBearPot and bearDiv ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bearish Label",
	 text=" Bear ",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearColor,
	 textcolor=textColor
	 )

//-----
// Hidden Bearish
plot(
	 showDiv and plotHiddenBear and phFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bearish",
	 linewidth=2,
	 color=(hiddenBearDiv ? hiddenBearColor : noneColor)
	 )

plotshape(
	 showDiv and plotHiddenBearPot and hiddenBearDivPot ? osc[1] : na,
	 offset=-1,
	 title="Hidden Bearish Pot Label",
	 text="H",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearPotColor,
	 textcolor=textColorDivPot
	 )
	 
plotshape(
	 showDiv and plotHiddenBear and hiddenBearDiv ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bearish Label",
	 text=" H Bear ",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearColor,
	 textcolor=textColor
	 )
// ]
