Bollinger Bands (bb)
[[bb]] places two bands around a moving average using standard deviation. You can use it to visualize recent volatility, locate price relative to its short- or medium-term equilibrium, or compare multiple parameter setups in grid search.
When the bands widen, volatility increases. When they tighten, the market moves through a quieter phase. The middle line represents the average price over the selected period. The upper and lower bands show how far price moves away from that average.
Block declaration
A strategy can contain multiple [[bb]] blocks.
Each block produces three series: upper, middle, and lower. Each series is identified using the keys upper_id, middle_id, and lower_id.
You can override the source, symbol, or timeframe per block to run grid searches across different dimensions.
Examples
Minimal setup
This block computes Bollinger Bands using the block defaults: source = "close", length = 20, multiplier_upper = 2.0, multiplier_lower = 2.0, and type = "sma".
[[bb]]
upper_id = "bb_up"
middle_id = "bb_mid"
lower_id = "bb_low"Custom fixed setup
This block uses custom length, multiplier_upper, and multiplier_lower values.
[[bb]]
upper_id = "bb_up"
middle_id = "bb_mid"
lower_id = "bb_low"
length = 22
multiplier_upper = 2.2
multiplier_lower = 2.2Search for the optimal period and standard deviation multipliers
This block explores ranges of values for length, multiplier_upper, and multiplier_lower to identify the best-performing combination.
[[bb]]
upper_id = "bb_up"
middle_id = "bb_mid"
lower_id = "bb_low"
length.start = 15
length.stop = 25
length.step = 1
multiplier_upper.start = 1.5
multiplier_upper.stop = 2.5
multiplier_upper.step = 0.2
multiplier_lower.start = 1.5
multiplier_lower.stop = 2.5
multiplier_lower.step = 0.2Middle moving average type (multiple)
This block tests two moving average types for the middle band: sma and ema.
[[bb]]
upper_id = "bb_up"
middle_id = "bb_mid"
lower_id = "bb_low"
type = ["sma", "ema"]Parameters
| Parameter | Description |
|---|---|
upper_idString Required | Name of the upper band series. Example: upper_id = "bb_up" |
middle_idString Required | Name of the middle band series. Example: middle_id = "bb_mid" |
lower_idString Required | Name of the lower band series. Example: lower_id = "bb_low" |
sourceString or Array Optional | Input series used for the calculation. Accepted forms: source = "hl2" or source = ["close", "hl2"].Each value can be either a standard price source ( open, close, high, low, hl2, hlc3, ohlc4, hlcc4, volume) or the id of another indicator.Default value: "close" |
lengthInteger Optional | Period for the moving average and standard deviation; must be strictly greater than 1. Default value: 20.Usage: • Fixed: length = value with value >= 2• Grid search: – length.start = min_value with min_value >= 2– length.stop = max_value– length.step = value (optional, default 1) |
multiplier_upperDecimal Optional | Multiplier for the upper band; the value must be strictly greater than 0. Default value: 2.0.Usage: • Fixed: multiplier_upper = 2.0• Grid search: – multiplier_upper.start = min_value– multiplier_upper.stop = max_value– multiplier_upper.step = value (optional, default 1) |
multiplier_lowerDecimal Optional | Multiplier for the lower band; the value must be strictly greater than 0. Default value: 2.0.Usage: • Fixed: multiplier_lower = 2.0• Grid search: – multiplier_lower.start = min_value– multiplier_lower.stop = max_value– multiplier_lower.step = value (optional, default 1) |
typeString or Array Optional | Moving average type for the middle band. Default: "sma".Allowed values: "sma", "ema", "wma", "dema", "tema", "trima", "smma", "zlema", "rma".• Single: type = "ema"• Multiple: type = ["sma", "ema", "rma"] |
symbolString or Array Optional | Market symbol(s) used when source only consists of standard prices (open, close, high, low, hl2, hlc3, ohlc4, hlcc4, volume).If source mixes standard prices and indicator ids, symbol is applied only to combinations based on standard prices.If source contains only indicator ids, symbol is ignored.If omitted, the block inherits the [backtest] symbol.For symbol format, arrays, and alignment rules, see Exchanges, Symbols and Timeframes. |
timeframeString or Array Optional | Timeframe on which this indicator is computed. If timeframe is omitted, the computation uses the grid’s main timeframe defined in [backtest].For accepted formats and timeframe alignment rules, see Exchanges, Symbols and Timeframes. |
Available variables
Use the identifiers below directly in your expressions. The Bollinger Bands block exposes three numeric series (upper, middle, lower) and their parameters (length and multipliers), along with the input source, symbol, and timeframe.
Assume the block is configured as:
[[bb]]
upper_id = "bb_up"
middle_id = "bb_mid"
lower_id = "bb_lo"Then:
| Variable | Description |
|---|---|
bb_up or bb_up[0]Decimal | Current upper band value. |
bb_mid or bb_mid[0]Decimal | Current middle band (chosen MA type). |
bb_lo or bb_lo[0]Decimal | Current lower band value. |
bb_up[n]bb_mid[n]bb_lo[n]Decimal | Band value from n candles ago. |
bb_up.lengthbb_mid.lengthbb_lo.lengthDecimal | Moving average and standard deviation length in use. |
bb_up.multiplier_upperbb_mid.multiplier_upperbb_lo.multiplier_upperDecimal | Upper multiplier in use. |
bb_up.multiplier_lowerbb_mid.multiplier_lowerbb_lo.multiplier_lowerDecimal | Lower multiplier in use. |
bb_up.sourcebb_mid.sourcebb_lo.sourceString | Input series name. |
bb_up.symbolbb_mid.symbolbb_lo.symbolString | Symbol used. |
bb_up.timeframebb_mid.timeframebb_lo.timeframeString | Timeframe used. |
bb_up.typebb_mid.typebb_lo.typeString | Selected middle-band moving average type (sma, ema, …). |
Notes
- Numeric variables support arithmetic, comparisons, and logical operators.
- Text variables are strings intended for equality/inequality checks only.