Zero Phase Filtering [Repaint] - Experimental

Important !

The indicator is for experimental purpose only, it must not be used as a decisional tool but only as a visual one (like Zig-Zag , Fractal etc). The information this indicator display is uncertain and subject to drastic changes over time. If you have further question feel free to pm me.


Most of the filters you will find are causal, this mean that they depend on present and past input values, this explain the lag they produce. Non causal filters however will use future input values. A well know way to get a zero-phase filter is by using the forward backward method, but this is not possible in pinescript as i recall. So we have to use some kind of function that will display future values, this is possible using the security function in version 2 or the one in version 3 using barmerge.lookahead_on.

The Use Of A Repainting Indicator

Its always better to filter data in order to have a clearer view of what is happening, this can be useful when doing some forecasting or doing less formal kind of analysis. However since it repaint you cant use it as a signal provider or use signals of other indicators using this filter as source.

For example if you want to forecast a smooth indicator, the forecast of this indicator under normal circumstances could still have lag associated with it, so you would have to react before your forecast, this wont happen if you apply this filter as your indicator source.

The Filter

We smooth with a simple moving average the price provided by the security function twice, length control the smoothing level. Since security depend on the time frame you are in you must select your time frame in the indicator parameter selection window.

Filtering using 45 minutes time frame close price in a 5 minutes chart, we fix this by selecting our time frame.

Consider the fact that the input of the indicator is just periodic price, so sometimes the lag can sometimes be less or more than 0 and the estimation not centered.

The indicator can work on time frames up to 1h, after that the filter have some lag, i tried fixing this and i ended up having data errors.

Applying our filter as source for the rsi oscillator.


It is possible to have a kind of zero-phase filters, but it would be better if pinescript could support backward indexing thus making us able to do forward backward filtering.
Since noise can affect our analysis, applying smoothing without having to use offset in plot can be considered useful.

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Become a Patreon and get access to exclusive technical indicators!

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


Practice build perfect! keep it up Alex and others coding genius =D
+2 Reply
@ICEKI, I'm always glad to have your support :) Feel free to leave a suggestion.
I may be wrong, but I **think** that we can all use //@version=4 now. Not sure what features this has, what level of functionality it's at etc., but maybe it will allow for forward backward filtering.

All the same, thank you for leading the vanguard in Pine. I hope the new version allows you to realise some of the ideas you have.
+1 Reply
alexgrover chrysopoetics
@chrysopoetics, I wasn't aware of version 4, thanks for the info, anyway i doubt they made interesting implementations such as array/multi dimensional array, complex numbers, break/continue/while and eliminating all the redundant type like const type. Thanks for your support.
alexgrover alexgrover
@alexgrover, EDIT : I forgot that break and continue are already implemented, my bad.
i also tried something like this but ended in failure, i think valuewhen() and loops dont go well it seams or i made a mistake somewhere, this was my attempt on a norepainting mtf ma:

pine_ema(_tf, _src, _length) =>
// alpha = 2 / (_length + 1)
// sum = 0.0
// sum := alpha * _src + (1 - alpha) * nz(sum)
_alpha = 2 / (_length + 1)
_sum = 0.0

for _at_time = _length to 0
_tf_src = valuewhen((change(time(_tf)) != 0), _src, _at_time)
_previous_sum = nz(_sum, 0.0)
_sum := _alpha * _tf_src + (1 - _alpha) * _previous_sum
alexgrover RicardoSantos
@RicardoSantos, I'm not familiar with loops under pine but i think that valuewhen should work fine, the ability to loop backwards in pine might have some problems or just dont work the way it should. There is also a method involving repainting functions/indicator acting as a trigger instead of a source that was made with prorealcode, such method could fix the need of loops and redundant computations for the computation of zero-phase indicators.
RicardoSantos alexgrover
@alexgrover, yes i agree, the issue with valuewhen() and the for loop is that it only reads the first iteration of valuewhen in the loop
but i digress out of context now :p
Jittra RicardoSantos
@RicardoSantos, @alexgrover lookBack = isintraday and interval >= 1 ? tf/interval * 7 : isintraday and interval < 60 ? 60/interval * 24 * 7 : 7 fix the repainting.
Home Stock Screener Forex Screener Crypto Screener Economic Calendar About Chart Features Pricing Refer a friend House Rules Help Center Website & Broker Solutions Widgets Charting Solutions Lightweight Charting Library Blog & News Twitter
Profile Profile Settings Account and Billing Referred friends Coins My Support Tickets Help Center Private Messages Chat Sign Out