Rate of Change (roc)
[[roc]] compares the current value of source with its value length candles ago and expresses the difference relative to that starting value. For the usual case of a positive price source, +10 means that the current value is 10% above the start of the interval, -10 means that it is 10% below, and 0 means that it is unchanged. The series moves around zero and has no fixed bounds.
Because ROC measures relative change rather than a raw difference, it normalizes away the nominal price level: moves that occur at different price scales use the same percentage scale. length sets the distance between the two values being compared, so a short length covers a recent interval while a larger length covers a longer one. Only the two endpoints are compared; the path between them does not affect the result.
Block declaration
A strategy can contain multiple [[roc]] blocks. Each block outputs one numeric series, exposed through the block id.
Examples
Minimal setup
This block computes ROC on the close series. Because source and length are omitted, the defaults are source = "close" and length = 9.
[[roc]]
id = "roc"Custom fixed setup
This block computes ROC on hlc3 with a 20 candle lag.
[[roc]]
id = "roc"
source = "hlc3"
length = 20Finding the optimal length
This block explores several length values to identify the lag that best fits the strategy.
[[roc]]
id = "roc"
source = "close"
length.start = 5
length.stop = 30
length.step = 5Positive momentum filter
This example uses ROC inside a condition. The condition becomes true when the current source is above its value from length candles ago.
[[roc]]
id = "roc"
[[condition]]
id = "positive_momentum"
condition = "roc > 0"
next_block_id = "..."Parameters
| Parameter | Description |
|---|---|
idString Required | Unique identifier for the resulting series. |
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 | Lag in bars; when set, it must be between 1 and 10000.If length is omitted, the default value is 9.Usage: • Fixed: length = value• Grid search: – length.start = min_value– length.stop = max_value– length.step = value (optional, default 1) |
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 symbol is omitted, the block inherits the symbol defined in [backtest].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 ROC block exposes a numeric series and its length, along with the input source, symbol, and timeframe.
Assuming the block is configured as follows:
[[roc]]
id = "roc"Then:
| Variable | Description |
|---|---|
roc or roc[0]Decimal | Current rate of change as a percentage. The first length samples, plus any source warmup, are NaN. |
roc[n]Decimal | ROC value n candles ago. |
roc.lengthDecimal | Length being used. |
roc.sourceString | Name of the input series. |
roc.symbolString | Market symbol used by this block. |
roc.timeframeString | Timeframe used for the computation. |
Notes
- Numeric variables can be freely combined in formulas (arithmetic, comparisons, logical operators).
- Text variables are strings intended only for equality and inequality comparisons.
- The directional reading of the sign assumes that the past value is positive. A negative past value reverses that reading through the denominator.
- If the
sourcevaluelengthcandles ago is zero, ROC is positive or negative infinity according to the sign of the current value. If the current value is also zero, ROC isNaN.