OBV – On Balance Volume Divergence
Forums › ProRealTime English forum › ProBuilder support › OBV – On Balance Volume Divergence
- This topic has 2 replies, 2 voices, and was last updated 3 years ago by bharathr15.
Tagged: Divergence, divergences, OBV, On Balance Volume
-
-
09/05/2021 at 7:13 AM #176798
Hi,
On Balance Volume Divergence indicator didn’t available in prorealtime. Below mention pine script code converter into probuilder. Please some one help on this thank you.
study(title=”OBV Divergence +”, overlay=false)
// This study Plots Divergences for OBV along with 2 MA’s
// Choose between standard type of OBV and OBV Accumulation / Distribution thanks to spacetrader which seems to be a truer reading
// Divergence Alerts are provided so that you dont have to stare at your screen all dayOBVType = input(defval=”OBV Accumulation / Distribution”, title=”OBV Type”, options=[“Standard”, “OBV Accumulation / Distribution”])
OBVSource = input(defval=”Open”, title=”OBV Source – Recommend Close for Standard Type and Open For Non Standard Type “, options=[“Open”, “Close”])OBVColorMenu = input(title=”OBV – Directional Color Changing”, type = bool, defval = false)
FastMAType = input(defval=”SMA”, title=”Fast MA Type”, options=[“SMA”, “EMA”])
FastMALength = input(title=”Fast MA Length”, type = integer, defval = 10)
SlowMAType = input(defval=”SMA”, title=”Slow MA Type”, options=[“SMA”, “EMA”])
SlowMALength = input(title=”Slow MA Length”, type = integer, defval = 20)
DivOffsetInput = input(title=”—— Divergences Offset to Realistic Entry Point ——“, type = bool, defval = true)
DivOffset = DivOffsetInput? 0 : -2showdivs1 = input(true, title=”1 – Regular Divergences – OBV”)
showhidden1 = input(true, title=”1 – Hidden Divergences – OBV”)
showlabels1 = input(true, title=”1 – Divergence Labels — OBV”)showdivs2 = input(false, title=”2 – Regular Divergences – Fast MA”)
showhidden2 = input(false, title=”2 – Hidden Divergences – Fast MA”)
showlabels2 = input(false, title=”2 – Divergence Labels — Fast MA”)showdivs3 = input(false, title=”3 – Regular Divergences – Slow MA”)
showhidden3 = input(false, title=”3 – Hidden Divergences – Slow MA”)
showlabels3 = input(false, title=”3 – Divergence Labels — Slow MA”)OBVStandard = OBVSource==”Open” ? cum(sign(change(open)) * volume) : cum(sign(change(close)) * volume)
OBVAccDist = OBVSource==”Open” ? cum(volume * ((close – open)/(high-low))) : cum(volume * ((close – nz(close[1]))/(high – low)))
OBV = OBVType==”Standard” ? OBVStandard : OBVAccDist
// MA Calculations
FastMA= if FastMAType==”SMA”
sma(OBV,FastMALength)
else
ema(OBV,FastMALength)SlowMA= if SlowMAType==”SMA”
sma(OBV,SlowMALength)
else
ema(OBV,SlowMALength)OBVColorChanging = OBV > OBV[1] ? green : red
OBVColorOut = OBVColorMenu ? OBVColorChanging : #0000AAsrc = close
plot(OBV, title = “OBV”, linewidth = 2, color=OBVColorOut, transp=0)
plot(FastMA, title = “Fast MA”, linewidth = 1, style = line, color = orange)
plot(SlowMA, title = “Slow MA”, linewidth = 1, style = line, color = blue)//////////////////////
// Divergences
Dinput1=OBV
Dinput2=FastMA
Dinput3=SlowMA//——————————
//@RicardoSantos’ Divergence Scriptf_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
//————————-// 1st set of Divergences
fractal_top1 = f_fractalize(Dinput1) > 0 ? Dinput1[2] : na
fractal_bot1 = f_fractalize(Dinput1) < 0 ? Dinput1[2] : nahigh_prev1 = valuewhen(fractal_top1, Dinput1[2], 0)[2]
high_price1 = valuewhen(fractal_top1, high[2], 0)[2]
low_prev1 = valuewhen(fractal_bot1, Dinput1[2], 0)[2]
low_price1 = valuewhen(fractal_bot1, low[2], 0)[2]regular_bearish_div1 = fractal_top1 and high[2] > high_price1 and Dinput1[2] < high_prev1
hidden_bearish_div1 = fractal_top1 and high[2] < high_price1 and Dinput1[2] > high_prev1
regular_bullish_div1 = fractal_bot1 and low[2] < low_price1 and Dinput1[2] > low_prev1
hidden_bullish_div1 = fractal_bot1 and low[2] > low_price1 and Dinput1[2] < low_prev1
//————————-col1 = showdivs1 and regular_bearish_div1 ? red : hidden_bearish_div1 and showhidden1 ? maroon : na
col2 = showdivs1 and regular_bullish_div1 ? #00FF00EB : hidden_bullish_div1 and showhidden1 ? green : na
//col3 = regular_bearish_div1 ? red : hidden_bearish_div1 and showhidden1 ? red : na
//col4 = regular_bullish_div1 ? green : hidden_bullish_div1 and showhidden1 ? green : naplot(title=’OBV – Bearish Divergences’, series=(showdivs1 or showhidden1) and fractal_top1 ? Dinput1[2] : na, color=col1, linewidth=3, offset=DivOffset)
plot(title=’OBV – Bullish Divergences’, series=(showdivs1 or showhidden1) and fractal_bot1 ? Dinput1[2] : na, color=col2, linewidth=3, offset=DivOffset)// plot(title=’1 Bearish Divergences’, series=(showdivs1 or showhidden1) and fractal_top1 ? Dinput1[2] : na, color=col1, linewidth=3, offset=DivOffset)
// plot(title=’1 Bullish Divergences’, series=(showdivs1 or showhidden1) and fractal_bot1 ? Dinput1[2] : na, color=col2, linewidth=3, offset=DivOffset)//plot(title=’1 H D’, series=(showdivs1 or showhidden1) and fractal_top1 ? Dinput1[2] : na, style=circles, color=col3, linewidth=3, offset=DivOffset)
//plot(title=’1 L D’, series=(showdivs1 or showhidden1) and fractal_bot1 ? Dinput1[2] : na, style=circles, color=col4, linewidth=3, offset=DivOffset)plotshape(title=’OBV – Regular Bearish Divergence’, series=showdivs1 and regular_bearish_div1 and showlabels1 ? Dinput1[2] : na, text=’R’, style=shape.labeldown, location=location.absolute, color=na, textcolor=#FF0000FA, offset=DivOffset)
plotshape(title=’OBV – Hidden Bearish Divergence’ , series=hidden_bearish_div1 and showhidden1 and showlabels1 ? Dinput1[2] : na, text=’H’, style=shape.labeldown, location=location.absolute, color=na, textcolor=#800000FA, offset=DivOffset)
plotshape(title=’OBV – Regular Bullish Divergence’, series=showdivs1 and regular_bullish_div1 and showlabels1 ? Dinput1[2] : na, text=’R’, style=shape.labelup, location=location.absolute, color=na, textcolor=#00FF00FA, offset=DivOffset)
plotshape(title=’OBV – Hidden Bullish Divergence’ , series=hidden_bullish_div1 and showhidden1 and showlabels1 ? Dinput1[2] : na, text=’H’, style=shape.labelup, location=location.absolute, color=na, textcolor=#008000FA, offset=DivOffset)// 2nd set of Divergences
fractal_top2 = f_fractalize(Dinput2) > 0 ? Dinput2[2] : na
fractal_bot2 = f_fractalize(Dinput2) < 0 ? Dinput2[2] : nahigh_prev2 = valuewhen(fractal_top2, Dinput2[2], 0)[2]
high_price2 = valuewhen(fractal_top2, high[2], 0)[2]
low_prev2 = valuewhen(fractal_bot2, Dinput2[2], 0)[2]
low_price2 = valuewhen(fractal_bot2, low[2], 0)[2]regular_bearish_div2 = fractal_top2 and high[2] > high_price2 and Dinput2[2] < high_prev2
hidden_bearish_div2 = fractal_top2 and high[2] < high_price2 and Dinput2[2] > high_prev2
regular_bullish_div2 = fractal_bot2 and low[2] < low_price2 and Dinput2[2] > low_prev2
hidden_bullish_div2 = fractal_bot2 and low[2] > low_price2 and Dinput2[2] < low_prev2
//————————-col5 = showdivs2 and regular_bearish_div2 ? red : hidden_bearish_div2 and showhidden2 ? maroon : na
col6 = showdivs2 and regular_bullish_div2 ? #00FF00EB : hidden_bullish_div2 and showhidden2 ? green : na
//col7 = regular_bearish_div2 ? red : hidden_bearish_div2 and showhidden2 ? red : na
//col8 = regular_bullish_div2 ? green : hidden_bullish_div2 and showhidden2 ? green : naplot(title=’Fast MA – Bearish Divergences’, series=(showdivs2 or showhidden2) and fractal_top2 ? Dinput2[2] : na, color=col5, linewidth=3, offset=DivOffset)
plot(title=’Fast MA – Bullish Divergences’, series=(showdivs2 or showhidden2) and fractal_bot2 ? Dinput2[2] : na, color=col6, linewidth=3, offset=DivOffset)// plot(title=’2 Bearish Divergences’, series=(showdivs2 or showhidden2) and fractal_top2 ? Dinput2[2] : na, color=col5, linewidth=3, offset=DivOffset)
// plot(title=’2 Bullish Divergences’, series=(showdivs2 or showhidden2) and fractal_bot2 ? Dinput2[2] : na, color=col6, linewidth=3, offset=DivOffset)//plot(title=’2 H D’, series=(showdivs2 or showhidden2) and fractal_top2 ? Dinput2[2] : na, style=circles, color=col3, linewidth=3, offset=DivOffset)
//plot(title=’2 L D’, series=(showdivs2 or showhidden2) and fractal_bot2 ? Dinput2[2] : na, style=circles, color=col4, linewidth=3, offset=DivOffset)plotshape(title=’Fast MA – Regular Bearish Divergence’, series=showdivs2 and regular_bearish_div2 and showlabels2 ? Dinput2[2] : na, text=’R’, style=shape.labeldown, location=location.absolute, color=na, textcolor=#FF0000FA, offset=DivOffset)
plotshape(title=’Fast MA – Hidden Bearish Divergence’ , series=hidden_bearish_div2 and showhidden2 and showlabels2 ? Dinput2[2] : na, text=’H’, style=shape.labeldown, location=location.absolute, color=na, textcolor=#800000FA, offset=DivOffset)
plotshape(title=’Fast MA – Regular Bullish Divergence’, series=showdivs2 and regular_bullish_div2 and showlabels2 ? Dinput2[2] : na, text=’R’, style=shape.labelup, location=location.absolute, color=na, textcolor=#00FF00FA, offset=DivOffset)
plotshape(title=’Fast MA – Hidden Bullish Divergence’ , series=hidden_bullish_div2 and showhidden2 and showlabels2 ? Dinput2[2] : na, text=’H’, style=shape.labelup, location=location.absolute, color=na, textcolor=#008000FA, offset=DivOffset)// 3rd set of Divergences
fractal_top3 = f_fractalize(Dinput3) > 0 ? Dinput3[2] : na
fractal_bot3 = f_fractalize(Dinput3) < 0 ? Dinput3[2] : nahigh_prev3 = valuewhen(fractal_top3, Dinput3[2], 0)[2]
high_price3 = valuewhen(fractal_top3, high[2], 0)[2]
low_prev3 = valuewhen(fractal_bot3, Dinput3[2], 0)[2]
low_price3 = valuewhen(fractal_bot3, low[2], 0)[2]regular_bearish_div3 = fractal_top3 and high[2] > high_price3 and Dinput3[2] < high_prev3
hidden_bearish_div3 = fractal_top3 and high[2] < high_price3 and Dinput3[2] > high_prev3
regular_bullish_div3 = fractal_bot3 and low[2] < low_price3 and Dinput3[2] > low_prev3
hidden_bullish_div3 = fractal_bot3 and low[2] > low_price3 and Dinput3[2] < low_prev3
//————————-col9 =showdivs3 and regular_bearish_div3 ? red : hidden_bearish_div3 and showhidden3 ? maroon : na
col10 =showdivs3 and regular_bullish_div3 ? #00FF00EB : hidden_bullish_div3 and showhidden3 ? green : na
//col11 = regular_bearish_div1 ? red : hidden_bearish_div1 and showhidden1 ? red : na
//col12 = regular_bullish_div1 ? green : hidden_bullish_div1 and showhidden1 ? green : naplot(title=’Slow MA – Bearish Divergences’, series=(showdivs1 or showhidden1) and fractal_top3 ? Dinput3[2] : na, color=col9, linewidth=3, offset=DivOffset)
plot(title=’Slow MA – Bullish Divergences’, series=(showdivs1 or showhidden1) and fractal_bot3 ? Dinput3[2] : na, color=col10, linewidth=3, offset=DivOffset)
//plot(title=’1 H D’, series=(showdivs1 or showhidden1) and fractal_top1 ? Dinput1[2] : na, style=circles, color=col3, linewidth=3, offset=DivOffset)
//plot(title=’1 L D’, series=(showdivs1 or showhidden1) and fractal_bot1 ? Dinput1[2] : na, style=circles, color=col4, linewidth=3, offset=DivOffset)plotshape(title=’Slow MA – Regular Bearish Divergence’, series=showdivs3 and regular_bearish_div3 and showlabels3 ? Dinput3[2] : na, text=’R’, style=shape.labeldown, location=location.absolute, color=na, textcolor=#FF0000FA, offset=DivOffset)
plotshape(title=’Slow MA – Hidden Bearish Divergence’ , series=hidden_bearish_div3 and showhidden3 and showlabels3 ? Dinput3[2] : na, text=’H’, style=shape.labeldown, location=location.absolute, color=na, textcolor=#800000FA, offset=DivOffset)
plotshape(title=’Slow MA – Regular Bullish Divergence’, series=showdivs3 and regular_bullish_div3 and showlabels3 ? Dinput3[2] : na, text=’R’, style=shape.labelup, location=location.absolute, color=na, textcolor=#00FF00FA, offset=DivOffset)
plotshape(title=’Slow MA – Hidden Bullish Divergence’ , series=hidden_bullish_div3 and showhidden3 and showlabels3 ? Dinput3[2] : na, text=’H’, style=shape.labelup, location=location.absolute, color=na, textcolor=#008000FA, offset=DivOffset)///////////////////////////////////////////////////////////////////////////////
// Divergence Alerts
// OBV Divergence Alerts
alertcondition(regular_bearish_div1, title=’Alert – OBV – Regular Bearish Divergence’, message=’OBV – Regular Bearish Divergence’)
alertcondition(hidden_bearish_div1, title=’Alert – OBV – Hidden Bearish Divergence’, message=’OBV – Hidden Bearish Divergence’)
alertcondition(regular_bullish_div1, title=’Alert – OBV – Regular Bullish Divergence’, message=’OBV – Regular Bullish Divergence’)
alertcondition(hidden_bullish_div1, title=’Alert – OBV – Hidden Bullish Divergence’, message=’OBV – Hidden Bullish Divergence’)// OBV Fast MA Divergence Alerts
alertcondition(regular_bearish_div2, title=’Alert-FastMA-Regular Bearish Divergence’, message=’OBV – Fast MA – Regular Bearish Divergence’)
alertcondition(hidden_bearish_div2, title=’Alert-FastMA-Hidden Bearish Divergence’, message=’OBV – Fast MA – Hidden Bearish Divergence’)
alertcondition(regular_bullish_div2, title=’Alert-FastMA-Regular Bullish Divergence’, message=’OBV – Fast MA – Regular Bullish Divergence’)
alertcondition(hidden_bullish_div2, title=’Alert-FastMA-Hidden Bullish Divergence’, message=’OBV – Fast MA – Hidden Bullish Divergence’)// OBV Slow MA Divergence Alerts
alertcondition(regular_bearish_div3, title=’Alert-FastMA-Regular Bearish Divergence’, message=’OBV – Slow MA – Regular Bearish Divergence’)
alertcondition(hidden_bearish_div3, title=’Alert-FastMA-Hidden Bearish Divergence’, message=’OBV – Slow MA – Hidden Bearish Divergence’)
alertcondition(regular_bullish_div3, title=’Alert-FastMA-Regular Bullish Divergence’, message=’OBV – Slow MA – Regular Bullish Divergence’)
alertcondition(hidden_bullish_div3, title=’Alert-FastMA-Hidden Bullish Divergence’, message=’OBV – Slow MA – Hidden Bullish Divergence’)09/06/2021 at 11:36 AM #176851This is not a conversion of your code, but a custom indicator made out of a MACD divergences indicatopr found in the library.
This is the one to be added BELOW your price chart, like OBV.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647/////////////////////////////////////////////// OBV divergences ON OBV//// based on://// MACD divergences on MACD// by Jose Callao// twitter @jose7674//N = 40 //N is the number of bars to look back for a divergence.// Normal values are 20-40. Must be the same in both indicators//miOBV = OBV(close)Offset = miOBV * 0.1IF (BarIndex > 10+1+N) THEN///divergencia bajistaIF (miOBV[1]>miOBV AND miOBV[1]>miOBV[2]) THENextremum2=miOBV[1]extremum1=highest[N](miOBV)preciomax2=close[1]preciomax=Highest[N](close)IF(extremum2<extremum1 AND preciomax2>preciomax[1]) THENfor i=1 to Nif miOBV[i]=extremum1 thenzz=idrawsegment (barindex[1], miOBV[1]+Offset, barindex[zz], miOBV[zz]+Offset) coloured(200,0,0)endifnextendifendif///divergencia alcistaIF (miOBV[1]<miOBV AND miOBV[1]<miOBV[2]) THENextremum22=miOBV[1]extremum11=lowest[N](miOBV)preciomin2=close[1]preciomin=lowest[N](close)IF(extremum22>extremum11 AND preciomin2<preciomin[1]) THENfor i2=1 to Nif miOBV[i2]=extremum11[1] thenzz2=i2drawsegment(barindex[1], miOBV[1]-Offset, barindex[zz2], miOBV[zz2]-Offset) coloured(0,200,0)endifnextENDIFENDIFendifreturn miOBV as "OBV", 0 as "0"This one is to be added ON your price chart, like moving averages:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647/////////////////////////////////////////////// OBV divergences ON Price//// based on://// MACD divergences on Price// by Jose Callao// twitter @jose7674//N = 40 //N is the number of bars to look back for a divergence.// Normal values are 20-40. Must be the same in both indicatorsmiOBV = OBV(close)Offset = highest[N](range) * 0.2//IF (BarIndex > 10+1+N) THEN//we look for bearish divergencesIF (miOBV[1]>miOBV AND miOBV[1]>miOBV[2]) THENextremum2=miOBV[1]extremum1=highest[N](miOBV)preciomax2=close[1]preciomax=Highest[N](close)IF(extremum2<extremum1 AND preciomax2>preciomax[1]) THENfor i=1 to Nif close[i]=preciomax[1] thenzz=idrawsegment (barindex[1], close[1]+Offset, barindex[zz], close[zz]+Offset) coloured(200,0,0)endifnextendifendif//we look for bullish divergencesIF (miOBV[1]<miOBV AND miOBV[1]<miOBV[2]) THENextremum22=miOBV[1]extremum11=lowest[N](miOBV)preciomin2=close[1]preciomin=lowest[N](close)IF(extremum22>extremum11 AND preciomin2<preciomin[1]) THENfor i2=1 to Nif close[i2]=preciomin[1] thenzz2=i2drawsegment(barindex[1], close[1]-Offset, barindex[zz2], close[zz2]-Offset) coloured(0,200,0)endifnextendifendifendifreturn2 users thanked author for this post.
09/07/2021 at 10:35 AM #176923Thank you very much for your support it’s working good…
-
AuthorPosts