CopyRates does not return the last bars

 

Hello,

I am new in MQL5. I am trying to get the last three bars of EURUSD. As I am writing this post, it should return the last three bars, which were on 08.12.2023. But it returns the bars from 01.12.2023. What is wrong with my code?

void lastBars()
  {
// this array will hold the last 30 bars
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   
   int copied=CopyRates(Symbol(),_Period,0,3,rates);
   if(copied>0)
     {
      Print("Bars copied: "+copied);
      string format="open = %G, high = %G, low = %G, close = %G, volume = %d";
      string out;
      int size=fmin(copied,10);
      for(int i=0;i<ArraySize(rates);i++)
        {
         out=i+":"+TimeToString(StringFormat("Date: %s",TimeToString(rates[i].time,TIME_DATE)));
         out=out+" "+StringFormat(format,
                                  rates[i].open,
                                  rates[i].high,
                                  rates[i].low,
                                  rates[i].close,
                                  rates[i].tick_volume);
         Print(out);
        }
     }
   else Print("Failed to get history data for the symbol ",Symbol());
  }
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Ihsan Al-Hamd-Azeez:
   int copied=CopyRates(Symbol(),_Period,0,3,rates);

I am not sure your usage is correct - please see the documentation.

3rd argument is rates_mask, 4th is start, 5th is count.

There are some good examples in the documentation to follow, for example:

 matrix matrix_rates;
  if(matrix_rates.CopyRates(Symbol(), PERIOD_CURRENT, COPY_RATES_OHLCT, 1, 10))

https://www.mql5.com/en/docs/matrix/matrix_initialization/matrix_copyrates

Documentation on MQL5: Matrix and Vector Methods / Initialization / CopyRates
Documentation on MQL5: Matrix and Vector Methods / Initialization / CopyRates
  • www.mql5.com
CopyRates - Initialization - Matrix and Vector Methods - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
@R4tna C #: I am not sure your usage is correct - please see the documentation. 3rd argument is rates_mask, 4th is start, 5th is count. There are some good examples in the documentation to follow, for example:

You are using the "Matrix" method, while the OP is using the original and traditional method ...

int  CopyRates(
   string           symbol_name,       // symbol name
   ENUM_TIMEFRAMES  timeframe,         // period
   int              start_pos,         // start position
   int              count,             // data count to copy
   MqlRates         rates_array[]      // target array to copy
   );
Documentation on MQL5: Timeseries and Indicators Access / CopyRates
Documentation on MQL5: Timeseries and Indicators Access / CopyRates
  • www.mql5.com
CopyRates - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

You are using the "Matrix" method, while the OP is using the original and traditional method ...

Thanks for clarifying

 
Ihsan Al-Hamd-Azeez: I am new in MQL5. I am trying to get the last three bars of EURUSD. As I am writing this post, it should return the last three bars, which were on 08.12.2023. But it returns the bars from 01.12.2023. What is wrong with my code?

I did a bit of cleaning up on your code and these are the results on a H1 EURUSD chart ...

void OnStart() {
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int copied=CopyRates(_Symbol,_Period,0,3,rates);
   if(copied>0)
     {
      Print("Bars copied: ", copied);
      string format="%d: time = %s, open = %G, high = %G, low = %G, close = %G, volume = %d";
      int size=fmin(copied,10);
      for(int i=0;i<size;i++)
         PrintFormat( format, i, TimeToString( rates[i].time ),
            rates[i].open, rates[i].high, rates[i].low, rates[i].close, rates[i].tick_volume );
     }
   else Print("Failed to get history data for the symbol ",_Symbol);
};
2023.12.09 12:15:32.665 TestBar (EURUSD,H1)     Bars copied: 3
2023.12.09 12:15:32.665 TestBar (EURUSD,H1)     0: time = 2023.12.08 23:00, open = 1.07639, high = 1.07643, low = 1.07533, close = 1.0757, volume = 993
2023.12.09 12:15:32.665 TestBar (EURUSD,H1)     1: time = 2023.12.08 22:00, open = 1.07588, high = 1.07665, low = 1.07571, close = 1.07636, volume = 2142
2023.12.09 12:15:32.665 TestBar (EURUSD,H1)     2: time = 2023.12.08 21:00, open = 1.07573, high = 1.07618, low = 1.07567, close = 1.07588, volume = 2041
 
Fernando Carreiro #:

I did a bit of cleaning up on your code and these are the results on a H1 EURUSD chart ...

Unfortunately yes it can happen that CopyRates (or any Copy function) returns outdated data. But it's hard to reproduce. Usually it happens on the first launch after your restart MT5 which was closed for a while.

 
Alain Verleyen #:
But it's hard to reproduce.

M1 of a symbol whose chart you haven’t opened for a long time (and haven’t queried via MQL) may be a good option for this. But I'm not sure😄

For MT4 this is a good way to get an error. I think this should work for MT5 as well.

 
@Alain Verleyen #: Unfortunately yes it can happen that CopyRates (or any Copy function) returns outdated data. But it's hard to reproduce. Usually it happens on the first launch after your restart MT5 which was closed for a while.

That is possible, but is was not the case here. The OP was using a weird and incorrect conversion for the datetime, causing it to be the same for every line. That is why I cleaned up his code.

 out=i+":"+TimeToString(StringFormat("Date: %s",TimeToString(rates[i].time,TIME_DATE)));
2023.12.09 14:43:16.315 TestBarOr (EURUSD,D1)   Bars copied: 3
2023.12.09 14:43:16.315 TestBarOr (EURUSD,D1)   0:2023.12.09 00:00 open = 1.07913, high = 1.08008, low = 1.07236, close = 1.0757, volume = 101880
2023.12.09 14:43:16.315 TestBarOr (EURUSD,D1)   1:2023.12.09 00:00 open = 1.0764, high = 1.08177, low = 1.07553, close = 1.07936, volume = 95193
2023.12.09 14:43:16.315 TestBarOr (EURUSD,D1)   2:2023.12.09 00:00 open = 1.07952, high = 1.08047, low = 1.07588, close = 1.07636, volume = 75876
 
Fernando Carreiro #:

That is possible, but is was not the case here. The OP was using a weird and incorrect conversion for the datetime, causing it to be the same for every line. That is why I cleaned up his code.

Thank you for replying. But I used your modified code and I still get the same result

2023.12.09 16:24:45.061 2023.12.01 00:00:00   Bars copied: 3
2023.12.09 16:24:45.061 2023.12.01 00:00:00   0: time = 2023.11.30 23:59, open = 1.08872, high = 1.08875, low = 1.08852, close = 1.08852, volume = 56
2023.12.09 16:24:45.061 2023.12.01 00:00:00   1: time = 2023.11.30 23:58, open = 1.08872, high = 1.08879, low = 1.08872, close = 1.08873, volume = 47
2023.12.09 16:24:45.061 2023.12.01 00:00:00   2: time = 2023.11.30 23:57, open = 1.08873, high = 1.08876, low = 1.0887, close = 1.08872, volume = 58
 
Ihsan Al-Hamd-Azeez #:

Thank you for replying. But I used your modified code and I still get the same result

Of course if you are running it with the Strategy Tester !
Reason: