Try this:
//@version=4
strategy(title="NoScoobies Bollinger Bands", overlay=true)
source = close
length = input(20, minval=1, title = "Period") //Length of the Bollinger Band
mult = input(1.5, minval=0.001, maxval=50, title = "Standard Deviation") // Use 1.5 SD for 20 period MA; Use 2 SD for 10 period MA
basis = sma(source, length)
dev = mult * stdev(source, length)
@Jomy, You wouldn't know how to remove the recurring alerts, would you?
Jomy
⋅
I use what I call an "on-until-off" switch to stop multiple alerts. I'll paste a chunk of code below which should work. Take out your alertcondition lines, and try replacing with this chunk of code:
//@version=4
strategy(title="NoScoobies Bollinger Bands", overlay=true)
source = close
length = input(20, minval=1, title = "Period") //Length of the Bollinger Band
mult = input(1.5, minval=0.001, maxval=50, title = "Standard Deviation") // Use 1.5 SD for 20 period MA; Use 2 SD for 10 period MA
basis = sma(source, length)
dev = mult * stdev(source, length)
upper = basis + dev
lower = basis - dev
long=crossover(source, basis)
short=crossunder(source, basis)
close_long=crossunder(source, upper)
close_short=crossover(source, lower)
if long
strategy.entry("Long", strategy.long)
if close_long
strategy.close("Long", when = strategy.position_size>0)
if short
strategy.entry("Short", strategy.short)
if close_short
strategy.close("Short", when = strategy.position_size<0)
plot(basis, color=color.red,title= "SMA")
p1 = plot(upper, color=color.blue,title= "UB")
p2 = plot(lower, color=color.blue,title= "LB")
fill(p1, p2)