Problem on Pausing expert on every candle(Visual Mode)

 

Hi everyone ,

I use this simple code in order to pause tester on every candle. (for some reason!)

my problem is that sometimes it skips some candles and then it pause the test.

#import "user32.dll"
void keybd_event(int bVk, int bScan, int dwFlags,int dwExtraInfo);
#import
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  if(newCandle())
  breakpoint();
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void breakpoint()
  {
   if(IsVisualMode())
     {

      keybd_event(19,0,1,0);
      //Sleep(1);
      keybd_event(19,0,2,0);
      
     }
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool newCandle()
  {
   static datetime candletime = 0;
   bool result=false;

   if(candletime != iTime(_Symbol,PERIOD_CURRENT,0))
     {

      result=true;
      candletime = iTime(_Symbol,PERIOD_CURRENT,0);
     }

   return(result);

  }
//+------------------------------------------------------------------+