Difference calculus, examples.

 

I propose to collect indicators and experts on the difference calculus in this branch in the open source code.

If there will be interest, we will eventually gather or draw something useful. :)

I have rewritten the indicator in a clearer variant as an example:

//|                                Copyright 2016, Aleksey Panfilov. |
//|                                                filpan1@yandex.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2018, Aleksei Panfilov. filpan1@yandex.ru"
#property link      "filpan1@yandex.ru"
#property version   "1.2"
#property description    "2018_01_10_Polynom_s4_s2_p72"
#property strict

#include <MovingAverages.mqh>


#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots   2
//--- plot a1_
#property indicator_label1  "MACD"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrSilver
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- plot a2_
#property indicator_label2  "Signal"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot a3_
#property indicator_label3  "Fast_line_1"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrBlueViolet
#property indicator_style3  STYLE_SOLID
#property indicator_width3  6
//--- plot a4_
#property indicator_label4  "Fast_line_2"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrBlue
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1
//--- plot a5_
#property indicator_label5  "Slow_line_1"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrDarkGreen
#property indicator_style5  STYLE_SOLID
#property indicator_width5  4
//--- plot a6_
#property indicator_label6  "Slow_line_2"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrRed
#property indicator_style6  STYLE_SOLID
#property indicator_width6  1//--- input parameters

//         int   LIN_1_STEP    =4; //line_1_power
//input int      LIN_1_PLECHO  =72; //Fast_line_1_leverage
//         int   LIN_2_STEP    =2;//line_2_power
//input int      LIN_2_PLECHO  =78; //Fast_line_2_leverage
//         int   LIN_3_STEP    =4;//line_3_power
//input int      LIN_3_PLECHO  =72; //Slow_line_1_leverage
//         int   LIN_4_STEP    =2;//Slow_line_4_power
//input int      LIN_4_PLECHO  =72;//Slow_line_2_leverage

input        int TOCHKA_VHODA = 300;// start_point
//input int           base  =450;
      int   point_shift_1 = 0;
      int   point_shift_2 = 0;
//input int   Multiplikator = 10;
//input int   InpSignalSMA  = 9;  // Signal SMA Period



//--- indicator buffers

double a1_Buffer[];
double a2_Buffer[];
double a3_Buffer[];
double a4_Buffer[];
double a5_Buffer[];
double a6_Buffer[];
//double a7_Buffer[];
//double a8_Buffer[];
/**/


//===========================================================================================
   double Znach;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
 
//--- indicator buffers mapping
   SetIndexBuffer(0,a5_Buffer,INDICATOR_DATA);
   SetIndexBuffer(1,a6_Buffer,INDICATOR_DATA);
   SetIndexBuffer(2,a1_Buffer,INDICATOR_DATA);
   SetIndexBuffer(3,a2_Buffer,INDICATOR_DATA);
   SetIndexBuffer(4,a3_Buffer,INDICATOR_DATA);
   SetIndexBuffer(5,a4_Buffer,INDICATOR_DATA);
//----
//----
   SetIndexShift(2,-72);
   SetIndexShift(5,20);

//    if(TOCHKA_VHODA <= (LIN_1_PLECHO+25)*LIN_1_INTERVAL) TOCHKA_VHODA=(LIN_1_PLECHO+25)*LIN_1_INTERVAL;  
//    if(TOCHKA_VHODA <= (LIN_2_PLECHO+25)*LIN_2_INTERVAL) TOCHKA_VHODA=(LIN_2_PLECHO+25)*LIN_2_INTERVAL;  
//    if(TOCHKA_VHODA <= (LIN_3_PLECHO+25)*LIN_3_INTERVAL) TOCHKA_VHODA=(LIN_3_PLECHO+25)*LIN_3_INTERVAL;  
//    if(TOCHKA_VHODA <= (LIN_4_PLECHO+25)*LIN_4_INTERVAL) TOCHKA_VHODA=(LIN_4_PLECHO+25)*LIN_4_INTERVAL;  
//    if(TOCHKA_VHODA <= (base*2+25))                      TOCHKA_VHODA=(base*2+25);  

//------
//===========================================================================================
//===========================================================================================

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

   int i,z,limit;
//   int Bars=Bars(_Symbol,_Period);

   if(prev_calculated==0)// first calculation    
     {
      limit=rates_total-TOCHKA_VHODA;
      //--- set empty value for first limit bars
//Print("Bars=",Bars," rates_total=",rates_total," TOCHKA=",TOCHKA_VHODA," limit=",limit);
      if(limit<1)return(0);
      for(i=rates_total-1;i>=limit;i--)
      {
       a1_Buffer[i]=open[limit+1];
       a2_Buffer[i]=open[limit+1];
       a3_Buffer[i]=open[limit+1];
       a4_Buffer[i]=open[limit+1];
       a5_Buffer[i]=open[limit+1];
       a6_Buffer[i]=open[limit+1];
      }

     }
   else limit=rates_total-prev_calculated;
//--- main loop
   for(i=limit;i>=0 && !IsStopped();i--)
   {
//===========================================================================================
   Znach = 0; //iMA(NULL,0,base*2,0,MODE_SMA,PRICE_OPEN,i);
//===========================================================================================

 
      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]=  3160*a1_Buffer[i]   -6240   *a1_Buffer[i+1 ]    +  3081*a1_Buffer[i+2 ];

      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];  }}


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

//      a4_Buffer[i]=  2701*a3_Buffer[i]   -5328   *a3_Buffer[i+1 ]    +  2628 *a3_Buffer[i+2 ];



//================================================================================================================================================================================================
//================================================================================================================================================================================================

//   a5_Buffer[i] = (a2_Buffer[i+point_shift_1] - a4_Buffer[i+point_shift_2])* Multiplikator;

   } 
//----
//--- signal line counted in the 2-nd buffer

//     SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSignalSMA,a5_Buffer,a6_Buffer);

//--- done

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

What is difference calculus?

It is this:

Δf(xk) = f (xk+1) - f (xk)

 
Aleksey Panfilov:

I propose to collect indicators and experts on the difference calculus in this branch in the open source code.

I rewrote the indicator in a clearer variant as an example:

It looks like this on the chart:



The blue-red line is interpolation (finding a point within an interval) by a 4th degree polynomial with a leverage 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 shoulder 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 point of the last bar opening.

 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];  }}

 
Evgeny Belyaev:

What is difference calculus?

This kind of thing:

Δf(xk) = f (xk+1) - f (xk)


Yes.

It is directly related to Newton's binomial.

It is true for equidistant points:

1*Y1-2*Y2+1*Y3=0 is the difference equation of a straight line.

1*Y1-3*Y2+3*Y3-1*Y4=0 - the difference equation of the second degree parabola.

1*Y1-4*Y2+6*Y3-4*Y4+ 1*Y5=0 - difference equation of the parabola of the third degree.

It also overlaps with the topics:

https://www.mql5.com/ru/forum/61389/page48#comment_5633264

https://www.mql5.com/ru/forum/211220/page2#comment_5632736 .

Как измерить скорость цены
Как измерить скорость цены
  • 2017.07.20
  • www.mql5.com
Всем привет, тут такая тема Хочу использовать "ускорение" цены на тиковых интервалах, но не пойму как вообще посчитать скорость цены...
 
Aleksey Panfilov:

Yes.


That's what everybody did... writing...

Does the future depend on the past?

 
Evgeny Belyaev:

That's what everyone was doing... writing...

Does the future depend on the past?


For every action, there is a trace in the present, which of course affects the future. :))))

I propose to avoid philosophy in this thread, let us focus only on mathematics, programming, testing and optimization.

 
Aleksey Panfilov:

Yes.

It is directly related to Newton's binomial.

It is true for equidistant points:

Y1-2*Y2+Y3=0 - difference equation of a straight line.

Y1-3*Y2+3*Y3-Y4 =0 - difference equation of the second degree parabola.

Y1-4*Y2+6*Y3-4*Y4 + Y5 =0 - difference equation of the third degree parabola.

Have you tried this formula?

Y = a0 + a1X + a2X^2 + a3X^3 + a4X^4

where:

X is the price of the previous bar;

Y - price of the current bar.


And so we get the following picture:


 
Yousufkhodja Sultonov:

Have you tried this formula?

Y = a + bX + cX^2 + dX^3 + eX^4


Of course, in this form there are X and Y, and in the recurrence equation, only Y and all the coefficients (a + bX+ cX^2+ dX^3+ eX^4) are reduced they are replaced by the five values of Y itself.

 

I took a look at the code, did I get it right, this is some variant of the filter with feedback? And where did the coefficients 5061600, 4926624 and others come from?

Actually, where did the indicator come from, the Internet? ))

 
Alexey Volchanskiy:

1. Looked at the code, I understood correctly, this is some variant of the filter with feedback?

2.And where did the coefficients 5061600, 4926624 and others come from?

3. And where did the indium come from, from the Internet? ))

1. Yes. This filter is 400 years old with only written history: Descartes, Newton, Pascal, Taylor, Lagrange.

2. Coefficients are calculated. I think in the second year we were introduced to Lagrange and Taylor methods. There seem to be a lot of variants for calculating coefficients.

3. This one in particular, I drew today. :)))))

 

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


Reason: