Problem reading previous candle after re-connection

 

Hi,

I'm facing a problem when my MT5 looses its connection and reconnects. Here is the code (simplified):

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void PrintPreviousCandle()
  {
   if(!IsNewBar())
     {
      return;
     }

   const int iIndex=StartIndex();

   MqlDateTime mdtBar;
   datetime dtBar=iTime(_Symbol,_Period,iIndex);
   TimeToStruct(dtBar,mdtBar);
   LogFormat4("Previous candle: %02i:%02i:%02i",mdtBar.hour,mdtBar.min,mdtBar.sec);

   Print("Open: ",iOpen(_Symbol,_Period,iIndex));

   Print("High: ",iHigh(_Symbol,_Period,iIndex));

   Print("Low: ",iLow(_Symbol,_Period,iIndex);

   Print("Close: ",iClose(_Symbol,_Period,iIndex));

   Print("Volume: ",iVolume(_Symbol,_Period,iIndex));

   Print("Volume ticks: ",iTickVolume(_Symbol,_Period,iIndex));
}

Here is the log:

10:35:05.783    New bar detected
10:35:05.783    Previous candle: 10:30:00
10:35:05.783    Open: 33.48000000
10:35:05.783    High: 33.52000000
10:35:05.783    Low: 33.32000000
10:35:05.783    Close: 33.49000000
10:35:05.783    Volume: 552
10:35:05.783    Volume ticks: 552
10:36:05.392    Network '9999': connection to YYYY lost
10:41:06.104    Network '9999': authorized on YYYY through Server (ping: 18.03 ms, build 2470)
10:41:06.104    Network '9999': previous successful authorization performed from 999.99.999.99 on 2020.06.19 10:35:03
10:41:06.496    Network '9999': terminal synchronized with server name: 0 positions, 0 orders, 9999 symbols, 0 spreads
10:41:06.496    Network '9999': trading has been enabled - netting mode
10:45:00.740    New bar detected
10:45:00.740    Previous candle: 10:35:00
10:45:00.740    Open: 33.49000000
10:45:00.741    High: 33.49000000
10:45:00.741    Low: 33.32000000
10:45:00.741    Close: 33.38000000
10:45:00.741    Volume: 428
10:45:00.741    Volume ticks: 428

After re-connection I should read the previous candle at 10:40:00 and not at 10:35:00.


Thank you.

 
Felipe Augusto Torres Maggico: After re-connection I should read the previous candle at 10:40:00 and not at 10:35:00.
  1. Only if you received a tick between the re-connection and 10:45 and called your function. Your log implies no.
  2. You can only call IsNewBar once per tick; do you call it in OnInit also? Is it coded properly?

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.

 
William Roeder:
  1. Only if you received a tick between the re-connection and 10:45 and called your function. Your log implies no.
  2. You can only call IsNewBar once per tick; do you call it in OnInit also? Is it coded properly?

1. Agreed.

2. I run it only once per tick and keep the status in a member variable, here's my IsNewBar():

const bool        IsNewBar()   const { return m_bNewBar; };
Reason: