Linear Regression (linreg)
[[linreg]] summarizes the last length values of source with the straight line that best represents the window as a whole. The block returns neither the slope nor every point on that line. Instead, it produces one series containing the line’s value at the position selected by offset on each candle.
With offset = 0, the line is read at the endpoint corresponding to the current candle. A positive offset selects an earlier point on the same line, while a negative offset extends it beyond that endpoint. This extension is calculated entirely from the values already present in the window and contains no information from future candles.
Block declaration
A strategy can contain multiple [[linreg]] blocks. Each block outputs one numeric series: the value of the regression line evaluated at the configured offset.
Examples
Minimal setup
This block fits a linear regression over 20 candles of the close series and reports the value of the line on the current candle (default offset = 0). It uses the main symbol and timeframe defined in [backtest].
[[linreg]]
id = "linreg"
length = 20Specific source
This block computes the regression on the hlc3 source (average of high, low, and close) instead of close.
[[linreg]]
id = "linreg"
source = "hlc3"
length = 100Source from another indicator
This block fits a regression line over the series produced by the rsi block, smoothing it over 10 candles.
[[rsi]]
id = "rsi"
length = 14
[[linreg]]
id = "rsi_linreg"
source = "rsi"
length = 10Search for the optimal length
This block explores a range of length values to identify the best-performing window.
[[linreg]]
id = "linreg"
length.start = 10
length.stop = 100
length.step = 10Applying an offset
This block reads the regression line 2 candles back from its current end. A positive offset moves the evaluation point earlier along the fitted line, producing a slightly delayed, smoother reading.
[[linreg]]
id = "linreg"
length = 50
offset = 2Projecting with a negative offset
This block evaluates the regression line one candle ahead of the current one. A negative offset extends the fitted line beyond the window, turning the indicator into a short-range projection of the trend.
[[linreg]]
id = "linreg"
length = 50
offset = -1Search for the optimal offset
This block explores a range of offset values, from a backward reading to a forward projection, to identify the most relevant one. The range can include negative values.
[[linreg]]
id = "linreg"
length = 50
offset.start = -3
offset.stop = 3Dedicated symbol and timeframe
In this configuration, the regression runs on the daily series from BINANCE:ETHUSDT, independently of the primary symbol and timeframe used by the rest of the strategy. See Exchanges, Symbols and Timeframes for alignment rules.
[[linreg]]
id = "linreg"
length = 50
symbol = "BINANCE:ETHUSDT"
timeframe = "D"Parameters
| Parameter | Description |
|---|---|
idString Required | Unique name identifying the linreg series; the block exposes one numeric series with this identifier. |
sourceString or Array Optional | Input series used for the calculation. Accepted forms: source = "hlc3" or source = ["close", "hlc3"].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 Required | Number of candles in the regression window; the line is fitted over this many source values. Must be ≥ 1. Note: with length = 1 the window holds a single point, no line can be fitted, and the series is NaN on every candle.Usage: • Fixed: length = value• Grid search: – length.start = min_value– length.stop = max_value– length.step = value (optional, default 1) |
offsetInteger Optional | Position at which the fitted line is read, counted back from the current candle. 0 reads the line on the current candle; a positive value reads an earlier point along the line; a negative value projects the line beyond the current candle. May be negative.If offset is omitted, the default value is 0.Usage: • Fixed: offset = value• Grid search: – offset.start = min_value– offset.stop = max_value– offset.step = value (optional, default 1) |
symbolString or Array Optional | Market symbol(s) that provide the source series when source contains 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.For format and alignment details, 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
The linreg block exposes one numeric series, its configuration parameters, and metadata about the input source, symbol, and timeframe. You can use these identifiers directly inside your expressions.
Assume the block is configured as:
[[linreg]]
id = "linreg"
source = "close"
length = 20Then:
| Variable | Description |
|---|---|
linreg or linreg[0]Decimal | Current value of the regression line, read at the configured offset. |
linreg[n]Decimal | Linreg value from n candles ago. |
linreg.lengthDecimal | Regression window length in use. |
linreg.offsetDecimal | Offset applied. |
linreg.sourceString | Name of the input source series (e.g., close, hlc3, or another indicator id). |
linreg.symbolString | Symbol from which the input data is read. |
linreg.timeframeString | Timeframe on which the linreg is computed. |
Notes
- During warm-up, the first
length - 1candles have no full window and the series isNaN. If any value inside the window isNaN, the result for that candle isNaNas well. - Numeric variables support arithmetic, comparisons, and logical operators.
- Text variables are strings intended for equality/inequality checks only.