Indicators: FIR_filter

 

FIR_filter:

Moving Average, calculated using the digital filter. In this example the Hann Window is used.

Author: Vladimir

FIR_filter

 
And why is the PI number so accurate?
 

DC2008:
А зачем такая точность числа ПИ? 

I haven't given it much thought. It's easy to find any high precision constant on google. I suppose that if metatradair doesn't need such high precision, it will automatically determine how many decimal places to discard.
 

Are you sure the formula is correct ? Please compare it with these http://dspsystem.narod.ru/add/win/win.html

Скользящая средняя на основе цифрового фильтра. В данном примере используется Hann Window. Для изменения коэффициентов фильтра, редактируйте следующие строки в OnInit():

Regarding terminology, sorry, you change not filter coefficients, but window coefficients. They are different things. In your example the digital filter (DF) is a moving average (MA), and it (MA) can be used with any window (a small part of them is given in the link above). Each window, and there are many, serves a specific purpose. Usually it improves some property of the DF, but you have to pay for it, nothing is given for nothing, distortions are introduced into the output spectrum.

As for the accuracy of Pi, accuracy is never unnecessary. We will always have time to load the result. Here is a simple way to set it https://www.mql5.com/en/code/8309.

It's a very nice solution.

 pi = 4*MathArctan(1);
 
Prival:

As for the accuracy of pi, accuracy is never unnecessary. We can always load the result. Here is a simple way to set it https://www.mql5.com/en/code/8309.

You should not define pi as a variable. Let it be a constant.

But you don't need extra characters either. For double we know the number of characters that can be stored.

 
lea:

You should not define pi as a variable. It is better to let it be a constant.

But you don't need extra characters either. For double, we know the number of characters that can be stored.

you are wrong. i lost 2 weeks in my time simply because pi was not defined with maximum precision. i checked the spectrum construction obtained in matkad with that made using the FFT library https://www.mql5.com/en/code/9696.
 
Prival:

Are you sure the formula is correct ? Please compare it with these http://dspsystem.narod.ru/add/win/win.html

Regarding terminology, sorry, you change not filter coefficients, but window coefficients. They are different things. In your example the digital filter (DF) is a moving average (MA), and it (MA) can be used with any window (a small part of them is given in the link above). Each window, and there are many, serves a specific purpose. Usually it improves some property of the DF, but you have to pay for it, nothing is given for nothing, distortions are introduced into the output spectrum.

There are different formulas for the Hahn window and other similar windows. They are all identical if you think about it. The formula in your link has one big drawback: zero window values at n=0 and n=N-1. Since there is no sense to multiply prices by zeros, it turns out that the window only exists for prices with n=1...n=N-2. Now let's denote the number of prices for which the window is not equal to zero through Per, as I have, then we get N-2=Per or N=Per+2. Substitute this N into Hahn's formula in your link and you will get the same formula as mine.

About the name of the indicator. The name is right. By definition, what I have constructed is a digital filter with finite impulse response

The difference equation that defines the output of an FIR filter in terms of its input is:

y[n]=b_0 x[n] + b_1 x[n-1] + ... + b_N x[n-N]

where:

  • x[n] is the input signal,
  • y[n] is the output signal,
  • bi are the filter coefficients, and
  • N is the filter order

See here http://en.wikipedia.org/wiki/Finite_impulse_response

I've been in electronics for 21 years. I know this stuff. Thanks always for the comments though. It's nice to talk to knowledgeable people.

 
gpwr:

There are different formulas for the Hahn window and other similar windows. They are all identical if you think about it. The formula in your link has one big disadvantage: zero values of the window at n=0 and n=N-1. Since there is no sense to multiply prices by zeros, it turns out that the window only exists for prices with n=1...n=N-2. Now let's denote the number of prices for which the window is not equal to zero through Per, as I have, then we get N-2=Per or N=Per+2. Substitute this N into Hahn's formula in your link and you will get the same formula as mine.

....

Thanks for the wikipedia link. I will try to show with formulas, what you have done.

For your case it would be more accurate to write this formula as follows

y[n]=a_0*b_0* x[n] +a_1* b_1* x[n-1] + ... + a_N*b_N* x[n-N]

x[n] is the input signal,

y[n] is the output signal,

bi are the filter coefficients, and

а[i] are the window coefficients,

N is the filter order

I.e. you have convolved the DF coefficients(b) and the window coefficients(a).

Yes, you are right that there are different formulas for writing window functions. But this difference is caused by the fact in which domain the convolution is performed, in the frequency or time domain. And these formulas are rigidly linked by the Fourier transform http://ru.wikipedia.org/wiki/Оконное_преобразование_Фурье.

Although the formulas are similar, you should never confuse in which domain the convolution is performed, otherwise you get an abracodabra.

Now about n. In the formula, it's really important to determine whether it starts at 0 or 1 ("...the window only exists for prices with n=1.....n=N), if you shift by 1, then everything shifts and N-1, becomes N, not N-2

The exact formula for the Hahn window looks like this

p(t) = 0.5[1+cos(pi*t/tac)].

For those who want to explore this topic a little deeper I have attached a file with a lecture.

For traders

I will try to explain what we are talking about using an example you all know.

https://www.metatrader5.com/ru/terminal/help/indicators/trend_indicators/ma

Everyone knows:

(a) Simple Moving Average (SMA).

Its window is rectangular, i.e. each price value has the same weight a[i]= 1, regardless of the depth of history.

b) Linear Weighted Moving Average (LWMA)

In a weighted moving average, the most recent data is given more weight and earlier data is given less weight.

i.e. window coefficients a[1]=1, a[2]=1/2,a[3]=1/3 .... a[n]=1/n

SO. gpwr also glad to communicate, a radio engineer will always find a common language with an electronic engineer, especially if both are not new in this business and speak a universal language - the language of mathematics.

Files:
dsp.rar  1811 kb
 

there are only 3 indicators - price, closing price, and simple moving average