Files
QuanTAlib/lib/dynamics/ttm_trend/TtmTrend.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

99 lines
3.5 KiB
Markdown

# TTM_TREND: TTM Trend
> "The simplest trend indicator is the one you actually follow."
John Carter's TTM Trend uses a fast EMA (default period 6) applied to typical price (HLC/3) to determine short-term trend direction via slope sign. Output is a ternary trend state: +1 (bullish, EMA rising), -1 (bearish, EMA falling), or 0 (neutral, EMA unchanged). The indicator requires only 2 bars warmup, runs at O(1) per bar with O(1) space, and produces zero allocations in the hot path.
## Historical Context
John Carter developed the TTM (Trade the Markets) Trend indicator as a clean visual tool for identifying short-term trend direction, popularized through *Mastering the Trade* and the thinkorswim platform. Unlike complex multi-component trend systems, TTM Trend reduces trend detection to its minimum viable form: the slope of a fast exponential moving average. The very short default period (6) makes it responsive to recent price action, positioning it as a "first responder" trend filter meant to be combined with Carter's other TTM tools (Squeeze, Wave, LRC). The color-coded output (green/red/gray) provides at-a-glance trend assessment.
## Architecture & Physics
### 1. Typical Price
$$\text{TP}_t = \frac{H_t + L_t + C_t}{3}$$
Using typical price rather than close reduces susceptibility to closing-tick noise.
### 2. EMA Recursion
$$\alpha = \frac{2}{N + 1}$$
$$\text{EMA}_t = \alpha \cdot \text{TP}_t + (1 - \alpha) \cdot \text{EMA}_{t-1}$$
Or equivalently via FMA:
$$\text{EMA}_t = \text{FMA}(\alpha,\ \text{TP}_t - \text{EMA}_{t-1},\ \text{EMA}_{t-1})$$
### 3. Trend Classification
$$\text{Trend}_t = \text{sign}(\text{EMA}_t - \text{EMA}_{t-1})$$
| Value | State | Color |
|:------|:------|:------|
| +1 | Bullish | Green |
| -1 | Bearish | Red |
| 0 | Neutral | Gray |
### 4. Strength Measurement
$$\text{Strength}_t = \frac{|\text{EMA}_t - \text{EMA}_{t-1}|}{\text{EMA}_{t-1}} \times 100\%$$
This percentage rate-of-change quantifies how aggressively the trend is moving. High strength values indicate strong conviction; near-zero values suggest potential reversal.
### 5. Complexity
| Metric | Value |
|:-------|:------|
| Time | O(1) per bar |
| Space | O(1) (one EMA state + one previous value) |
| Warmup | 2 bars |
| Allocations | Zero in hot path |
## Mathematical Foundation
### Parameters
| Parameter | Type | Default | Constraint | Description |
|:----------|:-----|:--------|:-----------|:------------|
| period | int | 6 | > 0 | EMA lookback period (very fast by default) |
### Pseudo-code
```
TTM_TREND(bar, period=6):
tp = (bar.High + bar.Low + bar.Close) / 3
alpha = 2.0 / (period + 1)
if count == 0:
ema_val = tp
else:
ema_val = FMA(alpha, tp - ema_val, ema_val)
// Trend direction from slope sign
if count >= 1:
if ema_val > prev_ema:
trend = +1
else if ema_val < prev_ema:
trend = -1
else:
trend = 0
strength = abs(ema_val - prev_ema) / prev_ema * 100
prev_ema = ema_val
count += 1
return (ema_val, trend, strength)
```
### Period Selection
The default period of 6 makes TTM Trend extremely fast-reacting. The EMA half-life is approximately $\ln(2) / \ln(1 + 2/N) \approx 2.4$ bars for $N = 6$. This means the indicator responds within 2-3 bars of a price shift. Longer periods (12, 20) reduce whipsaws but delay detection. Carter's design intent was maximum responsiveness, with noise filtering delegated to companion indicators (Squeeze, Wave).
## Resources
- Carter, J. (2005). *Mastering the Trade*. McGraw-Hill.