MACD 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 MACD momentum.
//@version=5
indicator(title="MACD Potential Divergence - Fontiramisu", shorttitle="Potential Div MACD - Fontiramisu", timeframe="", timeframe_gaps=true)
import Fontiramisu/fontilab/7 as fontilab

// ] ------------------ MACD ---------------- [
// Getting inputs
fast_length = input(title="Fast Length", defval=12, group="MACD 30 9")
slow_length = input(title="Slow Length", defval=26, group="MACD 30 9")
src = input(title="Source", defval=close, group="MACD 30 9")
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9, group="MACD 30 9")
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"], group="MACD 30 9")
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"], group="MACD 30 9")
// Plot colors
col_macd = input(#2962FF, "MACD Line  ", inline="MACD", group="MACD 30 9")
col_signal = input(#FF6D00, "Signal Line  ", inline="Signal", group="MACD 30 9")

// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal
plot(hist, title="Histogram", style=plot.style_histogram, color=color.new(color.yellow,30))
plot(hist, title="Line", color=color.aqua, linewidth=2, display=display.all)
plot(macd, title="MACD", color=col_macd, display=display.none)
plot(signal, title="Signal", color=col_signal, display=display.none)


// ] -------------- 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 = hist 

// 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
	 )
	 
// ]
