MACD divergence signal.1

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

//@version=5

indicator(title='MACD divergence signal', timeframe='')


// Getting inputs

fast_length = input(title='Fast Length', defval=12)


slow_length = input(title='Slow Length', defval=26)
src = input(title='Source', defval=close)
signal_length = input.int(title='Signal Smoothing', minval=1, maxval=50, defval=9)
sma_source = input.string(title='Oscillator MA Type', defval='EMA', options=['SMA',
'EMA'])
sma_signal = input.string(title='Signal Line MA Type', defval='EMA',
options=['SMA', 'EMA'])
// Plot colors
col_macd = input.color(#2962FF, 'MACD Line ', group='Color Settings',
inline='MACD')
col_signal = input.color(#FF6D00, 'Signal Line ', group='Color Settings',
inline='Signal')
col_grow_above = input.color(#26A69A, 'Above Grow', group='Histogram',
inline='Above')
col_fall_above = input.color(#B2DFDB, 'Fall', group='Histogram', inline='Above')
col_grow_below = input.color(#FFCDD2, 'Below Grow', group='Histogram',
inline='Below')
col_fall_below = input.color(#FF5252, 'Fall', group='Histogram', inline='Below')
// Calculating
fast_ma = sma_source == 'SMA' ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == 'SMA' ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == 'SMA' ? ta.sma(macd, signal_length) : ta.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(ta.pivotlow(osc, lbL, lbR)) ? false : true


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

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

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


osc[lbR] < 0

// Price: Lower Low

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


priceHHZero = ta.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=color.new(bullColor, 0), textcolor=color.new(textColor, 0))

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

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

// Price: Higher Low

priceHL = low[lbR] > ta.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=color.new(bullColor, 0), textcolor=color.new(textColor, 0))

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

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


osc[lbR] > 0

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


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

// Price: Higher High

priceHH = high[lbR] > ta.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=color.new(bearColor, 0), textcolor=color.new(textColor, 0))

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

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

// Price: Lower High

priceLH = high[lbR] < ta.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=color.new(bearColor, 0), textcolor=color.new(textColor, 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy