data not updated when getting it from iHigh()

 

Hello,


I am trying to get the high value at a certain period of the day.

For example, when it is 11 pm, I want to retrieve the high value at 6 pm

To do so, I use iBarShift() to get the shift and then apply iHigh() to this shift


The problem is that very often the iBarShift returns -1 meaning that the candlestick is not available yet (5 hours later...),

I find it a bit surprising ...

Am i doing something wrong, or is there any way to refresh the data before ?


Note that i dont want to use High because i want to keep control over the timeframe


thank you for your help

 
quentinS: The problem is that very often the iBarShift returns -1 meaning that the candlestick is not available yet (5 hours later...), I find it a bit surprising ...

Am i doing something wrong, or is there any way to refresh the data before ?

  1. Why do you find it surprising that if you ask for the future, the terminal says huh? If it's not yet time look at the previous day's value. Draw rectangle around range of bars by hours - MQL4 and MetaTrader 4 - MQL4 programming forum
  2. Make sure the TF you want has been updated.
    bool download_history(
          ENUM_TIMEFRAMES   period=PERIOD_CURRENT)  /**< Standard timeframe. */{
          return download_history(_Symbol, period);
    }
    #define SYMBOL string
    bool download_history(
          SYMBOL            symbol,                 ///< The symbol required.
          ENUM_TIMEFRAMES   period=PERIOD_CURRENT)  /**< Standard timeframe. */{
       if(period == PERIOD_CURRENT)  period = ENUM_TIMEFRAMES(_Period);
       ResetLastError();
       datetime other = iTime(symbol, period, 0);
       return _LastError == 0;
    }
    // Indicator
    if(!download_history(TF) ) return prev_calculated;
    // EA
    while(!download_history(TF) ){ Sleep(1000); RefreshRates();
    
 


Thank you for your answer


But Im not sure you understood what i meant, i meant that i want to retrieve the candlestick from 6pm when it is 11pm,

im not asking for any future candlestick, i'm looking for an existing one.

Here is my code so it will be easier:


double calcHigh(datetime dpDate=NULL)
{
   if(dpDate == NULL) dpDate = TimeLocal(); 
   MqlDateTime date;
   TimeToStruct(dpDate, date);
   date.hour = 18;
   date.min = 0;
   date.sec = 0;

   double high = 0;

   // This datetime means today at 6pm
   datetime dt = StructToTime(date);
   int shift = iBarShift(NULL, PERIOD_H1, dt, true);
   
   // if we can retrieve data
   if (shift > 0)
   {
      high = iHigh(Symbol(), PERIOD_H1, shift);
   }

   return high;
}

This function is being called at 11pm by an EA, the problem is that i often get -1 for the shift
 
quentinS: This function is being called at 11pm by an EA, the problem is that i often get -1 for the shift
  1. If the TF of the chart is not H1, make sure that the H1 chart is updated. See #1.2
  2. Don't use local time, has nothing in common with server time. TimeCurrent.
  3. Your entire StructToTime code can be simplified. See #1.1 link.
 

Thank you for your help, i got my answer!


I just didnt want to depend on the timeframe associated with the chart but there is a function (ChartSetSymbolPeriod)

that allows me to control it from the EA

 
quentinS:

Thank you for your help, i got my answer!


I just didnt want to depend on the timeframe associated with the chart but there is a function (ChartSetSymbolPeriod)

that allows me to control it from the EA


I think you missed the key element from what whroeder1 recommended. You have to use TimeCurrent() and NOT TimeLocal(). What you're trying to do is take the time from your computer - in your time-zone - and ask for a bar of a different time-zone. You have to keep everything you do with regard to this procedure on server time using TimeCurrent()
 
nicholishen:

I think you missed the key element from what whroeder1 recommended. You have to use TimeCurrent() and NOT TimeLocal(). What you're trying to do is take the time from your computer - in your time-zone - and ask for a bar of a different time-zone. You have to keep everything you do with regard to this procedure on server time using TimeCurrent()


Yes i changed TimeLocal() to TimeCurrent() in my code, its more reliable i agree with that

But it wasn't my problem since i made sure to synchronize the time of the computer i trade on with the broker server time.

I dont know exactly how it works internally, but it seems to be easier to retrieve past data when the timeframe of the data we are looking for is the one set on the chart (at least with my broker)

Thanks for pointing it out though