MACD 3D with Signals [Quantigenics]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Quantigenics

//@version=5

indicator("MACD 3D with Signals [Quantigenics]")
Length          = input(defval = 10,title = "Length")
FirstDotSignal  = input(defval = false,title = "Standard Signal")
YellowDotSignal = input(defval = true,title = "Momentum Crossover Signal")
UpColor         = input(defval = color.rgb( 16,776,960 ),title = "Up Color")
DownColor       = input(defval = color.red, title = "Down Color")
Trendline       = input(defval = color.rgb(113, 113, 113),title = "Trend Line") 

StandardDev(series float macd3, simple int len,simple int datatype )=>
    val = ta.variance(macd3,len,datatype)//VariancePS(macd3,len,datatype)//
    if(val>0)
        val := math.sqrt(val)
    else    
        val := 0
    val

int FastLength = math.round(Length * 1.2)
int SlowLength = math.round(Length * 2.5)
MACD3D = (ta.ema( hlc3, FastLength ) - ta.ema( hlc3, SlowLength ))* 100//++
Avg = ta.ema(MACD3D, Length)//++
SDev = StandardDev(MACD3D, Length, 1)//++
UB = Avg + 0.43 * SDev
LB = Avg - 0.43 * SDev


chncolor = color.gray
int UporDown = na
if (MACD3D > MACD3D[1])
    chncolor := UpColor 
    UporDown:=1
else 
    chncolor := DownColor
    UporDown:=0
bool Crossover = false
bool Crossunder = false
Crossover := Crossover[1]
Crossunder := Crossunder[1]
dist = math.abs(ta.max(MACD3D)-ta.min(MACD3D))*.01
buysignal = (not Crossover and MACD3D > UB and YellowDotSignal) or (UporDown!= UporDown[1] and UporDown[1] == 0 and FirstDotSignal)
if (not Crossover and MACD3D > UB)
    Crossover   := true
    Crossunder  := false
    chncolor    := YellowDotSignal ? color.yellow : chncolor 
    alert( "Macd 3D cross up ", alert.freq_once_per_bar)
sellsignal = (not Crossunder and MACD3D < LB and YellowDotSignal) or (UporDown!= UporDown[1] and UporDown[1] == 1 and FirstDotSignal)
if (not Crossunder and MACD3D < LB)
    Crossover := false
    Crossunder := true
    chncolor    := YellowDotSignal? color.yellow : chncolor
    alert( "Macd 3D cross down ", alert.freq_once_per_bar)


Med = ta.median(LB + UB, 3) / 2
DIFF = math.abs(Med - MACD3D)

float difplot = 0
plotcolor = color.green
if (MACD3D > Med and DIFF >= DIFF[1])
    difplot := DIFF
    plotcolor := color.green
else if (MACD3D > Med and DIFF < DIFF[1])
    difplot := DIFF
    plotcolor := color.rgb(43, 101, 45)
else if (MACD3D < Med and DIFF <= DIFF[1])
    difplot := -DIFF
    plotcolor := color.red
else if (MACD3D < Med and DIFF > DIFF[1] )
    difplot := -DIFF
    plotcolor := color.rgb(117, 40, 40)

plot(difplot, title = "Differential", color = plotcolor, style = plot.style_histogram,linewidth = 2) 
plot(Med, title = "Trendline",  color = Trendline)
plot(MACD3D,title =  "Macd 3D", color = chncolor, style = plot.style_circles,linewidth = 2)   
plotshape(buysignal? MACD3D-dist : na, 'Buy', shape.labelup,location=location.absolute, color=color.green, size=size.small, offset=0)
plotshape(sellsignal? MACD3D+dist : na, 'Sell', shape.labeldown,location=location.absolute, color=color.rgb(195, 57, 57), size=size.small, offset=0)
