Stochastic oscillator: %K, %D, settings and how to test signals
The stochastic oscillator shows where the latest close sits inside the high-low range of recent bars, on a scale of 0 to 100. Its signals help in sideways markets and fail repeatedly in strong trends, so test them with costs and check that nearby settings give similar results.
By Soren Huang · Published · updated · Editorial policy
Contents
- How does the stochastic oscillator work?
- What are %K and %D?
- What is the difference between fast, slow and full stochastic?
- What do overbought, oversold and crossover signals mean?
- Why does the stochastic fail in strong trends?
- How do you backtest a stochastic rule?
- How do you check whether your stochastic settings are stable?
- Frequently asked questions
- References
The stochastic oscillator measures where the latest close sits inside the highest high and lowest low of a set number of recent bars, on a scale from 0 to 100. %K is that raw position. %D is a moving average of %K. Traders use readings above 80 or below 20, and %K crossing %D, as signals.
How does the stochastic oscillator work?
The stochastic oscillator works by comparing the close with the recent trading range, not with an average price. Think of a thermometer that shows where today's temperature sits between the coldest and the warmest day of the last two weeks. A reading of 100 means today is as warm as the warmest day. A reading of 0 means it is as cold as the coldest day.
The stochastic oscillator is an indicator that applies the same idea to price. It takes a lookback, which is the number of recent bars the calculation looks at, and finds the highest high and the lowest low in that window. Then it asks how far up that range the close is.
The raw value, called %K, is calculated like this:
%K = 100 × (C − L) ÷ (H − L)
- C is the close of the current bar.
- L is the lowest low of the last n bars, including the current bar.
- H is the highest high of the last n bars, including the current bar.
- n is the lookback. A common default is 14 bars.
Hypothetical example
Over the last 14 bars the highest high is 110 and the lowest low is 90, so the range is 20 points. The current bar closes at 105. The close is 15 points above the low, and 15 ÷ 20 = 0.75, so %K = 75. The close sits three quarters of the way up the recent range.
One edge case matters for backtests. If every bar in the window has the same high and low, the range is zero and the formula divides by zero. Platforms handle this case in different ways, so check what yours does before you compare results between tools.
What are %K and %D?
%K is the main line and %D is its slower signal line. %K is the raw position of the close in the range, as calculated above. %D is a simple moving average of %K, usually over 3 bars. Because %D trails %K, the two lines cross when the direction of %K changes, and those crossings are the basis of most stochastic signals.
Raw %K jumps around a lot on short bars. One wide bar can move it from 20 to 90 in a single step. Smoothing reduces those jumps, and different amounts of smoothing produce the fast, slow and full versions described next.
What is the difference between fast, slow and full stochastic?
The three versions use the same range calculation and differ only in how much smoothing they apply. Your chart platform may call the same thing by different names, so look at the settings, not the label.
| Version | %K line | %D line | Typical settings |
|---|---|---|---|
| Fast stochastic | Raw %K, no smoothing | 3-bar average of raw %K | 14, 3 |
| Slow stochastic | 3-bar average of raw %K (the fast %D) | 3-bar average of the slow %K | 14, 3, 3 |
| Full stochastic | Raw %K averaged over a period you choose | Average of that line over a period you choose | 14, 3, 3 by default, all three adjustable |
The full stochastic contains the other two. With %K smoothing set to 1 it is the fast stochastic. With %K smoothing set to 3 it is the standard slow stochastic. That is why "stochastic settings" usually means three numbers: lookback, %K smoothing and %D smoothing.
The KD indicator common on Taiwanese charting software is a close relative. It usually uses a 9-bar lookback and a smoothing that gives each new value one third of the weight and the previous value two thirds. The lines look similar to a slow stochastic but are not numerically the same. The KD indicator guide covers that version in detail.
What do overbought, oversold and crossover signals mean?
Overbought and oversold readings say the close is near the edge of its recent range, and crossovers say %K has turned. Neither one says that price must reverse. Traders combine them into rules like these:
- Level signal. A reading above 80 is called overbought: the close is in the top fifth of the range. A reading below 20 is called oversold: the close is in the bottom fifth.
- Crossover signal. %K crossing above %D is read as a buy signal. %K crossing below %D is read as a sell signal.
- Crossover inside a zone. Many traders only take a buy crossover when both lines are below 20, and a sell crossover when both are above 80. This filters out crossings in the middle of the range.
- Exit from a zone. Another version waits for %K to move back above 20 (for longs) or back below 80 (for shorts). This enters later but avoids buying while the reading is still falling.
- Divergence. Price makes a new low while the stochastic makes a higher low, or the reverse at a high. This is harder to define as an exact rule, so it is harder to test.
The 80 and 20 levels are conventions, not natural limits. Some traders use 70 and 30. In a backtest they are parameters like any other.
Why does the stochastic fail in strong trends?
In a strong trend the stochastic stays at an extreme for a long time, so "overbought means sell" keeps giving losing signals. The reason is in the formula. In a steady uptrend each close is near the top of the recent range, so %K sits above 80 bar after bar. Overbought here only means the trend is working.
A rule that sells every time %K crosses below %D above 80 will short again and again during that rise. Each crossing is a small dip inside the trend, and the trend then continues. The same happens in reverse in a steady downtrend, where buy signals below 20 keep failing.
Traders deal with this in three common ways, and each one is something to test, not assume:
- Trade with a trend filter. Take only buy signals when price is above a long moving average, and only sell signals when it is below.
- Skip trades when trend strength is high. For example, ignore reversal signals when the ADX, an indicator that measures how strong a trend is regardless of its direction, is above a chosen level.
- Use the stochastic as a pullback tool. In an uptrend, buy when the stochastic dips into oversold, rather than selling when it is overbought.
How do you backtest a stochastic rule?
You backtest a stochastic rule by writing it down so exactly that two people would place the same trades, then running it with realistic execution and costs. Vague rules such as "buy when it looks oversold" cannot be tested. These steps turn an idea into a test:
- Fix the version. Write the lookback, %K smoothing and %D smoothing, for example 14, 3, 3, and the type of average used.
- Write the entry. For example: buy when slow %K crosses above slow %D and both are below 20.
- Write the exit. For example: sell when %K crosses below %D above 80, or after a fixed stop or a fixed number of bars. A rule without an exit is not complete.
- Use next-bar execution. The stochastic is known only after the bar closes, so fill the order on the next bar, not at the close that created the signal.
- Include commission and slippage from the first run. Crossover rules on short bars can trade often, and frequent small trades are the first to fail when costs are added. See trading costs in backtests for how to compare the average trade with the round-trip cost.
- Set the session. Decide whether the bars include the night session. The range, and therefore %K, changes when overnight bars are added.
- Check long and short trades separately. In a market with a long upward drift, the buy side and the sell side of the same rule can behave very differently.
How do you check whether your stochastic settings are stable?
Settings are stable when nearby values give similar results. If only one exact combination works, the backtest has most likely found noise. Parameter stability is the property that a strategy's results change only gradually when its parameters change a little.
A stochastic rule has many knobs: lookback, two smoothing periods, two zone levels and the exit. Test enough combinations and one will look excellent by chance alone. Sullivan, Timmermann and White (1999) showed how testing many technical trading rules on the same data makes the best one look better than it is. Bailey and co-authors (2017) give a way to estimate how likely it is that the best backtest in a search is overfitted.
A practical check is a grid of results. Run the rule across a range of each parameter, then look at the pattern, not just the best cell.
Hypothetical example
You test lookbacks from 5 to 30 bars and oversold levels of 10, 15, 20, 25 and 30, with the exit and costs fixed. That is 26 × 5 = 130 combinations.
Result A, a spike: lookback 14 with level 20 shows a positive net result, but lookbacks 13 and 15 at the same level are negative. This is a single lucky cell.
Result B, a plateau: every lookback from 10 to 20 at levels 15 to 25 shows a positive net result with similar drawdown. The exact setting matters less, which is what you want to see.
After the grid, run a walk-forward analysis: choose the settings on one window of history, then test them unchanged on the following period, and repeat. If the chosen lookback jumps from 6 to 28 to 11 between windows, the rule has no stable preference, and the full-history result is not a reliable guide.
What this means if you use AlphaPrime (Valynox): strategy generation lets you set parameter ranges, entry and exit conditions and search constraints, so lookbacks and zone levels are searched as ranges with costs and next-bar execution included. You then judge the surviving rules by walk-forward results and your own thresholds in strategy validation, not by the single best number.
Frequently asked questions
What are the best stochastic settings?
There is no single best setting. The common default is 14, 3, 3 for the slow stochastic, and many Taiwanese platforms use 9 bars for KD. The useful question is whether a range of settings around your choice gives similar backtest results after costs.
Is the KD indicator the same as the stochastic oscillator?
They are built on the same idea: the position of the close inside the recent high-low range. The usual KD version uses a 9-bar lookback and a smoothing that gives the newest value one third of the weight. Its lines look like a slow stochastic but the values differ slightly, so specify which one you test.
What is the difference between the stochastic oscillator and RSI?
The stochastic compares the close with the highest high and lowest low of recent bars. RSI compares the size of recent up moves with recent down moves. Both run from 0 to 100 and are used for overbought and oversold readings, but they can disagree on the same chart.
Does the stochastic oscillator work on intraday futures charts?
It can be calculated on any bar size, but short bars give more crossings and therefore more trades. More trades mean commission and slippage take a larger share of each trade's result. Test it on your bar size with costs and the correct session included.
References
- Sullivan, R., Timmermann, A., & White, H. (1999). Data-Snooping, Technical Trading Rule Performance, and the Bootstrap. The Journal of Finance, 54(5), 1647–1691.
- Bailey, D. H., Borwein, J., López de Prado, M., & Zhu, Q. J. (2017). The Probability of Backtest Overfitting. Journal of Computational Finance, 20(4), 39–69.
What AlphaPrime does here
AlphaPrime's strategy generation turns rule ideas into testable entry and exit conditions with parameter ranges, and backtesting evaluates them in batches with fees, slippage, next-bar execution and trading sessions included.

