Indicators · condition block
Bollinger Bands
The Bollinger Bands block fires when the close lands outside a band drawn a set number of standard deviations above or below a moving average. Bands widen when the market is volatile and narrow when it is quiet.
How the engine evaluates it
The middle line is the simple moving average of the last N closes. The bands sit k standard deviations above and below it, using the population standard deviation of those same closes.
below bb_lower fires while the close is strictly below the lower band; above bb_upper while it is strictly above the upper band. Both are states.
The block needs N closes. Before your start date the engine loads extra history (at least 50 candles, up to 5,000, scaled to the longest indicator in the bot) so the indicator has settled by the first candle of the run.
mid = SMA(close, N)
sd = sqrt(mean(close²) − mid²) (population standard deviation)
upper = mid + k × sd, lower = mid − k × sd
Botscript syntax
In the Lab you pick this block from the Indicators group and fill in its fields. In the code view the same condition is written like this:
price below bb_lower(20,2)
price above bb_upper(20,2)
Settings
| Setting | Values | Default | Notes |
|---|---|---|---|
| Band | above upper, below lower | below lower | Which band the close must break. |
| Length | 2 – 5,000 | 20 | Window for the average and deviation. |
| Width | 0 – 100 standard deviations | 2 | 2 is John Bollinger's standard. |
| Timeframe | the run's candles, or 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d, 1w | the run's candles | Optional. At least the run's candle size and a whole multiple of it. |
On a higher timeframe
Add a timeframe at the end to read Bollinger Bands from bigger candles than the run uses, for example price below bb_lower(20,2) on 4h. The engine builds those candles from the run's own candles (UTC, weeks start on Monday) and evaluates the block only on candles that have closed, so there is no look-ahead. Lengths and lookbacks count the bigger candles.
Bollinger Bands is a state on a higher timeframe: the result from the last completed bigger candle holds on every run candle until the next one closes.
The timeframe must be at least the run's candle size and a whole multiple of it; otherwise the run stops with an error and is not charged. Multi-timeframe conditions covers the details, including warm-up.
How traders use it
- Mean reversion: buy
price below bb_lower(20,2), sell when price returns above the middle or touches the upper band. - Breakouts: a close above the upper band with rising ADX can mark the start of a trend.
- Volatility squeeze setups by combining with the volatility block.
Example bot
bot "Band Bounce"
when price below bb_lower(20,2) and rsi(14) < 35
then buy 50%
when price above bb_upper(20,2)
then sell 100%
- Buys half the cash when the close is below the lower band and RSI confirms weakness.
- Sells when the close pushes above the upper band.
Pitfalls
- In a strong trend price can ride a band for many candles: buying every close under the lower band in a crash is expensive.
- Closes, not wicks, are compared with the bands.
Bollinger Bands: frequently asked questions
Which standard deviation is used?
The population standard deviation of the last N closes (dividing by N), which is what most charting tools use for Bollinger Bands.
Does touching the band count?
No. The close must be strictly beyond the band.
Can I trade the middle band?
The middle band is the SMA with the same length, so use price crosses above sma(20) for it.
What is a good width?
2 standard deviations is the standard. Wider bands give fewer, more extreme signals.
Related blocks
Simulated results are hypothetical. They include only the fee and slippage you set on the bot, not every real-world cost or market condition, and past performance does not guarantee future results. Nothing here is financial advice.