Professors, Professors, please help, so Confused on the code execution !!!

 

So frustrated I haven't any idea how this happened!

The code ran on 2 brokers for 3 months. Today the issue below suddenly happened at 1 broker.

Please refer to the log and code below.  The code should run sentence by sentence based OnTick. However, please refere to the time marked in yellow. Why were the closed orders  printed after hours?

I thought and thought, but thought of nothing about the root cause except the doubt. Sentences of new tick can start to run although the sentences of previous ticks  haven't been executed or completed while VPS is slow.

Is it possible? How do the MQL4 code run indeed? Please, please help!!! Thank you very, very, very much!!! 

 

2015.12.16 16:30:17.426 xDangerEN_4D EURNOK.pro,M5: modify #36653643 buy 0.07 EURNOK.pro at 9.5695 sl: 0.0000 tp: 9.5813 ok
2015.12.16 16:30:17.426 xDangerEN_4D EURNOK.pro,M5: MP.B3_ModifyBuy(step) succeeded at 9.5813 with Bid/Ask 9.5634/9.5664
2015.12.16 16:30:18.363 xDangerEN_4D EURNOK.pro,M5: modify #36653643 buy 0.07 EURNOK.pro at 9.5695 sl: 0.0000 tp: 9.6025 ok
2015.12.16 16:30:18.363 xDangerEN_4D EURNOK.pro,M5: MP.B1_ModifyBuy(first) succeeded at 9.6025
2015.12.16 16:30:23.677 xDangerEN_4D EURNOK.pro,M5: modify #36653643 buy 0.07 EURNOK.pro at 9.5695 sl: 0.0000 tp: 9.5813 ok
2015.12.16 16:30:23.677 xDangerEN_4D EURNOK.pro,M5: MP.B3_ModifyBuy(step) succeeded at 9.5813 with Bid/Ask 9.5637/9.5663
2015.12.16 16:48:13.529 xDangerEN_4D EURNOK.pro,M5: PPP.1_This history POS 93 was closed with Possible Bid/Ask 9.5589/9.5622 as TakeProfit.
2015.12.16 16:48:13.529 xDangerEN_4D EURNOK.pro,M5: #36652937 2015.12.16 14:48:49 buy 0.07 EURNOK.pro 9.5658 0.0000 9.5947 2015.12.16 15:29:56 9.5735 -0.49 0.00 6.15 1 5858688

2015.12.17 00:20:08.525 xDangerES_4D EURSEK.pro,M5: WE.1_File updated successfully: EURSEK.pro_xDangerES_4D_20151113.txt
2015.12.17 00:20:08.525 xDangerES_4D EURSEK.pro,M5: MP.B4_ModifyBuy(step) at 9.3056 with Error: Trade timeout
2015.12.17 00:21:00.635 xDangerES_4D EURSEK.pro,M5: WE.1_File updated successfully: EURSEK.pro_xDangerES_4D_20151113.txt
2015.12.17 00:21:00.635 xDangerES_4D EURSEK.pro,M5: MP.B4_ModifyBuy(step) at 9.3056 with Error: No connection with trade server
2015.12.17 01:32:09.287 xDangerES_4D EURSEK.pro,M5: modify #36655076 buy 0.24 EURSEK.pro at 9.3169 sl: 0.0000 tp: 9.2732 ok
2015.12.17 01:32:09.287 xDangerES_4D EURSEK.pro,M5: MP.B3_ModifyBuy(step) succeeded at 9.2732 with Bid/Ask 9.2700/9.2744
2015.12.17 02:12:53.761 xDangerEN_4D EURNOK.pro,M5: PPP.1_This history POS 169 was closed with Possible Bid/Ask 9.5448/9.5477 as TakeProfit.
2015.12.17 02:12:53.761 xDangerEN_4D EURNOK.pro,M5: #36659173 2015.12.16 22:08:11 sell 0.06 EURNOK.pro 9.5895 0.0000 0.0000 2015.12.16 22:08:11 9.5929 -0.42 0.00 -2.34 0 5858688

 

 

void prepare()
  {
   int historyTotal=OrdersHistoryTotal(),cLocat;
   for(i=historyPOS; i<historyTotal; i++)
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true && OrderMagicNumber()==MAGIC)
        {
         cLocat=StringToInteger(OrderComment());
         accountBalance+=OrderProfit()+OrderCommission()+OrderSwap();
         PrintFormat("PPP.1_This history POS %i was closed with Possible Bid/Ask %s/%s as TakeProfit.",i+1,DoubleToStr(Bid,Digits),DoubleToStr(Ask,Digits));
         OrderPrint();
        }
   if(historyPOS!=historyTotal) historyPOS=historyTotal;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   prepare();
   closePosition();
   manageProfits();
   openPosition();
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
 

Date and time at the beginning of the line (2015.12.16 16:48:13.529) are taken from your pc at the time of the printout.

The other time (2015.12.16 15:29:56) is the close time of your broker.

What if the pc clock has been changed or is heavily lagging due to a bad load distribution of the vps by the vps-provider?

 
gooly:

Date and time at the beginning of the line (2015.12.16 16:48:13.529) are taken from your pc at the time of the printout.

The other time (2015.12.16 15:29:56) is the close time of your broker.

What if the pc clock has been changed or is heavily lagging due to a bad load distribution of the vps by the vps-provider?

