Advertisement
JustUncleL

Dual Bollinger Break Alert v1 Updated

Dec 19th, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.29 KB | None | 0 0
  1. // @version=2
  2. //
  3.  
  4. study(shorttitle="DULBBA V1", title="Dual Bollinger Break Alert v1 by JustUncleL", overlay=true)
  5.  
  6. //
  7. // Title: "Dual Bollinger Break Alert v1 by JustUncleL".
  8. // Author: JustUncleL
  9. //
  10. // * Description *
  11. // This Alert indicator is a variation of the standard Bollinger Band break strategy. This indicator uses two Bollinger
  12. // bands: one set to (20,2) and optionally the other set to (150,2). There is also included an optional Bollinger band mininum
  13. // width thresh-hold factor, expressed as % of SMA, anf optional Divergence criteria.
  14. //
  15. // When the faster Bollinger Upper or Lower band goes outside the slower Bollinger Upper or Lower band and a candle closes
  16. // outside the faster Bollinger band the candle is shaded in Blue and an alert is generated. The alerts are optionally
  17. // filtered by minimum width factor threshold and optionally confirmed by selected Divergence method.
  18. //
  19. // The Bollinger width threshold factor is dependent on timeframe and asset, so it will be needed to tuned for them.
  20. // The last two numbers displayed on the script heading give the fast and slow width factor of a selected candle.
  21. //
  22. // * Modifications *
  23. // v1 - original.
  24. //
  25.  
  26. // Bollinger Bands Inputs
  27. fastBBlen = input(20, minval=1, title="Fast Bollinger Length")
  28. fastBBmult = input(2.0, title="Fast StdDev Multiplier", minval=0.5, maxval=10, step=0.1)
  29. fastBBsource = input(close, title="Fast Bollinger Source")
  30. uslow = input(true, title="Use Slow Bollinger Band Filter")
  31. slowBBlen = input(150, minval=10, title="Slow Bollinger Length")
  32. slowBBmult = input(2.0, title="Slow StdDev Multiplier", minval=0.5, maxval=10, step=0.1)
  33. slowBBsource = input(close, title="Slow Bollinger Source")
  34.  
  35. fastWthreshold = input(0.4,minval=0.0, maxval=50.0, step=0.1, title="Fast Width Mininum Threshold (zero to disable)")
  36. slowWthreshold = input(1.0,minval=0.0, maxval=50.0, step=0.1, title="Slow Width Mininum Threshold (zero to disable)")
  37. //
  38. method = input(title='Method (0=RSI, 1=macd, 2=stoch, 3=volume, 4=acc/dist, 5=fisher, 6=cci, 7=BB %B, 8=IdealRSI, 9 == qqe RSI):', type=integer, defval=0, minval=0, maxval=9)
  39. SHOW_LABEL = input(title='Show Labels', type=bool, defval=true)
  40. SHOW_CHANNEL = input(title='Show Channel', type=bool, defval=false)
  41. uHid = input(false,title="Use Hidden Divergence in Strategy")
  42. uReg = input(true,title="Use Regular Divergence in Strategy")
  43. uDiv = uHid or uReg
  44. // || RSI / STOCH / VOLUME / ACC/DIST/ FISHER/ CCI/ BB %B Input:
  45. rsi_smooth = input(title='RSI/STOCH/Volume/ACC-DIST/Fisher/cci Smooth/BB %B length:', type=integer, defval=14)
  46. // || MACD Input:
  47. macd_src = input(title='MACD Source:', type=source, defval=close)
  48. macd_fast = input(title='MACD Fast:', type=integer, defval=12)
  49. macd_slow = input(title='MACD Slow:', type=integer, defval=26)
  50. macd_smooth = input(title='MACD Smooth Signal:', type=integer, defval=9)
  51. //
  52. high_source = input(close,title="High Source")
  53. low_source = input(close,title="Low Source")
  54.  
  55. // || Functions:
  56. f_top_fractal(_src)=>_src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0]
  57. f_bot_fractal(_src)=>_src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0]
  58. f_fractalize(_src)=>f_top_fractal(_src) ? 1 : f_bot_fractal(_src) ? -1 : 0
  59.  
  60. // ||••> START MACD FUNCTION
  61. f_macd(_src, _fast, _slow, _smooth)=>
  62. _fast_ma = sma(_src, _fast)
  63. _slow_ma = sma(_src, _slow)
  64. _macd = _fast_ma-_slow_ma
  65. _signal = ema(_macd, _smooth)
  66. _hist = _macd - _signal
  67. // ||<•• END MACD FUNCTION
  68.  
  69. // ||••> START ACC/DIST FUNCTION
  70. f_accdist(_smooth)=>_return=sma(cum(close==high and close==low or high==low ? 0 : ((2*close-low-high)/(high-low))*volume), _smooth)
  71. // ||<•• END ACC/DIST FUNCTION
  72.  
  73. // ||••> START FISHER FUNCTION
  74. f_fisher(_src, _window)=>
  75. _h = highest(_src, _window)
  76. _l = lowest(_src, _window)
  77. _value0 = .66 * ((_src - _l) / max(_h - _l, .001) - .5) + .67 * nz(_value0[1])
  78. _value1 = _value0 > .99 ? .999 : _value0 < -.99 ? -.999 : _value0
  79. _fisher = .5 * log((1 + _value1) / max(1 - _value1, .001)) + .5 * nz(_fisher[1])
  80. // ||<•• END FISHER FUNCTION
  81.  
  82. // Rolling Moving Average (or Wells Wilders MA)
  83. irma(p,l) =>
  84. irma = (nz(irma[1]) * (l - 1) + p) / l
  85.  
  86. // RSI function.
  87. irsi(p, l) =>
  88. up = irma(max(change(p), 0), l)
  89. down = irma(-min(change(p), 0), l)
  90. irsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
  91. //
  92.  
  93. //
  94. // --- Start the Homodyne Discriminator Caculations
  95. //
  96. idealRSI(p) =>
  97. C1 = 0.0962
  98. C2 = 0.5769
  99. Df = 0.5
  100. C3 = (nz(Period[1])*0.075+0.54)
  101. smooth = ((hl2*4.0) + (hl2[1]*3.0) + (hl2[2]*2.0) + (hl2[3]))/10.0
  102. dDeTrend = (smooth*C1 + nz(smooth[2])*C2 - nz(smooth[4])*C2 - nz(smooth[6])*C1)*C3
  103. Q1 = (dDeTrend*C1 + nz(dDeTrend[2])*C2 - nz(dDeTrend[4])*C2 - nz(dDeTrend[6])*C1)*C3
  104. I1 = nz(dDeTrend[3])
  105. jI = (I1*C1 + nz(I1[2])*C2 - nz(I1[4])*C2 - nz(I1[6])*C1)*C3
  106. jQ = (Q1*C1 + nz(Q1[2])*C2 - nz(Q1[4])*C2 - nz(Q1[6])*C1)*C3
  107. I2_ = I1 - jQ
  108. Q2_ = Q1 + jI
  109. I2 = 0.2*I2_ + 0.8*nz(I2[1])
  110. Q2 = 0.2*Q2_ + 0.8*nz(Q2[1])
  111. Re_ = I2*nz(I2[1]) + Q2*nz(Q2[1])
  112. Im_ = I2*nz(Q2[1]) - Q2*nz(I2[1])
  113. Re = 0.2*Re_ + 0.8*nz(Re[1])
  114. Im = 0.2*Im_ + 0.8*nz(Im[1])
  115. dp_ = iff(Re!=0 and Im!=0 , 6.28318/atan(Im/Re) , 0)
  116. II = nz(Period[1])
  117. dp = max(max(min(min(dp_,1.5*II),50),0.6667*II),6)
  118. Period = dp*0.2 + nz(Period[1])*0.8
  119. SmoothPeriod = 0.33*Period + nz(SmoothPeriod[1])*0.67
  120. rsiLen = round((SmoothPeriod*Df)-1) // Get variable RSI length from discriminator
  121. idealRSI = irsi(p,rsiLen) // Generate RSI.
  122.  
  123. // --- Bollinger Band Vdub BB %B
  124. pcBB(p,l) =>
  125. basis = sma(p, l)
  126. dev = 0.1*stdev(p, l)
  127. upper = basis + dev
  128. lower = basis - dev
  129. pcBB = (p - lower)/(upper - lower)
  130.  
  131. qqeRSI(p,l) =>
  132. Rsi = rsi(p,l)
  133. qqeRSI = ema(Rsi, 2)
  134.  
  135.  
  136.  
  137. // === End of Functions.
  138.  
  139. method_high = method == 0 ? rsi(high_source, rsi_smooth) :
  140. method == 1 ? f_macd(macd_src, macd_fast, macd_slow, macd_smooth) :
  141. method == 2 ? stoch(close, high, low, rsi_smooth) :
  142. method == 3 ? sma(volume, rsi_smooth) :
  143. method == 4 ? f_accdist(rsi_smooth) :
  144. method == 5 ? f_fisher(high_source, rsi_smooth) :
  145. method == 6 ? cci(high_source, rsi_smooth) :
  146. method == 7 ? pcBB(high_source, rsi_smooth) :
  147. method == 8 ? idealRSI(high_source) :
  148. method == 9 ? qqeRSI(high_source,rsi_smooth) :
  149. na
  150.  
  151. method_low = method == 0 ? rsi(low_source, rsi_smooth) :
  152. method == 1 ? f_macd(macd_src, macd_fast, macd_slow, macd_smooth) :
  153. method == 2 ? stoch(close, high, low, rsi_smooth) :
  154. method == 3 ? sma(volume, rsi_smooth) :
  155. method == 4 ? f_accdist(rsi_smooth) :
  156. method == 5 ? f_fisher(low_source, rsi_smooth) :
  157. method == 6 ? cci(low_source, rsi_smooth) :
  158. method == 7 ? pcBB(low_source, rsi_smooth) :
  159. method == 8 ? idealRSI(low_source) :
  160. method == 9 ? qqeRSI(low_source,rsi_smooth) :
  161. na
  162. // === End of Functions.
  163.  
  164.  
  165. fractal_top = f_fractalize(method_high) > 0 ? method_high[2] : na
  166. fractal_bot = f_fractalize(method_low) < 0 ? method_low[2] : na
  167.  
  168. high_prev = valuewhen(fractal_top, method_high[2], 1)
  169. high_price = valuewhen(fractal_top, high[2], 1)
  170. low_prev = valuewhen(fractal_bot, method_low[2], 1)
  171. low_price = valuewhen(fractal_bot, low[2], 1)
  172.  
  173. regular_bearish_div = fractal_top and high[2] > high_price and method_high[2] < high_prev
  174. hidden_bearish_div = fractal_top and high[2] < high_price and method_high[2] > high_prev
  175. regular_bullish_div = fractal_bot and low[2] < low_price and method_low[2] > low_prev
  176. hidden_bullish_div = fractal_bot and low[2] > low_price and method_low[2] < low_prev
  177.  
  178. plot(title='H F', series=uDiv and fractal_top ? high[2] : na, color=(regular_bearish_div and uReg) or (hidden_bearish_div and uHid)? maroon : not SHOW_CHANNEL ? na : silver, offset=-2)
  179. plot(title='L F', series=uDiv and fractal_bot ? low[2] : na, color=(regular_bullish_div and uReg) or (hidden_bullish_div and uHid) ? green : not SHOW_CHANNEL ? na : silver, offset=-2)
  180. plot(title='H D', series=uDiv and fractal_top ? high[2] : na, style=circles, color=(regular_bearish_div and uReg) or (hidden_bearish_div and uHid)? maroon : not SHOW_CHANNEL ? na : silver, linewidth=3, offset=-2)
  181. plot(title='L D', series=uDiv and fractal_bot ? low[2] : na, style=circles, color=(regular_bullish_div and uReg) or (hidden_bullish_div and uHid) ? green : not SHOW_CHANNEL ? na : silver, linewidth=3, offset=-2)
  182.  
  183. plotshape(title='+RBD', series=not SHOW_LABEL or not uReg? na : regular_bearish_div ? high[2] : na, text='R', style=shape.labeldown, location=location.absolute, color=maroon, textcolor=white, offset=-2)
  184. plotshape(title='+HBD', series=not SHOW_LABEL or not uHid? na : hidden_bearish_div ? high[2] : na, text='H', style=shape.labeldown, location=location.absolute, color=maroon, textcolor=white, offset=-2)
  185. plotshape(title='-RBD', series=not SHOW_LABEL or not uReg? na : regular_bullish_div ? low[2] : na, text='R', style=shape.labelup, location=location.absolute, color=green, textcolor=white, offset=-2)
  186. plotshape(title='-HBD', series=not SHOW_LABEL or not uHid? na : hidden_bullish_div ? low[2] : na, text='H', style=shape.labelup, location=location.absolute, color=green, textcolor=white, offset=-2)
  187.  
  188. // Centre of Bollinger
  189. fastBBbase = sma(fastBBsource, fastBBlen)
  190. slowBBbase = sma(slowBBsource, slowBBlen)
  191.  
  192. // Deviation
  193. fastBBdev = stdev(fastBBsource, fastBBlen)
  194. slowBBdev = stdev(slowBBsource, slowBBlen)
  195.  
  196. // Upper bands
  197. fastBBupper = fastBBbase + fastBBmult * fastBBdev
  198. slowBBupper = slowBBbase + slowBBmult * slowBBdev
  199.  
  200. // Lower Bands
  201. fastBBlower = fastBBbase - fastBBmult * fastBBdev
  202. slowBBlower = slowBBbase - slowBBmult * slowBBdev
  203.  
  204. // Bollinger Band Width = (Upper Band - Lower Band) / Simple Moving Average
  205. fastBBwidth = 100.0*(fastBBupper - fastBBlower) / fastBBbase
  206. slowBBwidth = 100.0*(slowBBupper - slowBBlower) / slowBBbase
  207.  
  208. // Breakout Deviation
  209. breakBBupper = fastBBsource>fastBBupper and (not uslow or fastBBupper>slowBBupper) and
  210. fastBBwidth>fastWthreshold and (not uslow or slowBBwidth>slowWthreshold) ? na(breakBBupper[1])? 1 : breakBBupper[1]+1 : 0
  211. breakBBlower = fastBBsource<fastBBlower and (not uslow or fastBBlower<slowBBlower) and
  212. fastBBwidth>fastWthreshold and (not uslow or slowBBwidth>slowWthreshold) ? na(breakBBlower[1])? 1 : breakBBlower[1]+1 : 0
  213.  
  214. //
  215. BBalert = breakBBupper or breakBBlower
  216. bcolor = BBalert ? blue : na
  217. barcolor(bcolor,title="BB break",editable=false)
  218.  
  219. // plot and fill upper bands
  220. fbu = plot(fastBBupper, title="Fast BB Upper", color=blue, linewidth=2, transp=10)
  221. fbm = plot(fastBBbase, title="Fast BB Base" , color=teal, linewidth=1, transp=50)
  222. fbl = plot(fastBBlower, title="Fast BB Lower", color=blue, linewidth=2, transp=10)
  223. fill(fbu, fbl, title="Fast BB Fill", color=blue, transp=95)
  224.  
  225. // plot and fill lower bands
  226. sbu = plot(slowBBupper, title="Slow BB Upper", color=red, linewidth=2, transp=10)
  227. sbm = plot(slowBBbase, title="Slow BB Base", color=maroon, linewidth=1, transp=50)
  228. sbl = plot(slowBBlower, title="Slow BB Lower", color=red, linewidth=2, transp=10)
  229. fill(sbu, sbl, title="Slow BB Fill", color=red, transp=95)
  230.  
  231. longCondition = uDiv ? regular_bullish_div and highest(breakBBlower,6)>0 : breakBBlower==1
  232. shortCondition = uDiv ? regular_bearish_div and highest(breakBBupper,6)>0 : breakBBupper==1
  233.  
  234. // create candle only required if set overlay=false
  235. //plotcandle(open, high, low, close, title='BB Candle', color = BBalert?blue : open < close ? green : red, wickcolor=black)
  236.  
  237. // indicate buy sell condition.
  238. plotshape(longCondition, title="BB Buy alert", style=shape.arrowup, location=location.belowbar, size=size.auto, text='BUY', color=green, transp=20)
  239. plotshape(shortCondition, title="BB Sell alert", style=shape.arrowdown, location=location.abovebar, size=size.auto, text='SELL', color=red, transp=20)
  240.  
  241.  
  242. // Display the calculated width factor
  243. plotshape(fastBBwidth, title="Fast Width Factor", style=shape.circle, location=location.bottom, size=size.tiny, color=gray, transp=50)
  244. plotshape(slowBBwidth, title="Slow Width Factor", style=shape.circle, location=location.bottom, size=size.tiny, color=gray, transp=50)
  245. // create Alert for TradingView's alrm sub-system.
  246. alertcondition(longCondition or shortCondition,title="DULBBB Alert",message="DULBBB Alert")
  247.  
  248. //EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement