alexgrover

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.

Settings

  • 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

Usages

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.

Details

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 "line.new" inside a loop, an exponential moving average is applied forward first, then once again backward inside the loop, "line.new" is used to plot the results backward.

Notes

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.






Check out the indicators we are making at luxalgo: www.tradingview.com/u/LuxAlgo/
开源脚本

本着真正的TradingView精神,该脚本的作者将其开源发布,以便交易者可以理解和验证它。为作者喝彩!您可以免费使用它,但在出版物中重复使用此代码受网站规则的约束。 您可以收藏它以在图表上使用。

免责声明

这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。

想在图表上使用此脚本?