new candles after reconnecting to server

 

hi friends,
I want to count the number of the new candles that have been formed after the server has been disconnected and reconnected again.

I'm using the following code but in "M1" timeframe when I manually disconnect and reconnect after some minutes, it doesn't show the correct number of the candles formed during the time that the server was disconnected.

void OnTick()
  {
//---  
 if(is_new_candle_formed_func()==0)
      Comment(candle_0_time_old);
   candle_0_time_old=iTime(symbol,PERIOD_M1,0);
  }
//+------------------------------------------------------------------+
//|                     is_new_candle_formed_func                    |
//+------------------------------------------------------------------+
int is_new_candle_formed_func()
  {
   candle_0_time_new=iTime(symbol,PERIOD_M1,0);

   if(candle_0_time_new==candle_0_time_old)
      return(0);
   else
     {
      int
      bar_num_new=iBarShift(symbol,PERIOD_M1,candle_0_time_new,True),
      bar_num_old=iBarShift(symbol,PERIOD_M1,candle_0_time_old,True),
      number_of_new_bars=bar_num_old-bar_num_new;
      Alert("candle_0_time_old= ",candle_0_time_old,
            "\nbar_num_old= ",bar_num_old,
            "\n\ncandle_0_time_new= ",candle_0_time_new,
            "\nbar_num_new= ",bar_num_new,
            "\n\nnumber_of_new_bars= ",number_of_new_bars);
      return(number_of_new_bars);
     }
  }
//+------------------------------------------------------------------+

your kind guidance is highly appreciated.

 
parham.trader: it doesn't show the correct number of the candles formed during the time that the server was disconnected.
  1. candle_0_time_new=iTime(symbol,PERIOD_M1,0);
    Unless the chart is that specific pair/TF, you must handle 4066/4073 errors.

  2. number_of_new_bars=bar_num_old-bar_num_new;
    The number of candles between A and B inclusive is A-B+1.
    7,8,9 is three numbers 9-7+1=3
 
whroeder1:
  1. Unless the chart is that specific pair/TF, you must handle 4066/4073 errors.

The problem is exactly about downloading new candles after reconnecting to server. 

After some minutes when I reconnect to the server, the "candle_0_time_old" and "candle_0_time_new" show the correct time but when I want to calculate their Bar number using iBarShift, the calculated Bar numbers are not showing the candle number correctly and it is exactly because the CANDLES ARE NOT FORMED YET AFTER RECONNECTING TO SERVER. 

The most odd issue about matter is that the "_LastError" is not equal to "4066" after reconnecting and I don't know how to handle this !

below I have attached the screen shot of the Alert that is made by the code:


 
parham.trader:

The problem is exactly about downloading new candles after reconnecting to server. 

After some minutes when I reconnect to the server, the "candle_0_time_old" and "candle_0_time_new" show the correct time but when I want to calculate their Bar number using iBarShift, the calculated Bar numbers are not showing the candle number correctly and it is exactly because the CANDLES ARE NOT FORMED YET AFTER RECONNECTING TO SERVER. 

The most odd issue about matter is that the "_LastError" is not equal to "4066" after reconnecting and I don't know how to handle this !

below I have attached the screen shot of the Alert that is made by the code:



Yes, after the connection is established they update the current candles first, and after a while they insert the missing bunch. So the shift of the watched candle happens a bit later than you expect.

 
Ex Ovo Omnia:

Yes, after the connection is established they update the current candles first, and after a while they insert the missing bunch. So the shift of the watched candle happens a bit later than you expect.


so how can I solve this problem?

I think the solution for this issue is using the Sleep function after reconnecting to the server to solve this problem but the main question is here HOW TO FIND OUT THAT THE CONNECTION TO THE SERVER WERE DISCONNECTED AND NOW IT IS RECONNECTED or in the other word HOW TO FIND OUT THAT THE CANDLES ARE NOT STILL FORMED AFTER RECONNECTING TO THE SERVER?

Thanking in advance,

 
parham.trader:  "_LastError" is not equal to "4066" after reconnecting
  1. Are you running on the M1 chart or not?
  2. You already showed that candle time old was zero. That means you didn't handle 4066/4073 errors.
  3. Why do you care? process the current candle and on the next tick you process everything else.
Reason: