is it a good practice to use infinite loop?

 
I am a .net programmer, but new to mql4. I am working on this EA for a friend of mine.
seems like "infinite loop" is the only solution.

Here's what I need to do. I need to check 6-7 currency pairs every minute to figure out the trend.
usually calculate is triggered by "tick", new tick coming in, "start()" function is triggered, then my calculation starts.

the problem with triggering by tick is if I have my EA drop on EUR/USD chart, during the nite, probably very slow, price not moving.
so instead calculate the price every minute, it will be calculated every half hr. At the same time, Japan Yen may go crazy.

my solution is to have an infinte loop to do the calculation, then put it sleep for 1 minute. this way I dont care if price moving slow or fast,
it will be calculated every minute.

But as a programmer, i know infinite loop is bad. not sure if this is right way to go.
 

you can put a EA on each of the Symbols and use global variables to "sync" them all, but that is also not the fine way.

mql4 was not designed to work on multicurrency's. so i in your case would go for infinite loop..

//z

 

I use one without issue for an EA specifically to monitor my connection status. Obviously I can't rely on tick data to run that given it's specifically looking for the loss of tick data!. It's coded as I want it with sleep in start() and then I call start() inside init(). Whether it's good practise... well it works without any problems so that is good enough for me.

hth

V

 

Yeah, I think you should use an infinite loop, but make sure you put RefreshRates() inside the loop. E.g.

while(1==1)
  {
   RefreshRates();  
   Your Code!
  }

Also, I think MQL5 can do this without the infinite loop, because of the event handling. I haven't studied MQL5 yet though so i'm not sure.

Chris

 

I use

while (!IsStopped()){

do something...

Sleep(100); // you need to have some sleep even if it is minimal...

}

 

I don't think there is any problem with using an infinite loop to monitor something. What you need to be careful of is if your loop starts to do something(like Print) without a way to interrupt the loop. This will fill your log with 160GB of nonsense and cause your platform to freeze. I did it to myself about 2 days ago.

 

Wow, this is some scary and dangerous programming. I hope you plan on having a way to break out of the loop, like changing a global variable from false to true or something like that and check on it in the loop. Also, I would not place it in an EA that is actually going to do some work, I would set it up as a monitor EA on a seperate chart not in use. If something does go wrong, it would be nice to kill it without risking trades.

 
LEHayes:
Wow, this is some scary and dangerous programming. I hope you plan on having a way to break out of the loop,
That's the purpose of the
while (!IsStopped()){

Just remove the EA from the chart or close the chart.

I would even expand it to

while( !(IsTesting() || IsStopped()) ){
since infinite loops and data from other pairs can't be used in the tester.
 
chrisbenjy:

Yeah, I think you should use an infinite loop, but make sure you put RefreshRates() inside the loop. E.g.

Also, I think MQL5 can do this without the infinite loop, because of the event handling. I haven't studied MQL5 yet though so i'm not sure.

Chris


one question about "RefreshRates()" . is this function gonna refresh all the currency pairs' data, or only refresh the current currency pair?
 
WHRoeder:
That's the purpose of the

Just remove the EA from the chart or close the chart.

I would even expand it to

since infinite loops and data from other pairs can't be used in the tester.

ye, if it needs to be closed, i just close the chart.
 

https://www.mql5.com/en/forum/112729, "Which EA model is best to use?"

https://www.mql5.com/en/forum/124308, "How to make EA function on an offline chart"

Reason: