Indicators · condition block
Stochastic oscillator
The stochastic block measures where the close sits within the high-low range of recent candles, from 0 (at the low) to 100 (at the high), and compares a smoothed reading with a level.
How the engine evaluates it
Raw %K is the close's position inside the range from the lowest low to the highest high of the last N candles, scaled to 0–100. If the range is zero, raw %K is 50.
The block uses smoothed %K: the simple average of the last smoothing raw values (3 by default). There is no separate %D line.
< and > compare strictly with the level and are states.
raw %K = (close − lowest low N) / (highest high N − lowest low N) × 100
%K = SMA(raw %K, smoothing)
Botscript syntax
In the Lab you pick this block from the Indicators group and fill in its fields. In the code view the same condition is written like this:
stoch(14,3) < 20
stoch(14,3) > 80
Settings
| Setting | Values | Default | Notes |
|---|---|---|---|
| Comparison | <, > | < | — |
| Level | 0 – 100 | 20 | 20 and 80 are the usual oversold and overbought lines. |
| %K length | 2 – 200 | 14 | Range window. |
| Smoothing | 1 – 20 | 3 | 1 means raw (fast) %K. |
| Timeframe | the run's candles, or 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d, 1w | the run's candles | Optional. At least the run's candle size and a whole multiple of it. |
On a higher timeframe
Add a timeframe at the end to read Stochastic from bigger candles than the run uses, for example stoch(14,3) < 20 on 4h. The engine builds those candles from the run's own candles (UTC, weeks start on Monday) and evaluates the block only on candles that have closed, so there is no look-ahead. Lengths and lookbacks count the bigger candles.
Stochastic is a state on a higher timeframe: the result from the last completed bigger candle holds on every run candle until the next one closes.
The timeframe must be at least the run's candle size and a whole multiple of it; otherwise the run stops with an error and is not charged. Multi-timeframe conditions covers the details, including warm-up.
How traders use it
- Oversold entries in an uptrend:
stoch(14,3) < 20with a moving-average filter. - Profit taking:
stoch(14,3) > 80withholding. - Fast timing inside a slower trend system.
Example bot
bot "Stochastic Dip"
when stoch(14,3) < 20 and supertrend(10,3) is bullish
then buy 50%
when stoch(14,3) > 80
then sell 100%
- Buys half the cash when smoothed %K falls below 20 while Supertrend says the trend is up.
- Sells when %K rises above 80.
Pitfalls
- Like RSI, stochastics can stay pinned at an extreme in a trend.
- Very short lengths react to noise on small candle sizes.
Stochastic: frequently asked questions
Is this fast or slow stochastic?
With smoothing 3 it is the slow %K (the smoothed line). Set smoothing to 1 for the fast, raw %K.
Is there a %D line?
Not as a separate condition. %D would be a further average of %K.
How does it relate to Williams %R?
Raw %K minus 100 equals Williams %R. They measure the same thing on different scales.
What happens in a flat range?
If the highest high equals the lowest low, raw %K is set to 50.
Related blocks
Simulated results are hypothetical. They include only the fee and slippage you set on the bot, not every real-world cost or market condition, and past performance does not guarantee future results. Nothing here is financial advice.