> *Least-squares polynomial fitting has been solving signal processing problems since 1964. That most traders still use medieval averaging techniques says more about the industry than the math.*
- SGMA is a Finite Impulse Response (FIR) filter that uses polynomial fitting to smooth data while preserving higher moments (peaks, valleys, and inf...
SGMA is a Finite Impulse Response (FIR) filter that uses polynomial fitting to smooth data while preserving higher moments (peaks, valleys, and inflection points). Unlike the Simple Moving Average (which flattens everything) or the Exponential Moving Average (which introduces phase lag), SGMA uses polynomial weighting to maintain the original signal's shape characteristics.
## Historical Context / The Standard
Abraham Savitzky and Marcel Golay published their seminal paper "Smoothing and Differentiation of Data by Simplified Least Squares Procedures" in *Analytical Chemistry* in 1964. The context was spectroscopy—scientists needed to smooth noisy absorption spectra without destroying the peaks that identified chemical compounds.
The Savitzky-Golay filter became the gold standard in scientific signal processing because it solved the fundamental trade-off: smoothing reduces noise but also reduces signal amplitude. SG filters preserve the signal's shape by fitting local polynomials—effectively "understanding" the curvature of the data rather than blindly averaging it.
Financial markets present the same challenge. Traders want smooth lines that don't lag, and they want to preserve the actual peaks and troughs that matter for trading decisions. SGMA adapts the Savitzky-Golay approach to price series.
## Architecture & Physics
SGMA computes weights based on the polynomial degree parameter. The weight formula is elegantly simple:
$$w_i = 1 - |x_i|^d$$
Where:
- $x_i = \frac{i - \text{half\_window}}{\text{half\_window}}$ (normalized position from -1 to +1)
- $d$ = polynomial degree (0 to 4)
### The Physics of Polynomial Degree
The degree parameter controls the weight distribution:
| Degree | Weight Shape | Trading Behavior |
| :--- | :--- | :--- |
| **0** | Flat (uniform) | Equivalent to SMA. All bars weighted equally. |
**Critical Edge Behavior**: For degree ≥ 1, the edge positions (oldest and newest values in the window) have weight = 0 because at the edges, $|x| = 1$, so $w = 1 - 1^d = 0$. This means the filter effectively ignores the boundary values—a deliberate design choice that prevents edge artifacts from corrupting the polynomial fit.
### The Compute Challenge
Unlike recursive filters (EMA, DEMA), SGMA requires a convolution over the entire window. QuanTAlib precomputes the weight vector upon initialization, reducing the runtime operation to a dot product.
QuanTAlib validates SGMA against mathematical properties rather than external libraries, as most libraries implement the full Savitzky-Golay convolution coefficients rather than the simplified polynomial weighting approach.
| Library | Status | Notes |
| :--- | :--- | :--- |
| **QuanTAlib** | ✅ | Reference implementation. Validated against math definition. |
| **Mode Consistency** | ✅ | Streaming, batch, span, and event modes produce identical results. |
| **Skender** | ❌ | No SGMA equivalent. |
| **TA-Lib** | ❌ | Not included. |
| **Tulip** | ❌ | Not included. |
| **Ooples** | ❌ | Not included. |
## Common Pitfalls
1.**Degree 0 Misuse**: If you want SMA, use SMA. Degree 0 SGMA is mathematically equivalent but wastes cycles on weight calculation.
2.**Even Period Confusion**: SGMA silently converts even periods to odd (period + 1). If you specify period=10, you get period=11. This is mathematically necessary for symmetric polynomial fitting, not a bug.
3.**Edge Weight Zero**: For degree ≥ 1, the oldest and newest values in the window have zero weight. This is intentional—it prevents edge effects. But it means:
- Bar corrections (`isNew=false`) have minimal effect at higher degrees
- The effective "active" period is shorter than the nominal period
4.**Cold Start**: SGMA requires a full window ($L$) to produce mathematically valid output. The first $L-1$ bars are warmup noise. Check `IsHot` before trading on the signal.
5.**High Degree Instability**: Degrees 3-4 concentrate weight heavily in the center. While this preserves shape, it also means a small number of bars dominate the output—approaching the behavior of a very short moving average with extra smoothing on the tails.