Files
QuanTAlib/lib/cycles/cg/cg.md
T
Miha Kralj 90d5638008 Add new moving average implementations: LTMA, MCNMA, NLMA, NMA, NYQMA, RAIN, and TRAMA
- LTMA (Linear Trend Moving Average): Introduces a predictive moving average using dual cascaded EMAs for trend estimation.
- MCNMA (McNicholl EMA): Implements a zero-lag TEMA using a cascaded EMA structure for enhanced responsiveness.
- NLMA (Non-Lag Moving Average): Utilizes a damped cosine kernel to achieve reduced lag in moving averages.
- NMA (Natural Moving Average): Adapts smoothing based on volatility profiles using a square-root kernel.
- NYQMA (Nyquist Moving Average): Applies the Nyquist-Shannon theorem to prevent aliasing in cascaded moving averages.
- RAIN (Rainbow Moving Average): Combines multiple SMA layers with weighted averages for multi-scale smoothing.
- TRAMA (Trend Regularity Adaptive Moving Average): Adapts smoothing based on the frequency of new highs and lows in price data.
2026-02-20 21:40:32 -08:00

80 lines
3.0 KiB
Markdown

# CG: Ehlers Center of Gravity
CG identifies potential turning points using the physics concept of weighted center of mass applied to a price window. Developed by John Ehlers, the oscillator measures where the "weight" of prices is concentrated within a lookback period, producing a leading indicator that oscillates around zero with minimal lag compared to traditional moving average crossover systems.
## Historical Context
John Ehlers introduced the Center of Gravity oscillator in *Cybernetic Analysis for Stocks and Futures* (2002). Drawing from classical mechanics, the indicator applies the concept that the center of mass of a distribution reveals its balance point. In the price context, the CG identifies where momentum is concentrated within a sliding window. Unlike momentum oscillators that differentiate price (and amplify noise), CG integrates position-weighted price, providing smoother turning point detection. The indicator's leading characteristic arises from the weighting scheme: as new prices shift the balance point, the CG responds before the window's simple average would.
## Architecture & Physics
### 1. Weighted Sum (Numerator)
Position-weighted accumulation over the lookback window:
$$Num = \sum_{i=1}^{n} i \cdot P_{t-n+i}$$
where $i$ ranges from 1 (oldest) to $n$ (newest), giving linearly increasing weight to more recent data.
### 2. Simple Sum (Denominator)
$$Den = \sum_{i=1}^{n} P_{t-n+i}$$
### 3. Center of Gravity
$$CG_t = \frac{Num}{Den} - \frac{n + 1}{2}$$
The term $\frac{n + 1}{2}$ is the geometric center of the window, centering the output around zero. When recent prices dominate, $CG > 0$ (bullish); when older prices dominate, $CG < 0$ (bearish).
### 4. Complexity
Streaming uses running sums for both numerator and denominator: $O(1)$ per bar with $O(n)$ memory for the ring buffer.
## Mathematical Foundation
### Parameters
| Parameter | Description | Default | Constraint |
|-----------|-------------|---------|------------|
| `period` | Lookback window length | 10 | $> 0$ |
### Pseudo-code
```
function CG(source, period):
buffer ← RingBuffer(period)
runNum ← 0 // weighted sum
runDen ← 0 // simple sum
for each price in source:
buffer.Add(price)
if buffer.Count < period: continue
// Compute from buffer (or maintain running sums)
num = 0
den = 0
for i = 0 to period-1:
w = i + 1
num += w * buffer[i]
den += buffer[i]
cg = (den ≠ 0) ? (num / den) - (period + 1) / 2.0 : 0
emit cg
```
### Output Interpretation
| Condition | Meaning |
|-----------|---------|
| $CG > 0$ | Weight concentrated in recent prices (bullish momentum) |
| $CG < 0$ | Weight concentrated in older prices (bearish momentum) |
| Zero crossing up | Momentum shifting bullish |
| Zero crossing down | Momentum shifting bearish |
| Hanging at extremes | Strong trend in progress |
## Resources
- **Ehlers, J.F.** *Cybernetic Analysis for Stocks and Futures*. Wiley, 2002.
- **Ehlers, J.F.** *Rocket Science for Traders*. Wiley, 2001.