mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-16 17:48:05 +00:00
62 lines
2.9 KiB
Markdown
62 lines
2.9 KiB
Markdown
# KAMA: Kaufman's Adaptive Moving Average
|
|
|
|
> "Perry Kaufman asked a simple question: 'Why should I use the same smoothing in a trending market as in a chopping market?' KAMA is the answer."
|
|
|
|
KAMA (Kaufman's Adaptive Moving Average) is an intelligent moving average that adjusts its smoothing speed based on market noise. When the price is moving steadily (high signal-to-noise ratio), KAMA speeds up to capture the trend. When the price is chopping sideways (low signal-to-noise ratio), KAMA slows down to filter out the noise.
|
|
|
|
## Historical Context
|
|
|
|
Perry Kaufman introduced KAMA in his book *Smarter Trading* (1998). It was one of the first widely adopted adaptive indicators, solving the problem of "whipsaws" in sideways markets without sacrificing responsiveness in trends.
|
|
|
|
## Architecture & Physics
|
|
|
|
KAMA uses an **Efficiency Ratio (ER)** to drive the smoothing constant of an EMA.
|
|
|
|
1. **Efficiency Ratio (ER)**: Measures the fractal efficiency of price movement.
|
|
- $ER = \frac{\text{Net Change}}{\text{Sum of Absolute Changes}}$
|
|
- ER approaches 1.0 in a straight line trend.
|
|
- ER approaches 0.0 in pure noise.
|
|
2. **Smoothing Constant (SC)**: Scales between a "Fast" EMA (e.g., 2-period) and a "Slow" EMA (e.g., 30-period) based on ER.
|
|
|
|
## Mathematical Foundation
|
|
|
|
$$ ER = \frac{|P_t - P_{t-n}|}{\sum_{i=0}^{n-1} |P_{t-i} - P_{t-i-1}|} $$
|
|
|
|
$$ SC = \left( ER \times (\text{FastAlpha} - \text{SlowAlpha}) + \text{SlowAlpha} \right)^2 $$
|
|
|
|
$$ \text{KAMA}_t = \text{KAMA}_{t-1} + SC \times (P_t - \text{KAMA}_{t-1}) $$
|
|
|
|
Note the squaring of the SC, which suppresses the response to noise even further.
|
|
|
|
## Performance Profile
|
|
|
|
KAMA is very efficient, with O(1) complexity thanks to the incremental volatility update.
|
|
|
|
| Metric | Score | Notes |
|
|
| :--- | :--- | :--- |
|
|
| **Throughput** | [N] ns/bar | O(1) updates |
|
|
| **Allocations** | 0 | Stack-based calculations only |
|
|
| **Complexity** | O(1) | Constant time update |
|
|
| **Accuracy** | 7/10 | Flattens in noise, tracks in trends |
|
|
| **Timeliness** | 8/10 | Accelerates quickly in strong trends |
|
|
| **Overshoot** | 9/10 | Very stable in sideways markets |
|
|
| **Smoothness** | 8/10 | Aggressive noise filtering |
|
|
|
|
## Validation
|
|
|
|
Validated against TA-Lib, Skender, Tulip, and Ooples.
|
|
|
|
| Library | Status | Notes |
|
|
| :--- | :--- | :--- |
|
|
| **QuanTAlib** | ✅ | Validated. |
|
|
| **TA-Lib** | ✅ | Matches `Kama` |
|
|
| **Skender** | ✅ | Matches `GetKama` |
|
|
| **Tulip** | ✅ | Matches `kama` |
|
|
| **Ooples** | ✅ | Matches `CalculateKaufmanAdaptiveMovingAverage` |
|
|
|
|
### Common Pitfalls
|
|
|
|
1. **Flatlining**: In very choppy markets, KAMA can become almost horizontal. This is a feature, not a bug—it's telling you to stay out.
|
|
2. **Parameters**: The standard settings are (10, 2, 30). 10 is the ER period, 2 is the fast EMA, 30 is the slow EMA. Tweaking the ER period changes the sensitivity to noise.
|
|
3. **Trend Following**: KAMA is excellent for trailing stops because it flattens out when momentum stalls.
|