OPEN-SOURCE SCRIPT
N Order EMA

The exponential moving average is one of the most fundamental tools in technical analysis, but its implementation is almost always locked to a single mathematical approach. I've always wanted to extend the EMA into an n-order filter, and after some time working through the digital signal processing mathematics, I finally managed to do it. This indicator takes the familiar EMA concept and opens it up to four different discretization methods, each representing a valid way to transform a continuous-time exponential smoother into a discrete-time recursive filter. On top of that, it includes adjustable filter order, which fundamentally changes the frequency response characteristics in ways that simply changing the period length cannot achieve.
The four discretization styles are impulse-matched, all-pole, matched z-transform, and bilinear (Tustin). The all-pole version is exactly like stacking multiple EMAs together but implemented in a single function with proper coefficient calculation. It uses a canonical form where you get one gain coefficient and the rest are zeros, with the feedback coefficients derived from the binomial expansion of the pole polynomial. The other three methods are attempts at making generalizations of the EMA in different ways. Impulse-matched creates the filter by matching the discrete-time impulse response to what the continuous EMA would produce. Matched z-transform directly maps the continuous poles to the z-domain using the exponential relationship. Bilinear uses the Tustin transformation with frequency prewarping to ensure the cutoff frequency is preserved despite the inherent warping of the mapping.
Honestly, they're all mostly the same in practice, which is exactly what you'd expect since they're all valid discretizations of the same underlying filter. The differences show up in subtle ways during volatile market conditions or in the exact phase characteristics, but for most trading applications the outputs will track each other closely. That said, the bilinear version works particularly well at low periods like 2, where other methods can sometimes produce numerical artifacts. I personally like the z-match for its clean frequency-domain properties, but the real point here is demonstrating that you can tackle the same problem from multiple mathematical angles and end up with slightly different but equally valid implementations.
The order parameter is where things get interesting. A first-order EMA is the standard single-pole recursive filter everyone knows. When you move to second-order, you're essentially cascading two filter sections, which steepens the roll-off in the frequency domain and changes how the filter responds to sudden price movements. Higher orders continue this progression. The all-pole style makes this particularly clear since it's literally stacking EMA operations, but all four discretization methods support arbitrary order. This gives you control over the aggressiveness of the smoothing that goes beyond just adjusting the period length.
On top of the core EMA calculation, I've included all the standard variants that people use for reducing lag. DEMA applies the EMA twice and combines the results to get faster response. TEMA takes it further with three applications. HEMA uses a Hull-style calculation with fractional periods, applying the EMA to the difference between a half-period EMA and a full-period EMA, then smoothing that result with the square root of the period. These are all implemented using whichever discretization method you select, so you're not mixing different mathematical approaches. Everything stays consistent within the chosen framework.
The practical upside of this indicator is flexibility for people building trading systems. If you need a moving average with specific frequency response characteristics, you can tune the order parameter instead of hunting for the right period length. If you want to test whether different discretization methods affect your strategy's performance, you can swap between them without changing any other code. For most users, the impulse-matched style at order 1 will behave almost identically to a standard EMA, which gives you a familiar baseline to work from. From there you can experiment with higher orders or different styles to see if they provide any edge in your particular market or timeframe.
What this really highlights is that even something as seemingly simple as an exponential moving average involves mathematical choices that usually stay hidden. The standard EMA formula you see in textbooks is already a discretized version of a continuous exponential decay, and there are multiple valid ways to perform that discretization. By exposing these options, this indicator lets you explore a parameter space that most traders never even know exists. Whether that exploration leads to better trading results is an empirical question that depends on your strategy and market, but at minimum it's a useful reminder that the tools we take for granted are built on arbitrary but reasonable mathematical decisions.
The four discretization styles are impulse-matched, all-pole, matched z-transform, and bilinear (Tustin). The all-pole version is exactly like stacking multiple EMAs together but implemented in a single function with proper coefficient calculation. It uses a canonical form where you get one gain coefficient and the rest are zeros, with the feedback coefficients derived from the binomial expansion of the pole polynomial. The other three methods are attempts at making generalizations of the EMA in different ways. Impulse-matched creates the filter by matching the discrete-time impulse response to what the continuous EMA would produce. Matched z-transform directly maps the continuous poles to the z-domain using the exponential relationship. Bilinear uses the Tustin transformation with frequency prewarping to ensure the cutoff frequency is preserved despite the inherent warping of the mapping.
Honestly, they're all mostly the same in practice, which is exactly what you'd expect since they're all valid discretizations of the same underlying filter. The differences show up in subtle ways during volatile market conditions or in the exact phase characteristics, but for most trading applications the outputs will track each other closely. That said, the bilinear version works particularly well at low periods like 2, where other methods can sometimes produce numerical artifacts. I personally like the z-match for its clean frequency-domain properties, but the real point here is demonstrating that you can tackle the same problem from multiple mathematical angles and end up with slightly different but equally valid implementations.
The order parameter is where things get interesting. A first-order EMA is the standard single-pole recursive filter everyone knows. When you move to second-order, you're essentially cascading two filter sections, which steepens the roll-off in the frequency domain and changes how the filter responds to sudden price movements. Higher orders continue this progression. The all-pole style makes this particularly clear since it's literally stacking EMA operations, but all four discretization methods support arbitrary order. This gives you control over the aggressiveness of the smoothing that goes beyond just adjusting the period length.
On top of the core EMA calculation, I've included all the standard variants that people use for reducing lag. DEMA applies the EMA twice and combines the results to get faster response. TEMA takes it further with three applications. HEMA uses a Hull-style calculation with fractional periods, applying the EMA to the difference between a half-period EMA and a full-period EMA, then smoothing that result with the square root of the period. These are all implemented using whichever discretization method you select, so you're not mixing different mathematical approaches. Everything stays consistent within the chosen framework.
The practical upside of this indicator is flexibility for people building trading systems. If you need a moving average with specific frequency response characteristics, you can tune the order parameter instead of hunting for the right period length. If you want to test whether different discretization methods affect your strategy's performance, you can swap between them without changing any other code. For most users, the impulse-matched style at order 1 will behave almost identically to a standard EMA, which gives you a familiar baseline to work from. From there you can experiment with higher orders or different styles to see if they provide any edge in your particular market or timeframe.
What this really highlights is that even something as seemingly simple as an exponential moving average involves mathematical choices that usually stay hidden. The standard EMA formula you see in textbooks is already a discretized version of a continuous exponential decay, and there are multiple valid ways to perform that discretization. By exposing these options, this indicator lets you explore a parameter space that most traders never even know exists. Whether that exploration leads to better trading results is an empirical question that depends on your strategy and market, but at minimum it's a useful reminder that the tools we take for granted are built on arbitrary but reasonable mathematical decisions.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.