Variance

[[variance]] measures how dispersed a numeric series is around its mean over a sliding window length.

It provides a pure volatility measure. High variance means recent values are spread out, while low variance means they are tightly clustered. The biased flag lets you choose between population and sample variance.

Examples

Variance on the closing price

Compute a single variance series on the closing price with a fixed 20-period window.

[[variance]]
id     = "variance"
length = 20
# source = "close"
# biased = true

Finding the optimal length

This block explores a range of values for length to identify the best-performing length.

[[variance]]
id           = "variance"
length.start = 10
length.stop  = 30
# source     = "close"
# biased     = true

Sample variance on a moving average

Compute the sample variance (biased = false) of a 50-period SMA.

[[moving_average]]
id     = "ma"
type   = "sma"
length = 50

[[variance]]
id     = "variance"
source = "ma"
length = 20
biased = false

Variance‑based volatility filter

Grid‑search both the variance window and the volatility threshold; downstream logic runs only when the variance of recent prices exceeds the searched threshold.

[[variance]]
id           = "variance"
source       = ["close", "hlc3"]
length.start = 10
length.stop  = 40

[[constant]]
id    = "variance_threshold"
start = 30.0
stop  = 70.0

[[condition]]
id            = "high_volatility"
condition     = "variance > variance_threshold"
next_block_id = "..."

Parameters

ParameterDescription
id
 String
 Required
Unique series name.
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 or Range
 Required
Variance window. It must be ≥ 1 when biased = true, and ≥ 2 when biased = false.

Usage:
• Fixed: length = value
• Grid search:
 – length.start = min_value
 – length.stop = max_value
 – length.step = value (optional, default 1)
biased
 Boolean
 Optional
Chooses between population and sample variance. When true, the block uses population variance (division by N). When false, it uses sample variance (division by N-1). Default is true.
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).
Symbols must include the exchange prefix in the EXCHANGE:SYMBOL format (for example "KUCOIN:BTCUSDT").
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 symbol from [backtest].
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 variance block exposes one numeric series, its parameters, and the input metadata.

Assume the block is configured as:

[[variance]]
id     = "variance"
length = 20

Then:

VariableDescription
variance or variance[0]
Decimal
Current variance value.
variance[n]
Decimal
Variance value from n candles ago.
variance.length
Decimal
Window length in use.
variance.biased
Decimal
Bias flag in use, 1 for population variance and 0 for sample variance.
variance.source
String
Name of the input series.
variance.symbol
String
Symbol used (display format).
variance.timeframe
String
Timeframe used for this block.

Notes

  • The computation supports both population and sample variance using the same biased flag you configure in TOML.
  • When biased = false, the window must contain at least two values because sample variance uses an N-1 divisor.