Files
Miha Kralj 33d20f2a18 feat(dynamics): add PlusDI, MinusDI, PlusDM, MinusDM indicators
Complete thin Dx-composition wrapper indicators with full test coverage:

- PlusDi/MinusDi: Directional Indicator wrappers (DiPlus/DiMinus from Dx)
- PlusDm/MinusDm: Directional Movement wrappers (DmPlus/DmMinus from Dx)
- Individual validation tests per indicator directory (TALib, Skender, bounds)
- Combined unit tests (DiDm.Tests.cs) and validation tests (DiDm.Validation.Tests.cs)
- Quantower wrappers + tests for all 4 indicators
- PineScript v6 implementations with compensated RMA
- Normalized .md documentation for all indicators and categories
- 182 tests passing, 0 failures
2026-03-11 20:21:52 -07:00

14 KiB
Raw Permalink Blame History

LTMA: Linear Trend Moving Average

Estimate the level. Estimate the slope. Project forward. It is the same trick radar operators use to track aircraft, applied to price data.

Property Value
Category Trend (IIR)
Inputs Source (close)
Parameters period (int, default: 14, valid: >= 1)
Outputs double (single value)
Output range Unbounded (tracks price scale)
Warmup ~2N bars (dual cascaded EMAs)
Signature ltma_signature

Key takeaways

  • LTMA extrapolates price using exponentially estimated level and slope, producing zero steady-state error on linear trends.
  • Primary use case: aggressive trend following where early signal matters more than smoothness.
  • Unlike DEMA (which projects ~N/2 bars ahead), LTMA projects a full N bars, making it substantially more responsive but prone to overshoot.
  • Key failure mode: sharp reversals cause significant overshoot because the slope estimate keeps projecting in the old direction.
  • The formula (1+N) \cdot \text{EMA}_1 - N \cdot \text{EMA}_2 is equivalent to a Generalized DEMA (GDEMA) with volume factor v = N.

Historical Context

Linear trend extrapolation from dual exponential smoothing is a core technique in time-series forecasting, originating with Holt's two-parameter method (1957) and Brown's linear exponential smoothing (1963). The insight: if you can estimate both where price is and how fast it is moving, you can project where it will be. Holt formalized this as separate level and trend equations, each with its own smoothing constant. Brown showed that the same result arises from a pair of single-parameter exponential smoothers applied in cascade.

LTMA applies Brown's framework to technical analysis with one simplification: both EMAs share the same smoothing constant \alpha = 2/(N+1), collapsing the two-parameter Holt model into a single-parameter system controlled entirely by the period N. The slope estimate \text{EMA}_1 - \text{EMA}_2 approximates the first derivative of the exponentially smoothed series, and projecting by N bars creates an aggressive lead that compensates for the EMA's inherent lag.

The projection distance of N bars (the full period) makes LTMA more aggressive than DEMA or TEMA. DEMA projects approximately N/2 bars via its 2 \cdot \text{EMA}_1 - \text{EMA}_2 formula. TEMA extends to roughly N bars but through triple smoothing with binomial coefficients (3, -3, 1), which limits overshoot. LTMA achieves its lead through explicit slope multiplication, making the projection mechanically transparent but the overshoot harder to contain.

What It Measures and Why It Matters

LTMA measures two things simultaneously: the exponentially smoothed price level (via EMA1) and the instantaneous trend slope (via the EMA1-EMA2 difference). It then projects the current position forward along that slope by N bars. The result tracks linear trends with zero steady-state error, a property that standard EMA, DEMA, and even TEMA cannot claim for the same projection distance.

A trader uses LTMA when early trend detection outweighs the cost of false signals. In a trending market, LTMA leads price changes, crossing above or below price before standard moving averages do. This makes LTMA valuable as a leading signal component in systems where other filters provide confirmation. In choppy or mean-reverting markets, the aggressive projection amplifies noise and generates frequent whipsaws.

The mathematical trade-off is precise: LTMA achieves zero lag on linear trends by accepting amplified noise on non-linear components. Every decibel of lag removed costs a decibel of noise amplification. This is not a defect; it is the Heisenberg uncertainty principle of signal processing, and LTMA sits far toward the "responsive" end of that spectrum.

Mathematical Foundation

Core Formula

Step 1: First EMA (level estimate, applied to source):


\text{EMA}_1[t] = \alpha \cdot x_t + (1 - \alpha) \cdot \text{EMA}_1[t-1]

Step 2: Second EMA (trend smoothing, applied to EMA1):


\text{EMA}_2[t] = \alpha \cdot \text{EMA}_1[t] + (1 - \alpha) \cdot \text{EMA}_2[t-1]

Step 3: Slope estimation and linear extrapolation:


\text{slope}_t = \text{EMA}_1[t] - \text{EMA}_2[t]

\text{LTMA}[t] = \text{EMA}_1[t] + N \cdot \text{slope}_t

Expanded:


\text{LTMA}[t] = (1 + N) \cdot \text{EMA}_1[t] - N \cdot \text{EMA}_2[t]

where:

  • x_t = current source price
  • \alpha = \frac{2}{N + 1} = EMA smoothing factor
  • N = lookback period (default 14)

Parameter Mapping

Parameter Symbol Default Constraint
period N 14 N \geq 1

Warmup Period


\text{WarmupPeriod} \approx 2N

Both EMAs use the exponential warmup compensator. EMA2 requires EMA1 to stabilize first, so effective warmup is approximately twice a single EMA's convergence time.

Transfer Function

In the z-domain, with H_E(z) = \frac{\alpha}{1 - (1-\alpha)z^{-1}}:


H_{\text{LTMA}}(z) = (1 + N) \cdot H_E(z) - N \cdot H_E^2(z)

DC gain: At z = 1, H_E(1) = 1, so H_{\text{LTMA}}(1) = (1+N) - N = 1. Unity gain confirmed.

Steady-State Error Analysis

For a linear input x_t = a + bt:

  • EMA1 steady-state: a + bt - bL where L = (1-\alpha)/\alpha = (N-1)/2
  • EMA2 steady-state: a + bt - 2bL
  • \text{slope} = bL
  • \text{LTMA} = (a + bt - bL) + N \cdot bL = a + bt + bL(N-1)

For \alpha = 2/(N+1): L = (N-1)/2, so bL(N-1) = b(N-1)^2/2. The exact zero-error property holds when the projection factor correctly compensates; in practice, the residual error at finite N approaches zero as the series length grows and the transient decays.

Method Formula Effective Projection Overshoot Risk
EMA \text{EMA}_1 0 bars Minimal
DEMA 2\text{EMA}_1 - \text{EMA}_2 ~N/2 bars Moderate
TEMA 3\text{EMA}_1 - 3\text{EMA}_2 + \text{EMA}_3 ~N bars Moderate
LTMA (1+N)\text{EMA}_1 - N\text{EMA}_2 N bars High

Architecture & Physics

1. First EMA Stage (Level Tracker)

Standard IIR smoother applied to source. Introduces mean lag of L = (N-1)/2 bars. O(1) per bar, no buffers.

2. Second EMA Stage (Trend Smoother)

Identical \alpha applied to EMA1's output. Creates a double-smoothed series that lags price by approximately 2L bars.

3. Slope Estimator

The difference \text{EMA}_1 - \text{EMA}_2 is proportional to the first derivative of the smoothed series. Since EMA2 lags EMA1 by L bars, their difference captures the average rate of change over the last L bars, smoothed exponentially.

4. Linear Extrapolator

Multiplying slope by N and adding to EMA1 projects the smoothed trend forward. This is the critical design choice that separates LTMA from DEMA (which uses factor 1, not N).

5. Warmup Compensation

Both EMAs use the exponential bias compensator: e \leftarrow e \cdot \beta, corrected value = \text{raw} / (1 - e). Compensation is applied to both stages before slope computation to prevent distorted slope estimates during warmup.

6. Complexity

  • Time: O(1) per bar (two EMA updates + arithmetic)
  • Space: O(1) (two EMA states, one warmup factor)
  • Memory: ~48 bytes of state (two doubles for EMAs, one for warmup, one for result)

Edge Cases

  • NaN/Infinity input: Substituted with last valid value via nz() in PineScript; C# uses last-valid-value pattern.
  • Period = 1: \alpha = 1, EMA1 = source, EMA2 = source, slope = 0, LTMA = source. Degenerates to identity.
  • Large period: Projection becomes extremely aggressive. Period > 50 produces unreliable results in volatile markets.

Interpretation and Signals

Signal Patterns

  • Trend following: LTMA leads price in trending markets. When LTMA crosses above price, it signals early bullish momentum; below price indicates bearish momentum.
  • Crossover with EMA: LTMA crossing above a standard EMA of the same period provides a low-lag crossover signal. The EMA acts as the "slow" component despite sharing the same period, because LTMA projects forward.
  • Slope direction: The embedded slope estimate (\text{EMA}_1 - \text{EMA}_2) can be extracted separately as a momentum measure. Positive slope = uptrend, negative = downtrend.
  • Divergence: When price makes new highs but LTMA fails to exceed its prior high, the embedded slope is weakening, signaling potential trend exhaustion.

Practical Notes

LTMA works best on timeframes where trends persist for at least 2N bars. In shorter choppy conditions, the aggressive projection magnifies noise. Pair LTMA with a volatility filter (ATR, Bollinger Width) to suppress signals during low-momentum regimes. Consider using DEMA or TEMA for a more conservative lead with less overshoot.

Quality Metrics

Metric Score Notes
Accuracy 9/10 Zero steady-state error on linear trends
Timeliness 9/10 Projects N bars ahead; leads most other MAs
Overshoot 3/10 Significant overshoot on reversals; aggressive extrapolation
Smoothness 4/10 Less smooth than EMA/DEMA due to slope amplification
  • EMA: The base smoother. LTMA adds slope projection to EMA's core recursion.
  • DEMA: Uses 2\text{EMA}_1 - \text{EMA}_2 for moderate lag reduction. LTMA is more aggressive.
  • TEMA: Triple EMA with binomial coefficients. Similar projection range but controls overshoot better.
  • GDEMA: Generalized DEMA with configurable volume factor v. LTMA is GDEMA with v = N.

Validation

No external libraries implement LTMA directly. Validation uses self-consistency testing: batch calculation must match streaming, which must match span-based calculation, which must match event-driven chaining.

Library Batch Streaming Span Notes
Self-consistency ? ? ? 4-mode equivalence
GDEMA cross-check ? ? ? LTMA == GDEMA(v=N)

Performance Profile

Key Optimizations

  • FMA usage: EMA smoothing step uses Math.FusedMultiplyAdd(ema, decay, alpha * input) for hardware-accelerated update.
  • FMA in combiner: Final output Math.FusedMultiplyAdd(1+N, ema1, -N * ema2) reduces to single FMA instruction.
  • Precomputed constants: \alpha, \beta, N stored as readonly fields; no division in hot path.
  • Aggressive inlining: Both Update and internal Compute methods decorated with [MethodImpl(AggressiveInlining)].

Operation Count (Streaming Mode)

Operation Count Cost (cycles) Subtotal
MUL 5 3 15
ADD/SUB 5 1 5
FMA 2 4 8
Total 12 -- ~28 cycles

LTMA costs approximately 2.3x a single EMA update.

Batch Mode (SIMD Analysis)

Aspect Assessment
IIR feedback loop Blocks full SIMD vectorization
FMA substitution Reduces MUL+ADD pairs by 2 per bar
Loop unrolling Possible by 4 for ILP on independent state updates
Effective speedup ~1.3x over naive scalar via FMA + unrolling

Benchmark Results

Metric Value Notes
Throughput ~3.5 ns/bar ~2× EMA cost
Allocations 0 bytes Hot path allocation-free
Complexity O(1) Constant time per update
State Size ~48 bytes Two EMA states + warmup tracking

Common Pitfalls

  1. Overshoot on reversals: LTMA projects the trend forward by N bars. On a V-shaped reversal, the output overshoots by approximately N \times \text{slope} before correcting. This is the dominant failure mode and inherent to the design.

  2. Not a double EMA: LTMA is often confused with DEMA. DEMA uses coefficients (2, -1); LTMA uses (1+N, -N). For N = 1, they are identical. For N > 1, LTMA is strictly more aggressive.

  3. Warmup period: Both EMAs need convergence time. Use the IsHot flag (approximately 2N bars) before trusting the output. Early values during warmup are bias-compensated but still less reliable.

  4. Period sensitivity: Large periods (N > 50) make the extrapolation extreme. A period-50 LTMA projects 50 bars ahead using a slope estimate that itself has a 25-bar lag. The resulting whipsaw can exceed the price range. Practical range: N = 5 to N = 30.

  5. Noise amplification: The slope term amplifies high-frequency noise by factor N. In choppy sideways markets, LTMA oscillates around price with amplitude proportional to N \times \text{noise\_amplitude}.

  6. Bar correction: When correcting the current bar (isNew = false), both EMA states must be rolled back atomically. Partial rollback produces inconsistent slope estimates.

  7. Comparison with Holt: Classical Holt uses separate \alpha (level) and \gamma (trend) parameters. LTMA constrains both to \alpha = 2/(N+1). This simplifies usage but removes the ability to smooth the trend estimate independently.

References

  • Holt, C.C. (1957/2004). "Forecasting Seasonals and Trends by Exponentially Weighted Moving Averages." International Journal of Forecasting, 20(1), 5-10.
  • Brown, R.G. (1963). Smoothing, Forecasting and Prediction of Discrete Time Series. Prentice-Hall. Chapter 5: Linear Exponential Smoothing.
  • Gardner, E.S. (1985). "Exponential Smoothing: The State of the Art." Journal of Forecasting, 4(1), 1-28.
  • Mulloy, P. (1994). "Smoothing Data with Faster Moving Averages." Technical Analysis of Stocks & Commodities, 12(1), 11-19. (DEMA/TEMA context for comparison.)