> "John Ehlers brought rocket science to trading. Literally. HTIT uses signal processing to find the trend by removing the cycle. It's not smoothing; it's extraction."
HTIT (Hilbert Transform Instantaneous Trend) is a trend-following indicator that doesn't rely on simple averaging. Instead, it uses the Hilbert Transform to measure the dominant cycle period of the market and then computes a trendline that filters out that specific cycle. It adapts to the market's rhythm rather than imposing a fixed period.
## Historical Context
John Ehlers, a pioneer in applying DSP to trading, introduced this in his book *Rocket Science for Traders*. He recognized that markets have cyclic components (noise) and trend components. By identifying the cycle, you can mathematically subtract it to reveal the pure trend.
Most trend indicators (SMA, EMA) are low-pass filters: they let low frequencies (trend) pass and block high frequencies (noise). The problem is that "noise" in markets isn't random white noise; it's often cyclic. A fixed-period SMA might filter out a 10-day cycle perfectly but amplify a 20-day cycle. HTIT solves this by measuring the cycle first, then tuning the filter to kill exactly that frequency.
## Architecture & Physics
This is a complex, multi-stage signal processing pipeline. It's not just a formula; it's a machine.
1.**Smooth**: 4-bar WMA to remove high-frequency noise (Nyquist limit).
2.**Detrend**: High-pass filter to remove the DC component (trend) temporarily to isolate the cycle.
3.**Hilbert Transform**: Compute In-Phase (I) and Quadrature (Q) components.
4.**Period Measurement**: Use the phase rate of change (Homodyne Discriminator) to measure the dominant cycle period.
5.**Trend Extraction**: Average the price over the measured dominant cycle period to cancel out the cycle.
6.**Post-Smoothing**: 4-bar WMA on the extracted trend for final polish.
The "physics" here is cancellation. If you average a sine wave over exactly one period, the result is zero. If you average Price (Trend + Cycle) over exactly one cycle period, the Cycle cancels out, leaving only the Trend.
## Mathematical Foundation
### 1. Pre-Smoothing
A 4-tap FIR filter removes high-frequency noise to prevent aliasing before the Hilbert Transform.
The signal is detrended and split into In-Phase ($I$) and Quadrature ($Q$) components using a 7-tap Hilbert Transform. The coefficients are optimized for market cycles (10-40 bars).
The phase rate of change is calculated using the complex conjugate product of the current and previous phasors. This is the "Homodyne Discriminator" - a fancy radio term for "measuring frequency by comparing a signal to a delayed version of itself."
We use `atan2` for robust phase calculation and maintain full double precision throughout the pipeline.
## Performance Profile
HTIT is computationally heavier than a simple MA but lighter than MAMA. The main cost is the loop for the Instantaneous Trend calculation, which sums up to 50 past prices.
- IT summation loop (~25 cycles avg, 13%) — O(N) complexity where N = dcPeriod (6-50)
**Note:** The IT loop iterates `dcPeriod` times (6-50 bars). The estimate above uses 25 as the average. Worst case (dcPeriod=50) adds ~50 cycles total.
### Batch Mode (SIMD Analysis)
HTIT is **not SIMD-parallelizable** across bars due to:
1. Recursive feedback in Hilbert transforms (I2, Q2 depend on previous values)
2. Period-dependent IT summation loop (variable iteration count)
3. Homodyne discriminator state dependencies
**Per-bar optimization with FMA:** The 4-tap smoothing stages and Hilbert transforms could benefit from FMA, saving ~4-8 cycles per bar.
### Quality Metrics
| Metric | Score | Notes |
| :--- | :---: | :--- |
| **Accuracy** | 9/10 | Extracts trend by mathematically canceling the dominant cycle |
| **Timeliness** | 7/10 | Adapts period, but IT averaging introduces inherent lag |
| `_state` | State | ~64B | Current Hilbert state |
| `_p_state` | State | ~64B | Previous state for rollback |
| **Total** | | **~960B** | Per indicator instance |
### Numerical Robustness
Uses `Math.Atan2` for proper quadrant handling in phase calculation, avoiding division-by-zero issues that plague `atan(y/x)` implementations.
### Common Pitfalls
1.**Warmup**: This indicator needs significant warmup (at least 12 bars, ideally 50+) for the feedback loops (period smoothing) to stabilize. Don't trust the first 50 bars.
2.**Lag**: While it adapts, the trendline still lags because it's essentially a dynamic SMA. The advantage is that the period is optimal for the current market condition, not that it has zero lag.
3.**Complexity**: Debugging this is a nightmare. Trust the math.
4.**Ranging Markets**: In a pure range, the "trend" should be flat. HTIT handles this well because the cycle cancellation works best when the cycle is clear.