Refactor documentation links in numerics, oscillators, reversals, and statistics modules to use relative paths; update Bias class to handle division by zero more robustly; remove obsolete CUMMEAN Pine script; enhance trend indicators documentation; add Visual Studio Code workspace configuration.

This commit is contained in:
Miha Kralj
2026-02-04 11:43:59 -08:00
parent c034cbd5e5
commit 3e854eac3f
60 changed files with 9944 additions and 2641 deletions
+179 -98
View File
@@ -1,138 +1,219 @@
# CG: Center of Gravity
## Overview and Purpose
> "The market's center of mass reveals where momentum shifts before price does."
The Center of Gravity (CG) indicator, developed by John Ehlers, is a cycle analysis tool that uses the physics concept of center of gravity to identify cycle turning points in financial markets. By calculating the balance point of price data over a specified period, the indicator creates an oscillator that can help traders anticipate potential reversal points in market cycles.
The Center of Gravity (CG) oscillator, developed by John Ehlers, identifies potential turning points in price action using the physics concept of weighted center of mass. It leads price movement, making it particularly useful for timing entries and exits before traditional indicators signal.
Unlike traditional moving averages that simply smooth price data, the Center of Gravity indicator treats price data as masses distributed over time and calculates where the "balance point" would be. This approach provides insights into the distribution of price momentum within the lookback period.
## Historical Context
## Core Concepts
John Ehlers introduced the Center of Gravity oscillator in his 2002 book "Cybernetic Analysis for Stocks and Futures." Ehlers, an electrical engineer turned trader, applied signal processing concepts to financial markets, creating indicators with minimal lag.
* **Physics-based approach:** Uses the center of gravity concept from physics where each price point represents a mass and the indicator finds the balance point
* **Oscillating indicator:** Provides an oscillator that fluctuates around zero based on price distribution
* **Cycle identification:** Particularly effective at identifying shifts in the dominant cycle within the lookback period
* **Zero-line analysis:** Oscillates around zero with crossovers indicating potential cycle phase changes
The CG oscillator draws from physics: just as the center of gravity of an object determines its balance point, the CG of price determines where momentum is concentrated. When prices cluster toward recent values (uptrend), CG is positive; when prices cluster toward older values (downtrend), CG is negative.
The core innovation of this indicator is its ability to measure where the "weight" of price data is concentrated within the lookback period, providing insights into market momentum distribution.
Unlike momentum oscillators that react to price changes, CG measures the distribution of price within the lookback window, providing leading rather than lagging signals.
## Common Settings and Parameters
## Architecture & Physics
| Parameter | Default | Function | When to Adjust |
| ------ | ------ | ------ | ------ |
| Length | 10 | Controls the lookback period for the Center of Gravity calculation | Increase for longer cycles and smoother signals, decrease for shorter cycles and more responsive signals |
| Source | source | Data source for calculation | Typically uses close; hlc3 provides balanced representation; hl2 for range-based analysis |
The CG indicator uses a sliding window (RingBuffer) to maintain price history and calculates a weighted center of mass that oscillates around zero.
**Pro Tip:** The optimal length setting often correlates with the dominant cycle length in the market. Start with shorter periods (8-14) for active markets and longer periods (20-30) for smoother, longer-term cycle identification.
### Core Components
## Calculation and Mathematical Foundation
1. **RingBuffer**: Maintains the sliding window of `period` values
2. **Weighted Sum (Numerator)**: Sum of position-weighted prices
3. **Simple Sum (Denominator)**: Sum of all prices in window
4. **Center Offset**: Subtracts the midpoint to center oscillation at zero
**Simplified explanation:**
The Center of Gravity calculates where the "balance point" would be if each price in the lookback period was treated as a mass at its time position. The result is then normalized to oscillate around zero by subtracting the theoretical center point.
### Calculation Flow
**Technical formula:**
The Center of Gravity is calculated as:
For each update:
1. Add new price to buffer
2. Compute weighted sum: Σ(position × price)
3. Compute simple sum: Σ(price)
4. Calculate center: weighted_sum / simple_sum
5. Subtract midpoint: result - (period + 1) / 2
CG = [Σ(i × Price[i-1]) / Σ(Price[i-1])] - (Length + 1) / 2
## Mathematical Foundation
Where:
* i ranges from 1 to Length (representing position weights)
* Price[i-1] is the price at position i-1 bars ago (current bar when i=1)
* The subtraction of (Length + 1) / 2 centers the oscillator around zero
* This represents the "balance point" where price data would be in equilibrium
### Center of Gravity Formula
The calculation process:
```
numerator = Σ(i × Price[i-1]) for i = 1 to Length
denominator = Σ(Price[i-1]) for i = 1 to Length
raw_cg = numerator / denominator
CG = raw_cg - (Length + 1) / 2
```
The CG at time $t$ is calculated as:
> 🔍 **Technical Note:** The algorithm calculates the weighted average position of prices, then subtracts the theoretical center point to create an oscillator. When prices are distributed evenly, CG equals zero. When recent prices dominate, CG becomes positive; when older prices dominate, CG becomes negative.
$$ CG_t = \frac{\sum_{i=1}^{n} i \cdot P_{t-n+i}}{\sum_{i=1}^{n} P_{t-n+i}} - \frac{n + 1}{2} $$
## Interpretation Details
where:
The Center of Gravity indicator provides several analytical perspectives:
- $n$ is the period (lookback length)
- $P_{t-n+i}$ is the price at position $i$ within the window
- $i$ ranges from 1 (oldest) to $n$ (newest)
* **Zero-line crossovers:**
* Crossing above zero: Suggests recent prices have more weight (potential upward momentum)
* Crossing below zero: Suggests older prices have more weight (potential downward momentum)
* Multiple crossovers may indicate choppy, non-trending conditions
### Numerator (Weighted Sum)
* **Extreme readings:**
* High positive values: Recent prices significantly outweigh older prices
* High negative values: Older prices significantly outweigh recent prices
* The magnitude indicates the strength of the price distribution bias
$$ Num = \sum_{i=1}^{n} i \cdot P_i = 1 \cdot P_1 + 2 \cdot P_2 + \ldots + n \cdot P_n $$
* **Divergence analysis:**
* Bullish divergence: Price makes lower lows while CG makes higher lows
* Bearish divergence: Price makes higher highs while CG makes lower highs
* These divergences can indicate potential shifts in price momentum
Recent prices (higher $i$) contribute more to the weighted sum.
* **Mean reversion characteristics:**
* CG tends to oscillate around zero over time
* Extreme readings often precede moves back toward the center line
* Can be used to identify potential reversal points
### Denominator (Simple Sum)
## Limitations and Considerations
$$ Den = \sum_{i=1}^{n} P_i $$
* **Market conditions:** Most effective in cyclical markets; may provide less clear signals during strong trending periods
* **Whipsaw potential:** Can generate false signals during low-volatility, range-bound conditions
* **Parameter sensitivity:** Length setting significantly affects responsiveness and noise levels
* **Interpretation complexity:** Requires understanding of the balance point concept for proper interpretation
* **Complementary tools:** Best used with trend identification tools and volume confirmation for optimal results
### Center with Zero Offset
The Center of Gravity works best when combined with other cycle analysis tools and should be part of a broader trading system that includes trend and momentum confirmation.
$$ CG = \begin{cases}
\frac{Num}{Den} - \frac{n + 1}{2} & \text{if } Den \neq 0 \\
0 & \text{if } Den = 0
\end{cases} $$
The subtraction of $(n + 1) / 2$ centers the oscillator at zero. Without this offset, CG would oscillate around the midpoint value.
### Properties
- **Range**: Approximately $\pm \frac{n-1}{2}$ depending on price distribution
- **Zero Crossing**: Indicates potential trend reversal
- **Positive Values**: Recent prices dominate (uptrend momentum)
- **Negative Values**: Older prices dominate (downtrend momentum)
- **Zero Value**: Prices evenly distributed (neutral momentum)
### Example Calculation
For prices [10, 12, 11, 13, 15] with period 5:
$$
Num = 1 \times 10 + 2 \times 12 + 3 \times 11 + 4 \times 13 + 5 \times 15 = 194
$$
$$
Den = 10 + 12 + 11 + 13 + 15 = 61
$$
$$
CG = \frac{194}{61} - \frac{5 + 1}{2} = 3.180 - 3.0 = 0.180
$$
The positive value indicates recent prices are weighted higher (uptrend bias).
## Performance Profile
### Operation Count (Streaming Mode, per Bar)
| Metric | Score | Notes |
| :--- | :--- | :--- |
| **Throughput** | ~15 ns/bar | O(1) with running sums |
| **Allocations** | 0 | Zero-allocation in hot path |
| **Complexity** | O(1) streaming | Recalculation O(N) on bar correction |
| **Accuracy** | 10 | Exact calculation, no approximations |
| Operation | Count | Cost (cycles) | Subtotal |
| :--- | :---: | :---: | :---: |
| ADD/SUB | n+1 | 1 | n+1 |
| MUL | n | 3 | 3n |
| DIV | 1 | 15 | 15 |
| **Total** | **2n+2** | — | **~4n+16 cycles** |
### Operation Count (per update)
*Where n = Length (default 10)*
**Default (n=10):** ~56 cycles per bar
**Breakdown:**
- Weighted sum Σ(i × price): n MUL + (n-1) ADD = 40 cycles
- Price sum Σ(price): (n-1) ADD = 9 cycles
- Division + centering: 1 DIV + 1 SUB = 16 cycles
### Complexity Analysis
| Mode | Complexity | Notes |
| Operation | Count | Notes |
| :--- | :---: | :--- |
| Streaming | O(n) | Full window iteration required (position weights) |
| Batch | O(n×m) | n = length, m = bars |
**Memory**: ~n×8 bytes (price buffer for lookback)
### SIMD Analysis
| Optimization | Applicable | Notes |
| :--- | :---: | :--- |
| AVX2 vectorization | ✅ | Weighted sum is dot product with constant weights |
| FMA | ✅ | `i × price + running_sum` pattern |
| Batch parallelism | ✅ | FIR structure allows full vectorization |
**SIMD Speedup (AVX2):** For n=10, weighted sum reduces from 10 MUL to ~2 vector ops (~5× speedup on dot product). Pre-computed weight vector [1,2,3,...,n] enables efficient `vfmadd` chains.
| ADD/SUB | ~6 | Running sum updates |
| MUL | ~2 | Position weighting |
| DIV | 2 | Center and offset calculation |
### Quality Metrics
| Metric | Score | Notes |
| :--- | :---: | :--- |
| **Accuracy** | 10/10 | Exact weighted centroid calculation |
| **Timeliness** | 7/10 | FIR introduces group delay ≈ n/2 |
| **Overshoot** | 6/10 | Linear weights can amplify recent volatility |
| **Smoothness** | 7/10 | Moderate smoothing from averaging |
| **Accuracy** | 10/10 | Exact weighted average |
| **Timeliness** | 9/10 | Leads price by design |
| **Overshoot** | 6/10 | Can overshoot at extremes |
| **Smoothness** | 7/10 | Some noise; often paired with signal line |
## Validation
| Library | Status | Notes |
| :--- | :--- | :--- |
| **TA-Lib** | N/A | Not available in TA-Lib |
| **Skender** | N/A | Not available in Skender |
| **Tulip** | N/A | Not available in Tulip |
| **PineScript** | ✅ | Validated against original ta.cg() |
CG is validated through mathematical properties:
- Constant price produces zero CG
- Linear uptrend produces positive CG
- Linear downtrend produces negative CG
- Values bounded by approximately ±(period-1)/2
## Common Pitfalls
1. **Period Selection**: Too short periods produce noisy signals; too long periods reduce responsiveness. Ehlers recommended 10 as a starting point.
2. **Signal Line**: CG is often smoothed with a 3-period SMA signal line. Trading raw CG crossings may produce false signals.
3. **Zero Line Crossings**: Not all zero crossings are tradeable. Confirm with price action or additional filters.
4. **Trending Markets**: In strong trends, CG may stay positive/negative for extended periods. Zero crossing may not occur until trend exhaustion.
5. **Flat Markets**: During consolidation, CG oscillates around zero without clear direction, producing whipsaws.
6. **Warmup Period**: CG requires a full window (`period` values) before producing reliable signals.
## Usage
```csharp
using QuanTAlib;
// Create a 10-period CG indicator
var cg = new Cg(period: 10);
// Update with new values
var result = cg.Update(new TValue(DateTime.UtcNow, 100.0));
// Access the last calculated CG value
Console.WriteLine($"CG: {cg.Last.Value}");
// Chained usage
var source = new TSeries();
var cgChained = new Cg(source, period: 10);
// Static batch calculation
var output = Cg.Calculate(source, period: 10);
// Span-based calculation
Span<double> outputSpan = stackalloc double[source.Count];
Cg.Batch(source.Values, outputSpan, period: 10);
```
## Applications
### Trend Reversal Detection
CG zero crossings often precede price reversals:
- CG crosses above zero: potential bullish reversal
- CG crosses below zero: potential bearish reversal
### Divergence Analysis
Like other oscillators, CG divergences from price can signal weakening trends:
- Price makes higher high, CG makes lower high: bearish divergence
- Price makes lower low, CG makes higher low: bullish divergence
### Momentum Confirmation
Use CG to confirm trend strength:
- Rising CG in uptrend: momentum supporting trend
- Falling CG in uptrend: momentum weakening, potential reversal
### Cycle Analysis
CG's leading nature makes it useful for timing cycle turns in conjunction with other Ehlers indicators.
## Signal Line Strategy
A common approach pairs CG with a trigger line:
```csharp
var cg = new Cg(10);
var trigger = new Sma(3); // 3-period smoothing of CG
// After updates:
double cgValue = cg.Last.Value;
double triggerValue = trigger.Update(cg.Last).Value;
// Buy when CG crosses above trigger
// Sell when CG crosses below trigger
```
## References
* Ehlers, J. F. (2002). *Rocket Science for Traders: Digital Signal Processing Applications*. John Wiley & Sons.
* Ehlers, J. F. (2013). *Cycle Analytics for Traders: Advanced Technical Trading Concepts*. John Wiley & Sons.
- Ehlers, J.F. (2002). *Cybernetic Analysis for Stocks and Futures*. Wiley.
- Ehlers, J.F. (2001). "The Center of Gravity Oscillator." *Technical Analysis of Stocks & Commodities*.
- TradingView PineScript Reference: ta.cg() function.