The augment_stochastic_oscillator function calculates the Stochastic Oscillator (%K and %D) for a financial instrument using either pandas or polars engine, and returns the augmented DataFrame.
The input data can be a pandas DataFrame or a pandas DataFrameGroupBy object containing the time series data for Stochastic Oscillator calculations.
required
date_column
str
The name of the column containing dates or timestamps.
required
high_column
str
The column containing high prices for the financial instrument.
required
low_column
str
The column containing low prices for the financial instrument.
required
close_column
str
The column containing closing prices for the financial instrument.
required
k_periods
Union[int, Tuple[int, int], List[int]]
The number of periods for calculating %K (fast stochastic). Can be an integer, a tuple of two integers (start and end periods), or a list of integers. Default is 14.
14
d_periods
int
The number of periods for calculating %D (slow stochastic), typically a moving average of %K. Default is 3.
3
reduce_memory
bool
If True, reduces memory usage of the DataFrame before calculation. Default is False.
False
engine
str
The computation engine to use: ‘pandas’ or ‘polars’. Default is ‘pandas’.
'pandas'
Returns
Name
Type
Description
pd.DataFrame
A pandas DataFrame augmented with columns: - {close_column}stoch_k{k_period}: Stochastic Oscillator %K for each k_period - {close_column}stoch_d{k_period}_{d_period}: Stochastic Oscillator %D for each k_period
Notes
The Stochastic Oscillator is a momentum indicator that compares a security’s closing price to its price range over a specific period, developed by George Lane. It consists of two lines:
%K: Measures the current close relative to the high-low range over k_periods.
%D: A moving average of %K over d_periods, smoothing the %K line.