Editors' picks
OPEN-SOURCE SCRIPT

Fractional EMA Kalman Filter [D7]

27 828
Fractional EMA Kalman Filter [D7]

1. Description
Fractional EMA Kalman Filter [D7] is an experimental smoothing and state-estimation tool that combines a Kalman filter framework with a fractional EMA input. The objective is to create a filter that remains subdued during ranging conditions while retaining responsiveness when directional structure emerges. By integrating adaptive variance logic and residual-based state control, D7 aims to balance smoothness, reactivity, and structural awareness.snapshot
2. Construct
D7 uses a single-state Kalman architecture with the following components:
  • State (X)
    The current filtered estimate of price, updated using a fractional-length EMA measurement input. This is what the filter believes the true underlying value to be at each step.
  • Innovation
    The difference between the incoming measurement and the current state estimate; the correction signal the filter acts on. Innovation = EMA − X
  • Kalman Gain (K)
    The adaptive weighting factor that balances trust between the new measurement and the prior prediction. K = P / (P + R)
  • P - State uncertainty variance
    Represents how uncertain the filter is about its current state estimate.
  • Q - Process noise variance
    Represents uncertainty in how the state evolves between steps. Higher Q makes the filter more responsive to change.
  • R - Measurement noise variance
    Represents uncertainty in the incoming measurement. Higher R makes the filter more sceptical of new observations.
State Update
Pine Script®
X := X + K × Innovation P := (1 − K) × (P + Q)

3. Features
  • Fractional EMA Layer
    Applies a double sub‑integer EMA length (e.g., 0.5‑period) to generate a high‑resolution measurement signal that responds to structural volatility and regime transitions faster than conventional smoothing. The fractional EMA is not intended to estimate price, but to precondition the observation space so that regime changes manifest as large, legible innovations. By transforming the input prior to filtering, the measurement emphasizes moments of structural change rather than steady‑state behavior, improving the responsiveness of adaptive state estimation.snapshot
  • Innovation Engine
    Computes the innovation/residual error squared and normalizes by ATR, producing a scale-consistent noise estimate that adapts across instruments without manual recalibration.snapshot
  • Dynamic Q/R Model
    Process noise (Q) and measurement noise (R) are derived from short and long lookback windows of the normalized residual variance. This allows the filter to adapt using its own tracking error rather than relying solely on external volatility measures. When prediction error is consistently large, the filter knows its model is inadequate for current conditions and adjusts accordingly.

    Most Kalman filter implementations use either fixed Q or R values or; external volatility proxies such as ATR to modulate Q/R. These approaches respond to price volatility, but not to whether the filter itself is tracking effectively.snapshot
  • R Gate
    A sensitivity floor applied to the residual variance. This ensures R does not fall below a structurally meaningful minimum, preventing the filter from becoming overconfident during low-volatility periods.

4. Rationale
  • Fractional EMA
    A fractional EMA intentionally overshoots and oscillates around price, producing a bounded zig‑zag structure. This behavior is intentional by design: the oscillation self‑balances under steady conditions, while disruptions to that balance generate exaggerated transient values during regime or volatility shifts. Compared to raw price or conventionally smoothed inputs, the fractional EMA yields an information‑dense measurement stream in which structural transitions are amplified, improving innovation visibility for adaptive Kalman filtering.snapshot
  • Adaptive Q/R
    Rather than relying on fixed noise parameters, D7 continuously derives Q and R from residual behavior. This allows the filter to self-tune across changing market conditions without manual recalibration:
    - During directional persistence, responsiveness increases
    - During noisy or unstable conditions, caution or scepticism increases
    - Prediction error becomes part of the control loop rather than external tuningsnapshot
    A core design goal of D7 is structural suppression during sideways conditions. When price lacks directional intent, residual variance decreases, causing Q to fall relative to R. This reduces the Kalman gain, and the filter trusts its prior state estimate more than incoming measurements. The result is a filter that compresses and flattens naturally, rather than oscillating as conventional moving averages do through range-bound price action.snapshot

5. Display Overview
The chart displays both a baseline Kalman filter and a faster-reacting Kalman filter simultaneously, allowing users to visually compare responsiveness, smoothness, and structural behavior under different parameter settings. This dual-filter layout is intended to help users observe how changes in Q/R dynamics and filter sensitivity affect state estimation across varying market conditions.

The fractional EMA input can also be optionally displayed. This observation is turned off by default to preserve chart clarity but may be enabled for users who wish to examine the relationship between the conditioned input signal and the resulting Kalman state estimates.snapshot
6. Default Settings
EMA lengths: 0.5, 0.5

Kalman base and fast filter settings:
Q multiplier (process noise): 0.002, 0.005
R (measurement noise): 50, 50
R Gate: 1, 1

7. Future Development
D7 serves as the foundational implementation of the Kalman filter within the ET Massif framework. Subsequent versions will introduce external regime-aware Q/R modulation and multi-state architecture, progressively increasing adaptability while preserving the core filter mechanics established here.

日本語概要 (Japanese Summary)
Fractional EMA Kalman Filter [D7] は、カルマンフィルターに Fractional EMA(小数期間EMA) を組み合わせた研究用インジケーターです。Fractional EMA とは、期間設定に整数ではなく小数点以下の値を用いる手法です。これにより、従来のEMAよりも短期的な価格変化やボラティリティの拡大を、より高精度かつ敏感に捉えることが可能になります。本インジケーターは、レンジ相場ではラインが水平(フラット)に推移してノイズを軽減する一方、トレンド発生時には極めて素早く反応するように設計されています。

中文概要(Chinese Summary)
Fractional EMA Kalman Filter [D7] 是一款結合卡爾曼濾波(Kalman Filter)與 分數階 EMA(Fractional EMA) 的研究型技術指標。所謂的 Fractional EMA,是指打破傳統整數週期的限制,採用小數參數進行計算。這項技術能更細膩地捕捉短期價格波動與結構性變化,提供比傳統指標更高的靈敏度。本指標的核心設計在於:橫盤整理時維持平滑走勢以過濾雜訊,而在趨勢發動時展現極速反應。此外,它能透過殘差(Residuals)動態調整參數 Q(過程雜訊)與 R(測量雜訊),確保在不同波動率下的自我適應能力。

Disclaimer:
This script is a research tool for market structure analysis and educational purposes only. It does not constitute financial advice. Trading involves risk.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.