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.2

Search 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.2

Middle 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

ParameterDescription
upper_id
 String
 Required
Name of the upper band series.
Example: upper_id = "bb_up"
middle_id
 String
 Required
Name of the middle band series.
Example: middle_id = "bb_mid"
lower_id
 String
 Required
Name of the lower band series.
Example: lower_id = "bb_low"
source
 String
 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"
length
 Integer
 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_upper
 Decimal
 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_lower
 Decimal
 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)
type
 String 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"]
symbol
 String
 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.
timeframe
 String
 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:

VariableDescription
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.length
bb_mid.length
bb_lo.length
Decimal
Moving average and standard deviation length in use.
bb_up.multiplier_upper
bb_mid.multiplier_upper
bb_lo.multiplier_upper
Decimal
Upper multiplier in use.
bb_up.multiplier_lower
bb_mid.multiplier_lower
bb_lo.multiplier_lower
Decimal
Lower multiplier in use.
bb_up.source
bb_mid.source
bb_lo.source
String
Input series name.
bb_up.symbol
bb_mid.symbol
bb_lo.symbol
String
Symbol used.
bb_up.timeframe
bb_mid.timeframe
bb_lo.timeframe
String
Timeframe used.
bb_up.type
bb_mid.type
bb_lo.type
String
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.