Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- study(title = "Bollinger Bands %B divergence and cloud", shorttitle = "%B div", format=format.price, precision=2)
- length = input(20, minval=1)
- src = input(close, title="Source")
- mult = input(2.0, minval=0.001, maxval=50)
- overbougth=input(0.0)
- oversold=input(1.0)
- hull_len=input(9, minval=1)
- basis = sma(src, length)
- dev = mult * stdev(src, length)
- upper = basis + dev
- lower = basis - dev
- bbr = (src - lower)/(upper - lower)
- hullma = wma(2*wma(bbr, hull_len/2)-wma(bbr, hull_len), round(sqrt(hull_len)))
- plot(bbr, color=color.teal)
- plot (hullma,color=color.orange)
- band1 = hline(overbougth, color=color.gray, linestyle=hline.style_dashed)
- band0 = hline(oversold, color=color.gray, linestyle=hline.style_dashed)
- //fill(band1, band0, color=color.teal,transp=95)
- //cloud
- mult_cloud = input(1.5, minval=0.001, maxval=50)
- basis_cloud = sma(bbr, length)
- dev_cloud = mult_cloud * stdev(bbr, length)
- upper_cloud = basis_cloud + dev_cloud
- lower_cloud = basis_cloud - dev_cloud
- up=plot(upper_cloud, color=color.black)
- lo=plot (lower_cloud,color=color.black)
- fill(up,lo,color=color.gray, transp=95)
- // Bullish
- bullishPrice = low
- priceMins = bullishPrice > bullishPrice[1] and bullishPrice[1] < bullishPrice[2]
- priceRightMin = valuewhen(priceMins, bullishPrice[1], 0)
- priceLeftMin = valuewhen(priceMins, bullishPrice[1], 1)
- oscRightMin = valuewhen(priceMins, bbr[1], 0)
- oscLeftMin = valuewhen(priceMins, bbr[1], 1)
- bullishNDiv = priceLeftMin > priceRightMin and oscLeftMin < oscRightMin
- bullishHDiv = priceLeftMin < priceRightMin and oscLeftMin > oscRightMin
- bullishNSig = bullishNDiv and not bullishNDiv[1] and bullishPrice[1] < bullishPrice
- bullishHSig = bullishHDiv and not bullishHDiv[1] and bullishPrice[1] > bullishPrice
- plot(bullishNSig ? 1 : 0, title="Normal Bullish Divergence", style=plot.style_histogram, linewidth=3, color=color.green)
- plot(bullishHSig ? 1 : 0, title="Hidden Bullish Divergence", style=plot.style_histogram, linewidth=1, color=color.green)
- // Bearish
- bearishPrice = high
- priceMaxs = bearishPrice < bearishPrice[1] and bearishPrice[1] > bearishPrice[2]
- priceRightMax = valuewhen(priceMaxs, bearishPrice[1], 0)
- priceLeftMax = valuewhen(priceMaxs, bearishPrice[1], 1)
- oscRightMax = valuewhen(priceMaxs, bbr[1], 0)
- oscLeftMax = valuewhen(priceMaxs, bbr[1], 1)
- bearishNDiv = priceLeftMax < priceRightMax and oscLeftMax > oscRightMax
- bearishHDiv = priceLeftMax > priceRightMax and oscLeftMax < oscRightMax
- bearishNSig = bearishNDiv and not bearishNDiv[1] and bearishPrice[1] < bearishPrice
- bearishHSig = bearishHDiv and not bearishHDiv[1] and bearishPrice[1] > bearishPrice
- plot(bearishNSig ? 1 : 0, title="Normal Bearish Divergence", style=plot.style_histogram, linewidth=3, color=color.red)
- plot(bearishHSig ? 1 : 0, title="Hidden Bearish Divergence", style=plot.style_histogram, linewidth=1, color=color.red)
- //alerts
- alertcondition(bbr >overbougth, title='Alert on overbougth', message='BB %B is in overboutgh area!')
- alertcondition(bbr <oversold, title='Alert on oversold', message='BB %B is in oversold area!')
- alertcondition(bullishNSig, title='Alert on normal bull divergence', message='Bullish divergence (normal)!')
- alertcondition(bullishHSig, title='Alert on hidden bull divergence', message='Bullish divergence (hidden)!')
- alertcondition(bearishNSig, title='Alert on normal bear divergence', message='Bearish divergence (normal)!')
- alertcondition(bearishHSig, title='Alert on hidden bear divergence', message='Bearish divergence (hidden)!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement