# Edge-Preserving Filter

771浏览
771
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.
Great work! Would love to see a linear regression/Bollinger Band script.

AmariMars
@AmariMars, Thanks for the support, i will consider working on what you mentioned :)

Thank you again for a great code and imagination

aaahopper
@aaahopper, Glad you like it :) Thanks for the support

Looks amazing, I love the simplicity!

jaggedsoft
@jaggedsoft, I'm delighted to hear that :) Feel free to modify the code and publish your findings

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

MasterPiece!!!!!!!!!!!Thanks for sharing!!!!

sudhir.mehta
@sudhir.mehta, Am i really worthy of such praise :D ? Thanks for your support, i'm really glad you like the script :)

alexgrover
@alexgrover, yes. Innovation is rare in this industry & it's nice to see people being creative. Keep doing what you're doing and you will get whatever you want out of life