Page 1 of 1

Divergence detection (3 Pivot points)

Posted: Wed Feb 22, 2023 1:36 pm
by DonBastardo
Hi guys,

I am trying to find a RSI divergence setup that works in combinations with ma, chart pattern , SR Levlels etc - not sure yet which combinations works best but thats why i am looking to code the first step in pine script to help me with backtesting.

The basic idea is to take the following divergence setup as the base for further analysis:

https://imgbox.com/HjmTJ3Ly

Rules for this setup:

1. This setup is reversal only so no need to plot hidden divergences
2. Between point A and B is a divergence and between point B and C is a divergence and between A and C is also a divergence

3. Very Important
3.1: on Price: point B is below A, and C is below A and B (only close price)
3.2: on RSI: point B is above A, and C is above A and B (meaning if point C is between A and B than the setup is invalid even if 3.1 is valid)
3.3: on Price: Furthermore setup is only valid if a candle closes above point A after point C has formed

4. It should be possible to configure within how many bars this setup should occur (point A to point D)


I would take the following public script: https://www.tradingview.com/script/CaMo ... e-Pine-v4/ by kingthies since this should have most of what I need. I think whats missing is to "only" include checks for point C and D.

I dont mind coding it myself but I need some pointers - I appreciate any help you can give me.

Thanks,
Don

Code: Select all

// @version=5
// { THIS SCRIPT IS SUBJECT TO THE TERMS OF THE MOZILLA PUBLIC LICENSE 2.0 (HTTPS://MOZILLA.ORG/MPL/2.O)
// © 2022 KINGTHIES }

indicator(title='RSI Divergence',shorttitle='RSI Div-KT', overlay=false, timeframe='')

// {FUNCTION
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
//} 

//{USER MENU
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")
//}

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"

// {DIVERGENCE INPUT
lbR = 5,lbL = 5, rangeUpper = 60, rangeLower = 5
plotBull = input(title='Plot Bullish', defval=true,group="Toggle Divergence Plotting"), plotBear = input(title='Plot Bearish', defval=true)
plotHiddenBull = input(title='Plot Hidden Bullish', defval=false), plotHiddenBear = input(title='Plot Hidden Bearish', defval=false)
// } 

osc = rsi
plFound = na(ta.pivotlow(osc, lbL, lbR)) ? false : true,phFound = na(ta.pivothigh(osc, lbL, lbR)) ? false : true

// {RSI TREND IDENTIFICATION 
_inRange(cond) =>
    bars = ta.barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper

oscHL = osc[lbR] > ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1]), oscLL = osc[lbR] < ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
oscHH = osc[lbR] > ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1]),oscLH = osc[lbR] < ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// }

// {PRICE TREND IDENTIFICATION
priceLL = low[lbR] < ta.valuewhen(plFound, low[lbR], 1), priceHL = low[lbR] > ta.valuewhen(plFound, low[lbR], 1)
priceHH = high[lbR] > ta.valuewhen(phFound, high[lbR], 1),priceLH = high[lbR] < ta.valuewhen(phFound, high[lbR], 1)
//}

//{DIVERGENCE IDENTIFICATION
bullCond = plotBull and priceLL and oscHL and plFound, hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound
bearCond = plotBear and priceHH and oscLH and phFound, hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound
// } 

// { COLOR ASSIGNMENT 
bearColor = color.red, bullColor = color.green, hiddenBullColor = color.new(color.green, 0), hiddenBearColor = color.new(color.red, 0)
textColor = color.white,noneColor = color.new(color.white, 100)
//}

// { PLOTS  
plot(osc, title='RSI', linewidth=1, color=color.rgb(128,128,128))
plot(rsiMA, "RSI-based MA", color=color.yellow)
rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(30, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper Bollinger Band", color=color.green)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower Bollinger Band", color=color.green)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill")
plot(plFound ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=3, color=bullCond ? bullColor : noneColor, transp=0)
plot(plFound ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=3, color=hiddenBullCond ? hiddenBullColor : noneColor, transp=0)
plot(phFound ? osc[lbR] : na, offset=-lbR, title='Regular Bearish', linewidth=3, color=bearCond ? bearColor : noneColor, transp=0)
plot(phFound ? osc[lbR] : na, offset=-lbR, title='Hidden Bearish', linewidth=3, color=hiddenBearCond ? hiddenBearColor : noneColor, transp=0)
// }
// {ALERTS 
alertcondition(bullCond,title='Bullish Divergence',message='Bullish Divergence Detected on {{ticker}}')
alertcondition(hiddenBullCond,title='Hidden Bull Divergence',message='Hidden Bull Divergence Detected on {{ticker}}')
alertcondition(bearCond,title='Bearish Divergence',message='Bearish Divergence Detected on {{ticker}}')
alertcondition(hiddenBearCond,title='Hidden Bear Divergence',message='Hidden Bearish Divergence Detected on {{ticker}}')
// } 


Re: Divergence detection (3 Pivot points)

Posted: Thu Feb 23, 2023 2:23 am
by Steve Burman
My suggestion is to contact Eric Thies who wrote the RSI Divergence indicator and get his thoughts on your questions.

Re: Divergence detection (3 Pivot points)

Posted: Tue Mar 14, 2023 6:22 am
by DonBastardo
Thanks Steve, I will do that.

Re: Divergence detection (3 Pivot points)

Posted: Mon Mar 27, 2023 5:19 am
by Steve Burman
Hi Don,

Did you get a reply from Eric Thies to your query?