Untitled

 avatar
unknown
plain_text
2 years ago
7.4 kB
8
Indexable
//@version=5

indicator(title='RSI/EMA & MACD DEMA & Divergence', shorttitle='RSI/EMA & MACD DEMA & Divergence', format=format.price, precision=2, timeframe='')





// RSI Divergence inputs

len = input.int(title="RSI Period", minval=1, defval=14)

src = input(title="RSI Source", defval=close)

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 = input(title="Plot Hidden Bullish", defval=false)

plotBear = input(title="Plot Bearish", defval=true)

plotHiddenBear = input(title="Plot Hidden Bearish", defval=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 = ta.rsi(src, len)



// Adjustable EMA period

emaPeriod = input.int(title="EMA Period", minval=1, defval=21)



// EMA Calculation

ema = ta.ema(osc, emaPeriod)



// Plot EMA

plot(ema, title="EMA", linewidth=2, color=color.red)



plot(osc, title="RSI", linewidth=2, color=color.white)

hline(50, title="Middle Line", color=#787B86, linestyle=hline.style_dotted)

obLevel = hline(70, title="Overbought", color=#787B86, linestyle=hline.style_dotted)

osLevel = hline(30, title="Oversold", color=#787B86, linestyle=hline.style_dotted)

fill(obLevel, osLevel, title="Background", color=color.rgb(33, 150, 243, 90))



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])



// Price: Lower Low



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

bullCondAlert = priceLL and oscHL and plFound

bullCond = plotBull and bullCondAlert



plot(

     plFound ? osc[lbR] : na,

     offset=-lbR,

     title="Regular Bullish",

     linewidth=2,

     color=(bullCond ? bullColor : noneColor)

     )



plotshape(

     bullCond ? osc[lbR] : na,

     offset=-lbR,

     title="Regular Bullish Label",

     text=" Bull ",

     style=shape.labelup,

     location=location.absolute,

     color=bullColor,

     textcolor=textColor

     )



//------------------------------------------------------------------------------

// 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)

hiddenBullCondAlert = priceHL and oscLL and plFound

hiddenBullCond = plotHiddenBull and hiddenBullCondAlert



plot(

     plFound ? osc[lbR] : na,

     offset=-lbR,

     title="Hidden Bullish",

     linewidth=2,

     color=(hiddenBullCond ? hiddenBullColor : noneColor)

     )



plotshape(

     hiddenBullCond ? osc[lbR] : na,

     offset=-lbR,

     title="Hidden Bullish Label",

     text=" H Bull ",

     style=shape.labelup,

     location=location.absolute,

     color=bullColor,

     textcolor=textColor

     )



//------------------------------------------------------------------------------

// Regular Bearish

// Osc: Lower High



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



// Price: Higher High



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



bearCondAlert = priceHH and oscLH and phFound

bearCond = plotBear and bearCondAlert



plot(

     phFound ? osc[lbR] : na,

     offset=-lbR,

     title="Regular Bearish",

     linewidth=2,

     color=(bearCond ? bearColor : noneColor)

     )



plotshape(

     bearCond ? osc[lbR] : na,

     offset=-lbR,

     title="Regular Bearish Label",

     text=" Bear ",

     style=shape.labeldown,

     location=location.absolute,

     color=bearColor,

     textcolor=textColor

     )





//------------------------------------------------------------------------------

// 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)



hiddenBearCondAlert = priceLH and oscHH and phFound

hiddenBearCond = plotHiddenBear and hiddenBearCondAlert



plot(

     phFound ? osc[lbR] : na,

     offset=-lbR,

     title="Hidden Bearish",

     linewidth=2,

     color=(hiddenBearCond ? hiddenBearColor : noneColor)

     )



plotshape(

     hiddenBearCond ? osc[lbR] : na,

     offset=-lbR,

     title="Hidden Bearish Label",

     text=" H Bear ",

     style=shape.labeldown,

     location=location.absolute,

     color=bearColor,

     textcolor=textColor

     )



alertcondition(bullCondAlert, title='Regular Bullish Divergence', message="Found a new Regular Bullish Divergence, `Pivot Lookback Right` number of bars to the left of the current bar")

alertcondition(hiddenBullCondAlert, title='Hidden Bullish Divergence', message='Found a new Hidden Bullish Divergence, `Pivot Lookback Right` number of bars to the left of the current bar')

alertcondition(bearCondAlert, title='Regular Bearish Divergence', message='Found a new Regular Bearish Divergence, `Pivot Lookback Right` number of bars to the left of the current bar')

alertcondition(hiddenBearCondAlert, title='Hidden Bearish Divergence', message='Found a new Hidden Bearish Divergence, `Pivot Lookback Right` number of bars to the left of the current bar')





//MACD DEMA

// Getting inputs

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

slow_length = input(title='Slow Length MACD', defval=26)

src_MACD = input(title='Source MACD', defval=close)

signal_length = input.int(title='Signal Smoothing', minval=1, maxval=50, defval=9)

sma_source = input(title='Simple MA (Oscillator)', defval=false)

sma_signal = input(title='Simple MA (Signal Line)', defval=false)



// Plot colors

col_grow_above = #26A69A

col_grow_below = #FFCDD2

col_fall_above = #B2DFDB

col_fall_below = #EF5350

col_macd = #d0ff00

col_signal = #ff0000



// Calculating DEMA

fast_dema = sma_source ? ta.sma(src_MACD, fast_length) : 2 * ta.ema(src_MACD, fast_length) - ta.ema(ta.ema(src_MACD, fast_length), fast_length)

slow_dema = sma_source ? ta.sma(src_MACD, slow_length) : 2 * ta.ema(src_MACD, slow_length) - ta.ema(ta.ema(src_MACD, slow_length), slow_length)

macd_dema = fast_dema - slow_dema

signal_dema = sma_signal ? ta.sma(macd_dema, signal_length) : 2 * ta.ema(macd_dema, signal_length) - ta.ema(ta.ema(macd_dema, signal_length), signal_length)

hist_dema = macd_dema - signal_dema



plot(1000 * hist_dema / src_MACD, title='Histogram DEMA', style=plot.style_columns, color=hist_dema >= 0 ? hist_dema[1] < hist_dema ? col_grow_above : col_fall_above : hist_dema[1] < hist_dema ? col_grow_below : col_fall_below, transp=0)

plot(1000 * macd_dema / src_MACD, title='MACD DEMA', color=color.new(col_macd, 0), linewidth=2)

plot(1000 * signal_dema / src_MACD, title='Signal DEMA', color=color.new(col_signal, 0), linewidth=2)

Editor is loading...
Leave a Comment