I checked previous reasons and no problem at all. It proves there is problem since the closed orders should be printed in the next tick once the order is closed. 

The code has run 3 months without problem and just has problem today. So my doubt is same as what you mentioned on VPS speed.

Is it possible for the Sentences of new tick can start to run although the sentences of previous ticks  haven't been executed or completed while VPS is slow?

How does MQL4 proceed while VPS is slow?

 

MQL4 can't recognize it.The only hint is that the 'virtual' pc clock gets slower and slower. Compare the times of the pc clocks.

I once realized that the clock of the vps was more that 30 min per day slower than real time.

The difference was so big that the ntp software refuses to set that vps clock. Of course the provider refuses any responsibility - so I quit!

 
gooly:

MQL4 can't recognize it.The only hint is that the 'virtual' pc clock gets slower and slower. Compare the times of the pc clocks.

I once realized that the clock of the vps was more that 30 min per day slower than real time.

The difference was so big that the ntp software refuses to set that vps clock. Of course the provider refuses any responsibility - so I quit!

 

In my case, you can see the VPS time in the bottom-right is completely same as the MT4 server time.  So believe it's not the issue of clock delay. 

Instead, I'm doubting if this statement still works while CPU/Memory overloaded, "In case when OnTick() function for the previous quote is being processed when a new quote is received, the new quote will be ignored by an Expert Advisor".  

Has any friend any idea or experience on it? Thanks in advance! 

 

Any comment or suggestion is welcome! 

 
gooly:

MQL4 can't recognize it.The only hint is that the 'virtual' pc clock gets slower and slower. Compare the times of the pc clocks.

I once realized that the clock of the vps was more that 30 min per day slower than real time.

The difference was so big that the ntp software refuses to set that vps clock. Of course the provider refuses any responsibility - so I quit!

 

Dear Gooly,

I used "EventSetMillisecondTimer(20)" on Amazon free VPS and did observe the delay from the VPS local time after hundreds of "OnTimer()" cycles. 

How can it happen as the VPS local time is still accurate? I'm confused. Is there something I can do in my code to prevent or mitigate the impact? 

 

Besides, my code runs in same speed all the time.

However you can see the VPS CPU is about 8% at the begining and it rises up gradually with hours. Finally it always be 100% busy.  

May I ask a favor for you to help me better understand this kind of issue? Thank you very much!  

 

Amazon's VPS seems to be very slow. If you run your terminal directly on a pc it will get let's say every 10th cpu-tick - the other 9 are for other threads.

On a VPS (same situation) the terminal didn't get every 10th tick of the cpu but if there are 100 other clients every it will get only every 100*10 tick of the cpu. BUT for your terminal it is again only the t10th tick.

I had once a VPS where the clock delayed about 30 min per day it was so slow that even ntp-time refuses to set times!

Beside that after I  complained the admin of the VPS logged in onto my VPS and changed the automatic time adjust of Windows!

Have you changed the option for the VPS-Admin to login to your VPS?

Amazon's VPS is meant for normal user and their individual web idea - I don't think it suits a trader (There is no free lunch!)

 
gooly:

Beside that after I  complained the admin of the VPS logged in onto my VPS and changed the automatic time adjust of Windows!

Have you changed the option for the VPS-Admin to login to your VPS?

 

Dear Gooly,

Thanks a lot. But how to change the option? 

 
jollydragon:

 

Dear Gooly,

I used "EventSetMillisecondTimer(20)" on Amazon free VPS and did observe the delay from the VPS local time after hundreds of "OnTimer()" cycles. 

How can it happen as the VPS local time is still accurate? I'm confused. Is there something I can do in my code to prevent or mitigate the impact? 

 

Besides, my code runs in same speed all the time.

However you can see the VPS CPU is about 8% at the begining and it rises up gradually with hours. Finally it always be 100% busy.  

May I ask a favor for you to help me better understand this kind of issue? Thank you very much!  

 Hello,

The difference of the time you are expecting and the local time passed  may be there in case your program needs more than 20ms for each iteration to complete; in that case, your estimated (expected) time should appear as less than the time passed 

Regarding CPU going from 8% to 100%, I believe it is the result of Amazon Cloud automatically allocating the necessary CPU power to your program - and no more than that, so probably no problem here  

best regards 

 
Demos:

 Hello,

The difference of the time you are expecting and the local time passed  may be there in case your program needs more than 20ms for each iteration to complete; in that case, your estimated (expected) time should appear as less than the time passed 

Regarding CPU going from 8% to 100%, I believe it is the result of Amazon Cloud automatically allocating the necessary CPU power to your program - and no more than that, so probably no problem here  

best regards 

 

Do you think the code below in "OnTimer()" costs over 20 ms to run?

 

input int timePeriod=50;

int timeTick=0;
ulong iniTime,curTime;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetMillisecondTimer(timePeriod);
   iniTime=TimeCurrent();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   string display="\n";
   timeTick++;
   curTime=TimeCurrent();

   display+=TimeToStr(curTime,TIME_DATE|TIME_SECONDS);
   display+=StringFormat("\nEvent Ticks: %i   Real Ticks: %u",timeTick,(curTime-iniTime)*1000/timePeriod);

   Comment(display);
  }
//+------------------------------------------------------------------+
Reason: