Advertisement
Guest User

PBB Chart overlay

a guest
Dec 11th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.29 KB | None | 0 0
  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. //signal only version. read the instructions on the main version
  5.  
  6.  
  7. study(title="%BB x7 Signals", precision=2, overlay=true)
  8.  
  9. //inputs
  10. bblen           = input(20.0, step=1,                 title="**BB: window length, use as main multiplier")
  11. bbStdDev        = input(2.0, step=0.05,               title="BB: Standard Deviations")
  12. bobLen          = input(1.0, step=0.05,               title="BB^2: Window length (multiplier of ^)")*bblen
  13. plot1_t         = input(3.57, title="Plot 1 - timescale(days)")
  14. plot2_t         = input(2.21, title="Plot 2 - timescale")
  15. plot3_t         = input(1.36, title="Plot 3 - timescale")
  16. plot4_t         = input(0.85, title="Plot 4 - timescale")
  17. plot5_t         = input(0.51, title="Plot 5 - timescale")
  18. plot6_t         = input(0.34, title="Plot 6 - timescale")
  19. plot7_t         = input(0.17, title="Plot 7 - timescale")
  20. //showVariance    = input(false, title="Show Variance between timescales?")
  21. //bBandsTransparency=input(95, title="Transparency of %BB lines")
  22.  
  23. srcSmooth       = input(4,                            title="Source Smoothing Amount")
  24. //bbma            = input(false, type=bool,              title="Use ALMA for BB?(or VW-SMA)")
  25. //offset          = input(0.55, step=0.05, minval=0.05, maxval=1.0, title="Alma: Offset")
  26. //sigma           = input(2, step=1, minval=1,          title="Alma: Sigma")
  27. volWeightAmt    = input(0.7,step=0.1,minval=0,maxval=1, title="VW-SMA: Volume Weight (0-1)")
  28.  
  29. getVWMA(_src,_len) =>
  30.     (vwma(_src,_len) * volWeightAmt) + (sma(_src,_len) * (1-volWeightAmt))
  31.  
  32. src = sma(close,srcSmooth)
  33.  
  34.  
  35. //get intervals to stick regardless of timeframe [TWITCHY]
  36. multIntra() => (isintraday ? 1.0 : (isdaily ? 1440.0 : isweekly ? 10080.0 : 43320.0))
  37. multDaily() => multIntra() / 1440.0
  38. getLengthMultiplier(_amt, _useDaily) => (_amt / (_useDaily ? multDaily() : multIntra())) / interval
  39. getLength(len, amountOf, useDaysNotMinutes) => round(len * getLengthMultiplier(amountOf, useDaysNotMinutes))
  40.  
  41.  
  42. getFullBB(_src, _len) =>
  43.     //bb_mid = bbma ? alma(_src, _len, offset, sigma) : getVWMA(_src, _len)
  44.     bb_mid = getVWMA(_src, _len)
  45.     dev = bbStdDev * stdev(_src, _len)
  46.     bb_top = bb_mid + dev
  47.     bb_low = bb_mid - dev
  48.     [bb_top,bb_low,bb_mid]
  49.  
  50. getPBB(len) =>
  51.     [bb_top,bb_low,bb_mid] = getFullBB(src,len)
  52.     resultPBB = (src - bb_low) / (bb_top - bb_low)
  53.     resultPBB
  54.    
  55.  
  56. useDays  = input(true,title="Timeframe in days?(or minutes)")
  57.  
  58.  
  59. pbb1     = getPBB(getLength(bblen, plot1_t, useDays))
  60. pbb2     = getPBB(getLength(bblen, plot2_t, useDays))
  61. pbb3     = getPBB(getLength(bblen, plot3_t, useDays))
  62. pbb4     = getPBB(getLength(bblen, plot4_t, useDays))
  63. pbb5     = getPBB(getLength(bblen, plot5_t, useDays))
  64. pbb6     = getPBB(getLength(bblen, plot6_t, useDays))
  65. pbb7     = getPBB(getLength(bblen, plot7_t, useDays))
  66. pbbAvg   = (pbb1 + pbb2 + pbb3 + pbb4 + pbb5 + pbb6 + pbb7)          / 7
  67. bbLenAvg = (plot7_t+plot6_t+plot5_t+plot4_t+plot3_t+plot2_t+plot1_t) / 7
  68.  
  69. [bobTop,bobLow,bobMid] = getFullBB(pbbAvg, getLength(bobLen,bbLenAvg,useDays))
  70.  
  71.  
  72. bbsMax = max(pbb1,max(pbb2,max(pbb3,max(pbb4,max(pbb5,max(pbb6,pbb7))))))
  73. bbsMin = min(pbb1,min(pbb2,min(pbb3,min(pbb4,min(pbb5,min(pbb6,pbb7))))))
  74. bbsVariance = bbsMax-bbsMin
  75.  
  76. //strategy params
  77. showSignals               = input(true,  "--Show Buy/Sell Signal")
  78. showRange                 = input(false, "--Show Signal Range")
  79. showMiddle                = input(false, "--Show Middle Band")
  80. dispWidth                 = input(10.00, "-----Display Width")
  81. bbsCloseVariancePointLow  = input(0.35, title="--Max Variance for Buy Signal",    step=0.05)
  82. buyRangeAdjust            = input(0.05, title="--Buy range adjust (+/-):", step=0.025)
  83. buyMixBB                  = input(0.15, title="--Mix BB^2 bottom into Buy Range (0-1)", step=0.1,minval=0,maxval=1)
  84.  
  85. bbsCloseVariancePointHigh = input(0.50, title="--Max Variance for Sell Signal ",  step=0.05)
  86. sellRangeAdjust           = input(0.10, title="--Sell range adjust (+/-):",     step=0.025)
  87. sellMixBB                 = input(0.85, title="--Mix BB^2 top into Sell Range (0-1)", step=0.1,minval=0,maxval=1)
  88. buyRange   = (  buyMixBB  * bobLow  ) + buyRangeAdjust
  89. sellRange  = (  sellMixBB * bobTop  ) - sellRangeAdjust + (1 - sellMixBB)
  90.  
  91. weakVariance              = input(0.10, title="--Threshold of Variance for weaker signals",  step=0.05, minval=0)
  92. weakInput                 = input(0.075, title="--Threshold of H/L Point for weaker signals", step=0.025, minval=0)
  93. buySignal       = bbsVariance < bbsCloseVariancePointLow                     and pbbAvg < buyRange
  94. weakBuySignal1  = bbsVariance < bbsCloseVariancePointLow  + (weakVariance/2) and pbbAvg < buyRange + (weakInput / 2)
  95. weakBuySignal2  = bbsVariance < bbsCloseVariancePointLow  + weakVariance     and pbbAvg < buyRange + weakInput
  96.  
  97. sellSignal      = bbsVariance < bbsCloseVariancePointHigh                    and pbbAvg > sellRange
  98. weakSellSignal1 = bbsVariance < bbsCloseVariancePointHigh + (weakVariance/2) and pbbAvg > sellRange - (weakInput / 2)
  99. weakSellSignal2 = bbsVariance < bbsCloseVariancePointHigh + weakVariance     and pbbAvg > sellRange - weakInput
  100.  
  101. showSell = pbbAvg>bobMid
  102. [bbT,bbL,bbM] = getFullBB(src, getLength(dispWidth,bbLenAvg,true))
  103. pPrice = pbbAvg * (bbT-bbL) + bbL
  104. pAdjust = src/pPrice
  105. convertToPrice(_input) => (_input * (bbT-bbL) + bbL) * pAdjust
  106.  
  107.  
  108. sellRangePrice = convertToPrice(sellRange)
  109. sellRangePrice1 = convertToPrice(sellRange - weakInput)
  110.  
  111. buyRangePrice = convertToPrice(buyRange)
  112. buyRangePrice1 = convertToPrice(buyRange + weakInput)
  113.  
  114. bobCol = round(bobMid *1000) == round(bobMid[1] *1000) ? #858400 : bobMid > bobMid[1] ? #22ff00:#FF2200
  115. midLine1 = plot(showMiddle?convertToPrice(0.5):na, transp=100,editable=false)
  116. plotBobMid = plot(convertToPrice(bobMid), transp=100, editable=false, title="bb^2 sma")
  117. fill(midLine1,plotBobMid, transp=70,color=#22ff00)
  118. //fill(plotBobTop,plotBobLow,transp=95,color=bobCol,title="bb^2")
  119. sRangeCol = showSell ? bbsVariance < bbsCloseVariancePointHigh    ? color(#FF2222, 50) : bbsVariance < bbsCloseVariancePointHigh + weakVariance ? color(#FF2222, 67) : color(#FF2222, 71) : color(#FF2222, 85)
  120. bRangeCol = not showSell ? bbsVariance < bbsCloseVariancePointLow ? color(#FF2222, 50) : bbsVariance < bbsCloseVariancePointLow + weakVariance  ? color(#FF2222, 67) : color(#FF2222, 71) : color(#FF2222, 85)
  121. sr1 = plot(showRange ? sellRangePrice  : na, color=sRangeCol,editable=false)
  122. sr2 = plot(showRange ? sellRangePrice1 : na, transp=100,editable=false)
  123. br1 = plot(showRange ? buyRangePrice   : na, color=bRangeCol,editable=false)
  124. br2 = plot(showRange ? buyRangePrice1  : na, transp=100,editable=false)
  125. fill(sr1,sr2,color=sRangeCol,transp=70,editable=false)
  126. fill(br1,br2,color=bRangeCol,transp=70,editable=false )
  127.  
  128. plotshape(showSignals?buySignal:na, title="Buy", style=shape.arrowup, location=location.belowbar, color=#01ff96, transp=0, size=size.normal, editable=true)
  129. plotshape(showSignals?weakBuySignal1:na,  title="Weak Buy",   style=shape.arrowup,   location=location.belowbar, color=#01ff96, transp=80, size=size.tiny,  editable=true)
  130. plotshape(showSignals?weakBuySignal2:na,  title="Weaker Buy",   style=shape.arrowup,   location=location.belowbar, color=#01ff96, transp=50, size=size.small, editable=true)
  131.  
  132. plotshape(showSignals?sellSignal:na, title="Sell", style=shape.arrowdown, location=location.abovebar, color=#054e92, transp=0, size=size.normal, editable=true)
  133. plotshape(showSignals?weakSellSignal1:na, title="Weak Sell", style=shape.arrowdown, location=location.abovebar, color=#054e92, transp=80, size=size.tiny,  editable=true)
  134. plotshape(showSignals?weakSellSignal2:na, title="Weaker Sell", style=shape.arrowdown, location=location.abovebar, color=#054e92, transp=50, size=size.small, editable=true)
  135.  
  136.  
  137. //variance graph
  138. //varianceGraphTop = bobMid+(bbsVariance/4)
  139. //varianceGraphLow = bobMid-(bbsVariance/4)
  140. //vgt = plot(showVariance ? varianceGraphTop : na, transp=100, editable=false, title="Variance Top")
  141. //vgb = plot(showVariance ? varianceGraphLow : na, transp=100, editable=false, title="Variance Bottom")
  142. //vgc = bbsVariance<min(bbsCloseVariancePointLow,bbsCloseVariancePointHigh)? white : bbsVariance<max(bbsCloseVariancePointLow,bbsCloseVariancePointHigh)? #56A4D8 : #285774
  143. //fill(vgt,vgb,color=vgc,transp=80)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement