MyAlgoMate
// INDICATOR FORMULA

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:

Download the free Stochastic Excel sheet (.xlsx)

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

  1. 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.
  2. 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.
  3. Move the window on one candle and repeat. %K is 74.34 on Day 15 and 42.40 on Day 16.
  4. Average the last three %K values for %D. Day 16: (78.61 + 74.34 + 42.40) ÷ 3 = 65.12.
  5. 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.

One row per candle, oldest first. Paste straight from Excel or your broker's download; a date or time column becomes the row label. Filled with our example prices.

Latest values

%K: 96.7039%D (3): 91.9385
Stochastic calculated from the example prices
DayHighLowCloseHighest high (14)Lowest low (14)%K%D (3)
Day 11,010.00990.601,001.15
Day 21,004.60987.75987.95
Day 31,011.50986.001,009.15
Day 41,015.001,001.851,012.60
Day 51,012.00990.80993.25
Day 61,012.60990.901,008.60
Day 71,018.951,003.801,013.60
Day 81,012.95994.751,001.55
Day 91,033.25995.301,031.55
Day 101,046.151,028.501,040.05
Day 111,041.801,027.701,033.15
Day 121,065.801,031.151,064.80
Day 131,086.001,056.851,083.55
Day 141,090.251,067.251,067.951,090.25986.0078.6091
Day 151,074.301,061.451,063.501,090.25986.0074.3405
Day 161,067.351,025.901,030.201,090.25986.0042.398165.1159
Day 171,033.001,011.751,012.201,090.25990.8021.518446.0857
Day 181,019.90979.85981.601,090.25979.851.585121.8339
Day 19988.30969.40970.551,090.25969.400.95168.0184
Day 20973.10959.05960.901,090.25959.051.41011.3156
Day 21980.35958.30978.301,090.25958.3015.15735.8396
Day 22981.50957.00958.851,090.25957.001.38845.9852
Day 23963.00942.60946.751,090.25942.602.81076.4521
Day 24957.70931.65937.951,090.25931.653.97232.7238
Day 25945.75926.05927.851,090.25926.051.09622.6264
Day 26924.60910.25911.201,090.25910.250.52781.8654
Day 27919.05900.00905.151,090.25900.002.70701.4437
Day 28904.50884.70885.951,074.30884.700.65931.2980
Day 29891.25871.55874.701,067.35871.551.60881.6583
Day 30880.70849.70855.801,033.00849.703.32791.8653
Day 31868.95858.15862.151,019.90849.707.31494.0839
Day 32875.05859.50866.45988.30849.7012.08517.5760
Day 33870.55843.00845.10981.50843.001.51626.9721
Day 34854.70846.55851.55981.50843.006.17336.5916
Day 35863.70847.85860.20981.50843.0012.41886.7028
Day 36862.95845.40854.80963.00843.009.83339.4751
Day 37882.25858.05877.20957.70843.0029.816917.3563
Day 38893.65868.85888.45945.75843.0044.233627.9613
Day 39908.35889.60905.40924.60843.0076.470650.1737
Day 40913.10895.65896.80919.05843.0070.742963.8157
Day 41894.65887.85889.60913.10843.0066.476571.2300
Day 42898.55884.80895.30913.10843.0074.607770.6090
Day 43920.60889.25919.00920.60843.0097.938179.6741
Day 44920.60916.30918.35920.60843.0097.100589.8821
Day 45919.30903.80906.25920.60843.0081.507792.1821
Day 46937.35900.30932.10937.35843.0094.435691.0146
Day 47947.15930.20944.35947.15845.4097.248291.0638
Day 48949.20937.25948.70949.20845.4099.518397.0674
Day 49971.15949.05966.35971.15845.4096.182997.6498
Day 50965.90954.75955.60971.15858.0586.251193.9841
Day 51971.35954.15969.10971.35868.8597.804993.4130
Day 52997.00966.55984.35997.00884.8088.725590.9272
Day 53992.30980.25980.90997.00884.8085.650690.7270
Day 54984.70961.40962.50997.00884.8069.251381.2092
Day 55964.60953.00960.20997.00884.8067.201474.0345
Day 56969.05954.50967.35997.00889.2572.482669.6451
Day 57982.20960.60971.00997.00900.3073.112770.9322
Day 58987.10967.55981.75997.00900.3084.229676.6083
Day 59999.95979.00994.85999.95900.3094.882184.0748
Day 601,015.15987.351,012.351,015.15930.2096.703991.9385
Everything is worked out in your browser. Nothing you paste is stored or sent.

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:

  1. A

    Day

    A2 downwards: your dates, oldest at the top.

  2. B

    High

    B2 downwards: the high prices, oldest at the top.

  3. C

    Low

    C2 downwards: the low prices, oldest at the top.

  4. D

    Close

    D2 downwards: the closing prices, oldest at the top.

  5. E

    Highest high (14)

    E15 =MAX(B2:B15) — then copy it down to the last row.

  6. F

    Lowest low (14)

    F15 =MIN(C2:C15) — then copy it down to the last row.

  7. G

    %K

    G15 =(D15-F15)/(E15-F15)*100 — then copy it down to the last row.

  8. 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 %K

The example prices, all three lines on the same days:

Fast %K, fast %D (= slow %K) and slow %D on the example prices
DayFast %KFast %D = slow %KSlow %D
Day 201.411.3210.39
Day 303.331.871.61
Day 4070.7463.8247.32
Day 5086.2593.9896.23
Day 6096.7091.9484.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.

How many %K points a ₹1 change in the close moves, for different 14-candle ranges
14-candle range%K points per ₹1 of close
₹520.0
₹1010.0
₹502.0
₹1001.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 %D

The 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.

// FAQ

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.

Share this page