OPEN-SOURCE SCRIPT
EDCF: Ehlers Distance Coefficient Filter

The Ehlers Distance Coefficient Filter (EDCF) is a nonlinear adaptive FIR filter that computes its smoothing weights dynamically from the sum of squared price differences across the observation window. Recent bars that diverge most from the rest of the window receive the highest weight, making the filter faster than a comparable SMA in trending conditions while degenerating to a simple average when prices are flat.
HISTORICAL CONTEXT
John F. Ehlers introduced EDCF in "Nonlinear Ehlers Filters" (Stocks & Commodities, V.19:4, 2001) and in the accompanying MESA Software paper. The core insight: instead of fixing weights by position (as EMA or WMA do) or by spectral design (as Ehlers' own Laguerre and MESA filters do), let the price data itself determine how much each bar contributes. Bars that are close in price to their neighbors get low weight; bars that represent breakouts or large moves get high weight. Ehlers specifically chose squared distances over Euclidean distances to amplify the response to large price changes.
HOW IT WORKS
For each bar, EDCF evaluates the last length bars and assigns each a coefficient based on its total squared distance from all other bars in the window.
Step 1 — Distance-squared coefficient
For each bar position i in the window, compute how different its price is from every other bar in the window:
Dist² = Σ (Price − Price[i+k])² k = 1 to length−1
A bar surrounded by similar prices gets a low coefficient. A bar at the edge of a sharp move gets a high one.
Step 2 — Normalized weighted average
The output is the weighted average of all bars, normalized by the total weight:
EDCF = Σ(Dist² × Price) / Σ(Dist²)
Flat-price fallback
When all prices in the window are identical, all distance-squared coefficients are zero. The filter falls back to the current price — equivalent to SMA behavior with zero lag.
Why squared distances?
Ehlers explicitly chose Σ(diff²) over √(Σ(diff²)) to heighten filter response. Squaring amplifies the weight difference between trending and ranging bars, making the adaptive effect more pronounced.
INPUTS & PARAMETERS
- Source — Input series. Default: close.
- Length — Filter window length. Default: 15. Range: ≥ 2.
Larger length → smoother output, more lag, higher computational cost (O(length²) per bar).
Practical ceiling for real-time use: length ≤ 30. Above that, the nested loop becomes expensive on tick data.
HOW TO USE
Apply as a trend-following overlay on any instrument and timeframe.
Trend following
- In a trending market, EDCF weights the most-displaced bars heavily and tracks price more closely than a same-period EMA. Use it as a dynamic trailing reference.
- Price above EDCF → bullish bias. Price below → bearish bias.
Ranging detection
- When EDCF flattens and price oscillates tightly around it, the filter is approximating an SMA — a signal that the market has entered consolidation and trend-following signals should be discounted.
Breakout confirmation
- A sharp price extension that pulls EDCF steeply in one direction indicates the recent bars carry high distance-squared weight — confirmation that the move is statistically significant relative to the recent window, not just noise.
Asymmetric behavior awareness
- In a strong trend, EDCF behaves like a trailing stop: it follows closely in the direction of the trend but lags on reversals. Factor this into exit timing.
LIMITATIONS
- O(n²) complexity: Each bar requires a nested loop over the window. At length = 15 the cost is ~400 cycles/bar; at length = 30 it is ~1500 cycles/bar. Keep length ≤ 30 for real-time tick processing.
- Not an IIR filter: Despite being categorized as a filter, EDCF is a purely FIR structure — finite window, no feedback. It has no pole dynamics, unlike Ehlers' Laguerre or MESA SuperSmoother.
- Asymmetric lag: Response is faster in the direction of existing momentum and slower on reversals. This is a feature in trending regimes and a liability at turning points.
- No direct validation baseline: TA-Lib, Skender, and Tulip have no equivalent function. Validation is against the original Ehlers paper formulation and internal streaming/batch/span consistency.
- Warmup: Requires length bars before IsHot = true. Earlier outputs are provisional.
REFERENCES
- Ehlers, J. F. "Nonlinear Ehlers Filters." Stocks & Commodities, V.19:4, pp. 25–34, 2001.
- Ehlers, J. F. "Ehlers Filters." MESA Software technical paper.
HISTORICAL CONTEXT
John F. Ehlers introduced EDCF in "Nonlinear Ehlers Filters" (Stocks & Commodities, V.19:4, 2001) and in the accompanying MESA Software paper. The core insight: instead of fixing weights by position (as EMA or WMA do) or by spectral design (as Ehlers' own Laguerre and MESA filters do), let the price data itself determine how much each bar contributes. Bars that are close in price to their neighbors get low weight; bars that represent breakouts or large moves get high weight. Ehlers specifically chose squared distances over Euclidean distances to amplify the response to large price changes.
HOW IT WORKS
For each bar, EDCF evaluates the last length bars and assigns each a coefficient based on its total squared distance from all other bars in the window.
Step 1 — Distance-squared coefficient
For each bar position i in the window, compute how different its price is from every other bar in the window:
Dist² = Σ (Price − Price[i+k])² k = 1 to length−1
A bar surrounded by similar prices gets a low coefficient. A bar at the edge of a sharp move gets a high one.
Step 2 — Normalized weighted average
The output is the weighted average of all bars, normalized by the total weight:
EDCF = Σ(Dist² × Price) / Σ(Dist²)
Flat-price fallback
When all prices in the window are identical, all distance-squared coefficients are zero. The filter falls back to the current price — equivalent to SMA behavior with zero lag.
Why squared distances?
Ehlers explicitly chose Σ(diff²) over √(Σ(diff²)) to heighten filter response. Squaring amplifies the weight difference between trending and ranging bars, making the adaptive effect more pronounced.
INPUTS & PARAMETERS
- Source — Input series. Default: close.
- Length — Filter window length. Default: 15. Range: ≥ 2.
Larger length → smoother output, more lag, higher computational cost (O(length²) per bar).
Practical ceiling for real-time use: length ≤ 30. Above that, the nested loop becomes expensive on tick data.
HOW TO USE
Apply as a trend-following overlay on any instrument and timeframe.
Trend following
- In a trending market, EDCF weights the most-displaced bars heavily and tracks price more closely than a same-period EMA. Use it as a dynamic trailing reference.
- Price above EDCF → bullish bias. Price below → bearish bias.
Ranging detection
- When EDCF flattens and price oscillates tightly around it, the filter is approximating an SMA — a signal that the market has entered consolidation and trend-following signals should be discounted.
Breakout confirmation
- A sharp price extension that pulls EDCF steeply in one direction indicates the recent bars carry high distance-squared weight — confirmation that the move is statistically significant relative to the recent window, not just noise.
Asymmetric behavior awareness
- In a strong trend, EDCF behaves like a trailing stop: it follows closely in the direction of the trend but lags on reversals. Factor this into exit timing.
LIMITATIONS
- O(n²) complexity: Each bar requires a nested loop over the window. At length = 15 the cost is ~400 cycles/bar; at length = 30 it is ~1500 cycles/bar. Keep length ≤ 30 for real-time tick processing.
- Not an IIR filter: Despite being categorized as a filter, EDCF is a purely FIR structure — finite window, no feedback. It has no pole dynamics, unlike Ehlers' Laguerre or MESA SuperSmoother.
- Asymmetric lag: Response is faster in the direction of existing momentum and slower on reversals. This is a feature in trending regimes and a liability at turning points.
- No direct validation baseline: TA-Lib, Skender, and Tulip have no equivalent function. Validation is against the original Ehlers paper formulation and internal streaming/batch/span consistency.
- Warmup: Requires length bars before IsHot = true. Earlier outputs are provisional.
REFERENCES
- Ehlers, J. F. "Nonlinear Ehlers Filters." Stocks & Commodities, V.19:4, pp. 25–34, 2001.
- Ehlers, J. F. "Ehlers Filters." MESA Software technical paper.
Script open-source
Dans l'esprit TradingView, le créateur de ce script l'a rendu open source afin que les traders puissent examiner et vérifier ses fonctionnalités. Bravo à l'auteur! Bien que vous puissiez l'utiliser gratuitement, n'oubliez pas que la republication du code est soumise à nos Règles.
Rigorous technical indicators for Pine Script 6 with O(1) streaming, defensible math, and proper initialization. Fully documented, implemented in C#, transcribed to Pine Script, and wrapped for Python.
Clause de non-responsabilité
Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.
Script open-source
Dans l'esprit TradingView, le créateur de ce script l'a rendu open source afin que les traders puissent examiner et vérifier ses fonctionnalités. Bravo à l'auteur! Bien que vous puissiez l'utiliser gratuitement, n'oubliez pas que la republication du code est soumise à nos Règles.
Rigorous technical indicators for Pine Script 6 with O(1) streaming, defensible math, and proper initialization. Fully documented, implemented in C#, transcribed to Pine Script, and wrapped for Python.
Clause de non-responsabilité
Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.