# Edge-Preserving Filter

1722 visualizzazioni
Introduction

Edge-preserving smoothing is often used in image processing in order to preserve edge information while filtering the remaining signal. I introduce two concepts in this indicator, edge preservation and an adaptive cumulative average allowing for fast edge-signal transition with period increase over time. This filter have nothing to do with classic filters for image processing, those filters use kernels convolution and are most of the time in a spatial domain.

Edge Detection Method

We want to minimize smoothing when an edge is detected, so our first goal is to detect an edge. An edge will be considered as being a peak or a valley, if you recall there is one of my indicator who aim to detect peaks and valley (reference at the bottom of the post), since this estimation return binary outputs we will use it to tell our filter when to stop filtering.

Filtering Increase By Using Multi Steps Cumulative Average

The edge detection is a binary output, using a exponential smoothing could be possible and certainly more efficient but i wanted instead to try using a cumulative average approach because it smooth more and is a bit more original to use an adaptive architecture using something else than exponential averaging. A cumulative average is defined as the sum of the price and the previous value of the cumulative average and then this result is divided by n with n = number of data points. You could say that a cumulative average is a moving average with a linear increasing period.

So lets call CMA our cumulative average and n our divisor. When an edge is detected CMA = close price and n = 1 , else n is equal to previous n+1 and the CMA act as a normal cumulative average by summing its previous values with the price and dividing the sum by n until a new edge is detected, so there is a "no filtering state" and a "filtering state" with linear period increase transition, this is why its multi-steps.

The Filter

The filter have two parameters, a length parameter and a smooth parameter, length refer to the edge detection sensitivity, small values will detect short terms edges while higher values will detect more long terms edges. Smooth is directly related to the edge detection method, high values of smooth can avoid the detection of some edges.

smooth = 200

smooth = 50

smooth = 3

Conclusion

Preserving the price edges can be useful when it come to allow for reactivity during important price points, such filter can help with moving average crossover methods or can be used as a source for other indicators making those directly dependent of the edge detection.

Rsi with a period of 200 and our filter as source, will cross triggers line when an edge is detected

Feel free to share suggestions ! Thanks for reading !

References

Peak/Valley estimator used for the detection of edges in price.
Script open-source

Nello spirito di condivisione promosso da TradingView, l'autore (al quale vanno i nostri ringraziamenti) ha deciso di pubblicare questo script in modalità open-source, così che chiunque possa comprenderlo e testarlo. Puoi utilizzarlo gratuitamente, ma il riutilizzo del codice è subordinato al rispetto del Regolamento. Per aggiungerlo al grafico, mettilo tra i preferiti.

Vuoi usare questo script sui tuoi grafici?
Patreon: https://www.patreon.com/alexgrover

You can also check out some of the indicators I made for luxalgo : https://www.tradingview.com/u/LuxAlgo/#published-scripts

## Commenti

It allways completely baffles me how you get that much output out of so few lines!!
Wonderfull work!
Rispondi
Duyck
@Duyck, i feel the same his explanation is longer than the code itself and have a nice day Alex lol
Rispondi
ernie76
@ernie76, Thanks for the support :)
Rispondi
Duyck
@Duyck, Thanks for your regular support :) Most of the time the use of recursion allow for more complex outputs without the need of a high number of code. If you look how the sma function is made you will see that the code lack efficiency in comparison with an exponential moving average who use recursion and produce a similar result.
Rispondi
Thank you again for a great code and imagination
Rispondi
aaahopper
@aaahopper, Glad you like it :) Thanks for the support
Rispondi
Looks amazing, I love the simplicity!
Rispondi
jaggedsoft
@jaggedsoft, I'm delighted to hear that :) Feel free to modify the code and publish your findings
Rispondi
alexgrover
@alexgrover, I haven't tested it extensively but it looks very good for usage as a screener or confirmation for other indicators. The only criteria I tested so far is if c > c
Rispondi
MasterPiece!!!!!!!!!!!Thanks for sharing!!!!
Rispondi
Profilo Impostazioni profilo Account e fatturazione Amici invitati Coin I miei ticket di supporto Centro di supporto Messaggi Privati Chat Esci