-
Notifications
You must be signed in to change notification settings - Fork 0
/
bollinger_band_strategy.pinescript
37 lines (26 loc) · 1.44 KB
/
bollinger_band_strategy.pinescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//@version=4
// this script was taken from https://kodify.net/tradingview/strategies/bollinger-breakout/ and modified for demonstration purposes
strategy(title="Bollinger Breakout", overlay=true, initial_capital=1000)
smaLength = input(title="SMA Length", type=integer, defval=350)
ubOffset = input(title="Upper Band Offset", type=float, defval=2.5, step=0.5)
lbOffset = input(title="Lower Band Offset", type=float, defval=2.5, step=0.5)
//usePosSize = input(title="Use Position Sizing?", type=bool, defval=true)
//riskPerc = input(title="Risk %", type=float, defval=0.5, step=0.25)
smaValue = sma(close, smaLength)
stdDev = stdev(close, smaLength)
upperBand = smaValue + (stdDev * ubOffset)
lowerBand = smaValue - (stdDev * lbOffset)
//riskEquity = (riskPerc / 100) * strategy.equity
//atrCurrency = (atr(20) * syminfo.pointvalue)
//posSize = usePosSize ? floor(riskEquity / atrCurrency) : 1
plot(series=smaValue, title="SMA", color=teal)
plot(series=upperBand, title="UB", color=green, linewidth=2)
plot(series=lowerBand, title="LB", color=red, linewidth=2)
enterLong = crossover(close, upperBand)
exitLong = crossunder(close, smaValue)
enterShort = crossunder(close, lowerBand)
exitShort = crossover(close, smaValue)
strategy.entry(id="long_entry", long=true, qty=10, when=enterLong)
strategy.entry(id="short_entry", long=false, qty=10, when=enterShort)
strategy.close(id="long_entry", when=exitLong)
strategy.close(id="short_entry", when=exitShort)