Difference calculus, examples. - page 8

 
Yuriy Asaulenko:


What is the advantage of RU?

I don't claim an advantage at all.

In my opinion:

If we initially imagine an image of the simulated system (even a complex one: airplane, car) with some certainty, it is more convenient to mark up space at once (e.g. Cartesian system) and simulate the system using the whole mathematical apparatus. The approach "design from the top".

If you do not have a complete picture of the system (approached the forest, swamp, or forex:) I'm talking about myself ), then we have to rely on the steps already taken and a fairly narrow view. This is closer to the difference (recurrence) calculus. The "research from the bottom" approach. Algorithms allow even on the basis of a few steps to understand what is happening in terms ofthe "design from the top" approach.

In this analogy, a neural network, is a system with drones - probes that investigate, suggest a path and an overall picture. In the process of research it will make a lot of programmed decisions on the importance of the observed and the choice. Personally, I'm not always willing (or even forced :)) ) to rely on other people's decisions. :)))))


As much as I could answer. )) Only we are one step away from Philo_Sophia again. I do not want to go there. ))

Forum on trading, automated trading systems & strategy testing.

Difference calculus, examples.

Aleksey Panfilov, 2018.01.10 16:51


I suggest that there is no philosophy in this thread, just mathematics, programming, testing, optimization.


 
Nikolai Semko:

SMA from SMA is, in my opinion, "not in the eye, but in the eye" and by the way about Fourier. Maybe we'll get there some more.

P/S 01.02.2018 I think there is a difference too, try to repeat yourBanzai.mq4indicator on this line.

The SMA is a sawtooth by nature only very small, so when taking the second difference I assume a sawtooth line of the indicator.

 
Aleksey Panfilov:

Of course, the following differences can also be considered as new information.

However, already on the first difference it is not quite clear to me what algebraic line we draw. And with the increase of the "leverage" everything gets confused there. ))))

For example, here is another variant of using the increment of price as new information. Here the increment is read explicitly as a difference.

1*Y1-1*Y2-2*Y2+3*Y3-1*Y4 =0

1*Y1-1*Y2-3*Y2+6*Y3-4*Y4 + 1*Y5 =0

1*Y1-1*Y2-4*Y2+10*Y3-10*Y4 + 5*Y5 -1*Y6=0

1*Y1-1*Y2-5*Y2+15*Y3-20*Y4 + 15*Y5 -6*Y6 + 1*Y7=0

1*Y1-1*Y2-6*Y2+21*Y3-35*Y4 + 35*Y5 -21*Y6 + 7*Y7 -1*Y8=0


2*Y2=1*Y1-1*Y2+3*Y3-1*Y4

3*Y2=1*Y1-1*Y2+6*Y3-4*Y4 + 1*Y5

4*Y2=1*Y1-1*Y2+10*Y3-10*Y4 + 5*Y5 -1*Y6

5*Y2=1*Y1-1*Y2+15*Y3-20*Y4 + 15*Y5 -6*Y6 + 1*Y7

6*Y2=1*Y1-1*Y2+21*Y3-35*Y4 + 35*Y5 -21*Y6 + 7*Y7 -1*Y8

      a1_Buffer[i]=(open[i]-open[i+1]   +3*a1_Buffer[i+1 ]   -1*a1_Buffer[i+2 ]  )/2;
      a2_Buffer[i]=(open[i]-open[i+1]   +6*a2_Buffer[i+1 ]   -4*a2_Buffer[i+2 ]   +1*a2_Buffer[i+3 ]  )/3;
      a3_Buffer[i]=(open[i]-open[i+1]   +10*a3_Buffer[i+1 ]  -10*a3_Buffer[i+2 ]  +5*a3_Buffer[i+3 ]  -1*a3_Buffer[i+4 ])/4;
      a4_Buffer[i]=(open[i]-open[i+1]   +15*a4_Buffer[i+1 ]  -20*a4_Buffer[i+2 ]  +15*a4_Buffer[i+3 ]  -6*a4_Buffer[i+4 ]  +1*a4_Buffer[i+5 ])/5;
      a5_Buffer[i]=(open[i]-open[i+1]   +21*a5_Buffer[i+1 ]  -35*a5_Buffer[i+2 ]  +35*a5_Buffer[i+3 ]  -21*a5_Buffer[i+4 ]  +7*a5_Buffer[i+5 ]  -1*a5_Buffer[i+6 ])/6;
In the figure you can see that the line (red) built conditionally by the polynomial of the fifth degree (by the number of connected points) also began to steadily hold near the graph.


It's as if at the expense of the polynomial of the fourth degree on the first differences (price increments) we have increased (by the number of points) the degree to the fifth.

Forum on trading, automated trading systems & strategy tester

Difference calculus, examples.

Maxim Dmitrievsky, 2018.01.31 05:37

The matrix has already degenerated :) In such cases, regularization is used, degrees are reduced
Is this it, regularization?



 
Imho, the Perspectron is the best difference calculus. That's what I use.
 

