Jump to content

while loop in EA causes 100% CPU


ido370

Recommended Posts

i created an EA but it contains a while loop, but when i run it, my CPU load goes to 100%, even backtesting won't start. Can someone show what i am doing wrong in my code to prevent this, and so that i can backtest normally?

 

//Wait for first VALID signal and enter trade - WHILE loop
  while (ready==0)
  {
     //Create waiting label
     ObjectCreate("wait", OBJ_LABEL, 0, 0, 0);// Creating obj. 
     ObjectSet("wait", OBJPROP_CORNER, 1); // Reference corner 
     ObjectSet("wait", OBJPROP_XDISTANCE, 5);// X coordinate 
     ObjectSet("wait", OBJPROP_YDISTANCE, 15);// Y coordinate
     ObjectSetText("wait", "Waiting for signal..",15,"Arial Bold",White);
     
     Trendlord=iMA(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,1);
     BarClose=iClose(NULL,0,1);
     BarOpen=iOpen(NULL,0,1);
  
     if (BarOpen > Trendlord&& BarClose < Trendlord) 
     {
     OrderSend(Symbol(),OP_SELL,Lotsize,Bid,50,NULL,NULL,NULL,MagicNumber,0,Red);
     ObjectDelete("wait");
     ready = 1;
     }
  
     if (BarOpen < Trendlord&& BarClose > Trendlord) 
     {
     OrderSend(Symbol(),OP_BUY,Lotsize,Ask,50,NULL,NULL,NULL,MagicNumber,0,Blue);
     ObjectDelete("wait");
     ready = 1;
     }
   }

 

when ready =1 it should continue with the rest of my code. i just placed this in it, so it wont start trading when attaching the EA, but waits for first signal/cross first.

Link to comment
Share on other sites

Hi,

 

This is a very bad idea......you can run this loop once at the opening of any new bar. However, if you insist, I suppose you could try RefreshRates(). There is no new data entering you loop. RefreshRates() updates the pre-defined TimeSeries arrays, but I am not sure that it updates "other" timeframe arrays"(ie iOpen rather than Open[]) , so you may need to address that issue as well before it will work.

Edited by kennyhubbard
Link to comment
Share on other sites

already fixed. removed the while loop and changed to if..

also added the following right after start() and added variable static datetime barStart, so it will only check on new bar, not every tick.

if (barStart < Time[0]) //start of new bar

{

barStart = Time[0];

 

thanks for the help.

Link to comment
Share on other sites

FYI, while loops in EA's need to call the Sleep() function to keep them from using 100% CPU. Sleep() releases the CPU to other processes until it times out.

http://docs.mql4.com/common/Sleep

 

Also, if one is using a loop in an EA, the RefreshRates() function should be called regularly to update the incoming tick data.

http://docs.mql4.com/windows/RefreshRates

Edited by buster1221
Clarification
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...