Center of Gravity Oscillator (CG)

Calculate Ehlers’ Center of Gravity (CG) Oscillator: the weighted position of the prices within a window, which marks the turning points of the cycle with little lag.

The Center of Gravity Oscillator of Ehlers (2002) is the balance point of the prices in the window, with the latest price at position 1, turned negative. When prices rise, the weight shifts to the latest prices and the balance point moves towards them, so the oscillator rises with the price, smoothed and with almost no lag. The trigger is the previous value, and a crossing of the two marks a turn. Ehlers sets the window to half the dominant cycle.

The formula is as follows:

\[\text{Center of Gravity} = - \frac{\sum(i + 1) \cdot \operatorname{Price}(\text{t-i})}{\sum \;\operatorname{Price}(\text{t-i})} \;\text{for}\; i = 0 \;\text{to Window} - 1\] \[\text{Trigger} = \text{Center of Gravity}(t-1)\]

Example · AAPL, MSFT · 2021 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_center_of_gravity_oscillator:

from financetoolkit import Toolkit

toolkit = Toolkit(
    tickers=["AAPL", "MSFT"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
)

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

Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:

How did Ehlers' Center of Gravity Oscillator of Apple and its trigger line move in 2025?

Which returns:

Parameters

get_center_of_gravity_oscillator accepts the following parameters.

ParameterTypeDescriptionExample
periodstrThe time period to consider for historical data. Options are: “intraday” (intraday prices), “daily” (daily prices), “weekly” (weekly prices), “monthly” (monthly prices), “quarterly” (quarterly prices), “yearly” (yearly prices). Defaults to “daily”."weekly"
close_columnstrThe column name for closing prices in the historical data. Defaults to “Adj Close”, in which case the high and low prices are adjusted for dividends in the same way."Close"
median_priceboolWhether to filter the median price (High + Low) / 2, as Ehlers does, or the close of close_column, as TA-Lib and pandas-ta-classic do. Defaults to True.True
windowintThe number of periods, ideally half the dominant cycle. Defaults to 10.20
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise).2
growthboolWhether to calculate the growth of the Center of Gravity Oscillator. Defaults to False.True
lagint | list[int]The lag to use for the growth calculation. Defaults to 1.4
standardizeboolWhether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False.True
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column

References

  • Paper Ehlers, J.F. (2002). “The Center Of Gravity Oscillator.” Technical Analysis of Stocks & Commodities, 20(5), 20-24.
  • Library Referenced against TA-Lib’s CG
  • Library Referenced against pandas-ta-classic’s cg
Share