Bollinger Bands Strategy

//@version=5
strategy(“Bollinger Bands Strategy”, overlay=true)

// Input parameters for Bollinger Bands
length = input.int(20, 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, color=color.blue, title=”Basis”)
plot(upper, color=color.red, title=”Upper Band”)
plot(lower, color=color.green, 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, color=color.green, style=shape.labelup, text=”BUY”)
plotshape(series=sellCondition, location=location.abovebar, color=color.red, style=shape.labeldown, text=”SELL”)