Very bad performance issue in mql4

 

Hi,

I have an EA that is structured like this:

The main mq4 is holding an array with 2 class objects called 'Side', one for buy and one for sell. Measuring the execution time of the Side method that contains the logic takes 2microseconds. Measuring the same method from outside the class it takes 15microseconds.

This is very acceptable to run in strategy tester.

The big problem comes if i add another class on top of this, the strategy tester takes minutes to simulate something that was taking less than a sec before.

Is mql4 so bad? Should i not use classes? Any recommendation to reuse my code?


Not sure it matter but i'm using a mac, so MT4 is emulated.

 
Cristian Baluta:I have an EA that is structured like this: The main mq4 is holding an array with 2 class objects called 'Side', one for buy and one for sell. Measuring the execution time of the Side method that contains the logic takes 2microseconds. Measuring the same method from outside the class it takes 15microseconds. This is very acceptable to run in strategy tester. The big problem comes if i add another class on top of this, the strategy tester takes minutes to simulate something that was taking less than a sec before. Is mql4 so bad? Should i not use classes? Any recommendation to reuse my code? Not sure it matter but i'm using a mac, so MT4 is emulated.

How to do you expect us to answer you?

You have not shown your code, so we can't tell what the problem is. Is it a MQL or MetaTrader bug?

Probably not! Most likely, it is your own code that has bugs or logic problems, but we can't tell because you have not provided any details.

If you want users to take your query seriously, then you should put effort into your query. Show proper code that can demonstrate the problem, and that we can compile, run and test to see if the situation can be reproduced. Also supply log output and explain in detail.

Only then can we provide answers or guidance to resolve the issue.

 
Cristian Baluta:

Until you can provide more details, check out Code Profiling, it should help you pinpoint exactly the line(s) of code that's causing you problems.

Profiling means collecting program parameters during its execution. During a profiling, the execution time and the number of calls of individual functions and program code lines are measured. With this tool, the programmer is able to find and optimize the slowest code sections.

Profiling can be performed on the normal chart of the trading platform, as well as using history data in the Strategy Tester. In the first case, a program is launched on a chart that is updated in real time. You can check the program behavior in real conditions. In the second case, the program is launched in the Strategy Tester in the visual mode. The advantage of this method is that you do not need to wait for real data from a trade server or occurrence of certain trading conditions.

Code profiling - Developing programs - MetaEditor Help
Code profiling - Developing programs - MetaEditor Help
  • www.metatrader5.com
Profiling means collecting program parameters during its execution. During a profiling, the execution time and the number of calls of individual...
 

It is very difficult to post the code, i'm trying to isolate the problem.

Here is a sample on a smaller scale:

To run the first EA with all the logic and stuff it takes: 2022.04.25 10:49:24.546 AUDCAD,M15: 1147677 tick events (1532 bars, 1148677 bar states) processed in 0:00:00.273 (total time 0:00:00.346)

To run the second EA with everything commented except this code it takes: 2022.04.25 10:51:46.089 AUDCAD,M15: 1147677 tick events (1532 bars, 1148677 bar states) processed in 0:00:02.878 (total time 0:00:02.948)

Note that the first EA contains a longer version of this StringFormat as well.


string comments = StringFormat("                                LOTS: %.2f -> %.2f    GRID_STEP: %i    TP: %i    BED: %i", 
                                 MinLots, MaxLots, MinGridStep, TakeProfit, BED);
 
Cristian Baluta #:

It is very difficult to post the code, i'm trying to isolate the problem.

Here is a sample on a smaller scale:

To run the first EA with all the logic and stuff it takes: 2022.04.25 10:49:24.546 AUDCAD,M15: 1147677 tick events (1532 bars, 1148677 bar states) processed in 0:00:00.273 (total time 0:00:00.346)

To run the second EA with everything commented except this code it takes: 2022.04.25 10:51:46.089 AUDCAD,M15: 1147677 tick events (1532 bars, 1148677 bar states) processed in 0:00:02.878 (total time 0:00:02.948)

Note that the first EA contains a longer version of this StringFormat as well.


To give any meanful comparisons you need to compare over a much larger run period.

Comparing less than a second to less than 3 seconds is meaningless anything could be impacting it. 

 

For posterity this were the results for 7years:


2022.04.25 18:56:16.970 AUDCAD,M15: 130319333 tick events (175568 bars, 130320333 bar states) processed in 0:00:44.659 (total time 0:00:51.401)


2022.04.25 18:54:43.095 AUDCAD,M15: 130319333 tick events (175568 bars, 130320333 bar states) processed in 0:05:17.558 (total time 0:05:24.235)


I wasted few hours commenting code and stuff but i finally moved step by step code from the slow EA to the good EA and tested at each step and it started to work, can't explain why because it's the same code.


I however abandoned the idea of multiple pairs because i'm getting some errors in the journal and only the current pair opens trades.

2022.04.25 20:32:57.889 TestGenerator: unmatched data error (volume limit 1884 at 2022.04.21 08:00 exceeded)

2022.04.25 20:32:57.871 TestGenerator: unmatched data error (low value 0.93191 at 2022.04.15 23:45 is not reached from the least timeframe, low price 0.93194 mismatches)