MFI Divergence Indicator 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © thekarasystem

//@version=5
indicator(title="TKS::INDI:MFI Divergence", format=format.price, overlay=false,  timeframe="", timeframe_gaps=true)

//Get User Inputs

len = input.int(title="MFI Period", minval=1, defval=14)
src = input(title="MFI Source", defval=close)
plotBull = input(title="Plot Bullish", defval=true)
plotHiddenBull = input(title="Plot Hidden Bullish", defval=true)
plotBear = input(title="Plot Bearish", defval=true)
plotHiddenBear = input(title="Plot Hidden Bearish", defval=true)
Lookback_Right = input(title="Pivot Lookback Right", defval=5)
Lookback_Left = input(title="Pivot Lookback Left", defval=20)
rangeUpper = input(title="Max of Lookback Range", defval=100)
rangeLower = input(title="Min of Lookback Range", defval=5)

ON_RSI = input.bool(title="Show RSI", tooltip="Will display the RSI in light blue. The parameters of the MFI apply", defval=true)

//Get MFI
MFI = ta.mfi(src, len)
RSI = ta.rsi(src, len)

//Plotting MFI
bearColor = color.red
hbearColor = color.orange
bullColor = color.green
hbullColor = #73BF16
textColor = color.white
noneColor = color.new(color.white, 100)

// Calculate the differences between MFI and RSI.
dif = math.abs(RSI-MFI)
plot(MFI, title="MFI", linewidth=1, color=(dif < 20 ? #D30895 : color.yellow  ))
plot(RSI, title="RSI", linewidth=1, color=(ON_RSI ? color.new(color.blue, 60) : na))
hline(50, title="Middle Line", color=#787B86)
obLevel = hline(80, title="Overbought", color=#787B86, linestyle=hline.style_dotted)
osLevel = hline(20, title="Oversold", color=#787B86, linestyle=hline.style_dotted)
fill(obLevel, osLevel, title="Background", color=color.rgb(33, 150, 243, 90))

//Regular Bullish Pivot Point

Pivot_Low_Found_Regular_Bullish = na(ta.pivotlow(MFI, Lookback_Left, Lookback_Right)) ? false : true
_in_Range_Regular_Bullish(cond) =>
	bars = ta.barssince(cond == true)
	rangeLower <= bars and bars <= rangeUpper

//Detect Regular Bullish Divergence

MFI_Higher_Low_Regular_Bullish = MFI[Lookback_Right] > ta.valuewhen(Pivot_Low_Found_Regular_Bullish, MFI[Lookback_Right], 1) and _in_Range_Regular_Bullish(Pivot_Low_Found_Regular_Bullish[1])
Price_Lower_Low_Regular_Bullish = low[Lookback_Right] < ta.valuewhen(Pivot_Low_Found_Regular_Bullish, low[Lookback_Right], 1)
condition_Regular_Bullish = plotBull and MFI_Higher_Low_Regular_Bullish and Price_Lower_Low_Regular_Bullish and Pivot_Low_Found_Regular_Bullish

//Plotting Regular Bullish Divergence

plot(Pivot_Low_Found_Regular_Bullish ? MFI[Lookback_Right] : na, offset=-Lookback_Right, title="Bull", linewidth=1, color=(condition_Regular_Bullish ? bullColor : noneColor))

plotshape(condition_Regular_Bullish ? MFI[Lookback_Right] : na, offset=-Lookback_Right, title="Regular Bullish Label", text=" Bull ", style=shape.labeldown, location=location.absolute, color=bullColor, textcolor=textColor)

//Hidden Bullish Pivot Point

Pivot_Low_Found_Hidden_Bullish = na(ta.pivotlow(MFI, Lookback_Left, Lookback_Right)) ? false : true
_in_Range_Hidden_Bullish(cond) =>
	bars = ta.barssince(cond == true)
	rangeLower <= bars and bars <= rangeUpper

//Detect Hidden Bullish Divergence

MFI_Lower_Low_Hidden_Bullish = MFI[Lookback_Right] < ta.valuewhen(Pivot_Low_Found_Hidden_Bullish, MFI[Lookback_Right], 1) and _in_Range_Hidden_Bullish(Pivot_Low_Found_Hidden_Bullish[1])
Price_Higher_Low_Hidden_Bullish = low[Lookback_Right] > ta.valuewhen(Pivot_Low_Found_Hidden_Bullish, low[Lookback_Right], 1)
condition_Hidden_Bullish = plotHiddenBull and MFI_Lower_Low_Hidden_Bullish and Price_Higher_Low_Hidden_Bullish and Pivot_Low_Found_Hidden_Bullish

//Plotting Hidden Bullish Divergence

plot(Pivot_Low_Found_Hidden_Bullish ? MFI[Lookback_Right] : na, offset=-Lookback_Right, title="HBull", linewidth=1, color=(condition_Hidden_Bullish ? hbullColor : noneColor))

plotshape(condition_Hidden_Bullish ? MFI[Lookback_Right] : na, offset=-Lookback_Right, title="Hidden Bullish Label", text=" HBull ", style=shape.labeldown, location=location.absolute, color=hbullColor, textcolor=textColor)

//Regular Bearish Pivot Point

Pivot_High_Found_Regular_Bearish = na(ta.pivothigh(MFI, Lookback_Left, Lookback_Right)) ? false : true
_in_Range_Regular_Bearish(cond) =>
	bars = ta.barssince(cond == true)
	rangeLower <= bars and bars <= rangeUpper

//Detect Regular Bearish Divergence

MFI_Lower_High_Regular_Bearish = MFI[Lookback_Right] < ta.valuewhen(Pivot_High_Found_Regular_Bearish, MFI[Lookback_Right], 1) and _in_Range_Regular_Bearish(Pivot_High_Found_Regular_Bearish[1])
Price_Higher_High_Regular_Bearish = high[Lookback_Right] > ta.valuewhen(Pivot_High_Found_Regular_Bearish, high[Lookback_Right], 1)
condition_Regular_Bearish = plotBear and MFI_Lower_High_Regular_Bearish and Price_Higher_High_Regular_Bearish and Pivot_High_Found_Regular_Bearish

//Plotting Regular Bearish Divergence

plot(Pivot_High_Found_Regular_Bearish ? MFI[Lookback_Right] : na, offset=-Lookback_Right, title="Bear", linewidth=1, color=(condition_Regular_Bearish ? bearColor : noneColor))

plotshape(condition_Regular_Bearish ? MFI[Lookback_Right] : na, offset=-Lookback_Right, title="Regular Bearish Label", text=" Bear ", style=shape.labeldown, location=location.absolute, color=bearColor, textcolor=textColor)

//Hidden Bearish Pivot Point

Pivot_High_Found_Hidden_Bearish = na(ta.pivothigh(MFI, Lookback_Left, Lookback_Right)) ? false : true
_in_Range_Hidden_Bearish(cond) =>
	bars = ta.barssince(cond == true)
	rangeLower <= bars and bars <= rangeUpper

//Detect Hidden Bearish Divergence

MFI_Higher_High_Hidden_Bearish = MFI[Lookback_Right] > ta.valuewhen(Pivot_High_Found_Hidden_Bearish, MFI[Lookback_Right], 1) and _in_Range_Hidden_Bearish(Pivot_High_Found_Hidden_Bearish[1])
Price_Lower_High_Hidden_Bear = high[Lookback_Right] < ta.valuewhen(Pivot_High_Found_Hidden_Bearish, high[Lookback_Right], 1)
condition_Hidden_Bearish = plotHiddenBear and MFI_Higher_High_Hidden_Bearish and Price_Lower_High_Hidden_Bear and Pivot_High_Found_Hidden_Bearish

//Plotting Hidden Bearish Divergence

plot(Pivot_High_Found_Hidden_Bearish ? MFI[Lookback_Right] : na, offset=-Lookback_Right, title="HBear", linewidth=1, color=(condition_Hidden_Bearish ? hbearColor : noneColor))

plotshape(condition_Hidden_Bearish ? MFI[Lookback_Right] : na, offset=-Lookback_Right, title="Hidden Bearish Label", text=" HBear ", style=shape.labeldown, location=location.absolute, color=hbearColor, textcolor=textColor)

