Find MACD Divergence with specific MACD proportions
Posted: Mon Mar 07, 2022 8:20 pm
0
Hello!
I'm a total beginner... The code below finds a MACD Divergence when the height of B is no more than 1/2 the height of A (see image in the link below). No matter what I try, I cannot find a way to make the code find the divergence only when also C is no more than 1/2 the height of A. Anybody can help me with this?
https://i.stack.imgur.com/WRPwB.png
Hello!
I'm a total beginner... The code below finds a MACD Divergence when the height of B is no more than 1/2 the height of A (see image in the link below). No matter what I try, I cannot find a way to make the code find the divergence only when also C is no more than 1/2 the height of A. Anybody can help me with this?
https://i.stack.imgur.com/WRPwB.png
Code: Select all
//@version=5
indicator(title='MACD Divergence', shorttitle='MACD Divergence')
//MACD Histogram
source = close
fastLength = input.int(12, minval=1, title='Fast Length'), slowLength = input.int(26, minval=1, title='Slow Length')
signalLength = input.int(9, minval=1, title='Signal Length')
fastMA = ta.ema(source, fastLength)
slowMA = ta.ema(source, slowLength)
macd = fastMA - slowMA
signal = ta.sma(macd, signalLength)
hist = macd - signal
outMACD = macd
outSignal = signal
outHist = hist
histA = outHist < outHist[1] and outHist > 0
histB = outHist > outHist[1] and outHist > 0
histC = outHist > outHist[1] and outHist <= 0
histD = outHist < outHist[1] and outHist <= 0
//MACD Histogram Colors
macd_above = outMACD >= outSignal
macd_below = outMACD < outSignal
plot_color = histA ? #B2DFDB : histB ? #26A69A : histC ? #FFCDD2 : histD ? #FF5252 : na
macd_color = #2962FF
signal_color = #FF6D00
plot(outMACD ? outMACD : na, title='MACD Line', color=color.new(macd_color, 0), linewidth=1)
plot(outSignal ? outSignal : na, title='Signal Line', color=color.new(signal_color, 0), style=plot.style_line, linewidth=1)
plot(outHist ? outHist : na, title='Histogram', color=plot_color, style=plot.style_columns, linewidth=1)
//Bullish Divergence
bullHistB = outHist
bullHistB := outHist < 0 and outHist[1] > 0 ? outHist : outHist > 0 ? 0 : outHist < bullHistB[1] ? outHist : bullHistB[1]
bullHistA = bullHistB
bullHistA := bullHistB == 0 and bullHistB[1] < 0 ? bullHistB[1] : bullHistA[1]
bullPriceB_high = high
bullPriceB_high := bullHistB == 0 ? 0 : bullHistB < bullHistB[1] ? high : bullPriceB_high[1]
bullPriceB_low = low
bullPriceB_low := bullHistB == 0 ? 0 : bullHistB < bullHistB[1] ? low : bullPriceB_low[1]
bullPriceA_low = bullPriceB_low
bullPriceA_low := bullPriceB_low == 0 and bullPriceB_low[1] > 0 ? bullPriceB_low[1] : bullPriceA_low[1]
bullDiv = bullHistA[1] * 0.5 < bullHistB[1] and bullPriceA_low[1] > bullPriceB_high[1] and (bullHistB < 0 and bullHistB == bullHistB[1] and bullHistB != bullHistB[2] or bullHistB == 0 and outHist[1] == bullHistA)
bgcolor(bullDiv ? color.lime : na, offset=-1, title='Bullish Divergence')