Stochastic RSI (stoch_rsi)
[[stoch_rsi]] measures momentum from RSI values instead of raw price. It is more responsive than RSI.
The block exposes two numeric series on a 0 to 100 scale. %K is the main smoothed line. %D is a simple moving average of %K, which gives you a steadier read.
Overbought and oversold zones are not fixed. You define these thresholds in your strategy and can explore them with grid search. The 80 and 20 levels are common references.
Block declaration
A strategy can contain multiple [[stoch_rsi]] blocks.
Each block produces two numeric series: %K and %D.
Examples
Minimal setup
This block computes StochRSI on close and keeps optional parameters at their default values, shown in comments.
[[stoch_rsi]]
k_id = "stoch_k"
d_id = "stoch_d"
# source = "close"
# rsi_length = 14
# stoch_length = 14
# smooth_k = 3
# smooth_d = 3Custom fixed setup
This block uses a custom source and custom values.
[[stoch_rsi]]
k_id = "stoch_k"
d_id = "stoch_d"
source = "hl2"
rsi_length = 12
stoch_length = 16
smooth_k = 4
smooth_d = 4Search for the optimal lengths
This block uses hl2 as the source and explores ranges of values for the RSI, stochastic, and smoothing windows to identify the best-performing configuration.
[[stoch_rsi]]
k_id = "stoch_k"
d_id = "stoch_d"
source = "hl2"
rsi_length.start = 10
rsi_length.stop = 20
rsi_length.step = 2
stoch_length.start = 10
stoch_length.stop = 20
stoch_length.step = 2
smooth_k.start = 2
smooth_k.stop = 6
smooth_d.start = 2
smooth_d.stop = 6Searching overbought and oversold zones
This example combines StochRSI with [[constant]] blocks to optimize overbought and oversold thresholds while enforcing oversold < overbought.
[[constant]]
id = "overbought"
start = 70
stop = 90
step = 5
[[constant]]
id = "oversold"
start = 10
stop = 30
step = 5
[[stoch_rsi]]
k_id = "stoch_k"
d_id = "stoch_d"
# source = "close"
# rsi_length = 14
# stoch_length = 14
# smooth_k = 3
# smooth_d = 3
[constraints]
condition = "oversold < overbought"
[[condition]]
id = "k_above_overbought"
condition = "stoch_k > overbought"
next_block_id = "..."
[[condition]]
id = "k_below_oversold"
condition = "stoch_k < oversold"
next_block_id = "..."Parameters
| Parameter | Description |
|---|---|
k_idd_idString Required | Unique identifiers for the %K and %D 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" |
rsi_lengthInteger Optional | Window used for the internal RSI; must be ≥ 2 when specified. If rsi_length is omitted, the default value is 14.Usage: • Fixed: rsi_length = value• Grid search: – rsi_length.start = min_value– rsi_length.stop = max_value– rsi_length.step = value (optional, default 1) |
stoch_lengthInteger Optional | Window used by the stochastic applied to the RSI; must be ≥ 1 when specified. If stoch_length is omitted, the default value is 14.Usage: • Fixed: stoch_length = value• Grid search: – stoch_length.start = min_value– stoch_length.stop = max_value– stoch_length.step = value (optional, default 1) |
smooth_kInteger Optional | Moving-average length applied to the raw stochastic to produce %K; must be ≥ 1 when specified. If smooth_k is omitted, the default value is 3.Usage: • Fixed: smooth_k = value• Grid search: – smooth_k.start = min_value– smooth_k.stop = max_value– smooth_k.step = value (optional, default 1) |
smooth_dInteger Optional | Moving-average length applied to %K to obtain %D; must be ≥ 1 when specified. If smooth_d is omitted, the default value is 3.Usage: • Fixed: smooth_d = value• Grid search: – smooth_d.start = min_value– smooth_d.stop = max_value– smooth_d.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 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 StochRSI block exposes two numeric series (%K and %D) and their lengths, along with the input source, symbol, and timeframe.
Assume the block is configured as:
[[stoch_rsi]]
k_id = "stoch_k"
d_id = "stoch_d"Then:
| Variable | Description |
|---|---|
stoch_k orstoch_k[0]Decimal | Current %K value. |
stoch_d orstoch_d[0]Decimal | Current %D value. |
stoch_k[n]Decimal | %K value from n candles ago. |
stoch_d[n]Decimal | %D value from n candles ago. |
stoch_k.rsi_lengthDecimal | Internal RSI window. |
stoch_k.stoch_lengthDecimal | Stochastic window applied to the RSI. |
stoch_k.smooth_kDecimal | Smoothing length for %K. |
stoch_k.smooth_dDecimal | MA length applied to %K to produce %D. |
stoch_k.sourceString | Input series name for %K. |
stoch_d.sourceString | Input series name for %D. |
stoch_k.symbolString | Symbol used for %K. |
stoch_d.symbolString | Symbol used for %D. |
stoch_k.timeframeString | Timeframe used for %K. |
stoch_d.timeframeString | Timeframe used for %D. |
Notes
- Numeric variables support arithmetic, comparisons, and logical operators.
- Text variables are strings intended for equality/inequality checks only.