Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // @author LazyBear [https://www.tradingview.com/u/LazyBear/]
- // @credits http://www.hiltinvestmentfund.com/html/squeeze.html
- // Trading strategy based on Bollinger Bands & Keltner Channel. Added SAR / Highlights to make it really easy ;)
- // v02 - Fixed the SqzRelease condition (only if both BB lines come out, it is marked as a "release").
- // - Changed the color codes [RED=>Sqz, GREEN=>Release]
- // - Updated the default multFactor to be 2
- // v01 - initial release
- //
- study(shorttitle = "TS 1 [LB]", title="Trading strategy [BB / KC] [LazyBear]", overlay=true)
- length = input(20, minval=1, title="Length"), mult = input(2.0, minval=0.001, maxval=50, title="MultFactor")
- // showBarColor = input(true, title="Highlight Bear/Bull points (KC)", type=bool)
- showBarColor = false
- useTrueRange = input(true, title="Use TrueRange (KC)", type=bool)
- // Note that "highlightStrategy" takes precedence over showBarColor.
- highlightStrategy = input(true, title="Highlight strategy points", type=bool)
- startSAR = input(0.02, title="Start (SAR)")
- incrementSAR = input(0.02, title="Increment (SAR)")
- maximumSAR = input(0.2, title="Maximum (SAR)")
- // Calculate BB
- source = close
- basis = sma(source, length)
- dev = mult * stdev(source, length)
- upperBB = basis + dev
- lowerBB = basis - dev
- plot(basis, color=red, linewidth=2)
- p1 = plot(upperBB, color=red, linewidth=2)
- p2 = plot(lowerBB, color=red, linewidth=2)
- fill(p1, p2, color = red)
- // Calculate KC
- ma = ema(source, length)
- range = useTrueRange ? atr(length) : high - low
- rangema = ema(range, length)
- upper = ma + rangema * mult
- lower = ma - rangema * mult
- c = lime
- u = plot(upper, color=c, title="Upper")
- plot(ma, color=c, title="Basis")
- l = plot(lower, color=c, title="Lower")
- fill(u, l, color=green, transp=80)
- offset = 1
- bearish = low < lower
- bear_point = bearish ? (low-offset) : na
- bear_color = bearish ? red : na
- bullish = high > upper
- bull_point = bullish ? (high+offset) : na
- bull_color = bullish ? green : na
- bar_color = bearish ? bear_color : (bullish ? bull_color : na)
- plot(bear_point, color = bear_color, style=cross, linewidth=2)
- plot(bull_point, color = bull_color, style=cross, linewidth=2)
- bgcolor((showBarColor and not highlightStrategy) ? bar_color : na)
- bb_inside_kc = ((upperBB < upper) and (lowerBB > lower))
- bb_outside_kc = ((upperBB > upper) and (lowerBB < lower))
- strat_sqz_color = bb_inside_kc ? red : (bb_outside_kc ? green : na)
- bgcolor(highlightStrategy ? strat_sqz_color : na)
- // SAR
- outSAR = sar(startSAR, incrementSAR, maximumSAR)
- plot(outSAR, style=cross, color=blue)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement