PineCoders

Computing FIR Filters Using Arrays [WMA Example]

Over the years, many FIR filters have been proposed by the Pine community, with the standard way of computing them being `for` loops. The arrival of arrays allows for a new, more efficient way to compute them.

This script provides a template showing how you can compute FIR filters using Pine arrays.

FIR Filters

FIR stands for "Finite Impulse Response", and is associated with types of filters whose impulse response reaches a steady state.

FIR filters are calculated using convolution, or more simply put, using a weighted sum between a set of filter coefficients and past input values over a finite window.

In Pine, FIR filters are generally computed inside a `for` loop executing three processes:

1- Computing the coefficients.
2- Summing all the computed coefficients.
3- Performing the weighted sum between the inputs values and the computed coefficients.

Then we divide the result of our weighted sum by the sum of the coefficients obtained in step 2.

Because the computations inside the `for` loop execute on each bar, execution time can be significant when the calculation of coefficients is complex. This is where arrays are handy, as we can compute the coefficients just once, store them into an array, and use them in a weighted sum without the need to recalculate them over and over. This drastically reduces the computation time required to calculate a FIR filter.

The new `array.sum()` function helps eliminate step 2, thus further decreasing computation time.

How to Use This Template

All you need to do is to put the code that computes your coefficients in the first `for` loop (variable `w`). If the code that computes your coefficients contains more than one line, just make sure your final coefficient is placed in variable `w` (or change the `value` argument in `array.push()`). Another option is to declare a function that computes the coefficients and use it instead of variable `w`.



Look first. Then leap.


Tools and ideas for all Pine coders: www.pinecoders.com
Our Pine FAQ & Code: www.pinecoders.com/faq_and_code/
Pine news broadcasts: t.me/PineCodersSquawkBox or twitter.com/PineCoders
오픈 소스 스크립트

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

면책사항

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

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