Download MetaTrader 5

Problem with CopyHigh/Low

To add comments, please log in or register
svengralla
116
svengralla  

Hi,


I have problems with the code below.

When MT5 is freshly started and the profile is loaded, CopyHigh/Low returns nonsense values.

Only after reinitialization by switching timeframes, the correct values are loaded.

Whats wrong here?


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2014.06.06 20:24:31.881    test (EURUSD,M15)    yesterday high: 1.36695 yesterday low: 1.35027
2014.06.06 20:24:26.183    test (EURUSD,M30)    yesterday high: 1.994478425714124e-294 yesterday low: 1.026107400517258e-314

void OnInit()
  {  
  double yesterday_high[1], yesterday_low[1];

  CopyHigh(_Symbol,PERIOD_D1,1,1,yesterday_high);
  CopyLow(_Symbol,PERIOD_D1,1,1,yesterday_low);

  Print("yesterday high: ",yesterday_high[0]," yesterday low: ", yesterday_low[0]);
  }
Alain Verleyen
Moderator
32809
Alain Verleyen  
svengralla:

Hi,


I have problems with the code below.

When MT5 is freshly started and the profile is loaded, CopyHigh/Low returns nonsense values.

Only after reinitialization by switching timeframes, the correct values are loaded.

Whats wrong here?


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2014.06.06 20:24:31.881    test (EURUSD,M15)    yesterday high: 1.36695 yesterday low: 1.35027
2014.06.06 20:24:26.183    test (EURUSD,M30)    yesterday high: 1.994478425714124e-294 yesterday low: 1.026107400517258e-314

Don't use CopyHigh/Low in OnInit().

Check the return value when you are using function(s).

svengralla
116
svengralla  
angevoyageur:

Don't use CopyHigh/Low in OnInit().

Check the return value when you are using function(s).

Same problem when writing the code to OnCalculate.

Only after timeframe switch the correct value is loaded.

Have to check out this behaviour further when markets are open.

Are there alternatives to CopyHigh/Low in order to access yesterdays Hi/Lo?


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[])
                
  {  
  double yesterday_high[1], yesterday_low[1];

  Print("CopyHigh: ",CopyHigh(_Symbol,PERIOD_D1,1,1,yesterday_high));
  Print("CopyLow: ",CopyLow(_Symbol,PERIOD_D1,1,1,yesterday_low));

  Print("yesterday high: ",yesterday_high[0]," yesterday low: ", yesterday_low[0]);
               
  return(0);
  } 

2014.06.07 09:48:38.810    test (EURUSD,M30)    yesterday high: 1.36695 yesterday low: 1.35027
2014.06.07 09:48:38.810    test (EURUSD,M30)    CopyLow: 1
2014.06.07 09:48:38.810    test (EURUSD,M30)    CopyHigh: 1
2014.06.07 09:48:25.484    test (EURUSD,M15)    yesterday high: 2.629640530693013e-278 yesterday low: 1.0
2014.06.07 09:48:25.484    test (EURUSD,M15)    CopyLow: -1
2014.06.07 09:48:25.483    test (EURUSD,M15)    CopyHigh: -1


Rodrigo Malacarne
Moderator
10253
Rodrigo Malacarne  
svengralla:

Same problem when writing the code to OnCalculate.

Are there alternatives to CopyHigh/Low in order to access yesterdays Hi/Lo?

Hello svengralla,

try first to copy the data, then you can print the data you're getting.

By the way, if you want to get yesterday's data, you have to copy 2 days of data (index 0 means "today" and index 1 means "yesterday").

Something like:

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[])
                
  {  
  double yesterday_high[2], yesterday_low[2];
  
  if(CopyHigh(_Symbol,PERIOD_D1,0,2,yesterday_high)!=2) return;
  if(CopyLow(_Symbol,PERIOD_D1,0,2,yesterday_low)!=2) return;

  Print("yesterday high: ",yesterday_high[1]," yesterday low: ", yesterday_low[1]);
               
  return(rates_total);
  } 
svengralla
116
svengralla  
Malacarne:

Hello svengralla,

try first to copy the data, then you can print the data you're getting.

By the way, if you want to get yesterday's data, you have to copy 2 days of data (index 0 means "today" and index 1 means "yesterday").

Something like:

Hi Malacarne,


thanks for your comment.

Still, immediately after the launch of MT5, also with your code CopyHigh/Low will fail.

I guess, this is a fundamental problem related to initialization.


However, I found a convenient workaround and will leave it with this.

I will load the profile twice. On the first initialization CopyHigh/Low fails, on the second initialization its ok.

That way, I don't have to manually switch timeframes back  and forth on all charts in order to initialize correctly.


P.S.

"if you want to get yesterday's data, you have to copy 2 days of data (index 0 means "today" and index 1 means "yesterday")."


Are you sure? CopyHigh/Low allows for free access of the starting point.

You can choose any single data point without having to load the whole series before that point.

Rodrigo Malacarne
Moderator
10253
Rodrigo Malacarne  
svengralla:

Hi Malacarne,


thanks for your comment.

Still, immediately after the launch of MT5, also with your code CopyHigh/Low will fail.

I guess, this is a fundamental problem related to initialization.


Are you sure? CopyHigh/Low allows for free access of the starting point.

You can choose any single data point without having to load the whole series before that point.

That's correct. As we are talking about a small amount of data, to copy 1 or 2 days of data will have no impact in performance...

Regarding your problem, I really think there is no "fundamental problem related to initialization"... so, we can't help you further if we don't have more information about how you initialize your code or what error messages you're getting...

Alain Verleyen
Moderator
32809
Alain Verleyen  
Malacarne:

Hello svengralla,

try first to copy the data, then you can print the data you're getting.

By the way, if you want to get yesterday's data, you have to copy 2 days of data (index 0 means "today" and index 1 means "yesterday").

Something like:

Your code is not correct. With the code you posted this line is wrong :

  Print("today's highyesterday high: ",yesterday_high[1]," today's lowyesterday low: ", yesterday_low[1]);

Index 1 of yesterday_high[] is today data, index 0 is yesterday data.

Alain Verleyen
Moderator
32809
Alain Verleyen  
svengralla:

Same problem when writing the code to OnCalculate.

Only after timeframe switch the correct value is loaded.

Have to check out this behaviour further when markets are open.

Are there alternatives to CopyHigh/Low in order to access yesterdays Hi/Lo?


2014.06.07 09:48:38.810    test (EURUSD,M30)    yesterday high: 1.36695 yesterday low: 1.35027
2014.06.07 09:48:38.810    test (EURUSD,M30)    CopyLow: 1
2014.06.07 09:48:38.810    test (EURUSD,M30)    CopyHigh: 1
2014.06.07 09:48:25.484    test (EURUSD,M15)    yesterday high: 2.629640530693013e-278 yesterday low: 1.0
2014.06.07 09:48:25.484    test (EURUSD,M15)    CopyLow: -1
2014.06.07 09:48:25.483    test (EURUSD,M15)    CopyHigh: -1

There is nothing abnormal with that. You are trying to get data which are not directly available (D1 data on a M15 or M30 chart), so the CopyXXX can fail, even in OnCalculate.

Simply return and wait next tick as in Malacarne code.


svengralla
116
svengralla  
angevoyageur:

There is nothing abnormal with that. You are trying to get data which are not directly available (D1 data on a M15 or M30 chart), so the CopyXXX can fail, even in OnCalculate.

Simply return and wait next tick as in Malacarne code.


Ok, thanks. I'll check it out when markets are open again.

Rodrigo Malacarne
Moderator
10253
Rodrigo Malacarne  
angevoyageur:

Your code is not correct. With the code you posted this line is wrong :

Index 1 of yesterday_high[] is today data, index 0 is yesterday data.

I forgot to mention the function ArraySetAsSeries . Of course I was supposing it was set to true.
To add comments, please log in or register