RicardoSantos

[RS]Price Divergence Detector V2

UPDATE: added CCI to the list of oscilators.
Remove from Favorite Scripts Add to Favorite Scripts
study(title='[RS]Price Divergence Detector V2', shorttitle='D', overlay=true)
//  ||  General Input:
method = input(title='Method (0=rsi, 1=macd, 2=stoch, 3=volume, 4=acc/dist, 5=fisher, 6=cci):', type=integer, defval=0, minval=0, maxval=6)
SHOW_LABEL = input(title='Show Labels?', type=bool, defval=false)
SHOW_CHANNEL = input(title='Show Channel?', type=bool, defval=false)
//  ||  RSI / STOCH / VOLUME / ACC/DIST Input:
rsi_smooth = input(title='RSI/STOCH/Volume/ACC-DIST/Fisher/cci Smooth:', type=integer, defval=5)
//  ||  MACD Input:
macd_src = input(title='MACD Source:', type=source, defval=close)
macd_fast = input(title='MACD Fast:', type=integer, defval=12)
macd_slow = input(title='MACD Slow:', type=integer, defval=26)
macd_smooth = input(title='MACD Smooth Signal:', type=integer, defval=9)
//  ||  Functions:
f_top_fractal(_src)=>_src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0]
f_bot_fractal(_src)=>_src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0]
f_fractalize(_src)=>f_top_fractal(_src) ? 1 : f_bot_fractal(_src) ? -1 : 0

//  ||••>   START MACD FUNCTION
f_macd(_src, _fast, _slow, _smooth)=>
    _fast_ma = sma(_src, _fast)
    _slow_ma = sma(_src, _slow)
    _macd = _fast_ma-_slow_ma
    _signal = ema(_macd, _smooth)
    _hist = _macd - _signal
//  ||<••   END MACD FUNCTION

//  ||••>   START ACC/DIST FUNCTION
f_accdist(_smooth)=>_return=sma(cum(close==high and close==low or high==low ? 0 : ((2*close-low-high)/(high-low))*volume), _smooth)
//  ||<••   END ACC/DIST FUNCTION

//  ||••>   START FISHER FUNCTION
f_fisher(_src, _window)=>
    _h = highest(_src, _window)
    _l = lowest(_src, _window)
    _value0 = .66 * ((_src - _l) / max(_h - _l, .001) - .5) + .67 * nz(_value0[1])
    _value1 = _value0 > .99 ? .999 : _value0 < -.99 ? -.999 : _value0
    _fisher = .5 * log((1 + _value1) / max(1 - _value1, .001)) + .5 * nz(_fisher[1])
//  ||<••   END FISHER FUNCTION

method_high = method == 0 ? rsi(high, rsi_smooth) :
        method == 1 ? f_macd(macd_src, macd_fast, macd_slow, macd_smooth) :
        method == 2 ? stoch(close, high, low, rsi_smooth) :
        method == 3 ? sma(volume, rsi_smooth) :
        method == 4 ? f_accdist(rsi_smooth) :
        method == 5 ? f_fisher(high, rsi_smooth) :
        method == 6 ? cci(high, rsi_smooth) :
        na
method_low = method == 0 ? rsi(low, rsi_smooth) :
        method == 1 ? f_macd(macd_src, macd_fast, macd_slow, macd_smooth) :
        method == 2 ? stoch(close, high, low, rsi_smooth) :
        method == 3 ? sma(volume, rsi_smooth) :
        method == 4 ? f_accdist(rsi_smooth) :
        method == 5 ? f_fisher(low, rsi_smooth) :
        method == 6 ? cci(low, rsi_smooth) :
        na

fractal_top = f_fractalize(method_high) > 0 ? method_high[2] : na
fractal_bot = f_fractalize(method_low) < 0 ? method_low[2] : na

high_prev = valuewhen(fractal_top, method_high[2], 1) 
high_price = valuewhen(fractal_top, high[2], 1)
low_prev = valuewhen(fractal_bot, method_low[2], 1) 
low_price = valuewhen(fractal_bot, low[2], 1)

regular_bearish_div = fractal_top and high[2] > high_price and method_high[2] < high_prev
hidden_bearish_div = fractal_top and high[2] < high_price and method_high[2] > high_prev
regular_bullish_div = fractal_bot and low[2] < low_price and method_low[2] > low_prev
hidden_bullish_div = fractal_bot and low[2] > low_price and method_low[2] < low_prev

plot(title='H F', series=fractal_top ? high[2] : na, color=regular_bearish_div or hidden_bearish_div ? maroon : not SHOW_CHANNEL ? na : silver, offset=-2)
plot(title='L F', series=fractal_bot ? low[2] : na, color=regular_bullish_div or hidden_bullish_div ? green : not SHOW_CHANNEL ? na : silver, offset=-2)
plot(title='H D', series=fractal_top ? high[2] : na, style=circles, color=regular_bearish_div or hidden_bearish_div ? maroon : not SHOW_CHANNEL ? na : silver, linewidth=3, offset=-2)
plot(title='L D', series=fractal_bot ? low[2] : na, style=circles, color=regular_bullish_div or hidden_bullish_div ? green : not SHOW_CHANNEL ? na : silver, linewidth=3, offset=-2)

plotshape(title='+RBD', series=not SHOW_LABEL ? na : regular_bearish_div ? high[2] : na, text='R', style=shape.labeldown, location=location.absolute, color=maroon, textcolor=white, offset=-2)
plotshape(title='+HBD', series=not SHOW_LABEL ? na : hidden_bearish_div ? high[2] : na, text='H', style=shape.labeldown, location=location.absolute, color=maroon, textcolor=white, offset=-2)
plotshape(title='-RBD', series=not SHOW_LABEL ? na : regular_bullish_div ? low[2] : na, text='R', style=shape.labelup, location=location.absolute, color=green, textcolor=white, offset=-2)
plotshape(title='-HBD', series=not SHOW_LABEL ? na : hidden_bullish_div ? low[2] : na, text='H', style=shape.labelup, location=location.absolute, color=green, textcolor=white, offset=-2)
Ede
10 months ago
Thank you. What do H @R refer to ?
Reply
RicardoSantos PRO Ede
10 months ago
Hidden/Regular
+1 Reply
andreas73
10 months ago
Thanks RicardoSantos
Its very nice. great work.
Reply
accidentjev2
10 months ago
Muchas gracias, Ricardo! It's a gem. One question: how would you tweak the script for it to show weekly divergences on a daily timeframe?
Reply
RicardoSantos PRO accidentjev2
10 months ago
track the close time of the week and check the divergence with a security() then
Reply
accidentjev2 RicardoSantos
10 months ago
I tried adding the following...

tf = input(title='Timeframe', defval='W')
_src = security(tickerid, tf)

But it returns errors... Could you please make V2 MTF-able? TIA
Reply
RicardoSantos PRO accidentjev2
10 months ago
sure, not anytime soon tho lots of stuff todo :p
Reply
accidentjev2 RicardoSantos
10 months ago
Take your time, boss ;)
Reply
ramkirshnavarma
8 months ago
Ricardo,

I am testing Divergence detector version 2. It seems indicator is not detecting all the divergence. In the image(path), please check the arrow mark. Is there any solution to fix this. I have given oscillator length as 7 and Method 0.

https://s30.postimg.org/mxglxki75/Image.png
Reply
RicardoSantos PRO ramkirshnavarma
8 months ago
is the rsi using low as source on the oscillator?
Reply
ramkirshnavarma RicardoSantos
8 months ago
I have updated the source from CLOSE to LOW . Please see the image below. But still not showing. Is there any other setting to be fixed

https://s13.postimg.org/rx4jnjucn/Source.png
Reply
Sir is there any way to get this indicator for MT4...
Reply
OppLinks
2 months ago
This is priceless!! Thank RicardoSantos!
Reply
Ideas Scripts Chart
United States
United Kingdom
India
España
Italia
Brasil
Россия
Türkiye
日本
한국
Home Stock Screener Economic Calendar How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Priority Support Feature Request Blog & News FAQ Help & Wiki Twitter
Private Messages Chat Ideas Published Followers Following Priority Support Public Profile Profile Settings Billing Sign Out