Stochastic Oscillator (stoch)

[[stoch]] places the current close between the lowest low and highest high observed over the k_length window. %K expresses that position on a 0 to 100 scale: 0 corresponds to the window’s low, 100 to its high, and an intermediate value to the proportional position between them. It describes position within the recent range, not the direction of the move.

The block also exposes %D, a simple moving average of %K over d_length candles. %D changes more smoothly than %K while remaining on the same scale.

Block declaration

A strategy can contain multiple [[stoch]] blocks. Each block outputs two numeric series, %K and %D.

The [[stoch]] block does not accept a source key and always uses the standard high, low, and close price series.

Examples

Minimal setup

[[stoch]]
k_id = "stoch_k"
d_id = "stoch_d"
# k_length = 14
# d_length = 3

Custom fixed lengths

This block uses custom k_length and d_length values.

[[stoch]]
k_id     = "stoch_k"
d_id     = "stoch_d"
k_length = 12
d_length = 4

Search for the optimal lengths

This block explores the possible combinations of k_length and d_length to identify the best-performing configuration.

[[stoch]]
k_id           = "stoch_k"
d_id           = "stoch_d"
k_length.start = 10
k_length.stop  = 20
d_length.start = 2
d_length.stop  = 6

Searching overbought and oversold zones

This example combines [[stoch]] with two [[constant]] blocks to optimize overbought and oversold thresholds. The oversold < overbought constraint keeps combinations consistent, and the conditions use both %K and %D to reduce noise.

[[constant]]
id    = "overbought"
start = 70
stop  = 90
step  = 5

[[constant]]
id    = "oversold"
start = 10
stop  = 30
step  = 5

[[stoch]]
k_id = "stoch_k"
d_id = "stoch_d"
# k_length = 14
# d_length = 3

[constraints]
condition = "oversold < overbought"

[[condition]]
id            = "stoch_overbought"
condition     = "stoch_k > overbought and stoch_d > overbought"
next_block_id = "..."

[[condition]]
id            = "stoch_oversold"
condition     = "stoch_k < oversold and stoch_d < oversold"
next_block_id = "..."

Parameters

ParameterDescription
k_id
d_id
 String
 Required
Unique identifiers for the %K and %D series.
k_length
 Integer or Table
 Optional
Window used to compute %K (number of candles); must be ≥ 1 when specified.
If k_length is omitted, the default value is 14.

Usage:
• Fixed: k_length = 14
• Grid search:
 – k_length.start = min_value
 – k_length.stop = max_value
 – k_length.step = value (optional, default 1)
d_length
 Integer or Table
 Optional
Window length for the moving average of %K (number of candles); must be ≥ 1 when specified.
If d_length is omitted, the default value is 3.

Usage:
• Fixed: d_length = 3
• Grid search:
 – d_length.start = min_value
 – d_length.stop = max_value
 – d_length.step = value (optional, default 1)
symbol
 String
 or Array
 Optional
Market symbol(s) from which this block reads its candles (high, low, close). If symbol is omitted, the block inherits the primary 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.

The source key is not accepted by the [[stoch]] block.
The oscillator always relies on the high, low, and close series of the selected market and timeframe.

Available variables

The stoch block exposes two numeric series (%K and %D), the lengths used in the calculation, and the symbol and timeframe context.

Assume the block is configured as:

[[stoch]]
k_id = "stoch_k"
d_id = "stoch_d"

Then:

VariableDescription
stoch_k or
stoch_k[0]
Decimal
Current %K value.
stoch_d or
stoch_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.k_length
Decimal
Length used to compute %K (stochastic window).
stoch_k.d_length
Decimal
Length used to smooth %K and produce %D.
stoch_k.high_src
stoch_k.low_src
stoch_k.close_src
stoch_d.high_src
stoch_d.low_src
stoch_d.close_src
String
Names of the input sources used (high, low, and close) on each output.
stoch_k.symbol
String
Market symbol used for %K.
stoch_d.symbol
String
Market symbol used for %D.
stoch_k.timeframe
String
Timeframe used for %K.
stoch_d.timeframe
String
Timeframe used for %D.

Notes:

  • Numeric variables combine freely in expressions (arithmetic, comparisons, and logical operators).
  • Text variables are strings intended for equality and inequality checks.