Difference calculus, examples. - page 4

 
Yousufkhodja Sultonov:

When you increase the sample to N=100, the 4th degree equation gives the following result:



Mathematically speaking, the boundaries of your search are very blurred. Could you please elaborate on how it looks like?

 
Nikolai Semko:

Yes, I was mistaken. I thought you were really using approximation. I took a closer look at your code and realized that it is not an approximation, but just a trivial averaging, albeit very unusual. After that you move the purple line and the red line 72 bars to the left and finish drawing the red tail of 92 bars, and it is redrawn with every new bar. The blue line is formed from the shifted purple line. And by the way, it is more correct to use the close price rather than the open one. If you change it to close, you can immediately see that the red tail of 92 bars jumps with every tick.

Shifting to the left of the moving averages has no usefulness and practical application. It only serves to look nice and fit.

We need to understand the difference between approximation and smoothing (averaging). When approximating, coefficients of a function (polynomial, Fourier, Bezier, spline, etc.) are calculated on a given data interval, and as a rule, these coefficients all change when at least one data value changes, therefore the function is redrawn on the entire observed data interval. But when averaging, only one current point on the basis of previous data is considered, therefore averaging (smoothing) is not redrawn, but always lags behind the data unlike approximation.
And I don't understand what polynomial of some degree and Newton's Binom have to do with it, if there aren't even any degrees in the code.

))

Uh-huh, and now also a sinusoid without an explicit Sin function.

The difference equation for sine:https://dxdy.ru/post1247421.html#p1247421

      a1_Buffer[i]=((open[i] - Znach)    +5061600*a1_Buffer[i+1 ]-7489800   *a1_Buffer[i+2 ]+4926624*a1_Buffer[i+3 ]-1215450*a1_Buffer[i+4 ])/1282975;

      a2_Buffer[i]=  2701*a1_Buffer[i]   -5328   *a1_Buffer[i+1 ]    +  2628 *a1_Buffer[i+2 ];

      a4_Buffer[i+92]=a1_Buffer[i];   if(i<=1) { for(z=92-1;z>=0;z--){        a4_Buffer[i+0+z]=  5*a4_Buffer[i+1+z]  -  10*a4_Buffer[i+2+z]   +   10*a4_Buffer[i+3+z]  -  5*a4_Buffer[i+4+z]  +  1*a4_Buffer[i+5+z];  }}

      a3_Buffer[i+292]=a1_Buffer[i];   if(i<=1) { for(z=292-1;z>=0;z--){         a3_Buffer[i+0+z]=  2.998096443*a3_Buffer[i+1+z]  -  2.998096443*a3_Buffer[i+2+z]   +   1*a3_Buffer[i+3+z]   ;  }}

      a5_Buffer[i+92]=a1_Buffer[i];   if(i<=1) { for(z=92-1;z>=0;z--){        a5_Buffer[i+0+z]=  3*a5_Buffer[i+1+z]  -  3*a5_Buffer[i+2+z]   +   1*a5_Buffer[i+3+z]   ;  }}

      a6_Buffer[i+292]=a1_Buffer[i];   if(i<=1) { for(z=292-1;z>=0;z--){         a6_Buffer[i+0+z]=  3.998096443*a6_Buffer[i+1+z]  -  5.996192886*a6_Buffer[i+2+z]   +   3.998096443*a6_Buffer[i+3+z]  -  1*a6_Buffer[i+4+z] ;  }}

The calculation of sine waves with a period of 144 is highlighted. The first one is near the constant (green in the figure), the second one is near the sloping line (red in the figure).

Рекуррентная формула для синуса : Дискуссионные темы (М) - Страница 7
  • dxdy.ru
В принципе, используется и рекуррентное вычисление через возвратное уравнение второго порядка, и через комплексную экспоненту. Первое менее расходно по ресурсам (умножение и два сложения и две ячейки памяти) по сравнению со вторым (два умножения, четыре сложения, две ячейки памяти при постоянной частоте), но накапливается погрешность быстрее...
 

I probably should have attached a "reading list" to the branch right away. ))

There is a lot of literature on the subject, so I will suggest a couple of thin books to my taste:

Calculus of finite differences by Leonid Kuzmich Lakhtin.

Markushevich A.I. Return Sequences

 
Aleksey Panfilov:

))

Uh-huh, and now also a sinusoid without an explicit Sin function.

The difference equation for sine:https://dxdy.ru/post1247421.html#p1247421

The calculation of the sinusoids with a period of 144 is highlighted. The first one is near the constant (green in the figure), the second one is near the sloping line (red in the figure).


Thank you, Alexey for the literature. I admit that recursion is noteworthy and can be successfully applied to speed up some functions or algorithms, but to tell the truth, I am not sure about it.
I just advocate calling things by their proper names and using common terminology, so that there is no confusion. In my opinion, it would have been more logical to mention recursion at the beginning of this thread, and not to mention interpolation, approximation and polynomials, as they are not shown in your example. And it would have been more correct to focus on the shift of the indicator to the left, so as not to mislead others by excessive correctness of forms, because not everyone likes to look into other people's code, I got caught up in it too.

Alexey, as far as I understand you are a pro in the matter of recursion. Personally I am a fool in this field. I have a question, a request and a suggestion for you. Can you use your methods to speed up the Fourier approximation? Something tells me it is possible. If you can do it, it will be wOW!!! and usefulness will be enormous. I attach an example of approximation with Fourier extrapolation on MT5 (it just works much faster and simply better). I took this example from here, and slightly improved it for clarity by adding mouse control with Ctrl key (which changes the start position) and Shift (which changes the observation period and the number of harmonics at the same time). Can you give it a try?

This indicator is as follows: first click on the chart with the mouse (to activate the window), press Ctrl (and release it) and move the mouse to change the start position; to finish the process, press any key (except Ctrl and Shift). The same goes for the Shift key to change the period (the range of bars for the approximating function).

Files:
Fourier.mq5  16 kb
 

Forum on trading, automated trading systems and testing trading strategies

Difference calculus, examples.

Nikolai Semko, 2018.01.12 00:43


I'm just advocating that things should be called by their proper names and generally accepted terminology should be used, so that there is no confusion. In my opinion, it would have been more logical to mention recursion at the beginning of this thread, and not to mention interpolation, approximation and polynomials, as they are not shown in your example. And it would have been more correct to focus on the shift of the indicator to the left, so as not to mislead others by excessive correctness of forms, because not everyone likes to look into other people's code, I fell for it too.


Nikolai, thanks for the post and the attached indicator.

And I completely agree, first of all there is a need for unambiguous understanding of terms and names.

I will explain my position.

You can draw a line on two points, it means to find any point of this line either inside the interval between the points (interpolation) or outside the interval between the points (extrapolation).

Using three points, we can draw a single-valued curve corresponding to a square parabola expressed as a linear square equation in the Cartesian coordinate system. This means that it is also possible to find any point of this curve either within the interval between extreme points (interpolation) or outside this interval (extrapolation). The law by which these points are constructed remains polynomial. I add, also on the three points, at least, it is possible to draw a single-valued sinusoid, if we assume a sinusoidal law of development, or a circle, if we assume its presence.

Thus the interpolation by a polynomial of the second degree over three points (in our case, two of which accumulate the previous story and the third carries new information) of the fourth, turns out to be a necessary (there may be other laws) and sufficient definition of the action or process.

Unless, of course, you suggest other terms for it.

That said, I completely agree that if you need to plot a curve by the number of values exceeding the minimum required number, you need to use statistically (or otherwise) sound methods of weighting the values, including regression.
 
Aleksey Panfilov:


Nikolai, thank you for the message and the attached indicator.

And I completely agree, first of all you need an unambiguous understanding of the terms and names.

I will explain my position.

On two points it is possible to draw a line, it means to find any point of this line, or inside the interval between the points (interpolation), or outside the interval between the points (extrapolation).

Using three points, we can draw a single-valued curve corresponding to a square parabola expressed as a linear square equation in the Cartesian coordinate system. This means that it is also possible to find any point of this curve either within the interval between extreme points (interpolation) or outside this interval (extrapolation). In this case, the law by which these points are constructed remains polynomial. I add, also on the three points, at least, it is possible to draw a single-valued sinusoid, if we assume a sinusoidal law of development, or a circle, if we assume its presence.

Thus the interpolation by a polynomial of the second degree over three points (in our case, two of which accumulate the previous story and the third carries new information) of the fourth, turns out to be a necessary (there may be other laws) and sufficient definition of the action or process.

Unless, of course, you suggest other terms for it.

At the same time, I fully agree that if you need to build a curve for the number of values exceeding the minimum required number, you need to use statistically (or otherwise) justified methods of value weighting, including regression.

How to construct a polynomial curve by three points, I recently implemented in this code . I suggest you read it.

But your code doesn't calculate the polynomial by three points:

Aleksey Panfilov:

It looks like this on the graph:

The blue-red line is interpolation (finding a point within an interval) by a 4th degree polynomial with a shoulder of 72.

a1_Buffer[i]=((open[i] - Znach)    +5061600*a1_Buffer[i+1 ]-7489800   *a1_Buffer[i+2 ]+4926624*a1_Buffer[i+3 ]-1215450*a1_Buffer[i+4 ])/1282975;

The thin blue line is extrapolation (finding a point outside the interval) by the polynomial of degree 2 with leverage 78.

a2_Buffer[i]=  3160*a1_Buffer[i]   -6240   *a1_Buffer[i+1 ]    +  3081*a1_Buffer[i+2 ];

The red line is the line of construction of the polynomial of degree 4. It is redrawn and based on the opening point of the last bar.

a4_Buffer[i+92]=a1_Buffer[i];   if(i<=10) { for(z=92-1;z>=0;z--){        a4_Buffer[i+0+z]=  5*a4_Buffer[i+1+z]  -  10*a4_Buffer[i+2+z]   +   10*a4_Buffer[i+3+z]  -  5*a4_Buffer[i+4+z]  +  1*a4_Buffer[i+5+z];  }}


What you call a 2nd degree polynomial and a 4th degree polynomial are not polynomials.

Because the formula for a 2nd degree polynomial is:

Price = a+b*i+c*i²

and the formula of the polynomial of the 4th degree:

Price = a+b*i+c*i²+d*i³+f*i⁴

Where

a,b,c,d,f are the coefficients to be calculated

i is the number of the bar.

And you have an ornate way of averaging, in which (using the example of what you call a polynomial of the 2nd degree) the current point of one line i is calculated from the last three points (i,i+1 and i+2) of another line with different weight coefficients. This is called averaging (or smoothing). That is why you shifted the graph 72 bars to the left, in order to hide the lag of the graph, which occurs as a result of averaging.

In myexample, the calculation of the polynomial by three points is clearly visible.

 
Nikolai Semko:

In myexample, the calculation of the polynomial by three points is clearly and clearly visible.

Your indicator is very good.

If you have the same one for the four, then attach it to any three points of the gray line of the indicator from post 23 of this thread.

This line (gray) is calculated in the buffer:

 a5_Buffer[i+92]=a1_Buffer[i];   if(i<=1) { for(z=92-1;z>=0;z--){        a5_Buffer[i+0+z]=  3*a5_Buffer[i+1+z]  -  3*a5_Buffer[i+2+z]   +   1*a5_Buffer[i+3+z]   ;  }}
1*Y1-3*Y2+3*Y3-1*Y4=0 - difference equation of second degree parabola. For equidistant points.
 
Aleksey Panfilov:

Your indicator is very good.

If you have the same one for the four, then attach it to any three points of the gray line of the indicator from post 23 of this thread.

This line (gray) is calculated in the buffer:

1*Y1-3*Y2+3*Y3-1*Y4=0 is the difference equation of a second degree parabola. For equidistant points.

I have the code for MT4 further down there in the comments

Yes your gray line is a polynomial. And the red tail of 92 bars from 2 message that you redraw to the shifted average is also a polynomial and it just redraws. But you also call everything else a polynomial and at the same time claimed that your polynomial is not redrawn. And this is not true. That's why I'm asking you to call things by their proper names.

And by the way, in my example forming the next points in the polynomial is done through calculation of coefficients and this algorithm is faster than your "difference calculus", although I also apply the difference with the previous value:

for(i=1; i<=Bar[0]; i++) { ArcDownBuffer[i]=ArcDownBuffer[i-1]+D2; D2+=2*C2;}

your option:

for(z=92-1;z>=0;z--){        a5_Buffer[i+0+z]=  3*a5_Buffer[i+1+z]  -  3*a5_Buffer[i+2+z]   +   1*a5_Buffer[i+3+z]   ;  }
 
Nikolai Semko:

I have a code for MT4 further down in the comments

Yes your gray line is a polynomial. And the red tail of the 92 bar from the 2nd message, which you are redrawing to the shifted average, is also a polynomial and it is overdrawn. But you also call everything else a polynomial and at the same time claimed that your polynomial is not redrawn. And this is not true. That is why I ask you to call things by their proper names.


Yes, I agree with the polynomial of the second degree, only 4 points involved in the construction, or 6 points for the polynomial of the fourth degree. The whole line that is obtained and not redrawn is of course not a polynomial, it is only constructed in the cases considered using a polynomial of a certain degree.

The thin lines only visualize those polynomials with which the next point is constructed.

It seems that we have agreed on the terms. :)


And by the way, in my example formation of the next points in the polynomial is through calculation of coefficients and this algorithm is faster than your "difference calculus", although I also apply the difference with the previous value:

for(i=1; i<=Bar[0]; i++) { ArcDownBuffer[i]=ArcDownBuffer[i-1]+D2; D2+=2*C2;}

your option:

for(z=92-1;z>=0;z--){        a5_Buffer[i+0+z]=  3*a5_Buffer[i+1+z]  -  3*a5_Buffer[i+2+z]   +   1*a5_Buffer[i+3+z]   ;  }

Regarding speed, you're probably right.

I suggest we not touch the performance issues and probably negligence (mine first of all :)) ) in coding, as long as they are not critical.

 
Nikolai Semko:

Can you use your methods to speed up the Fourier approximation? Something tells me it is possible. If you can do it, it will be a WOW!!! and the practical benefit will be enormous. I attach an example of approximation with Fourier extrapolation on MT5 (it just works much faster and simply better). I took this example from here, and slightly improved it for clarity by adding mouse control with Ctrl key (which changes the start position) and Shift (which changes the observation period and the number of harmonics at the same time). Can you give it a try?

This indicator is as follows: first click on the chart with the mouse (to activate the window), press Ctrl (and release it) and move the mouse to change the start position; to finish the process, press any key (except Ctrl and Shift). The same with the Shift key to change the period (range of bars to calculate the approximating function).


Regarding Fourier, the topic is rich. If you are interested, we will touch it from time to time.

Most likely, the main questions will arise with respect to the problem statement (due to the other method). Now, as far as I understand, the indicator selects the most amplitude frequencies from the Fourier spectrum.

I had an idea to bolt theFourier indicator to an already averaged polynomial line. I have a suspicion, that its extrapolated readings will change slower.

Reason: