Advertisement
Guest User

7x %BB Strategy

a guest
Aug 15th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //@version=3
  2. //created by gamazama. send me a shout if u find this usefull.
  3. //usage: tradingview->pine editor->paste->add to chart
  4. // %BB: The price's position in the boilinger band is converted to a range from 0-1. The midpoint is at 0.5
  5. // Description of parameters
  6. // "BB:Window Length" is the standard BB size of 20 candles.
  7. // The indicator plots 7 different %BB's on different timescales
  8. // they are calculated independantly of the timescale if you are viewing eg 12h,3d, 30m the output will be the same
  9. // You can enter 7 timescales, eg. if you want to plot a range of bbands of the 12h up to 3d graphs, enter values between 0.5 and 3
  10. // take note if you double the main multiplier to 40, it is the same as doubling all your timescales
  11. // You can turn the transparency of the 7x%BB's to 100 to hide them, their average is plotted as a thick cyan line
  12. // the default values are optimized for a bear market
  13. //
  14. // "Variance" is a measure of how much the 7 BB's agree, and changes colour based on the thresholds used for the strategy
  15. //
  16. // Then there is a few standard settings:
  17. // "Source Smoothing Amount" applies a basic small sma on the price.
  18. // It should be turned down when viewing candles with less information, like 1D or more
  19. // Standard BBands use an SMA, here there is an option to use ALMA or VWMA or SMA
  20. // Standard Alma settings for when ALMA is checked
  21. // Volume Weight settings for when ALMA is unchecked, the same as SMA at 0, and the same as VWMA at 1
  22.  
  23. // BB^2 is a bband drawn around the average %BB. Adjust the multiplier to change its window length
  24. // The BB^2 changes color when price moves up or down
  25.  
  26. // Now its time to look at the parameters which affect the buy/sell signals
  27. // turn on "show signal range" - you see some red lines
  28. // buy and sell each have 3 settings
  29. // max variance will affect the brigtness of the signal range
  30. // range adjust will move the range up/down
  31. // mix BB^2 blends between a straight line (0) and BB^2's top or bottom (1)
  32.  
  33. // a threshold of "variance" and "h/l points" is available for weaker signals.
  34. // these thresholds can be increased to show more weak signals
  35.  
  36.  
  37. // the 'signal-only' version of the indicator plots buy & sell signals on the price chart
  38. // it has 3 extra options
  39. // "show signal range" converts the signal range (red lines) to be relative to the price chart
  40. // "show middle band" fills a colour between 0.5 & the mid BB^2 and converts relative to the price chart
  41. // display width controls how much these lines diverge from the price
  42.  
  43. // enjoy, I hope that is easy enough to understand, haha.
  44.  
  45.  
  46.  
  47. //This file uses %BB code created by yield
  48. //If you find them useful please consider making him donation, thank you.
  49. //Bitcoin: 3F636VrPCdnbfrdP5kS4C6fHWVBffXNKCu
  50.  
  51. study(title="%BB x7", precision=2, overlay=false)
  52.  
  53. //inputs
  54. bblen           = input(20.0, step=1,                 title="**BB: window length, use as main multiplier")
  55. plot1_t         = input(3.57, title="Plot 1 - timescale(days)")
  56. plot2_t         = input(2.21, title="Plot 2 - timescale")
  57. plot3_t         = input(1.36, title="Plot 3 - timescale")
  58. plot4_t         = input(0.85, title="Plot 4 - timescale")
  59. plot5_t         = input(0.51, title="Plot 5 - timescale")
  60. plot6_t         = input(0.34, title="Plot 6 - timescale")
  61. plot7_t         = input(0.17, title="Plot 7 - timescale")
  62. bBandsTransparency=input(95,  title="Transparency of %BB lines")
  63. showVariance    = input(false,title="Show Variance between timescales?")
  64.  
  65. srcSmooth       = input(4,                             title=".. Source Smoothing Amount",minval=1)
  66. bbma            = input(false, type=bool,              title=".. Use ALMA for BB?(or VW-SMA)")
  67. offset          = input(0.55, step=0.05, minval=0.05, maxval=1.0, title=".. Alma: Offset")
  68. sigma           = input(2, step=1, minval=1,          title=".. Alma: Sigma")
  69. volWeightAmt    = input(0.7,step=0.1,minval=0,maxval=1, title=".. VW-SMA: Volume Weight (0-1)")
  70. bobLen          = input(1.0, step=0.05,               title=".. BB^2: Window length (multiplier)")*bblen
  71.  
  72.  
  73. getVWMA(_src,_len) =>
  74.     (vwma(_src,_len) * volWeightAmt) + (sma(_src,_len) * (1-volWeightAmt))
  75.  
  76. src = sma(close,srcSmooth)
  77.  
  78.  
  79. //get intervals to stick regardless of timeframe
  80. multIntra() => (isintraday ? 1.0 : (isdaily ? 1440.0 : isweekly ? 10080.0 : 43320.0))
  81. multDaily() => multIntra() / 1440.0
  82. getLengthMultiplier(_amt, _useDaily) => (_amt / (_useDaily ? multDaily() : multIntra())) / interval
  83. getLength(len, amountOf, useDaysNotMinutes) => round(len * getLengthMultiplier(amountOf, useDaysNotMinutes))
  84.  
  85.  
  86. getPBB(len) =>
  87.     bb_mid = bbma ? alma(src, len, offset, sigma) : getVWMA(src, len)
  88.     dev = 2.0 * stdev(src, len)
  89.     bb_top = bb_mid + dev
  90.     bb_low = bb_mid - dev
  91.     resultPBB = (src - bb_low) / (bb_top - bb_low)
  92.     //resultBBW = (bb_top - bb_low)/bb_mid
  93.     resultPBB
  94.  
  95. getFullBB(_src, _len) =>
  96.     bb_mid = bbma ? alma(_src, _len, offset, sigma) : getVWMA(_src, _len)
  97.     dev = 2.0 * stdev(_src, _len)
  98.     bb_top = bb_mid + dev
  99.     bb_low = bb_mid - dev
  100.     //resultPBB = (_src - bb_low) / (bb_top - bb_low)
  101.     //resultBBW = (bb_top - bb_low)/bb_mid
  102.     [bb_top,bb_low,bb_mid]
  103.    
  104. //input(true,title="use days?")
  105. plot1_d  = true
  106. plot2_d  = true
  107. plot3_d  = true
  108. plot4_d  = true
  109. plot5_d  = true
  110. plot6_d  = true
  111. plot7_d  = true
  112.  
  113. pbb1 = getPBB(getLength(bblen, plot1_t, plot1_d))
  114. pbb2 = getPBB(getLength(bblen, plot2_t, plot2_d))
  115. pbb3 = getPBB(getLength(bblen, plot3_t, plot3_d))
  116. pbb4 = getPBB(getLength(bblen, plot4_t, plot4_d))
  117. pbb5 = getPBB(getLength(bblen, plot5_t, plot5_d))
  118. pbb6 = getPBB(getLength(bblen, plot6_t, plot6_d))
  119. pbb7 = getPBB(getLength(bblen, plot7_t, plot7_d))
  120. pbbAvg = (pbb1 + pbb2 + pbb3 + pbb4 + pbb5 + pbb6 + pbb7) / 7
  121.  
  122. pbbcolor = white
  123.  
  124. hline(0, 'Zero', linestyle=dashed, linewidth=1, color=#7B68EE, editable=false)
  125. hline(.5, 'Half', linestyle=dashed, linewidth=1, color=#7B68EE, editable=false)
  126. hline(1, 'One', linestyle=dashed, linewidth=1, color=#7B68EE, editable=false)
  127.  
  128.  
  129. plot(pbb1, linewidth=1, transp=bBandsTransparency, style=line, editable=false, color=pbbcolor, title="% BB1")
  130. plot(pbb2, linewidth=1, transp=bBandsTransparency, style=line, editable=false, color=pbbcolor, title="% BB2")
  131. plot(pbb3, linewidth=1, transp=bBandsTransparency, style=line, editable=false, color=pbbcolor, title="% BB3")
  132. plot(pbb4, linewidth=1, transp=bBandsTransparency, style=line, editable=false, color=pbbcolor, title="% BB4")
  133. plot(pbb5, linewidth=1, transp=bBandsTransparency, style=line, editable=false, color=pbbcolor, title="% BB5")
  134. plot(pbb6, linewidth=1, transp=bBandsTransparency, style=line, editable=false, color=pbbcolor, title="% BB6")
  135. plot(pbb7, linewidth=1, transp=bBandsTransparency, style=line, editable=false, color=pbbcolor, title="% BB7")
  136.  
  137. plot(pbbAvg, linewidth=2, transp=20, style=line, editable=true, color=#51edef, title="% BB AVG")
  138.  
  139. bbLenAvg = (plot7_t+plot6_t+plot5_t+plot4_t+plot3_t+plot2_t+plot1_t)/7
  140.  
  141. [bobTop,bobLow,bobMid] = getFullBB(pbbAvg, getLength(bobLen,bbLenAvg,true))
  142. plotBobTop = plot(bobTop, linewidth=1, transp=100, style=line, editable=false, title="bb^2 Top")
  143. plotBobLow = plot(bobLow, linewidth=1, transp=100, style=line, editable=false, title="bb^2 Low")
  144. plotBobMid = plot(bobMid, linewidth=1, transp=49,  style=line, editable=false, title="bb^2 sma", color=blue)
  145. bobCol = round(bobMid *1000) == round(bobMid[1] *1000) ? #058480 : bobMid > bobMid[1] ? #01ff96:#043f81
  146. fill(plotBobTop,plotBobLow,transp=79,color=bobCol,title="bb^2")
  147.  
  148.  
  149. bbsMax = max(pbb1,max(pbb2,max(pbb3,max(pbb4,max(pbb5,max(pbb6,pbb7))))))
  150. bbsMin = min(pbb1,min(pbb2,min(pbb3,min(pbb4,min(pbb5,min(pbb6,pbb7))))))
  151. bbsVariance = bbsMax-bbsMin
  152.  
  153. //strategy params
  154. showRange                 = input(false, "--Show Signal Range")
  155. bbsCloseVariancePointLow  = input(0.35, title="--Max Variance for Buy Signal",    step=0.05)
  156. buyRangeAdjust            = input(0.05, title="--Buy range adjust:", step=0.025)
  157. buyMixBB                  = input(0.15, title="--Mix BB^2 into Buy Range (0-1)", step=0.1,minval=0,maxval=1)
  158. bbsCloseVariancePointHigh = input(0.50, title="--Max Variance for Sell Signal ",  step=0.05)
  159. sellRangeAdjust           = input(0.10, title="--Sell range adjust:",     step=0.025)
  160. sellMixBB                 = input(0.85, title="--Mix BB^2 into Sell Range (0-1)", step=0.1,minval=0,maxval=1)
  161.  
  162. buyRange   = (  buyMixBB  * bobLow  ) + buyRangeAdjust
  163. sellRange  = (  sellMixBB * bobTop  ) - sellRangeAdjust + (1 - sellMixBB)
  164. buySignal  = bbsVariance  < bbsCloseVariancePointLow   and pbbAvg < buyRange
  165. sellSignal = bbsVariance < bbsCloseVariancePointHigh  and pbbAvg > sellRange
  166.  
  167. weakVariance              = input(0.1, title="--Threshold of Variance for weaker signals",  step=0.05, minval=0)
  168. weakInput                 = input(0.075, title="--Threshold of H/L Point for weaker signals", step=0.025, minval=0)
  169. weakBuySignal1  = bbsVariance < bbsCloseVariancePointLow  + weakVariance     and pbbAvg < buyRange + weakInput
  170. weakSellSignal1 = bbsVariance < bbsCloseVariancePointHigh + weakVariance     and pbbAvg > sellRange - weakInput
  171. weakBuySignal2  = bbsVariance < bbsCloseVariancePointLow  + (weakVariance/2) and pbbAvg < buyRange + (weakInput / 2)
  172. weakSellSignal2 = bbsVariance < bbsCloseVariancePointHigh + (weakVariance/2) and pbbAvg > sellRange - (weakInput / 2)
  173.  
  174. sRangeCol = bbsVariance < bbsCloseVariancePointHigh ? #FF0000 : bbsVariance < bbsCloseVariancePointHigh + weakVariance ? #550000 : #350000
  175. sr1 = plot(showRange ? sellRange : na, color=sRangeCol,editable=false)
  176. sr2 = plot(showRange ? sellRange - weakInput : na, transp=90,editable=false)
  177.  
  178. bRangeCol = bbsVariance < bbsCloseVariancePointLow ? #FF0000 : bbsVariance < bbsCloseVariancePointLow + weakVariance ? #550000 : #350000
  179. br1 = plot(showRange ? buyRange : na, color=bRangeCol,editable=false)
  180. br2 = plot(showRange ? buyRange + weakInput : na, transp=90,editable=false)
  181. fill(sr1,sr2,color=sRangeCol,transp=90,editable=false)
  182. fill(br1,br2,color=bRangeCol,transp=90,editable=false)
  183.  
  184. plotshape(buySignal, title="Dot mark Up", style=shape.arrowup, location=location.bottom, color=#01ff96, transp=0, size=size.normal, editable=false)
  185. plotshape(sellSignal, title="Dot mark Down", style=shape.arrowdown, location=location.top, color=#054e92, transp=0, size=size.normal, editable=false)
  186.  
  187. plotshape(weakBuySignal1,  title="Dot mark Up 1",   style=shape.arrowup,   location=location.bottom, color=#01ff96, transp=99, size=size.tiny,  editable=false)
  188. plotshape(weakSellSignal1, title="Dot mark Down 1", style=shape.arrowdown, location=location.top, color=#054e92, transp=99, size=size.tiny,  editable=false)
  189. plotshape(weakBuySignal2,  title="Dot mark Up 2",   style=shape.arrowup,   location=location.bottom, color=#01ff96, transp=92, size=size.small, editable=false)
  190. plotshape(weakSellSignal2, title="Dot mark Down 2", style=shape.arrowdown, location=location.top, color=#054e92, transp=92, size=size.small, editable=false)
  191.  
  192.  
  193.  
  194.  
  195. //variance graph
  196. varianceGraphTop = bobMid+(bbsVariance/4)
  197. varianceGraphLow = bobMid-(bbsVariance/4)
  198. vgt = plot(showVariance ? varianceGraphTop : na, transp=100, editable=false, title="Variance Top")
  199. vgb = plot(showVariance ? varianceGraphLow : na, transp=100, editable=false, title="Variance Bottom")
  200. vgc = bbsVariance<min(bbsCloseVariancePointLow,bbsCloseVariancePointHigh)? white : bbsVariance<max(bbsCloseVariancePointLow,bbsCloseVariancePointHigh)? #56A4D8 : #285774
  201. fill(vgt,vgb,color=vgc,transp=80)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement