Advertisement
freeman03

Untitled

Oct 29th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. //@version=4
  2. study(title = "Bollinger Bands %B divergence and cloud", shorttitle = "%B div", format=format.price, precision=2)
  3. length = input(20, minval=1)
  4. src = input(close, title="Source")
  5. mult = input(2.0, minval=0.001, maxval=50)
  6. overbougth=input(0.0)
  7. oversold=input(1.0)
  8. hull_len=input(9, minval=1)
  9. basis = sma(src, length)
  10. dev = mult * stdev(src, length)
  11. upper = basis + dev
  12. lower = basis - dev
  13. bbr = (src - lower)/(upper - lower)
  14.  
  15. hullma = wma(2*wma(bbr, hull_len/2)-wma(bbr, hull_len), round(sqrt(hull_len)))
  16.  
  17.  
  18. plot(bbr, color=color.teal)
  19. plot (hullma,color=color.orange)
  20. band1 = hline(overbougth, color=color.gray, linestyle=hline.style_dashed)
  21. band0 = hline(oversold, color=color.gray, linestyle=hline.style_dashed)
  22. //fill(band1, band0, color=color.teal,transp=95)
  23.  
  24. //cloud
  25. mult_cloud = input(1.5, minval=0.001, maxval=50)
  26. basis_cloud = sma(bbr, length)
  27. dev_cloud = mult_cloud * stdev(bbr, length)
  28. upper_cloud = basis_cloud + dev_cloud
  29. lower_cloud = basis_cloud - dev_cloud
  30. up=plot(upper_cloud, color=color.black)
  31. lo=plot (lower_cloud,color=color.black)
  32. fill(up,lo,color=color.gray, transp=95)
  33. // Bullish
  34. bullishPrice = low
  35. priceMins = bullishPrice > bullishPrice[1] and bullishPrice[1] < bullishPrice[2]
  36.  
  37. priceRightMin = valuewhen(priceMins, bullishPrice[1], 0)
  38. priceLeftMin = valuewhen(priceMins, bullishPrice[1], 1)
  39.  
  40. oscRightMin = valuewhen(priceMins, bbr[1], 0)
  41. oscLeftMin = valuewhen(priceMins, bbr[1], 1)
  42.  
  43. bullishNDiv = priceLeftMin > priceRightMin and oscLeftMin < oscRightMin
  44. bullishHDiv = priceLeftMin < priceRightMin and oscLeftMin > oscRightMin
  45.  
  46. bullishNSig = bullishNDiv and not bullishNDiv[1] and bullishPrice[1] < bullishPrice
  47. bullishHSig = bullishHDiv and not bullishHDiv[1] and bullishPrice[1] > bullishPrice
  48.  
  49. plot(bullishNSig ? 1 : 0, title="Normal Bullish Divergence", style=plot.style_histogram, linewidth=3, color=color.green)
  50. plot(bullishHSig ? 1 : 0, title="Hidden Bullish Divergence", style=plot.style_histogram, linewidth=1, color=color.green)
  51.  
  52. // Bearish
  53. bearishPrice = high
  54. priceMaxs = bearishPrice < bearishPrice[1] and bearishPrice[1] > bearishPrice[2]
  55.  
  56. priceRightMax = valuewhen(priceMaxs, bearishPrice[1], 0)
  57. priceLeftMax = valuewhen(priceMaxs, bearishPrice[1], 1)
  58.  
  59. oscRightMax = valuewhen(priceMaxs, bbr[1], 0)
  60. oscLeftMax = valuewhen(priceMaxs, bbr[1], 1)
  61.  
  62.  
  63. bearishNDiv = priceLeftMax < priceRightMax and oscLeftMax > oscRightMax
  64. bearishHDiv = priceLeftMax > priceRightMax and oscLeftMax < oscRightMax
  65.  
  66. bearishNSig = bearishNDiv and not bearishNDiv[1] and bearishPrice[1] < bearishPrice
  67. bearishHSig = bearishHDiv and not bearishHDiv[1] and bearishPrice[1] > bearishPrice
  68.  
  69. plot(bearishNSig ? 1 : 0, title="Normal Bearish Divergence", style=plot.style_histogram, linewidth=3, color=color.red)
  70. plot(bearishHSig ? 1 : 0, title="Hidden Bearish Divergence", style=plot.style_histogram, linewidth=1, color=color.red)
  71.  
  72. //alerts
  73. alertcondition(bbr >overbougth, title='Alert on overbougth', message='BB %B is in overboutgh area!')
  74. alertcondition(bbr <oversold, title='Alert on oversold', message='BB %B is in oversold area!')
  75. alertcondition(bullishNSig, title='Alert on normal bull divergence', message='Bullish divergence (normal)!')
  76. alertcondition(bullishHSig, title='Alert on hidden bull divergence', message='Bullish divergence (hidden)!')
  77. alertcondition(bearishNSig, title='Alert on normal bear divergence', message='Bearish divergence (normal)!')
  78. alertcondition(bearishHSig, title='Alert on hidden bear divergence', message='Bearish divergence (hidden)!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement