ido370 Posted December 29, 2010 Report Share Posted December 29, 2010 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. Quote Link to comment Share on other sites More sharing options...
kennyhubbard Posted December 30, 2010 Report Share Posted December 30, 2010 (edited) 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 December 30, 2010 by kennyhubbard Quote Link to comment Share on other sites More sharing options...
ido370 Posted December 30, 2010 Author Report Share Posted December 30, 2010 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. Quote Link to comment Share on other sites More sharing options...
buster1221 Posted January 6, 2011 Report Share Posted January 6, 2011 (edited) 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 January 6, 2011 by buster1221 Clarification Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.