[SERVICE DESK] Error in getting the time of the senior TF in the timer! - page 3

 
Vitaly Gorbunov:
I think I've got it! Is the indicator launched right away together with the terminal? If so, before checking wait for communication with the server IsConnected() you have a very fast timer it has no time to synchronize!

Nice try. I was beginning to think it would work. But... No:

2018.09.21 15:16:40.519 Custom indicator test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: removed
2018.09.21 15:32:29.397 Custom indicator test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: loaded successfully
2018.09.21 15:32:29.415 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: initialized
2018.09.21 15:32:29.508 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.532 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.557 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.587 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.620 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.651 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.683 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.716 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.743 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.776 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.807 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.841 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.872 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.901 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.935 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.972 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:29.996 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: OnTimer: Связь отсутствует!
2018.09.21 15:32:30.059 test_isNewDayInTimer_iBarShirt() EURGBP.e,M5: test_isNewDayInTimer_iBarShirt().mq4: Актуальное время открытия бара М15 = 2018.09.21 13:15. Ошибка #0

I replaced the OnTimer() function in the previous code with this one:

//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   if( !IsConnected() )
        {
         Print( __FUNCTION__,": Связь отсутствует!" );
         return;
        }
//--- Проверяем, записано ли время открытия текущего бара М15
   if(!CheckCurrentM15OpenTime())                        // Если время не записано
      return;                                                // Выходим
  }
 
Vitaly Gorbunov:

I think I've got it! Is the indicator started immediately with the terminal? If so, before checking wait for a connection to the server IsConnected() you have a very fast timer it does not have time to synchronize!

Or do it like this.

But we'll have to take into account the difference between server time and local time. Write back with the results!

I'm not going to make crutches. There's an obvious bug in the language! We are waiting for developers...

 

Very interesting, it is supposed to work. But then again the timer is very fast, maybe the terminal doesn't have time to start accepting quotes. I have to think about it. How about making a normal timer for 5 seconds to test the idea?

 
Vitaly Gorbunov:

Very interesting, it is supposed to work. But then again the timer is very fast, maybe the terminal doesn't have time to start accepting quotes. I have to think about it. What if we make a normal timer for 5 seconds to test the idea?

And I don't need a "fit" to check the idea. In real life, I need the fastest possible timer.

 
Vitaly Gorbunov:

Very interesting, it is supposed to work.

You can always run the indicator in your terminal, wait 15 minutes and report back.

 

Okay, but I have a lot of other work to do. As soon as I get a break, I'll do an experiment.

And I think I even have an idea how to catch it.

 

Try it like this!

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict
#property indicator_chart_window
datetime ChkTime;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//--- Время открытия текущего часа, дня и недели
datetime _m15OpenTime=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//Сохраняем старое время  
   ChkTime=TimeCurrent();
//--- Запускаем таймер
   if(!EventSetMillisecondTimer(20))
     {
      Print(__FUNCTION__,": ОШИБКА #",GetLastError(),": таймер с частотой 20 ms не установлен!");
      return( INIT_FAILED );
     }
//--- Сбрасываем время открытия текущего бара м15
   _m15OpenTime=0;
//---
   return( INIT_SUCCEEDED );
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {

   return( rates_total );
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   if(!IsConnected())
     {
      Print(__FUNCTION__,": Связь отсутствует!");
      return;
     }
//Если есть соединение проверяем что прошла синхронизация времни     
   if(IsConnected() && ChkTime==TimeCurrent())
     {
      return;
     }
//--- Проверяем, записано ли время открытия текущего бара М15
   if(!CheckCurrentM15OpenTime()) // Если время не записано
      return;// Выходим
  }
//+------------------------------------------------------------------+
//| Проверяем, записано ли время открытия текущего бара М15             |
//+------------------------------------------------------------------+
bool CheckCurrentM15OpenTime()
  {
//--- Проверяем, записано ли время
   if(_m15OpenTime==0) // Если время не записано
     {
      //---
      ResetLastError();
      if(iBarShift(Symbol(),PERIOD_M15,TimeCurrent(),true)==-1)
        {
         Print(__FILE__+": Данные истории по последнему часу отсутствуют! Ошибка #",GetLastError());
         return( false );
        }
      //---
      if(GetLastError()==ERR_NO_ERROR)
        {
         ResetLastError();
         //--- Запоминаем время открытия бара
         _m15OpenTime=iTime(NULL,PERIOD_M15,0);
         //---
         Print(__FILE__,": Актуальное время открытия бара М15 = "+TimeToString(_m15OpenTime)+". Ошибка #",GetLastError());
         //--- Возвращаем истину
         return( true );
        }
      else
         return( false );
     }
//--- Время открытия недели ранее записано. Возвращаем истину
   return( true );
  }
