OPEN-SOURCE SCRIPT

Template For Custom FIR Filters - Make Your Moving Average

Introduction

FIR filters (finite impulse response) are widely used in technical analysis, there is the simple or arithmetic moving average, the triangular, the weighted, the least squares...etc. A FIR filter is characterized by the fact that its impulse response (the output of a filter using an impulse as input) is finite, this mean that the impulse response won't have infinite outputs unlike IIR filters.

They are extremely simple to design to, even without the Fourier transform, this is why i post this template that will let you create custom filters from step responses. Don't hesitate to post your results.

How It Works

Originally you create your filters from the frequency response you want your filter to have, this is because the inverse Fourier transform of the frequency response is the filter impulse response.

After that step you use convolution (convolution is the sum of the product between the signal and the impulse response) and you will have your filter. But we don't have Fourier transforms in pine so how can we possibly make FIR filters from convolution ? Well here the thing, the impulse response is the derivative of the step response and the step response is the sum of the impulse response, this mean we can create filters from step responses.

스냅샷

Step response of a moving average.

Step responses are easy to design, you just need a function that start at 0 and end up at 1.

How To Use The Template

All the work is done for you, the only thing you need to do is to enter your function at line 5 :

f(x)=> your function

For example if you want your filter to have a step response equal to sqrt(x) just enter :

f(x)=> sqrt(x)

This will give the following filter output :

스냅샷

You can create custom step responses from online graphing tools like fooplot or wolfram alpha, i recommend fooplot.

You can also design your filter step response from the line 14/15/16, b will be your filter step response, just use a, for example b = pow(a,2), then replace output in plot by b and use overlay false, you can also plot step, if you like your step response copy the content of b and paste after f(x) =>.

Filter Characteristics

The impulse response determine how many of a certain signal you want in your filter, this is also called weighting, you can think of filter design as cooking where your ingredients are the the signal at different periods and the impulse response determine how many of an ingredient you must include in the recipe. The step response can also tell you about your filter characteristics, for example :

스냅샷

This one converge faster to the step function, this mean that the filter will have less lag.

스냅샷

However this one converge slower to the step function, this mean the filter might have more lag but could be smoother.

Be aware that you must find a good weighting balance, else you can have output equals to the signal or just a delayed version of the signal without smoothing.

Real Case

Lets design a sine weighted moving average (swma), this FIR filter use the first 180 degrees of a sine wave function as impulse response.

스냅샷

Impulse response of the swma.

We can design it from the step response without much problems, remember that the impulse response is the derivative of the step response, therefore the derivative of the step response is equal to the first 180 degrees of a sine wave, the derivative of the cosine function is a sine function, therefore :

f(x)=> .5*(1 - cos(x*pi))

스냅샷

And voila.

Designing A BandPass Filter

The bandpass filter like a low-pass and high pass filter, you can think of it as a smooth oscillator.

To design a bandpass filter your step response must be bell shaped, or starting at 0 and ending at 0, for example :

f(x)=>sin(x*pi) give :

스냅샷


Conclusion

Just use fooplot and experiment, you could get nice filters, i will try to post some using this template but it would be really nice to have other people use it. If you need further help pm me.

Thanks for reading !


bandpassdesignfilterFIRMoving Averagespinescriptsmoothingtemplate

오픈 소스 스크립트

진정한 TradingView 정신에 따라, 이 스크립트의 저자는 트레이더들이 이해하고 검증할 수 있도록 오픈 소스로 공개했습니다. 저자에게 박수를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 출판물에서 이 코드를 재사용하는 것은 하우스 룰에 의해 관리됩니다. 님은 즐겨찾기로 이 스크립트를 차트에서 쓸 수 있습니다.

차트에 이 스크립트를 사용하시겠습니까?


Check out the indicators we are making at luxalgo: tradingview.com/u/LuxAlgo/
또한 다음에서도:

면책사항