Save data in array

 

Hello everyone, i have a little problem please


I want to copy the difference between the last 2 prices in an array,
when I display the array , it does not save each price,
it saves only the last price in all array cells.

as you can see in the attached image

void OnTimer()
  {
   MqlRates bufferPrix[2];
   MqlTick last_tick[2];
    double tableau_dif[10];
    int Size=ArraySize(tableau_dif);
    int nbreBougies = CopyRates(_Symbol,_Period,0,2,bufferPrix);
    double dif = bufferPrix[0].close - bufferPrix[1].close; 
        
     for(int i=1;i<Size;i++)  {
        tableau_dif[i] = dif;
     }
 //--- AFFICHAGE DES ELEMENTS DU TABLEAU 
   string 
   com=StringFormat(" au prix passé de : %s\r\n",DoubleToString(bufferPrix[1].close));
   com=com+StringFormat(" La difference est de     : %s\r\n",DoubleToString(dif));
   
   com=com+StringFormat(" Element du tableau 1          : %s\r\n",DoubleToString(tableau_dif[1]));
   com=com+StringFormat(" Element du tableau 2         : %s\r\n",DoubleToString(tableau_dif[2]));
   com=com+StringFormat(" Element du tableau 3         : %s\r\n",DoubleToString(tableau_dif[3]));
   
   Comment(com);
  }

i want each cell to record the price of each second like this

Elément du tableau 1  -0.22200000
Elément du tableau 2  -0.21200000
Elément du tableau 3  -0.20300000

how can i proceed?

Thank you for your time and for your help
Files:
Prix.PNG  3 kb
 

You're storing the same variable in all elements (and starting at 1 instead of 0, unless that's intended). "dif" needs to be calculated inside the loop: you would need to do the copyRates (with i,2 instead of 0,2 I think) and recalculate dif each time

And better yet would be copying 11 rates at the beginning and doing buffer[i]-buffer[i+1]

 
Manuel Alejandro Cercos Perez #:

You're storing the same variable in all elements (and starting at 1 instead of 0, unless that's intended). "dif" needs to be calculated inside the loop: you would need to do the copyRates (with i,2 instead of 0,2 I think) and recalculate dif each time

And better yet would be copying 11 rates at the beginning and doing buffer[i]-buffer[i+1]

ok thank you for your reaction sir

I change as you proposed now this is the result , i dont know if i do the mistake some where

void OnTimer()
  {
   MqlRates bufferPrix[11];
    double tableau_dif[10];
    int Size=ArraySize(tableau_dif); 
           
     for(int i=1;i<Size;i++)  {
     double dif = bufferPrix[i].close - bufferPrix[i-1].close;
      if(CopyRates(_Symbol,_Period,i,2,bufferPrix)==2) 
        tableau_dif[i] = dif;
        
        Print("Cel N°"+i+"  "+tableau_dif[i]);
       }
     
 //--- AFFICHAGE DES ELEMENTS DU TABLEAU 
  // string 
  // com=StringFormat(" Le prix actuel est de : %s\r\n",DoubleToString(bufferPrix[1].close));
  //com=com+StringFormat(" La difference est de     : %s\r\n",DoubleToString(dif));
   
  // com=com+StringFormat(" Element du tableau 1          : %s\r\n",DoubleToString(tableau_dif[9]));
 //  com=com+StringFormat(" Element du tableau 2         : %s\r\n",DoubleToString(tableau_dif[2]));
 //  com=com+StringFormat(" Element du tableau 3         : %s\r\n",DoubleToString(tableau_dif[3]));
   
   //Comment(com);
  }

result

2022.08.12 22:39:18.847 2022.02.05 00:00:01   Cel N°6  7.730307101146981e+191
2022.08.12 22:39:18.847 2022.02.05 00:00:01   Cel N°7  -8.443605441810686e-148
2022.08.12 22:39:18.847 2022.02.05 00:00:01   Cel N°8  8.443605441810686e-148
2022.08.12 22:39:18.847 2022.02.05 00:00:01   Cel N°9  1.7585357597144683e+59
2022.08.12 22:39:19.348 2022.02.05 00:00:02   Cel N°1  -0.6890000000003056
2022.08.12 22:39:19.348 2022.02.05 00:00:02   Cel N°2  -5077.986
2022.08.12 22:39:19.348 2022.02.05 00:00:02   Cel N°3  -3.815736827118017e-236
2022.08.12 22:39:19.348 2022.02.05 00:00:02   Cel N°4  0.0
2022.08.12 22:39:19.348 2022.02.05 00:00:02   Cel N°5  -7.730307101146981e+191
2022.08.12 22:39:19.348 2022.02.05 00:00:02   Cel N°6  7.730307101146981e+191
2022.08.12 22:39:19.348 2022.02.05 00:00:02   Cel N°7  -8.443605441810686e-148
2022.08.12 22:39:19.348 2022.02.05 00:00:02   Cel N°8  8.443605441810686e-148
2022.08.12 22:39:19.348 2022.02.05 00:00:02   Cel N°9  1.7585357597144683e+59
 

A few key things left yet...

int Size = 10;
double tableau_dif[10];
   
MqlRates bufferPrix[];
if (CopyRates(_Symbol,_Period,0,Size+1, bufferPrix))
{
   for(int i=0; i<Size; i++)
   {
      tableau_dif[i] = bufferPrix[i+1].close - bufferPrix[i].close;
      Print("Cel N°",i,"  ",tableau_dif[i]);
   }
}

In your case you would be working with an almost empty array (it's size 11, but it only copies the first 2 each time), the thing is copying before and then using the values (the order is also important, first copy then calculate the dif).

Also consider that the results may be inverted as of what you would expect: when you copy rates into an array (which is not set as series by default) the candle you expect to be "0" (the newest) would be "size-1", here 10 (the rates array is size 11): if the order of the results is inverted you can make the loop go backwards, or if the dif sign is the opposite of what it should be then flip the order of the substraction

 
Manuel Alejandro Cercos Perez #:

A few key things left yet...

In your case you would be working with an almost empty array (it's size 11, but it only copies the first 2 each time), the thing is copying before and then using the values (the order is also important, first copy then calculate the dif).

Also consider that the results may be inverted as of what you would expect: when you copy rates into an array (which is not set as series by default) the candle you expect to be "0" (the newest) would be "size-1", here 10 (the rates array is size 11): if the order of the results is inverted you can make the loop go backwards, or if the dif sign is the opposite of what it should be then flip the order of the substraction

Thank you sir you were really important for my problem it's working perfectly 

now

I would like the table to record the prices in real time, but it just saves the first 10 past prices and repeats the same old prices as the program progresses

how can i get them in reel time? or if the table can save the current prices every time the prices move one tick

2022.08.13 11:00:03.910 2022.05.01 00:00:04   Cel N°0  7961.584
2022.08.13 11:00:03.910 2022.05.01 00:00:04   Cel N°1  7957.645
2022.08.13 11:00:03.910 2022.05.01 00:00:04   Cel N°2  7953.628
2022.08.13 11:00:03.910 2022.05.01 00:00:04   Cel N°3  7948.657
2022.08.13 11:00:03.910 2022.05.01 00:00:04   Cel N°4  7944.429
2022.08.13 11:00:03.910 2022.05.01 00:00:04   Cel N°5  7940.511
2022.08.13 11:00:03.910 2022.05.01 00:00:04   Cel N°6  7936.371
2022.08.13 11:00:03.910 2022.05.01 00:00:04   Cel N°7  7932.219
2022.08.13 11:00:03.910 2022.05.01 00:00:04   Cel N°8  7968.251
2022.08.13 11:00:03.910 2022.05.01 00:00:04   Cel N°9  7963.787
2022.08.13 11:00:04.490 2022.05.01 00:00:05   Cel N°0  7961.584
2022.08.13 11:00:04.490 2022.05.01 00:00:05   Cel N°1  7957.645
2022.08.13 11:00:04.490 2022.05.01 00:00:05   Cel N°2  7953.628
2022.08.13 11:00:04.490 2022.05.01 00:00:05   Cel N°3  7948.657
2022.08.13 11:00:04.490 2022.05.01 00:00:05   Cel N°4  7944.429
2022.08.13 11:00:04.490 2022.05.01 00:00:05   Cel N°5  7940.511
2022.08.13 11:00:04.490 2022.05.01 00:00:05   Cel N°6  7936.371
2022.08.13 11:00:04.490 2022.05.01 00:00:05   Cel N°7  7932.219
2022.08.13 11:00:04.490 2022.05.01 00:00:05   Cel N°8  7968.251
2022.08.13 11:00:04.490 2022.05.01 00:00:05   Cel N°9  7963.787
2022.08.13 11:00:05.071 2022.05.01 00:00:06   Cel N°0  7961.584
2022.08.13 11:00:05.071 2022.05.01 00:00:06   Cel N°1  7957.645

Thanks you sir he same repeting prices in the table 

 
Noxtube #:

Thank you sir you were really important for my problem it's working perfectly 

now

I would like the table to record the prices in real time, but it just saves the first 10 past prices and repeats the same old prices as the program progresses

how can i get them in reel time? or if the table can save the current prices every time the prices move one tick

Thanks you sir he same repeting prices in the table


Yes, change thisl line


CopyRates(_Symbol,_Period,0,Size+1, bufferPrix)

To this



CopyRates(_Symbol,_Period,0,Size, bufferPrix)



EDIT:

Sorry, I just saw how you are using the data. My suggestion is wrong.


int Size = 10;
double tableau_dif[10];
   
MqlRates bufferPrix[];
if (CopyRates(_Symbol,_Period,0,Size+1, bufferPrix))
{
   for(int i=0; i<Size; i++)
   {
      tableau_dif[i] = bufferPrix[i+1].close - bufferPrix[i].close;
      Print("Cel N°",i,"  ",tableau_dif[i]);
   }
}

This is the proper version already.

The most current price should the last array value, and it should keep updating. 

You know markets are closed today, and these values will not change, right?

in case this code in inside an indicator, when you query CopyRates with index = 0, you will always get the most recent available data. 

you would need to change the code, but it would be necessary to see all of the code to give advice.

 
Noxtube #:

Thank you sir you were really important for my problem it's working perfectly 

now

I would like the table to record the prices in real time, but it just saves the first 10 past prices and repeats the same old prices as the program progresses

how can i get them in reel time? or if the table can save the current prices every time the prices move one tick

Thanks you sir he same repeting prices in the table 

If you're only dealing with the current symbol you can use OnTick instead, that one is called every time the candles change (but you would need to initialize the table in OnInit, now that the markets are closed it wouldn't get any tick). If you want to preserve the calculated values, then make the table a global variable (outside of the function scope)

 

ok thank you for your interventions thank you very much

i'm waiting for the market to be open to try this solution , i'll comme back to you soon thanks

 
Manuel Alejandro Cercos Perez #:

If you're only dealing with the current symbol you can use OnTick instead, that one is called every time the candles change (but you would need to initialize the table in OnInit, now that the markets are closed it wouldn't get any tick). If you want to preserve the calculated values, then make the table a global variable (outside of the function scope)

hello the market has resumed, but I still don't have a solution

here is what you suggested but it gives me an error

int OnInit()
  {
 int Size = 10;
double tableau_dif[10];
//---
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
MqlRates bufferPrix[];
if (CopyRates(_Symbol,_Period,0,Size+1, bufferPrix))
{
   for(int i=0; i<Size; i++)
   {
      tableau_dif[i] = bufferPrix[i+1].close - bufferPrix[i].close;
      Print("Cel N°",i,"  ",tableau_dif[i]);
   }
}

Please help me please, if only I can save the current prices that pass at each tick in my table, it's over, it solves my problem.

thank you in advance

 
Mr  Manuel Alejandro Cercos Perez i need your last help please
Reason: