Incorrect price information from iHigh()

 

Hi everyone,

I am having a problem with my EA. Specifically when I use the following function:


//+------------------------------------------------------------------+
//| DailyPivot calculation function                                  |
//+------------------------------------------------------------------+
double DailyPivot(string CurrencyPair)
{
   double dHigh, dLow, dClose;
 
   dHigh = iHigh(CurrencyPair,PERIOD_D1,1);
   dLow = iLow(CurrencyPair,PERIOD_D1,1);
   dClose = iClose(CurrencyPair,PERIOD_D1,1);
   Print("dHigh: ",dHigh);
   Print("dLow: ",dLow);
   Print("dClose: ",dClose);
   return (dHigh + dLow + dClose)/3;
}

Most of the time I get the correct information. However, on occasion I get false information. Today is the 24th August 2020. When I called this function a few minutes ago, I first got the daily candle information for the 19th August 2020. When I called it again 3 seconds later, I got the correct candle information (for the 21st August 2020). Has anyone encountered this problem before?

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
Marc Arlt:

Hi everyone,

I am having a problem with my EA. Specifically when I use the following function:


Most of the time I get the correct information. However, on occasion I get false information. Today is the 24th August 2020. When I called this function a few minutes ago, I first got the daily candle information for the 19th August 2020. When I called it again 3 seconds later, I got the correct candle information (for the 21st August 2020). Has anyone encountered this problem before?

Please read this MQL5 Reference https://www.mql5.com/en/docs/series/copyhigh

When requesting data from an Expert Advisor or script, downloading from the server will be initiated, if  the terminal does not have these data locally, or building of a required timeseries will start, if data can be built from the local history but they are not ready yet. The function will return the amount of data that will be ready by the moment of timeout expiration, but history downloading will continue, and at the next similar request the function will return more data.

Documentation on MQL5: Timeseries and Indicators Access / CopyHigh
Documentation on MQL5: Timeseries and Indicators Access / CopyHigh
  • www.mql5.com
The function gets into high_array the history data of highest bar prices for the selected symbol-period pair in the specified quantity. It should be noted that elements ordering is from present to past, i.e., starting position of 0 means the current bar. If you know the amount of data you need to copy, it should better be done to a statically...
 

You have to analyze the return value for all of these calls.

Return Value

The High price of the bar (indicated by the 'shift' parameter) on the corresponding chart or 0 in case of an error

So if some return 0 then of course you are going to get a different output.

//+------------------------------------------------------------------+
//| DailyPivot calculation function                                  |
//+------------------------------------------------------------------+
double DailyPivot(string CurrencyPair)
{
   double dHigh, dLow, dClose;
 
   dHigh = iHigh(CurrencyPair,PERIOD_D1,1);
   if(dHigh == 0)
    {
     return(-1);
    }
   dLow = iLow(CurrencyPair,PERIOD_D1,1);
   if(dLow == 0)
    {
     return(-1);
    }
   dClose = iClose(CurrencyPair,PERIOD_D1,1);
   if(dClose == 0)
    {
     return(-1);
    }
   Print("dHigh: ",dHigh);
   Print("dLow: ",dLow);
   Print("dClose: ",dClose);
   return (dHigh + dLow + dClose)/3;
}

Then when you call the function you check it's outcome also.

double pivot = DailyPivot("EURUSD");

if(pivot == -1)
 {
  Print("Pivot error !");
 }

if(pivot != -1)
 {
  Print("Pivot "+(string)pivot);
 }

Always check because it's possible that the data you are requesting is not available and this can (and will) result in undesirable results if left unchecked.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
Roberto Jacobs:

Please read this MQL5 Reference https://www.mql5.com/en/docs/series/copyhigh

When requesting data from an Expert Advisor or script, downloading from the server will be initiated, if  the terminal does not have these data locally, or building of a required timeseries will start, if data can be built from the local history but they are not ready yet. The function will return the amount of data that will be ready by the moment of timeout expiration, but history downloading will continue, and at the next similar request the function will return more data.


Thanks for your reply Roberto. That makes sense except that the data I had was already downloaded locally in my MT4 history centre. Is this what CopyHigh does? Or do I need to call CopyHigh regardless?

 
Marco vd Heijden:

You have to analyze the return value for all of these calls.

So if some return 0 then of course you are going to get a different output.

Then when you call the function you check it's outcome also.

Always check because it's possible that the data you are requesting is not available and this can (and will) result in undesirable results if left unchecked.

Hi Marco, thanks for your reply. Yeah that is a simple filter I could use for zero values, however, in some instances (like the example I mentioned) when it does return values, but they are just for the wrong day!
 

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4 2019.05.20

On MT5: Unless the chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
          Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum 2019.05.31
          Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
          Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum
          SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum 2019.09.03
          OnCalculate during weekend MT5 - General - MQL5 programming forum

 
Marc Arlt:

Hi everyone,

I am having a problem with my EA. Specifically when I use the following function:


Most of the time I get the correct information. However, on occasion I get false information. Today is the 24th August 2020. When I called this function a few minutes ago, I first got the daily candle information for the 19th August 2020. When I called it again 3 seconds later, I got the correct candle information (for the 21st August 2020). Has anyone encountered this problem before?

do you check digit count when you get the value ?
because in getting these values sometimes its possible to have overflow.
 
Marc Arlt:
Hi Marco, thanks for your reply. Yeah that is a simple filter I could use for zero values, however, in some instances (like the example I mentioned) when it does return values, but they are just for the wrong day!
hmm, well you are returning yesterdays iHight here, is that right ?
because if you want current day's, you need to input 0 in change of 1.
 

Hi everyone

I am new here and I got confused, can any  one enlight me please, why the questions in this forum look more like coding instead of trading and is it normal that I don't understand anything?

 
HAMED DANESH:
hmm, well you are returning yesterdays iHight here, is that right ?
because if you want current day's, you need to input 0 in change of 1.
Nope. Asking for yesterday's values and returning values from 3 days ago
 
Marc Arlt: Nope. Asking for yesterday's values and returning values from 3 days ago

To be expected. #5

Reason: