MACD Modified
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Ron_C

//Code is built upon TradingView MACD Indicator

//@version=5
indicator("MACD Modified", shorttitle = "MACD MOD")

//------------ Inputs {
//MACD Inputs
macdType = input.string(title = "MACD Calculation", defval = "Traditional", options = ["Traditional","Modified"], group = "MACD")
fastLength = input(title="Fast Length", defval=12, group = "MACD")
slowLength = input(title="Slow Length", defval=26, group = "MACD")
src = input(title="Source", defval=close, group = "MACD")
signalLength = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9, group ="MACD")
smaSource = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"], group = "MACD")
smaSignal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"], group = "MACD")

// Plot colors
colMacd = input(#2962FF, "MACD Line  ", group="Color Settings", inline="MACD")
colSignal = input(#FF6D00, "Signal Line  ", group="Color Settings", inline="Signal")
colGrowAbove = input(#26A69A, "Above   Grow", group="Histogram", inline="Above")
colFallAbove = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
colGrowBelow = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
colFallBelow = input(#FF5252, "Fall", group="Histogram", inline="Below")
colBetweenOSOB = input(color.new(color.blue,70), title = "Between OB/OS", group = "Overbought/Oversold", inline = "Above")

//Overbought Oversold Type
obosStyle = input.string("Pivot", options = ["Pivot", "Bollinger Bands", "Previous High/Low"], title = "OB/OS Type", group = "Overbought/Oversold Condition")

//}


//------------ MACD {
fastMA = smaSource == "SMA" ? ta.sma(src, fastLength) : ta.ema(src, fastLength)
slowMA = smaSource == "SMA" ? ta.sma(src, slowLength) : ta.ema(src, slowLength)
macd = macdType == "Traditional" ? fastMA - slowMA : ((fastMA - slowMA) / slowMA)*100
signal = smaSignal == "SMA" ? ta.sma(macd, signalLength) : ta.ema(macd, signalLength)
hist = macd - signal

//Plots
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? colGrowAbove : colFallAbove) : (hist[1] < hist ? colGrowBelow : colFallBelow)))
plot(macd, title="MACD", color=colMacd)
plot(signal, title="Signal", color=colSignal)
//}


//------------ MACD Overbought and Oversold{
//Pivots Inputs
pivotHighLimit = input(0, title = "Pivots High Lower Limit", group = "Pivots")
pivotLowLimit = input(0, title = "Pivots Low Upper Limit", group = "Pivots")
sizeInput = input.int(10, "Array size", minval = 0, maxval = 100000, group = "Pivots")
pivotStrength = input.int(5, "Pivot Strength", minval = 0, maxval = 100000, group = "Pivots")

//Pivots Variables 
var pivotLowPlot = float(na)
var pivotLowCross = color(na)
var pivotHighPlot = float(na)
var pivotHighCross = color(na)
var pivotHighAvgArray = float(na)
var pivotLowAvgArray = float(na)

//Bollinger Band Variables
var middle = float(na)
var upper = float(na)
var lower = float(na)
var dev = float(na)
bbLength = input(200, title = "Length", group = "Bollinger Bands")
bbStdDev = input(2, title = "Number of Standard Deviations", group = "Bollinger Bands")
bbSource = input.string("MACD", title = "Source", options = ["MACD", "Signal"], group = "Bollinger Bands")
bbSrce = bbSource == "MACD" ? macd : signal

//Previous High/Low Variables
var pivotLowPlotHL = float(na)
var pivotHighPlotHL = float(na)

//Pivots Calcs
if obosStyle == "Pivot"
    var lowPivots = array.new_float(sizeInput)
    var highPivots = array.new_float(sizeInput)
    pivotLow = ta.pivotlow(macd, pivotStrength, pivotStrength)
    pivotHigh = ta.pivothigh(macd, pivotStrength, pivotStrength)
    if pivotLow < pivotLowLimit 
        pivotLowCross := color.new(color.red,0)
        if array.size(lowPivots) == sizeInput
            array.shift(lowPivots)
            array.push(lowPivots, pivotLow)
            pivotLowPlot := pivotLow
        else
            array.push(lowPivots, pivotLow)
    else
        pivotLowCross := color(na)
    if pivotHigh > pivotHighLimit
        pivotHighCross := color.new(color.green,0)
        if array.size(highPivots) == sizeInput
            array.shift(highPivots)
            array.push(highPivots, pivotHigh)
            pivotHighPlot := pivotHigh
        else
            array.push(highPivots, pivotHigh)
    else 
        pivotHighCross := color(na)
    pivotLowAvgArray := array.avg(lowPivots)
    pivotHighAvgArray := array.avg(highPivots)

//Bollinger Band Calcs
else if obosStyle == "Bollinger Bands"
    middle := ta.sma(bbSrce, bbLength)
    dev := bbStdDev * ta.stdev(bbSrce, bbLength)
    upper := middle + dev
    lower := middle - dev

//Previous High/Low
else
    pivotLow = ta.pivotlow(macd, pivotStrength, pivotStrength)
    pivotHigh = ta.pivothigh(macd, pivotStrength, pivotStrength)
    if pivotLow < 0 
        pivotLowPlotHL := pivotLow

    if pivotHigh > 0
        pivotHighPlotHL := pivotHigh

//}

//------------ MACD Overbought and Oversold Plots{ 
//Pivots
//Plot Crosses
plot(pivotLowPlot, title = "Oversold Marker", style = plot.style_cross, color = pivotLowCross, linewidth = 3, offset=-pivotStrength)
plot(pivotHighPlot, title = "Overbought Marker", style = plot.style_cross, color = pivotHighCross, linewidth = 3, offset=-pivotStrength)

//Plot Overbought and Oversold Lines
p1 = plot(pivotHighAvgArray, title = "Overbought Pivot", color=color.purple)
p2 =plot(pivotLowAvgArray, title = "Oversold Pivot", color=color.purple)
hline(0.0, color=color.black, linestyle=hline.style_dotted, linewidth=1)
fill(p1, p2, color = colBetweenOSOB)

//Bollinger Bands
plot(middle, color=color.yellow, title = "BB Middle")
p3 = plot(upper, color=color.blue, title = "BB Upper")
p4 = plot(lower, color=color.blue, title = "BB Lower")
fill(p3, p4, color = colBetweenOSOB)

//Previous High/Low
p5 = plot(pivotLowPlotHL, title = "Previous Low")
p6 = plot(pivotHighPlotHL, title = "Previous High")
fill(p5, p6, color = colBetweenOSOB)
//}