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

//@version=4
study("MACD Advanced",shorttitle="MACD Adv", resolution="",overlay=false,format=format.price)

// Getting inputs
fast_length = input(title="Fast Length", type=input.integer, defval=12)
slow_length = input(title="Slow Length", type=input.integer, defval=26)
src = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source = input(title="Oscillator MA Type", type=input.string, defval="EMA", options=["SMA", "EMA"])
sma_signal = input(title="Signal Line MA Type", type=input.string, defval="EMA", options=["SMA", "EMA"])
// Plot colors
col_macd = input(#2962FF, "MACD Line  ", input.color, group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line  ", input.color, group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "Above   Grow", input.color, group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", input.color, group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", input.color, group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", input.color, group="Histogram", inline="Below")
//divergence
len = input(title="Divergence Checker Period", minval=1, defval=14)
lbR = input(title="Pivot Lookback Right", defval=5)
lbL = input(title="Pivot Lookback Left", defval=5)
rangeUpper = input(title="Max of Lookback Range", defval=60)
rangeLower = input(title="Min of Lookback Range", defval=5)


// Calculating
fast_ma = sma_source == "SMA" ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source == "SMA" ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? sma(macd, signal_length) : ema(macd, signal_length)


//divergence
osc = macd


plFound = na(pivotlow(osc, lbL, lbR)) ? false : true
phFound = na(pivothigh(osc, lbL, lbR)) ? false : true
_inRange(cond) =>
	bars = barssince(cond == true)
	rangeLower <= bars and bars <= rangeUpper

//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low

oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Lower Low

priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)
bullCond =  priceLL and oscHL and plFound


//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low

oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Higher Low

priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)
hiddenBullCond = priceHL and oscLL and plFound

//------------------------------------------------------------------------------
// Regular Bearish
// Osc: Lower High

oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Higher High

priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)

bearCond =  priceHH and oscLH and phFound



//------------------------------------------------------------------------------
// Hidden Bearish
// Osc: Higher High

oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Lower High

priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)

hiddenBearCond = priceLH and oscHH and phFound

crosover= crossover(macd,signal)
crosunder=crossunder(macd,signal)

var bool strong_bull=false
var bool bull=false
var bool bear=false
var bool strong_bear=false

strong_bull:=crosover and bullCond

strong_bear:=crosunder and bearCond


bull:=crosover and not hiddenBearCond and macd<0 and signal<0

bear:=crosunder and not hiddenBullCond and macd>0 and signal>0


hist = macd - signal
plot(0)
plot(macd, title="MACD", color=col_macd)
plot(signal, title="Signal", color=col_signal)
plotshape(bull?true: na,text="Bullish Signal",size=size.tiny,offset=0,color=color.green,textcolor=color.green,style=shape.flag,location=location.absolute)
plotshape(bear?true: na,text="Bearish Signal",location=location.absolute,size=size.tiny,offset=0,color=color.red,textcolor=color.red)
plotshape(strong_bull?true: na,text="Strong Bullish Signal",style=shape.flag,size=size.tiny,offset=2,color=color.green,textcolor=color.green,location=location.absolute)
plotshape(strong_bear?true: na,text="Strong Bearish Signal",style=shape.flag,size=size.tiny,offset=2,color=color.red,textcolor=color.red,location=location.absolute)


