Stochastic oscillator (stochastic indicator): the formula for %K and %D (with Excel)
The stochastic oscillator, often just called the stochastic indicator, shows where the close sits inside the last 14 candles' high–low range (%K) and the 3-period average of that (%D). Here is how it is worked out — with a worked example, fast vs slow vs full, what 80 and 20 measure, the Excel formulas, Python, a free Excel sheet and a box to calculate the stochastic from your own prices.
Last updated:
Needs: high, low, close · Standard settings: %K period 14 · %D period 3
The Stochastic formula
Highest high = highest High of the last 14 candles (including this one) Lowest low = lowest Low of the last 14 candles %K = (Close − Lowest low) ÷ (Highest high − Lowest low) × 100 %D = simple average of the last 3 %K values
%K is 0 when the close equals the 14-candle low and 100 when it equals the 14-candle high. If the high and the low are the same (no range at all), %K cannot be calculated.
How to calculate Stochastic step by step
- Find the highest high and lowest low of the last 14 candles. For Day 14 (Day 1 to Day 14) they are 1,090.25 and 986.00.
- Place the close inside that range. Day 14 closed at 1,067.95: %K = (1,067.95 − 986.00) ÷ (1,090.25 − 986.00) × 100 = 78.61.
- Move the window on one candle and repeat. %K is 74.34 on Day 15 and 42.40 on Day 16.
- Average the last three %K values for %D. Day 16: (78.61 + 74.34 + 42.40) ÷ 3 = 65.12.
- Repeat for every candle. On Day 60 the example's %K is 96.70 and %D is 91.94.
Worked example — and calculate Stochastic from your own prices
The table works through 60 daily candles of an imaginary stock, column by column — the same columns as the Excel sheet. To calculate Stochastic for your own stock, clear the box and paste your high, low, close from Excel or your broker's download.
Latest values
| Day | High | Low | Close | Highest high (14) | Lowest low (14) | %K | %D (3) |
|---|---|---|---|---|---|---|---|
| Day 1 | 1,010.00 | 990.60 | 1,001.15 | ||||
| Day 2 | 1,004.60 | 987.75 | 987.95 | ||||
| Day 3 | 1,011.50 | 986.00 | 1,009.15 | ||||
| Day 4 | 1,015.00 | 1,001.85 | 1,012.60 | ||||
| Day 5 | 1,012.00 | 990.80 | 993.25 | ||||
| Day 6 | 1,012.60 | 990.90 | 1,008.60 | ||||
| Day 7 | 1,018.95 | 1,003.80 | 1,013.60 | ||||
| Day 8 | 1,012.95 | 994.75 | 1,001.55 | ||||
| Day 9 | 1,033.25 | 995.30 | 1,031.55 | ||||
| Day 10 | 1,046.15 | 1,028.50 | 1,040.05 | ||||
| Day 11 | 1,041.80 | 1,027.70 | 1,033.15 | ||||
| Day 12 | 1,065.80 | 1,031.15 | 1,064.80 | ||||
| Day 13 | 1,086.00 | 1,056.85 | 1,083.55 | ||||
| Day 14 | 1,090.25 | 1,067.25 | 1,067.95 | 1,090.25 | 986.00 | 78.6091 | |
| Day 15 | 1,074.30 | 1,061.45 | 1,063.50 | 1,090.25 | 986.00 | 74.3405 | |
| Day 16 | 1,067.35 | 1,025.90 | 1,030.20 | 1,090.25 | 986.00 | 42.3981 | 65.1159 |
| Day 17 | 1,033.00 | 1,011.75 | 1,012.20 | 1,090.25 | 990.80 | 21.5184 | 46.0857 |
| Day 18 | 1,019.90 | 979.85 | 981.60 | 1,090.25 | 979.85 | 1.5851 | 21.8339 |
| Day 19 | 988.30 | 969.40 | 970.55 | 1,090.25 | 969.40 | 0.9516 | 8.0184 |
| Day 20 | 973.10 | 959.05 | 960.90 | 1,090.25 | 959.05 | 1.4101 | 1.3156 |
| Day 21 | 980.35 | 958.30 | 978.30 | 1,090.25 | 958.30 | 15.1573 | 5.8396 |
| Day 22 | 981.50 | 957.00 | 958.85 | 1,090.25 | 957.00 | 1.3884 | 5.9852 |
| Day 23 | 963.00 | 942.60 | 946.75 | 1,090.25 | 942.60 | 2.8107 | 6.4521 |
| Day 24 | 957.70 | 931.65 | 937.95 | 1,090.25 | 931.65 | 3.9723 | 2.7238 |
| Day 25 | 945.75 | 926.05 | 927.85 | 1,090.25 | 926.05 | 1.0962 | 2.6264 |
| Day 26 | 924.60 | 910.25 | 911.20 | 1,090.25 | 910.25 | 0.5278 | 1.8654 |
| Day 27 | 919.05 | 900.00 | 905.15 | 1,090.25 | 900.00 | 2.7070 | 1.4437 |
| Day 28 | 904.50 | 884.70 | 885.95 | 1,074.30 | 884.70 | 0.6593 | 1.2980 |
| Day 29 | 891.25 | 871.55 | 874.70 | 1,067.35 | 871.55 | 1.6088 | 1.6583 |
| Day 30 | 880.70 | 849.70 | 855.80 | 1,033.00 | 849.70 | 3.3279 | 1.8653 |
| Day 31 | 868.95 | 858.15 | 862.15 | 1,019.90 | 849.70 | 7.3149 | 4.0839 |
| Day 32 | 875.05 | 859.50 | 866.45 | 988.30 | 849.70 | 12.0851 | 7.5760 |
| Day 33 | 870.55 | 843.00 | 845.10 | 981.50 | 843.00 | 1.5162 | 6.9721 |
| Day 34 | 854.70 | 846.55 | 851.55 | 981.50 | 843.00 | 6.1733 | 6.5916 |
| Day 35 | 863.70 | 847.85 | 860.20 | 981.50 | 843.00 | 12.4188 | 6.7028 |
| Day 36 | 862.95 | 845.40 | 854.80 | 963.00 | 843.00 | 9.8333 | 9.4751 |
| Day 37 | 882.25 | 858.05 | 877.20 | 957.70 | 843.00 | 29.8169 | 17.3563 |
| Day 38 | 893.65 | 868.85 | 888.45 | 945.75 | 843.00 | 44.2336 | 27.9613 |
| Day 39 | 908.35 | 889.60 | 905.40 | 924.60 | 843.00 | 76.4706 | 50.1737 |
| Day 40 | 913.10 | 895.65 | 896.80 | 919.05 | 843.00 | 70.7429 | 63.8157 |
| Day 41 | 894.65 | 887.85 | 889.60 | 913.10 | 843.00 | 66.4765 | 71.2300 |
| Day 42 | 898.55 | 884.80 | 895.30 | 913.10 | 843.00 | 74.6077 | 70.6090 |
| Day 43 | 920.60 | 889.25 | 919.00 | 920.60 | 843.00 | 97.9381 | 79.6741 |
| Day 44 | 920.60 | 916.30 | 918.35 | 920.60 | 843.00 | 97.1005 | 89.8821 |
| Day 45 | 919.30 | 903.80 | 906.25 | 920.60 | 843.00 | 81.5077 | 92.1821 |
| Day 46 | 937.35 | 900.30 | 932.10 | 937.35 | 843.00 | 94.4356 | 91.0146 |
| Day 47 | 947.15 | 930.20 | 944.35 | 947.15 | 845.40 | 97.2482 | 91.0638 |
| Day 48 | 949.20 | 937.25 | 948.70 | 949.20 | 845.40 | 99.5183 | 97.0674 |
| Day 49 | 971.15 | 949.05 | 966.35 | 971.15 | 845.40 | 96.1829 | 97.6498 |
| Day 50 | 965.90 | 954.75 | 955.60 | 971.15 | 858.05 | 86.2511 | 93.9841 |
| Day 51 | 971.35 | 954.15 | 969.10 | 971.35 | 868.85 | 97.8049 | 93.4130 |
| Day 52 | 997.00 | 966.55 | 984.35 | 997.00 | 884.80 | 88.7255 | 90.9272 |
| Day 53 | 992.30 | 980.25 | 980.90 | 997.00 | 884.80 | 85.6506 | 90.7270 |
| Day 54 | 984.70 | 961.40 | 962.50 | 997.00 | 884.80 | 69.2513 | 81.2092 |
| Day 55 | 964.60 | 953.00 | 960.20 | 997.00 | 884.80 | 67.2014 | 74.0345 |
| Day 56 | 969.05 | 954.50 | 967.35 | 997.00 | 889.25 | 72.4826 | 69.6451 |
| Day 57 | 982.20 | 960.60 | 971.00 | 997.00 | 900.30 | 73.1127 | 70.9322 |
| Day 58 | 987.10 | 967.55 | 981.75 | 997.00 | 900.30 | 84.2296 | 76.6083 |
| Day 59 | 999.95 | 979.00 | 994.85 | 999.95 | 900.30 | 94.8821 | 84.0748 |
| Day 60 | 1,015.15 | 987.35 | 1,012.35 | 1,015.15 | 930.20 | 96.7039 | 91.9385 |
The example prices are made up for this page — they are not real market data. Stochastic is a calculation from past prices, not a forecast, a signal or advice to buy or sell. MyAlgoMate provides trading technology, not investment advice.
How to calculate Stochastic in Excel
Put the headings in row 1 and the first candle in row 2, oldest at the top. Then type these formulas — each column uses the ones to its left:
- A
Day
A2 downwards: your dates, oldest at the top.
- B
High
B2 downwards: the high prices, oldest at the top.
- C
Low
C2 downwards: the low prices, oldest at the top.
- D
Close
D2 downwards: the closing prices, oldest at the top.
- E
Highest high (14)
E15
=MAX(B2:B15)— then copy it down to the last row. - F
Lowest low (14)
F15
=MIN(C2:C15)— then copy it down to the last row. - G
%K
G15
=(D15-F15)/(E15-F15)*100— then copy it down to the last row. - H
%D (3)
H17
=AVERAGE(G15:G17)— then copy it down to the last row.
%K starts in row 15 (14 candles needed) and %D in row 17 (three %K values needed).
Prefer not to type them? The free sheet has every formula filled in for the example prices — replace the prices with yours and extend the rows. It opens in Microsoft Excel (we checked every value against the table above), and you can import it into Google Sheets or LibreOffice.
Download the free Stochastic Excel sheet (.xlsx)Fast, slow and full stochastic, side by side
The formula above is the fast stochastic. The slow stochastic (14, 3, 3) adds one more averaging step, and the full stochastic lets you choose both averaging lengths:
Fast: %K = raw %K %D = 3-period average of fast %K
Slow: %K = 3-period average of fast %K (the same as fast %D)
%D = 3-period average of slow %K
Full (14, X, Y): %K = X-period average of raw %K
%D = Y-period average of full %KThe example prices, all three lines on the same days:
| Day | Fast %K | Fast %D = slow %K | Slow %D |
|---|---|---|---|
| Day 20 | 1.41 | 1.32 | 10.39 |
| Day 30 | 3.33 | 1.87 | 1.61 |
| Day 40 | 70.74 | 63.82 | 47.32 |
| Day 50 | 86.25 | 93.98 | 96.23 |
| Day 60 | 96.70 | 91.94 | 84.21 |
In Excel each smoothing step is one more =AVERAGE column. Full (14, 1, 3) is the fast stochastic, and full (14, 3, 3) is the slow one. Stochastic RSI applies the same formula to RSI values instead of prices.
What 80 and 20 actually measure
A %K of 80 means the close is in the top fifth of the last 14 candles' high–low range; 20 means it is in the bottom fifth. Many charts draw lines at 80 and 20 and call the areas above and below "overbought" and "oversold". Those are labels for where the close sits in the recent range — they say nothing about what the price will do next. When the price keeps closing near its highs, %K can stay above 80 for a long time (and below 20 when it keeps closing near its lows).
Why the stochastic looks jumpy when the range is narrow
Every ₹1 change in the close moves %K by 100 ÷ (highest high − lowest low) points. With a wide 14-candle range a ₹1 move hardly shows; with a narrow range the same ₹1 swings %K a long way. On Day 14 of the example the range is ₹104.25, so each ₹1 moves %K by about 0.96 points.
| 14-candle range | %K points per ₹1 of close |
|---|---|
| ₹5 | 20.0 |
| ₹10 | 10.0 |
| ₹50 | 2.0 |
| ₹100 | 1.0 |
When the range is zero (high = low) the move would be infinite, which is why %K cannot be calculated then.
Divergence and crossovers, defined
- Divergence is when the price and the stochastic disagree: the price makes a lower low while %K makes a higher low, or the price makes a higher high while %K makes a lower high. It describes a mismatch between the price and where it closed in its range.
- A %K / %D crossover is the candle where %K moves above or below %D. Because %D is an average of %K, the two lines cross every time %K turns against its recent average — often, in a narrow range.
Both are descriptions of past prices, not forecasts or advice.
Stochastic vs RSI: two different calculations
The stochastic compares the latest close with the highest high and lowest low of the last 14 candles. The RSI compares the average size of up-closes with the average size of down-closes. Both run from 0 to 100, but they measure different things, so they often show different numbers on the same day. Charts usually draw the stochastic with lines at 80 and 20 and the RSI at 70 and 30. The stochastic uses the high and low as well as the close; the RSI uses closes only.
Stochastic in Python
import pandas as pd
df = pd.read_csv("prices.csv") # columns High, Low, Close
hh = df["High"].rolling(14).max()
ll = df["Low"].rolling(14).min()
df["k"] = (df["Close"] - ll) / (hh - ll) * 100 # fast %K
df["d"] = df["k"].rolling(3).mean() # fast %D (= slow %K)
df["slow_d"] = df["d"].rolling(3).mean() # slow %DThe first 13 rows have no %K and the first 15 no %D, as in the Excel sheet. A zero range gives a division by zero (shown as NaN or inf) — the same case where %K cannot be calculated.
Who created the stochastic oscillator
George C. Lane developed the stochastic oscillator in the late 1950s. The idea behind it is simple: look at where each close falls inside the recent high–low range. The original lookback was not fixed; 14 candles is the setting most charts use today, and 5 or 9 are also seen.
Like every indicator on these pages, the stochastic describes past prices. It is a calculation, not a forecast or advice; how you use it is your own decision.
Stochastic calculation — questions, answered.
%K = (Close − lowest low of the last 14 candles) ÷ (highest high of the last 14 − lowest low of the last 14) × 100. %D is the 3-period simple average of %K. Both run from 0 to 100.
High, low and close in columns B, C and D from row 2. In row 15 (the 14th candle): highest high =MAX(B2:B15), lowest low =MIN(C2:C15), %K =(D15-F15)/(E15-F15)*100. Copy them down. %D from row 17: =AVERAGE(G15:G17), copied down. The free sheet on this page has it all filled in.
Fast stochastic is the raw %K and its 3-period average %D, as on this page. Slow stochastic smooths once more: slow %K = the 3-period average of fast %K, and slow %D = the 3-period average of slow %K. Full stochastic lets you choose both smoothing lengths, which is what settings like 14, 3, 3 describe.
14 is the number of candles for the highest high and lowest low, the first 3 is how many %K values are averaged to smooth %K, and the second 3 is the length of the %D average. With a %K smoothing of 1 (no smoothing) you get the fast stochastic shown here.
Usually the settings: check whether the chart smooths %K (slow or full stochastic) and the lengths it uses. Spot vs futures prices and the timeframe also matter. Unlike RSI or EMA, the stochastic looks only at the last 14 candles, so the amount of history doesn't change it.
The stochastic indicator, or stochastic oscillator, shows where the latest close sits inside the high–low range of the last 14 candles, on a scale of 0 to 100. %K is that position; %D is the average of the last three %K values. It was developed by George C. Lane in the late 1950s.
It is calculated only from past prices — the last 14 highs, lows and closes — so like every indicator it describes what has already happened. It is often called a momentum indicator because it reacts to where the close sits in the recent range rather than to an average of prices.
That the close is exactly in the middle of the last 14 candles' high–low range. 100 means the close is at the 14-candle high, 0 that it is at the 14-candle low.
Williams %R uses the same 14-candle high and low but measures from the top: %R = (highest high − Close) ÷ (highest high − lowest low) × −100. It runs from −100 to 0 and equals %K − 100.
More indicator formulas, with Excel sheets
Turn your own rules into automated orders.
Already use Stochastic in your own rules? MyAlgoMate's algo trading software places the orders your rules call for at 150+ Indian brokers — and TradeBridge turns your own TradingView or Chartink alerts into live orders. You decide the rules; we provide the technology.