Are there Tips and Tricks to Avoid Overload in MetaTrader EA

 

Hello,

I am seeking some advice and tips regarding an issue I'm facing with MetaTrader Expert Advisors. I have noticed that when I run the EA on multiple charts and occasionally compile the source code while it's running, MetaTrader starts to become unresponsive and overloaded. It seems to get stuck or experience performance issues.

The EA operates within a while loop, calling the code every 3 seconds. While this approach works initially, it appears to lead to the overload problem when I interact with the source code and hit the compile button. I'm wondering if there are any strategies or best practices I can implement within the EA's code to prevent these blockades from occurring.

 
Email Account:

Hello,

I am seeking some advice and tips regarding an issue I'm facing with MetaTrader Expert Advisors. I have noticed that when I run the EA on multiple charts and occasionally compile the source code while it's running, MetaTrader starts to become unresponsive and overloaded. It seems to get stuck or experience performance issues.

The EA operates within a while loop, calling the code every 3 seconds. While this approach works initially, it appears to lead to the overload problem when I interact with the source code and hit the compile button. I'm wondering if there are any strategies or best practices I can implement within the EA's code to prevent these blockades from occurring.

Hi,

a while loop can clutter the code when a condition is created that is never met or when a condition is created that is always met, resulting in an infinite loop. An infinite loop occurs when the condition of the loop never becomes false, causing the loop to run continuously without stopping.

When a while loop executes infinitely, the code becomes saturated because the program is trapped in an endless loop and cannot advance beyond that point. This can cause the program to become unresponsive and unresponsive to other instructions or commands. In extreme cases, it can lead to a crash of the program or even the operating system.

It is important to ensure that the exit condition of the while loop is met at some point to avoid saturating the code. This may involve updating a control variable within the loop or using a logical expression that eventually becomes false.

 
Are there any useful functions that I can use within the while loop to detect if someone is currently compiling in the editor or to identify other issues and then stop the while loop?
 
Email Account #:
Are there any useful functions that I can use within the while loop to detect if someone is currently compiling in the editor or to identify other issues and then stop the while loop?

Are you using IsStopped in your loop ?

Example :

for(int i=0; i<ITERATION && !IsStopped(); i++)
{
...
}
 
Alain Verleyen #:
IsStopped()

Yes, this is how my loop is looking:

while(!IsStopped())
{
   OnTickCode();
   Sleep(3000);
}

I am starting this while loop in the OnInit() function call, because i did had before some other problems when i was using the Metatrader Event Timer, that was stopping sometimes without notice, then people recommend to use a while loop and because the OnTick() function only run when new ticks come, the idea comeup for using the while loop, but which now creates sometimes this overload problems.

Something like in Javascript try catch block could be usefull maybe for this while loop, but i dont have an idea how to build that in MQL.

 
Email Account #:

Yes, this is how my loop is looking:

I am starting this while loop in the OnInit() function call, because i did had before some other problems when i was using the Metatrader Event Timer, that was stopping sometimes without notice, then people recommend to use a while loop and because the OnTick() function only run when new ticks come, the idea comeup for using the while loop, but which now creates sometimes this overload problems.

Something like in Javascript try catch block could be usefull maybe for this while loop, but i dont have an idea how to build that in MQL.

That's a very bad recommendation to use a while loop instead of events. And you should not run it from OnInit() at all.

You should use OnTimer() and investigate what happened when " that was stopping sometimes without notice ".

 
Alain Verleyen #:
ou should use OnTimer() and investigate what happened when " that was stopping sometimes without notice ".

that was the problem that time, the Metatrader was not returning a error somehow but you could only read in the experts journal that the timer event did stop and so it was difficul to debug.

is it possible to restart a timer event again from OnTick function when you know it has stop?

 
Email Account #:

that was the problem that time, the Metatrader was not returning a error somehow but you could only read in the experts journal that the timer event did stop and so it was difficul to debug.

is it possible to restart a timer event again from OnTick function when you know it has stop?

Yes it is. But it should not stop at the first place, that's not a normal behaviour. It should better be investigated, I can help if you want.

Using infinite loop you will have other issues, as you noticed.

 
Alain Verleyen #:

Yes it is. But it should not stop at the first place, that's not a normal behaviour. It should better be investigated, I can help if you want.

Using infinite loop you will have other issues, as you noticed.

Thank you for your offer to help further. I will go back to working with onTimer events, and I hope that if the errors occur again where a timer stops working, I can somehow reproduce them and then post here so that we can find the reasons together. Because I remember last time, I would randomly encounter these problems, and I didn't exactly know why, making it difficult to conduct proper troubleshooting. Therefore, this time, I will observe it more closely for further analysis.
 
Email Account #:
Thank you for your offer to help further. I will go back to working with onTimer events, and I hope that if the errors occur again where a timer stops working, I can somehow reproduce them and then post here so that we can find the reasons together. Because I remember last time, I would randomly encounter these problems, and I didn't exactly know why, making it difficult to conduct proper troubleshooting. Therefore, this time, I will observe it more closely for further analysis.

You can add code to detect when is doesn't trigger, and print values in the log.

If you have to report it, then provide your log, and the relevant code. 

 
Alain Verleyen #:

You can add code to detect when is doesn't trigger, and print values in the log.

If you have to report it, then provide your log, and the relevant code. 

Thanks i will remember that when it happen again will let you know

Reason: