FYI: Unexpected Behaviour of CopyRates

 

This little piece of code:

int OnInit() {
   MqlRates r[];
   int nR,nB = iBars(_Symbol,    PERIOD_M1);
   Print(__LINE__,"  nB: ",nB,"  ",err(_LastError));ResetLastError();
   nR = CopyRates(_Symbol, PERIOD_M1,D'1970.01.01',TimeCurrent(),r);
   Print(__LINE__,"  0..tC,  nR: ",nR,"  ",err(_LastError));ResetLastError();
   nR = CopyRates(_Symbol, PERIOD_M1,TimeCurrent(),D'1970.01.01',r);
   Print(__LINE__,"  tC..0,  nR: ",nR,"  ",err(_LastError));ResetLastError();
   nR = CopyRates(_Symbol, PERIOD_M1,0,0,r);
   Print(__LINE__,"   0..0,  nR: ",nR,"  ",err(_LastError));ResetLastError();
   nR = CopyRates(_Symbol, PERIOD_M1,0,iBars(_Symbol,PERIOD_M1),r);
   Print(__LINE__,"   0..B,  nR: ",nR,"  ",err(_LastError));ResetLastError();
   nR = CopyRates(_Symbol, PERIOD_M1,iBars(_Symbol,PERIOD_M1),0,r);
   Print(__LINE__,"   B..0,  nR: ",nR,"  ",err(_LastError));ResetLastError();
return(INIT_FAILED);
}

causes this:

 98  nB: 371232  Err[0] No error! The operation completed successfully
100  0..tC,  nR: 100000  Err[0] No error! The operation completed successfully
102  tC..0,  nR: 100000  Err[0] No error! The operation completed successfully
104   0..0,  nR: -1  Err[4003] Wrong parameter when calling the system function
106   0..B,  nR: 101440  Err[0] No error! The operation completed successfully
108   B..0,  nR: -1  Err[4003] Wrong parameter when calling the system function
 
Nothing unexpected, you are limited by "Max bars in chart" settings, which is not a rigid value (the real max can vary from some % around the set value).
 

"Nothing unexpected" ?

I get three different values - for "Max bars in chart"(?): 371232, 100000, 101440 - that does not look like a valid maximum.

Beside that it is not obvious that chart-limitations are valid for a function like CopyRates!

Well for the others a way to deal with the internal limit of CopyRates - it may save you some time:

   int nR,nB = iBars(_Symbol,    PERIOD_M1),
         mxB = TerminalInfoInteger(TERMINAL_MAXBARS);

   Print(__LINE__,"  nB: ",nB,"  TERMINAL_MAXBARS: ",mxB,"  ",err(_LastError));ResetLastError();
   
   nR = CopyRates(_Symbol, PERIOD_M1,0,mxB+2,r);
   Print(__LINE__,"   0, xB,  nR: ",nR,"  ",nR<=0?"":_t2s(r[0].time)," - ",nR<=0?"":_t2s(r[nR-1].time),"  ",err(_LastError));ResetLastError();
   nR = CopyRates(_Symbol, PERIOD_M1,mxB,mxB+2,r);
   Print(__LINE__,"  xB, xB,  nR: ",nR,"  ",nR<=0?"":_t2s(r[0].time)," - ",nR<=0?"":_t2s(r[nR-1].time),"  ",err(_LastError));ResetLastError();
   nR = CopyRates(_Symbol, PERIOD_M1,mxB*2,mxB+2,r);
   Print(__LINE__," 2xB, xB,  nR: ",nR,"  ",nR<=0?"":_t2s(r[0].time)," - ",nR<=0?"":_t2s(r[nR-1].time),"  ",err(_LastError));ResetLastError();
   nR = CopyRates(_Symbol, PERIOD_M1,mxB*3,mxB+2,r);
   Print(__LINE__," 3xB, xB,  nR: ",nR,"  ",nR<=0?"":_t2s(r[0].time)," - ",nR<=0?"":_t2s(r[nR-1].time),"  ",err(_LastError));ResetLastError();
   nR = CopyRates(_Symbol, PERIOD_M1,mxB*4,mxB+2,r);
   Print(__LINE__," 4xB, xB,  nR: ",nR,"  ",nR<=0?"":_t2s(r[0].time)," - ",nR<=0?"":_t2s(r[nR-1].time),"  ",err(_LastError));ResetLastError();

I get:

98  nB: 371153  TERMINAL_MAXBARS: 100000  Err[0] No error! The operation completed successfully
105   0, xB,  nR: 100002  2018.03.28 23:13 - 2018.07.04 11:10  Err[0] No error! The operation completed successfully
107  xB, xB,  nR: 100002  2017.12.19 03:00 - 2018.03.28 23:14  Err[0] No error! The operation completed successfully
109 2xB, xB,  nR: 100002  2017.09.12 13:12 - 2017.12.19 03:01  Err[0] No error! The operation completed successfully
111 3xB, xB,  nR:  71153  2017.07.04 00:01 - 2017.09.12 13:13  Err[0] No error! The operation completed successfully
113 4xB, xB,  nR: -1   -   Err[4401] Requested history not found
 
Carl Schreiber:

"Nothing unexpected" ?

I get three different values - for "Max bars in chart"(?): 371232, 100000, 101440 - that does not look like a valid maximum.

Well maybe it's unexpected for you, but it's how it works.
 
Alain Verleyen:
Well maybe it's unexpected for you, but it's how it works.

But not documented!! And I don't have your crystal-ball. :(

Where did you get it?

 
Carl Schreiber:

But not documented!! And I don't have your crystal-ball. :(

Where did you get it?

Experience.
 

It's documented that Bars returns total number of bars in the history (371232), and CopyRates works within TERMINAL_MAXBARS limit in a chart (100000). As for 101440, it's apparent that 100000 is initial limit, and then when new bars are formed the number grows and most outdated bars are not removed (they're kept in memory to prevent fragmentation and eliminate unnecessary copy operations in the core), hence the number increases. Citation from documentation:

Older bars are not removed immediately from the data cache when the new ones appear. This allows not to recalculate an indicator at each new bar, but calculate its values for new bars instead.

Platform Settings - Getting Started - MetaTrader 5
Platform Settings - Getting Started - MetaTrader 5
  • www.metatrader5.com
The trading platform provides multiple settings to help you conveniently customize it. Click " Options" in the Tools menu or press "Ctrl+O". Charts — common settings of price charts and parameters of objects management: object selection straight after creation, immediate object configuration, and docking parameters; Trade — default parameters...
Reason: