0% found this document useful (0 votes)
175 views4 pages

MACD Divergence

This document defines a study for identifying divergences between a MACD oscillator and price using pivot highs and lows on the oscillator. It calculates regular and hidden bullish and bearish divergences, and plots the pivots and divergence signals on the chart along with alert conditions. User inputs allow customizing the fast/slow lengths, signal smoothing, and lookback parameters for the pivots and divergence detection.

Uploaded by

BiantoroKunarto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
175 views4 pages

MACD Divergence

This document defines a study for identifying divergences between a MACD oscillator and price using pivot highs and lows on the oscillator. It calculates regular and hidden bullish and bearish divergences, and plots the pivots and divergence signals on the chart along with alert conditions. User inputs allow customizing the fast/slow lengths, signal smoothing, and lookback parameters for the pivots and divergence detection.

Uploaded by

BiantoroKunarto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

//@version=4

study(title="MACD Divergences by @DaviddTech", resolution="")


// 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")
// 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)
hist = macd - signal
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] <
hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below :
col_fall_below)))
plot(macd, title="MACD", color=col_macd)
plot(signal, title="Signal", color=col_signal)

donttouchzero = input(title="Don't touch the zero line?", defval=true)

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)
plotBull = input(title="Plot Bullish", defval=true)
plotHiddenBull = false
plotBear = input(title="Plot Bearish", defval=true)
plotHiddenBear = false
bearColor = color.red
bullColor = color.green
hiddenBullColor = color.new(color.green, 80)
hiddenBearColor = color.new(color.red, 80)
textColor = color.white
noneColor = color.new(color.white, 100)
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]) and


osc[lbR] < 0

// Price: Lower Low

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


priceHHZero = highest(osc, lbL+lbR+5)
//plot(priceHHZero,title="priceHHZero",color=color.green)
blowzero = donttouchzero ? priceHHZero < 0 : true
bullCond = plotBull and priceLL and oscHL and plFound and blowzero

plot(
plFound ? osc[lbR] : na,
offset=-lbR,
title="Regular Bullish",
linewidth=2,
color=(bullCond ? bullColor : noneColor),
transp=0
)

plotshape(
bullCond ? osc[lbR] : na,
offset=-lbR,
title="Regular Bullish Label",
text=" Bull ",
style=shape.labelup,
location=location.absolute,
color=bullColor,
textcolor=textColor,
transp=0
)

//------------------------------------------------------------------------------
// 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 = plotHiddenBull and priceHL and oscLL and plFound

plot(
plFound ? osc[lbR] : na,
offset=-lbR,
title="Hidden Bullish",
linewidth=2,
color=(hiddenBullCond ? hiddenBullColor : noneColor),
transp=0
)
plotshape(
hiddenBullCond ? osc[lbR] : na,
offset=-lbR,
title="Hidden Bullish Label",
text=" H Bull ",
style=shape.labelup,
location=location.absolute,
color=bullColor,
textcolor=textColor,
transp=0
)

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

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


osc[lbR] > 0

priceLLZero = lowest(osc, lbL+lbR+5)


//plot(priceLLZero,title="priceLLZero", color=color.red)
bearzero = donttouchzero ? priceLLZero > 0 : true

// Price: Higher High

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

bearCond = plotBear and priceHH and oscLH and phFound and bearzero

plot(
phFound ? osc[lbR] : na,
offset=-lbR,
title="Regular Bearish",
linewidth=2,
color=(bearCond ? bearColor : noneColor),
transp=0
)

plotshape(
bearCond ? osc[lbR] : na,
offset=-lbR,
title="Regular Bearish Label",
text=" Bear ",
style=shape.labeldown,
location=location.absolute,
color=bearColor,
textcolor=textColor,
transp=0
)

//------------------------------------------------------------------------------
// 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 = plotHiddenBear and priceLH and oscHH and phFound

plot(
phFound ? osc[lbR] : na,
offset=-lbR,
title="Hidden Bearish",
linewidth=2,
color=(hiddenBearCond ? hiddenBearColor : noneColor),
transp=0
)

plotshape(
hiddenBearCond ? osc[lbR] : na,
offset=-lbR,
title="Hidden Bearish Label",
text=" H Bear ",
style=shape.labeldown,
location=location.absolute,
color=bearColor,
textcolor=textColor,
transp=0
)

alertcondition(bullCond, title="Bull", message="Regular Bull Div {{ticker}} XXmin")


alertcondition(bearCond, title="Bear", message="Regular Bear Div {{ticker}} XXmin")
alertcondition(hiddenBullCond, title="H Bull", message="Hidden Bull Div {{ticker}}
XXmin")
alertcondition(hiddenBearCond, title="H Bear", message="Hidden Bear Div {{ticker}}
XXmin")

You might also like