Customizable FIR Filter

Attached is a completely customizable FIR filter. It allows you to set all of of the coefficients in the matrix with up to 160 elements which transforms our data set and normalizes by dividing the result by the sum of the elements of the matrix.

If you have no idea what this filter does after those last two sentences, have no fear! The following is a write-up I did on the implementation of this filter for documentation purposes. If you want to try to create your very own super custom moving average, or if you would just like a little more insight as to what all those pretty little lines you draw all over your graphs REALLY are, please read on!

I am a firm believer that knowledge is power, in Forex and in life. So if you have any questions, comments, or anything else regarding anything I say. I encourage you to post or PM me (once I have reached 15 posts and can PM that is!). I love answering questions and helping people figure things out so they are better equipped to use their arsenal of indicators (and realize why EVERY indicator is limited). As a final note, please don't let this thread turn into a debate about the usefulness of indicators, the endless "zero-lag" debate, or anything else. It is simply meant to inform, and let people discover the power behind designing an indicator that fits THEIR needs.

I would say it is a safe bet that everyone has used a FIR filter whether they knew it or not. For those of you who aren't familiar with impulse response filters there are two basic classes, finite and infinite. But first and foremost, what is a filter? A filter is simply something that manipulates data according to a set of mathematically based rules in order to produce (hopefully) a more desirable result. Infinite response filters are ones that theoretically are affected by every piece of data, forever (hence the name). This means that in an infinite response filter, data from the 1980s is present in today's indicator data. That really isn't the point of the post though, I will discuss these filters more when I get a generic IIR (infinite impulse response) filter programmed. But just for future reference an EMA would be an example of an IIR.

Anyways on to more pertinent information. A finite impulse response filter (FIR) is a filter that is only affected by data in a given time frame. SMAs are a very simple FIR. Weighted moving averages are also FIR filters. Now, what a FIR actually does is pretty simple in application but still needs a little explaining before you can truly understand what the attached indicator does. By the way, if you have a background in digital filter processing, please "Pass Go, Collect Your $200," and download the indicator already! For everyone else, just bare with me for a little while longer and I promise your insight into moving averages will increase ten fold!

Now what a FIR filter actually does, is manipulate the 'weight' of the data in a certain 'window'. The period of you SMA is the same as this window. So a 10 SMA has a window of 10 bars, and a 100 SMA has a window of 100 bars. Before I continue with my explanation, I think a few visual aids are in order!

Let's say we have ten opening prices and we are going to apply a 3 SMA. The prices can be seen below.

http://i103.photobucket.com/albums/m...arsofprice.gif

Now what a FIR filter (and simple moving average) actually does is use matrix math to transform the data. Don't worry about the matrix end of it though, because it is very easily explained using a 'window' example. A SMA with a period of three means that it is looking at three prices at a time (common sense, I know) and what the SMA does is average the three values together. You probably could have guessed this on your own, but HOW does it average the values together using the 'window' approach is what I am about to show you. The 3 SMA window can be thought of as a box with three numbers in it. Now in a SMA all past data points we consider have the same 'weight' so all the numbers in the 3 SMA window are one. This can be seen below.

http://i103.photobucket.com/albums/m...3SMAwindow.gif

Now to use the filter we simply 'slide' the 3SMA window along all our prices. You multiply the number in the window, with the price directly above it. Add all these multiplied prices together, divide by how many numbers you have in your window, and VOILA! That’s the value of the 3 SMA at that point. A visual aid is definitely called for in this case! (to keep things consistent with charts, I am going to slide the 3SMA window from the right to the left so start reading the figure below in the upper right hand corner)

http://i103.photobucket.com/albums/m...plying3SMA.gif

Now I know this is a lot to ingest, but just look over it a few times. The important lesson is that the SMA is just a FIR filter with all the numbers in its 'window' equaling one. So if we had a 10 SMA it would be a box with ten 1's in it, and we would add up all the prices (multiplied by 1 or course!) in the filter's window, divide by 10, and that would be the value at the right hand side of the window. Then we move the filter window one spot to the left, and repeat the process!

Now finally onto the indicator!

The indicator attached is a generic FIR filter indicator. It can be used exactly like a SMA. Now you are probably thinking to yourself, "You mean I just waded through all that math, and all those stupid little charts just so that I could get an indicator that does exactly the same thing as a simple moving average!"

If you are thinking that, you should re-read what I said; this indicator CAN BE USED like an SMA, the difference is that the indicator lets you choose the values of the numbers in the 'window.' This means if you want a 6 SMA, set the indicator settings to have a FilerPeriod of 6, and leave all the "coef" in the indicator settings at 1. BUT if you want a 6 period weighed moving average, set the "coef"s in the indicator to 6, 5, 4, 3, 2, 1 (it doesn’t matter what any of the "coef" are past the 6th one, since we are only doing a 6 period WMA).

The "coef" in the indicator settings stand for coefficient and refers to the numbers in the filter 'window.' They are ordered so that coef_1 is multiplied by the current bar, coef_2 is multiplied by the previous bar, and so on and so forth.

Now I still haven't explained how to do anything TOO special with the indicator yet, but play with it for a few days, maybe even make up your own patterns of number and see what they do compared to a standard moving average. For example if you want the previous bar to be really important, and all the rest to be of equal importance. Use coefficients 1, 2, 1, 1, 1, 1…..

Please keep in mind though, that since this is based on the same principles as a SMA, it experiences the same amount of lag (the time it takes it to respond to a price movement). Just tinker around a little bit with the indicator and in a few days when I have time for another write-up (and all this has soaked in for anyone who has never used a filter before) I will post some more tricks to using this indicator such as….

Lag reduction techniques (Is zero lag really possible? How about a lag of -1?!?!)

Coefficient combinations for better smoothing

Coefficient combinations for 'filtering out' long term or short term trends

Until next time, have fun, get some pips, and for goodness sake don't spend TOO many hours trying to find the "perfect MA"

New Update!

The indicator has now been updated to only work from coefficient lists read from files. See later post for full explanation on format and limitations of the text files it can read from.

Attached is a completely customizable FIR filter. It allows you to set all of of the coefficients in the matrix with up to 160 elements which transforms our data set and normalizes by dividing the result by the sum of the elements of the matrix.

If you have no idea what this filter does after those last two sentences, have no fear! The following is a write-up I did on the implementation of this filter for documentation purposes. If you want to try to create your very own super custom moving average, or if you would just like a little more insight as to what all those pretty little lines you draw all over your graphs REALLY are, please read on!

I am a firm believer that knowledge is power, in Forex and in life. So if you have any questions, comments, or anything else regarding anything I say. I encourage you to post or PM me (once I have reached 15 posts and can PM that is!). I love answering questions and helping people figure things out so they are better equipped to use their arsenal of indicators (and realize why EVERY indicator is limited). As a final note, please don't let this thread turn into a debate about the usefulness of indicators, the endless "zero-lag" debate, or anything else. It is simply meant to inform, and let people discover the power behind designing an indicator that fits THEIR needs.

Finite Impulse Response(FIR) filters - The Grand Mama of Moving Averages!Finite Impulse Response(FIR) filters - The Grand Mama of Moving Averages!

I would say it is a safe bet that everyone has used a FIR filter whether they knew it or not. For those of you who aren't familiar with impulse response filters there are two basic classes, finite and infinite. But first and foremost, what is a filter? A filter is simply something that manipulates data according to a set of mathematically based rules in order to produce (hopefully) a more desirable result. Infinite response filters are ones that theoretically are affected by every piece of data, forever (hence the name). This means that in an infinite response filter, data from the 1980s is present in today's indicator data. That really isn't the point of the post though, I will discuss these filters more when I get a generic IIR (infinite impulse response) filter programmed. But just for future reference an EMA would be an example of an IIR.

Anyways on to more pertinent information. A finite impulse response filter (FIR) is a filter that is only affected by data in a given time frame. SMAs are a very simple FIR. Weighted moving averages are also FIR filters. Now, what a FIR actually does is pretty simple in application but still needs a little explaining before you can truly understand what the attached indicator does. By the way, if you have a background in digital filter processing, please "Pass Go, Collect Your $200," and download the indicator already! For everyone else, just bare with me for a little while longer and I promise your insight into moving averages will increase ten fold!

Now what a FIR filter actually does, is manipulate the 'weight' of the data in a certain 'window'. The period of you SMA is the same as this window. So a 10 SMA has a window of 10 bars, and a 100 SMA has a window of 100 bars. Before I continue with my explanation, I think a few visual aids are in order!

Let's say we have ten opening prices and we are going to apply a 3 SMA. The prices can be seen below.

http://i103.photobucket.com/albums/m...arsofprice.gif

Now what a FIR filter (and simple moving average) actually does is use matrix math to transform the data. Don't worry about the matrix end of it though, because it is very easily explained using a 'window' example. A SMA with a period of three means that it is looking at three prices at a time (common sense, I know) and what the SMA does is average the three values together. You probably could have guessed this on your own, but HOW does it average the values together using the 'window' approach is what I am about to show you. The 3 SMA window can be thought of as a box with three numbers in it. Now in a SMA all past data points we consider have the same 'weight' so all the numbers in the 3 SMA window are one. This can be seen below.

http://i103.photobucket.com/albums/m...3SMAwindow.gif

Now to use the filter we simply 'slide' the 3SMA window along all our prices. You multiply the number in the window, with the price directly above it. Add all these multiplied prices together, divide by how many numbers you have in your window, and VOILA! That’s the value of the 3 SMA at that point. A visual aid is definitely called for in this case! (to keep things consistent with charts, I am going to slide the 3SMA window from the right to the left so start reading the figure below in the upper right hand corner)

http://i103.photobucket.com/albums/m...plying3SMA.gif

Now I know this is a lot to ingest, but just look over it a few times. The important lesson is that the SMA is just a FIR filter with all the numbers in its 'window' equaling one. So if we had a 10 SMA it would be a box with ten 1's in it, and we would add up all the prices (multiplied by 1 or course!) in the filter's window, divide by 10, and that would be the value at the right hand side of the window. Then we move the filter window one spot to the left, and repeat the process!

Now finally onto the indicator!

The indicator attached is a generic FIR filter indicator. It can be used exactly like a SMA. Now you are probably thinking to yourself, "You mean I just waded through all that math, and all those stupid little charts just so that I could get an indicator that does exactly the same thing as a simple moving average!"

If you are thinking that, you should re-read what I said; this indicator CAN BE USED like an SMA, the difference is that the indicator lets you choose the values of the numbers in the 'window.' This means if you want a 6 SMA, set the indicator settings to have a FilerPeriod of 6, and leave all the "coef" in the indicator settings at 1. BUT if you want a 6 period weighed moving average, set the "coef"s in the indicator to 6, 5, 4, 3, 2, 1 (it doesn’t matter what any of the "coef" are past the 6th one, since we are only doing a 6 period WMA).

The "coef" in the indicator settings stand for coefficient and refers to the numbers in the filter 'window.' They are ordered so that coef_1 is multiplied by the current bar, coef_2 is multiplied by the previous bar, and so on and so forth.

Now I still haven't explained how to do anything TOO special with the indicator yet, but play with it for a few days, maybe even make up your own patterns of number and see what they do compared to a standard moving average. For example if you want the previous bar to be really important, and all the rest to be of equal importance. Use coefficients 1, 2, 1, 1, 1, 1…..

Please keep in mind though, that since this is based on the same principles as a SMA, it experiences the same amount of lag (the time it takes it to respond to a price movement). Just tinker around a little bit with the indicator and in a few days when I have time for another write-up (and all this has soaked in for anyone who has never used a filter before) I will post some more tricks to using this indicator such as….

Lag reduction techniques (Is zero lag really possible? How about a lag of -1?!?!)

Coefficient combinations for better smoothing

Coefficient combinations for 'filtering out' long term or short term trends

Until next time, have fun, get some pips, and for goodness sake don't spend TOO many hours trying to find the "perfect MA"

New Update!

The indicator has now been updated to only work from coefficient lists read from files. See later post for full explanation on format and limitations of the text files it can read from.

Attached Files

Customizable FIR Filter.ex4 6 KB | 524 downloads | Uploaded May 21, 2008 9:20pm

FIR coef.txt < 1 KB | 651 downloads | Uploaded May 21, 2008 9:20pm

Array Filters.mqh 9 KB | 549 downloads | Uploaded May 21, 2008 9:21pm

File Handling.mqh 11 KB | 530 downloads | Uploaded May 21, 2008 9:21pm

Customizable FIR Filter.mq4 2 KB | 622 downloads | Uploaded May 21, 2008 9:21pm

Kiss'n all the way to the top!