For optimization we will add to the indicator described in message 51 the possibility to change the leverage of extrapolation. Extrapolation by polynomial of the third degree.

//|                                Copyright 2018, 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  clrYellow
#property indicator_style2  STYLE_SOLID
#property indicator_width2  3
//--- plot a3_
#property indicator_label3  "Fast_line_1"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrSilver
#property indicator_style3  STYLE_SOLID
#property indicator_width3  8
//--- 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  3
//--- 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  3
//--- 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  3//--- 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 leverage = 72;
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,0);
   SetIndexShift(0,20);
   SetIndexShift(3,92-leverage);
   SetIndexShift(4,38);
   SetIndexShift(5,56);
   SetIndexShift(1,74);

//    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;

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


      a2_Buffer[i+92-leverage]=a5_Buffer[i+92-leverage]; 
      
//      a3_Buffer[i+38]=a5_Buffer[i+38]; 
      
//      a4_Buffer[i+56]=a5_Buffer[i+56];
        
//      a6_Buffer[i+74]=a5_Buffer[i+74];
      
 //     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]=a2_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]=a2_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]=a2_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] ;  }}


//      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

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

//--- done

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


Разностное исчисление, примеры.
Разностное исчисление, примеры.
  • 2018.01.18
  • www.mql5.com
Предлагаю собрать в эту ветку индикаторы и эксперты на разностном исчислении, в открытом коде...
 

To see the possibilities of this indicator, let's use the well knownMoving Average.mq4 Expert Advisor.

As a single signal we use the intersection of two curves. Let's make some changes:

//+------------------------------------------------------------------+
//|                                               Moving Average.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "Moving Average sample expert advisor"

#define  MAGICMA  20131111
//--- Inputs
input double Lots          =0.1;
input double MaximumRisk   =0.02;
input double DecreaseFactor=3;
input int    leverage_1  =72;
input int    leverage_2  =56;
//input int    MovingPeriod  =12;
//input int    MovingShift   =6;
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//--- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//--- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//--- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
           {
            Print("Error in history!");
            break;
           }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
            continue;
         //---
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1)
         lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//--- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |2018_02_02_EMA_Polynom_s4_s3_p72_v.2
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma_1,ma_2,ma_1_P,ma_2_P;
   int    res;
//--- go trading only for first tiks of new bar
//   if(Volume[0]>1) return;
//--- get Moving Average 
//   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
 ma_1=iCustom(NULL,0,"2018_02_02_EMA_Polynom_s4_s3_p72_v.2",leverage_1,300,3,0);
 ma_2=iCustom(NULL,0,"2018_02_02_EMA_Polynom_s4_s3_p72_v.2",leverage_2,300,3,0);
 ma_1_P=iCustom(NULL,0,"2018_02_02_EMA_Polynom_s4_s3_p72_v.2",leverage_1,300,3,1);
 ma_2_P=iCustom(NULL,0,"2018_02_02_EMA_Polynom_s4_s3_p72_v.2",leverage_2,300,3,1);
//--- sell conditions
   if(ma_2_P < ma_1_P && ma_1 < ma_2)
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }
//--- buy conditions
   if(ma_2_P > ma_1_P && ma_1 > ma_2)
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
      return;
     }
//---
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double ma_1,ma_2,ma_1_P,ma_2_P;
//--- go trading only for first tiks of new bar
//   if(Volume[0]>1) return;
//--- get Moving Average 
//   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
 ma_1=iCustom(NULL,0,"2018_02_02_EMA_Polynom_s4_s3_p72_v.2",leverage_1,300,3,0);
 ma_2=iCustom(NULL,0,"2018_02_02_EMA_Polynom_s4_s3_p72_v.2",leverage_2,300,3,0);
 ma_1_P=iCustom(NULL,0,"2018_02_02_EMA_Polynom_s4_s3_p72_v.2",leverage_1,300,3,2);
 ma_2_P=iCustom(NULL,0,"2018_02_02_EMA_Polynom_s4_s3_p72_v.2",leverage_2,300,3,2);
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //--- check order type 
      if(OrderType()==OP_BUY)
        {
         if(ma_2_P <= ma_1_P && ma_1 < ma_2)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
               Print("OrderClose error ",GetLastError());
           }
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(ma_2_P >= ma_1_P && ma_1 > ma_2)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
               Print("OrderClose error ",GetLastError());
           }
         break;
        }
     }
//---
  }
//+------------------------------------------------------------------+
//| OnTick function                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false)
      return;
//--- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//---
  }
//+------------------------------------------------------------------+
 


Files:
2018_02_02.zip  523 kb
 

EURUSD, M15, that 2-point spread is obvious:

Sorry, but I can't put the code.

Files:
 
Renat Akhtyamov:

Sorry, but I can't put the code

In any case, the last few bars are redrawn, which means the value of the code = 0.
 
Renat Akhtyamov:

EURUSD, M15, the same 2-point spread is apparent:

Sorry, but I can't put the code.

On the"four digits"?