//+------------------------------------------------------------------+



2018.09.21 15:02:42.910 Custom indicator test XAUUSD,H1: removed
2018.09.21 15:15:27.985 test XAUUSD,H1: initialized
2018.09.21 15:15:29.520 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:29.552 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:29.589 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:29.605 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:29.636 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:29.667 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:29.705 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:29.736 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:29.768 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:29.805 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:29.837 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:29.868 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:29.906 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:29.937 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:29.969 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:30.006 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:30.038 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:30.069 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:30.106 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:30.138 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:30.169 test XAUUSD,H1: OnTimer: Связь отсутствует!
2018.09.21 15:15:31.880 test XAUUSD,H1: test.mq4: Данные истории по последнему часу отсутствуют! Ошибка #4066
2018.09.21 15:15:31.900 test XAUUSD,H1: test.mq4: Данные истории по последнему часу отсутствуют! Ошибка #0
2018.09.21 15:15:31.940 test XAUUSD,H1: test.mq4: Данные истории по последнему часу отсутствуют! Ошибка #0
2018.09.21 15:15:31.990 test XAUUSD,H1: test.mq4: Данные истории по последнему часу отсутствуют! Ошибка #0
2018.09.21 15:15:32.010 test XAUUSD,H1: test.mq4: Данные истории по последнему часу отсутствуют! Ошибка #0
2018.09.21 15:15:32.074 test XAUUSD,H1: test.mq4: Данные истории по последнему часу отсутствуют! Ошибка #0
2018.09.21 15:15:32.100 test XAUUSD,H1: test.mq4: Данные истории по последнему часу отсутствуют! Ошибка #0
2018.09.21 15:15:32.120 test XAUUSD,H1: test.mq4: Данные истории по последнему часу отсутствуют! Ошибка #0
2018.09.21 15:15:32.174 test XAUUSD,H1: test.mq4: Данные истории по последнему часу отсутствуют! Ошибка #0
2018.09.21 15:15:32.274 test XAUUSD,H1: test.mq4: Актуальное время открытия бара М15 = 2018.09.21 15:15. Ошибка #0
 
Vitaly Gorbunov:

Try it like this!

There's the crutch again. You are trying to solve the problem by any means, even irrational ones, ignoring the cause of the problem - language errors. I don't think you should do that.

Added:

Yes, this kind of technique seems to work. As an option you can use it. Thank you! But I'd still like the functions to work properly.

 
Well I wouldn't say it's a crutch. I'm waiting for quotes to flow from the server otherwise the history won't be updated. Another option is to take local computer time and correct for time zone, which can be calculated automatically and in the end still wait for quotes update. It's a bit crooked but I don't think Metakvotov will change anything in MT4. At the end of the day it's a server connection problem that has to be handled one way or another.
 
Alexey Kozitsyn:

First a reply from @Ihor Herasko. Code for playback:

Result:

According to the log entries. The terminal was switched off at 14:25. Next, switched on at 14:30. We check the time of bar M15. We started with TF M1. The indicator (code above) showed actual open time 12:15 (terminal time, lagged behind my local time by 2 hours). The result should have been 12:30! Conclusion - the error is present. And this method proposed by @Ihor Herasko does not work.

There are two significant errors in your approach to check:

  1. In the OnInit() of the indicator it is impossible to obtain the correct data of any TF, even the current one. The OnInit() of the indicator at the start of the terminal is executed almost in an empty window (there may not even be the data that was at the time of terminal shutdown). Even the market environment is often still unavailable. The help contains a lot of warnings about what not to do in OnInit(). Therefore, there is not much to include in its code. Usually, this is a check of the values of the set parameters, binding of indicator buffers to arrays, and creation of classes, if such are used. Everything else has to be done in OnCalculate().
  2. All these checks can be done only after OnCalculate is called at least once.