Simple calculations not right in MQL5 LOG or when writing into a CSV file

 

Hello all,


I have a script here that calculates a moving average of the last N mid prices (N = 5, mid price = bid+ask/2) at every book event, (10++ per second).

My issue is about the simple calculation. I dont know if there is some kind of delay because of the loop that populates the array i created, of if there is some kind of lag because of the code alone being poorly written.

Does anyone have a clue ?

I made some screenshots of the console log in order to compare with my csv file and see if the errors where just happening in the CSV (possible lag of writing into the excel file) or even before it, at the console level. It turned out that the error is even in the console level, before the variables (INTs) are transformed (into STRINGs) and them written into the CSV file.

in the screenshot below, its clear that that the MP (mid price) is wrong in the printf. 58765+58780 / 2 = 58775 != 58772.5

idk whats goingon


      MP = (bid+ask) / 2 ;
      printf("bid = %G  ask = %G" , bid, ask);
      
      MAarray_IND10[index_midprice_IND10] = MP;                                     // fills arrayMA , each i with MidPrice
      
      for (int n=0; n < tamanhoDoArrayMA_IND10  ; n++)                              // counter from 0 till arraySize-1, step=1, n=6, n-1=5, 0 to 5
       {
         if(n>0 && n<tamanhoDoArrayMA_IND10)
         {
          SUMMAarray_IND10+=MAarray_IND10[n];                                       // sums array from i=1 a i=(arraysize-1), 
          printf( "MAarray_IND10[%G] = %G  MP=%G", n, MAarray_IND10[n],MP );        // sum 5 first elements of array from index 1 to 5.
         }
         if(n==0)
          { 
           PrevNarrayMP = MAarray_IND10[n-n];                                       // saves my 0 index mid price for later comparison
           printf( " PrevNarrayMP = %G", PrevNarrayMP ) ;
          }           
       }
 
Eduardo Gonzatti:

Hello all,


I have a script here that calculates a moving average of the last N mid prices (N = 5, mid price = bid+ask/2) at every book event, (10++ per second).

My issue is about the simple calculation. I dont know if there is some kind of delay because of the loop that populates the array i created, of if there is some kind of lag because of the code alone being poorly written.

Does anyone have a clue ?

I made some screenshots of the console log in order to compare with my csv file and see if the errors where just happening in the CSV (possible lag of writing into the excel file) or even before it, at the console level. It turned out that the error is even in the console level, before the variables (INTs) are transformed (into STRINGs) and them written into the CSV file.

in the screenshot below, its clear that that the MP (mid price) is wrong in the printf. 58765+58780 / 2 = 58775 != 58772.5


Hi Eduardo,

There is just one attribution point in your code, outside the loop, to populate the array (below):

MAarray_IND10[index_midprice_IND10] = MP; 

Are you sure this is correct?

Best Regards,

Rogério Figurelli

 
Rogerio Figurelli:

Hi Eduardo,

There is just one attribution point in your code, outside the loop, to populate the array (below):

Are you sure this is correct?

Best Regards,

Rogério Figurelli

Hi there Figurelli, thanks for the reply!

I dont know if I understood what you meant, but yes, there is only one place in the code where I populate the array, and thats the line.

If I go up into the code, the only place where I call the array itself is when I'm defining it. In the global variables, outside any block;

int tamanhoDoArrayMA_IND10 = TamanhoMA;
double MAarray_IND10[];

After that, i mention it again when populating it with 0's for the first time, in the OnInit block,

   ArrayResize(MAarray_IND10, tamanhoDoArrayMA_IND10, tamanhoDoArrayMA_IND10 );
   ArrayInitialize(MAarray_IND10,0);// 02082016d0837h

and then, after that, I populate it again, this time with the Mid Prices (MP), from 0 to 5 (array size -1) , calculating the Average for the 1 to 5 indexes (5 elements), and then comparing this average with the index 0 of the same array, like if I was comparing the moving average of the most recent 5 elements with the previous element just before this 5 most recent ones. Average MidPrice of last 5 events minus the 6th MidPrice, the one just before the 5 ones that were used in the calculation of the MA.

 static int index_midprice_IND10;
 static double PrevNarrayMP;
      MP = (bid+ask) / 2 ;
      printf("bid = %G  ask = %G MP = %G" , bid, ask, MP);
      
      MAarray_IND10[index_midprice_IND10] = MP;                                     
      
      for (int n=0; n < tamanhoDoArrayMA_IND10  ; n++)                              
       {
         if(n>0 && n<tamanhoDoArrayMA_IND10)
         {
          SUMMAarray_IND10+=MAarray_IND10[n];                                       
          printf( "MAarray_IND10[%G] = %G  MP=%G", n, MAarray_IND10[n],MP );      
         }
         if(n==0)
          { 
           PrevNarrayMP = MAarray_IND10[n-n];          
           printf( " PrevNarrayMP = %G", PrevNarrayMP ) ;
          }           
       }

      string SumArrayMAMidPrice_IND10_st = DoubleToString(SUMMAarray_IND10 ,2);

      MA_MidPrice_IND10 = ( SUMMAarray_IND10 /(tamanhoDoArrayMA_IND10-1) );
      
      double difMPMA = MA_MidPrice_IND10 - PrevNarrayMP;
// At the last lines of the code, still inside the onBookEvent block

prev_mid_price = MP;

      index_midprice_IND10 = index_midprice_IND10+1;
      
      if(index_midprice_IND10 == tamanhoDoArrayMA_IND10)      
       {                                                      
        index_midprice_IND10=0;                                   
       }  


Can you spot any errors on the logic ? My guess is that there is some kind of delay, since all of this is inside the OnBookEvent array, and, as we know, there are as much as 10++ events per second of this kind, in some contracts here.


Best

 
Anyone? 

Thanks 


 
Eduardo Gonzatti:
Anyone? 

Thanks 


If you want help you should provide a code that compiles, not just some snippets.

in the screenshot below, its clear that that the MP (mid price) is wrong in the printf. 58765+58780 / 2 = 58775 != 58772.5

It's not clear at all. Where this 58775 value comes from ? The right answer is 58772.5 which is the value printed in your log, unless I missed something ?
 
Alain Verleyen:

If you want help you should provide a code that compiles, not just some snippets.

I am sorry, i didnt know that . That one should post a code that compiles, instead of snippets. Ill do that next time.

Alain Verleyen:

It's not clear at all. Where this 58775 value comes from ? The right answer is 58772.5 which is the value printed in your log, unless I missed something ?

You are right, I posted the wrong picture above. I accidentally saved the RIGHT SCREEN after highlighting another one where the error showed.

Ill correct it as soon as the market is open again here, with a log screenshot.

And, as I cannot correct the original post, what i wanted to show was something like this here.. This time in the CSV file that the MQL code writes:


Thanks for pointing up the error.

Nevertheless, there is something happening, as the file that should have the MID PRICE values just like the "=AVERAGE" formula of Excel, but there are some (~3%) that get lost in the way.

What I'm thinking is that maybe there is some delay in the file writing, and if I'm right , this error should NOT appear on the LOG (as i accidentally posted on the original post, no errors). As the log printing SHOULD (IMHO) be a lot faster, and is way "earlier" on the code, before any calculations or any transformations from integers or doubles to strings, as i have done in the part (end of the code) that writes into CSV files.

Ill test it and post again tomorrow morning!

@Alain Verleyen,  do you know if there is such lag ? like when transforming a double variable to a string , and that could slow the correct writing into a CSV file ?

best regards

 
Eduardo Gonzatti:

I am sorry, i didnt know that . That one should post a code that compiles, instead of snippets. Ill do that next time.

You are right, I posted the wrong picture above. I accidentally saved the RIGHT SCREEN after highlighting another one where the error showed.

Ill correct it as soon as the market is open again here, with a log screenshot.

And, as I cannot correct the original post, what i wanted to show was something like this here.. This time in the CSV file that the MQL code writes:


Thanks for pointing up the error.

Nevertheless, there is something happening, as the file that should have the MID PRICE values just like the "=AVERAGE" formula of Excel, but there are some (~3%) that get lost in the way.

What I'm thinking is that maybe there is some delay in the file writing, and if I'm right , this error should NOT appear on the LOG (as i accidentally posted on the original post, no errors). As the log printing SHOULD (IMHO) be a lot faster, and is way "earlier" on the code, before any calculations or any transformations from integers or doubles to strings, as i have done in the part (end of the code) that writes into CSV files.

Ill test it and post again tomorrow morning!

@Alain Verleyen,  do you know if there is such lag ? like when transforming a double variable to a string , and that could slow the correct writing into a CSV file ?

best regards

Well, I found out that there are no mistakes in calculation inside the terminal LOG, which means that the erros only occur when writing into the CSV file.

I was guessing that the problem was indeed when I transformed the DOUBLES into STRINGS before writing them into the CSV (i do this for simplicity when manipulating data inside VBA). I just modified the code so it wont do this anymore (will write DOUBLES into the CSV) so i could compare the accuracy of the calculations. But, inside the CSV file there are still some erros just like the one I posted above in the screenshot.

I've simplified the code a lot between the possible erros (when filling the array and when finally writing the data into a CSV file) so I can see if the errors persist.

I'll keep posting here about this and, if anyone has ever found something similar with their code, please fell free to chime in! =)

My first guess is that (I HOPE) the calculations inside the terminal are OK and they just lag when writing into CSV (be it for code complexity (not really I guess) or be it for a slow HD, maybe...)