alexgrover

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 !



Check out the indicators we are making at luxalgo: www.tradingview.com/u/LuxAlgo/
오픈 소스 스크립트

이 스크립트의 오써는 참된 트레이딩뷰의 스피릿으로 이 스크립트를 오픈소스로 퍼블리쉬하여 트레이더들로 하여금 이해 및 검증할 수 있도록 하였습니다. 오써를 응원합니다! 스크립트를 무료로 쓸 수 있지만, 다른 퍼블리케이션에서 이 코드를 재사용하는 것은 하우스룰을 따릅니다. 님은 즐겨찾기로 이 스크립트를 차트에서 쓸 수 있습니다.

면책사항

이 정보와 게시물은 TradingView에서 제공하거나 보증하는 금융, 투자, 거래 또는 기타 유형의 조언이나 권고 사항을 의미하거나 구성하지 않습니다. 자세한 내용은 이용 약관을 참고하세요.

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