Bollinger Bands Strategy

strategy(“Bollinger Bands Strategy”, overlay=true)

// Input parameters for Bollinger Bands
length =, title=”Length”)
src = input(close, title=”Source”)
mult = input.float(2.0, title=”Multiplier”)

// Calculate Bollinger Bands
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis – dev

// Plot Bollinger Bands
plot(basis,, title=”Basis”)
plot(upper,, title=”Upper Band”)
plot(lower,, title=”Lower Band”)

// Buy condition: Price crosses below the lower band
buyCondition = ta.crossover(src, lower)
if (buyCondition)
strategy.entry(“Buy”, strategy.long)

// Sell condition: Price crosses above the upper band
sellCondition = ta.crossunder(src, upper)
if (sellCondition)
strategy.entry(“Sell”, strategy.short)

// Plot buy and sell signals on the chart
plotshape(series=buyCondition, location=location.belowbar,, style=shape.labelup, text=”BUY”)
plotshape(series=sellCondition, location=location.abovebar,, style=shape.labeldown, text=”SELL”)