Calculate the Stochastic Oscillator indicator for a given price series.

The Stochastic Oscillator is a momentum indicator that shows the location of the close relative to the high-low range over a set number of periods. It consists of the %K line (fast) and the %D line (slow).

The formula is as follows:

\[\%K = 100 \cdot ((\text{Close} - \text{Lowest Low}) / (\text{Highest High} - \text{Lowest Low}))\] \[\%D = \operatorname{SMA}(\%K,\; \text{smooth\_window})\]

Also known as: stochastic oscillator, percent K, percent D.

Stochastic Oscillator in Python

get_stochastic_oscillator is part of the Technicals module of the open-source Finance Toolkit. Install it with:

pip install financetoolkit -U

Then call get_stochastic_oscillator as shown below.

from financetoolkit import Toolkit

toolkit = Toolkit(tickers=["AAPL", "MSFT"])

toolkit.technicals.get_stochastic_oscillator().xs("AAPL", level=1, axis="columns")

Which returns:

Date Stochastic %D Stochastic %K
2026-06-18 34.4881 35.4097
2026-06-22 32.012 32.0786
2026-06-23 30.1799 23.0513
2026-06-24 24.7058 18.9873
2026-06-25 15.082 3.2073
2026-06-26 15.0576 22.9782
2026-06-29 18.0181 27.8689
2026-06-30 35.0981 54.4472
2026-07-01 51.4243 71.9567
2026-07-02 74.7297 97.7853

Parameters

get_stochastic_oscillator accepts the following parameters:

  • period (str, optional): The time period to consider for historical data. Can be “daily”, “weekly”, “quarterly”, or “yearly”. Defaults to “daily”.
  • close_column (str, optional): The column name for closing prices in the historical data. Defaults to “Adj Close”.
  • window (int, optional): The number of periods to consider for the %K line calculation. Defaults to 14.
  • smooth_window (int, optional): The number of periods used to smooth the %K line into the %D signal line. Defaults to 3.
  • rounding (int | None, optional): The number of decimals to round the results to. Defaults to 4.
  • growth (bool, optional): Whether to calculate the growth of the %K and %D values. Defaults to False.
  • lag (int | list[int], optional): The lag to use for the growth calculation. Defaults to 1.
  • standardize (bool, optional): Whether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False.
  • smooth_widow (int | None, optional): Deprecated misspelling of smooth_window, accepted so that existing callers keep working. Passing it emits a DeprecationWarning and forwards the value to smooth_window. Defaults to None.
Share