Indicator using function CopyRates does not does not retrieves prices from Broker server Data

 

Hi,

I am currently programming an indicator multi currencies and mmulti timeframes by using mql4 language and I am not able to retrieve past data from broker server.

For example I am trying to retrieve 200 last past prices on EURUSD H4 via CopyRates function  while the  indicator is attached attached to another symbol.

and it retrieves only the 10 last bars.

Here is the code for my example :


int start()
{
   //return true every minute elapsed
   if(!isNewMinute()) return 0;

   MqlRates rates[];

   ArraySetAsSeries(rates,true);
   int copiedBar=CopyRates("EURUSD",PERIOD_H4,0,200,rates);
   Print("Bar retrieved:",copiedBar);

   return(0);
}


To retrieve past data I  have to go to the EURUSD H4 chart and scroll it backwards. Isn't there an automatic way to download from within MQL4? 

 
Dorian Baranes:

Hi,

I am currently programming an indicator multi currencies and mmulti timeframes by using mql4 language and I am not able to retrieve past data from broker server.

For example I am trying to retrieve 200 last past prices on EURUSD H4 via CopyRates function  while the  indicator is attached attached to another symbol.

and it retrieves only the 10 last bars.

Here is the code for my example :


int start()
{
   //return true every minute elapsed
   if(!isNewMinute()) return 0;

   MqlRates rates[];

   ArraySetAsSeries(rates,true);
   int copiedBar=CopyRates("EURUSD",PERIOD_H4,0,200,rates);
   Print("Bar retrieved:",copiedBar);

   return(0);
}


To retrieve past data I  have to go to the EURUSD H4 chart and scroll it backwards. Isn't there an automatic way to download from within MQL4? 

It works - the code I used for testing :

#property  indicator_chart_window
#property  indicator_buffers 0
#property  indicator_plots   0

int  OnInit()                   { return(0); }
void OnDeinit(const int reason) {            }
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[])
{ 
   MqlRates rates[];
      ArraySetAsSeries(rates,true);  
         int copiedBar=CopyRates("EURUSD",PERIOD_H4,0,200,rates);
      Comment("Bars retrieved:",copiedBar);
   return(rates_total);
}

the result :

The only part I left out is the "if(!isNewMinute()) return 0;"

 
Dorian Baranes: To retrieve past data I  have to go to the EURUSD H4 chart and scroll it backwards. Isn't there an automatic way to download from within MQL4?
On MT4: Unless the current chart is the specific pair/TF referenced, you must handle 4066/4073 errors.
          Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
 
Mladen Rakic:

It works - the code I used for testing :

the result :

The only part I left out is the "if(!isNewMinute()) return 0;"


Hi Madlen,

Thanks to parcipate. Even with your code I have the same issue.

I have changed the currency pair to EURNZD in your code and executed in a EURUSD H1 chart:

#property  indicator_chart_window
#property  indicator_buffers 0
#property  indicator_plots   0

int  OnInit()                   { return(0); }
void OnDeinit(const int reason) {            }
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[])
{ 
   MqlRates rates[];
      ArraySetAsSeries(rates,true);  
         int copiedBar=CopyRates("EURNZD",PERIOD_H4,0,200,rates);
      Comment("Bars retrieved:",copiedBar);
   return(rates_total);
}

Below is the result. As you can see 13 bars have been  retrieved and this amount does not increase which means prices from server are not loaded at all:

result

 

Dorian Baranes: Thanks to parcipate. Even with your code I have the same issue. I have changed the currency pair to EURNZD in your code and executed in a EURUSD H1 chart:

Below is the result. As you can see 13 bars have been  retrieved and this amount does not increase which means prices from server are not loaded at all:

As @whroeder1 has pointed out, you have to handle 4066/4073 errors as there are delays when retrieving data from other symbols. All the code presented here so far is incomplete and does not do the necessary checking.

The following thread discusses these issues, but please be warned that the thread did get a little bit verbose and off topic at times, so read it carefully and completely from start to finish to get all the necessary information:

 
Dorian Baranes:

Hi Madlen,

Thanks to parcipate. Even with your code I have the same issue.

I have changed the currency pair to EURNZD in your code and executed in a EURUSD H1 chart:

Below is the result. As you can see 13 bars have been  retrieved and this amount does not increase which means prices from server are not loaded at all:


That will happen if the target time frame was not downloaded for a long time. After a few ticks you shall notice that it will get all the data eventually (and I must say that mt4 handles that better than mt5)

Just check the last error and do the appropriate code handling in case when you get an error

 
Fernando Carreiro:

As @whroeder1 has pointed out, you have to handle 4066/4073 errors as there are delays when retrieving data from other symbols. All the code presented here so far is incomplete and does not do the necessary checking.

The following thread discusses these issues, but please be warned that the thread did get a little bit verbose and off topic at times, so read it carefully and completely from start to finish to get all the necessary information:


Hi,

Thank you this thread help me a lot even though it was a long dicussion. So I have finally used the function ArrayCopyRates to load historical prices from server. Also since it is for an indicator and based on what you said :

However, in an Indicator, this does not seem to be the case. The array is not a virtual copy but instead a static copy set in time at the moment that the "ArrayCopyRates()" was called. The data does not update when the Symbol is different to the chart symbol. When it is the same symbol as the chart, then the array data is "live" and updates as expected, but when it is of another symbol, it is a static copy.

I call ArrayCopyRates every time on start function in order to refresh price data since it is not a virtual copy.

Regards,

Dorian

 
Dorian Baranes: Thank you this thread help me a lot even though it was a long dicussion. So I have finally used the function ArrayCopyRates to load historical prices from server. Also since it is for an indicator and based on what you said :

However, in an Indicator, this does not seem to be the case. The array is not a virtual copy but instead a static copy set in time at the moment that the "ArrayCopyRates()" was called. The data does not update when the Symbol is different to the chart symbol. When it is the same symbol as the chart, then the array data is "live" and updates as expected, but when it is of another symbol, it is a static copy.

I call ArrayCopyRates every time on start function in order to refresh price data since it is not a virtual copy.

Please note that you don't have to use "ArrayCopyRates" and can still continue to use "CopyRates" instead (or any other symbol data retrieval function). You just have to apply the same principles of checking the return value as well as any 4066/4073 errors.
Reason: