Forward Backward EMA [Repaint]

Perform forward-backward filtering using exponential averaging, thus providing a zero-phase exponential moving average . The output repaint and cannot be used as input for other indicators.


  • Length : moving average period
  • Src : data input of the moving average
  • Plot Color : the color of the displayed plot
  • Line Width : width of the plotted line


The main usage of moving averages is to provide an estimate of the underlying trend in the price by removing higher term variations from it. Non-causal (repainting) indicators are limited to offline applications, as such, they are most useful for summary analyses, note that it is still possible to infer from the output of repainting indicators, however since past outputs are subject to changes, it is extremely difficult to track the effectiveness of such indicators, and in online applications they only track the price, making them equally useful for predictive applications than following the direction of an individual candle.

Non-causal filters can be useful in order to have a better view of symbols with a relatively uninformative evolution.


Causal filters have lag, this is the cost of using past observations as inputs, the more past observations you use, the more lag you will obtain (assuming these past observations have non-zero weights). There are various solutions to reduce the lag of a moving average, the most simple one relying on giving higher weights to more recent observations, another one relies on introducing gain in the filter passband, that is amplifying certain variations in the input signal while attenuating/removing higher term ones, finally, we can use adaptive moving averages to avoid excessive lag.

All these previous solutions can be used causally, but they are far from being perfect, as the lag reduction is often done at the cost of smoothness, if we were to keep the original smoothness of the filter while having no lag we would need to use non-causal solutions. The most common solution is to directly use future values as inputs, such moving averages are called "two-sided" moving averages since they use past values as input (left side) as well as future input values (right side), this is equivalent to shifting the results of a moving average backward.

The advantages of two-sided moving averages is that they conserve the original amplitude response of the moving average, however, it won't be possible to compute the most recent values of the moving average (since we won't have access to future values at a certain point), an alternative method heavily used in digital signal processing is forward-backward filtering.

The method consists of applying a filter forward in time, then we apply it once again backward. In order for you to have an easier understanding of this process think about applying a moving average normally starting at time t = 0, then apply that moving average once again using the previous results as input but start from t = N-1, that is from the most recent point, and proceed backward, plotting the result from left to right until you get back to t = 0.

From this, it follows that forward-backward filtering applies a filter twice, the resulting filter is thus a two-passes filter, this results in an even smoother output (more precisely the filter amplitude response is squared).

Forward-backward filtering can be done in Pinescript by using the function "" inside a loop, an exponential moving average is applied forward first, then once again backward inside the loop, "" is used to plot the results backward.


It is important to note that forward-backward filtering is a repainting process, all the results of the indicator you see on the chart are subject to change over time. Since the method make use of line. new you will have around only 54 visible observations, with the impossibility of using them as input for other indicators. If you see indicators in the future with the same characteristics be aware that they will repaint.

Never purchase/rent filters that appear as having no lag, they are either repainting or the results are coming from a lucky shot or from an overfitted model, it is impossible to make both zero-lag and causal moving averages with pinescript, if you have doubt don't test your luck, better safe than sorry.


Become a Patreon and get access to exclusive technical indicators!

You can also check out some of the indicators I made for luxalgo :

本著真正的TradingView精神,該腳本的作者將其開源發布,以便交易者可以理解和驗證它。為作者喝彩吧!您可以免費使用它,但在出版物中重複使用此代碼受網站規則的約束。 您可以收藏它以在圖表上使用。