Advertisement
Guest User

BB/KC Squeeze Play in Pinescript [v02] [LazyBear]

a guest
Feb 21st, 2014
3,649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. //
  2. // @author LazyBear [https://www.tradingview.com/u/LazyBear/]
  3. // @credits http://www.hiltinvestmentfund.com/html/squeeze.html
  4. // Trading strategy based on Bollinger Bands & Keltner Channel. Added SAR / Highlights to make it really easy ;)
  5. // v02 - Fixed the SqzRelease condition (only if both BB lines come out, it is marked as a "release").
  6. // - Changed the color codes [RED=>Sqz, GREEN=>Release]
  7. // - Updated the default multFactor to be 2
  8. // v01 - initial release
  9. //
  10. study(shorttitle = "TS 1 [LB]", title="Trading strategy [BB / KC] [LazyBear]", overlay=true)
  11.  
  12. length = input(20, minval=1, title="Length"), mult = input(2.0, minval=0.001, maxval=50, title="MultFactor")
  13. // showBarColor = input(true, title="Highlight Bear/Bull points (KC)", type=bool)
  14. showBarColor = false
  15. useTrueRange = input(true, title="Use TrueRange (KC)", type=bool)
  16. // Note that "highlightStrategy" takes precedence over showBarColor.
  17. highlightStrategy = input(true, title="Highlight strategy points", type=bool)
  18.  
  19. startSAR = input(0.02, title="Start (SAR)")
  20. incrementSAR = input(0.02, title="Increment (SAR)")
  21. maximumSAR = input(0.2, title="Maximum (SAR)")
  22.  
  23. // Calculate BB
  24. source = close
  25. basis = sma(source, length)
  26. dev = mult * stdev(source, length)
  27. upperBB = basis + dev
  28. lowerBB = basis - dev
  29. plot(basis, color=red, linewidth=2)
  30. p1 = plot(upperBB, color=red, linewidth=2)
  31. p2 = plot(lowerBB, color=red, linewidth=2)
  32. fill(p1, p2, color = red)
  33.  
  34. // Calculate KC
  35. ma = ema(source, length)
  36. range = useTrueRange ? atr(length) : high - low
  37. rangema = ema(range, length)
  38. upper = ma + rangema * mult
  39. lower = ma - rangema * mult
  40. c = lime
  41. u = plot(upper, color=c, title="Upper")
  42. plot(ma, color=c, title="Basis")
  43. l = plot(lower, color=c, title="Lower")
  44. fill(u, l, color=green, transp=80)
  45.  
  46. offset = 1
  47. bearish = low < lower
  48. bear_point = bearish ? (low-offset) : na
  49. bear_color = bearish ? red : na
  50. bullish = high > upper
  51. bull_point = bullish ? (high+offset) : na
  52. bull_color = bullish ? green : na
  53.  
  54. bar_color = bearish ? bear_color : (bullish ? bull_color : na)
  55. plot(bear_point, color = bear_color, style=cross, linewidth=2)
  56. plot(bull_point, color = bull_color, style=cross, linewidth=2)
  57.  
  58. bgcolor((showBarColor and not highlightStrategy) ? bar_color : na)
  59.  
  60. bb_inside_kc = ((upperBB < upper) and (lowerBB > lower))
  61. bb_outside_kc = ((upperBB > upper) and (lowerBB < lower))
  62. strat_sqz_color = bb_inside_kc ? red : (bb_outside_kc ? green : na)
  63. bgcolor(highlightStrategy ? strat_sqz_color : na)
  64.  
  65. // SAR
  66. outSAR = sar(startSAR, incrementSAR, maximumSAR)
  67. plot(outSAR, style=cross, color=blue)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement