MT4 Taking forever to backtest a simple EA

 

Hello Community,

I have just started coding my own EA and have stumbled upon this problem: Whenever I try to back-test my EA, It takes a LOT of time to complete, to a point where I just manually stop it after 2 hours and notice that barely 5% of it was finished. Now I know that complex EAs may require a lot of time to test, but take a look at mine, it really just is a test to further expand in the future. Please let me know if there is something slowing the process down in my code or if I just have to do a few steps before running the back-test.  Kindly Find attached the settings of back-test and my code.
Thanks in advance

Best Regards,

Lord Odin

Files:
bt1.png  16 kb
HW1.mq4  2 kb
 
LordOdin: I have just started coding my own EA and have stumbled upon this problem: Whenever I try to back-test my EA, It takes a LOT of time to complete, to a point where I just manually stop it after 2 hours and notice that barely 5% of it was finished. Now I know that complex EAs may require a lot of time to test, but take a look at mine, it really just is a test to further expand in the future. Please let me know if there is something slowing the process down in my code or if I just have to do a few steps before running the back-test.  Kindly Find attached the settings of back-test and my code.

It is taking long because you are checking values and doing repetitive calculations on every single incoming tick, without any need to do so.

Change your code to detect when a new bar is opened (see below) and only then should you check for the new values and update your calculations accordingly.

Also, you are using very old type of code. Update your code to use the newer style which has been in use for many years now.

void OnTick()
{
   // Check for New Bar (Compatible with both MQL4 and MQL5)
   static datetime dtBarCurrent = WRONG_VALUE;
   datetime dtBarPrevious = dtBarCurrent;
   dtBarCurrent = (datetime) SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE );
   boolNewBarFlag = ( dtBarCurrent != dtBarPrevious );

   if( boolNewBarFlag )
   {
      // Do something ...
   }
   else
   {
      // Do something else ...
   }

   // Do things ...
}
Updated MQL4 - MQL4 Reference
Updated MQL4 - MQL4 Reference
  • docs.mql4.com
Starting from build 600, MQL4 programming language has been completely revised reaching the level of MQL5 - now you can develop trading robots in MQL4/5 using the unified MetaEditor development environment, single style, libraries and debugging tools. MQL4 is popular among automated system developers due to the ease of learning and a huge...
 
Fernando Carreiro:

It is taking long because you are checking values and doing repetitive calculations on every single incoming tick, without any need to do so.

Change your code to detect when a new bar is opened (see below) and only then should you check for the new values and update your calculations accordingly.

Also, you are using very old type of code. Update your code to use the newer style which has been in use for many years now.

Thank you Fernando for your fast reply. I will update my code as suggested and give it a try. As for the old type of code, are you referring to the start(), init() and deinit() functions to be replaced by OnStart(), etc... ? Or is there something else in my code that is also very old? Sorry, I started learning MQL4 only a week ago.


Best Regards,

Lord Odin

 
Lord Odin: Thank you Fernando for your fast reply. I will update my code as suggested and give it a try. As for the old type of code, are you referring to the start(), init() and deinit() functions to be replaced by OnStart(), etc... ? Or is there something else in my code that is also very old? Sorry, I started learning MQL4 only a week ago.

Yes, start(), init() and deinit() to be replaced by OnInit(), OnTick() not OnStart() and OnDeinit(), but that is not all, so read up the documentation on the link I provided.

 

Greetings,

It turns out, as stupid as it may seem, the visual bar of the strategy tester was NOT showing. I just expanded the tab and put it on 32 and Voila, worked like a charm. Thanks anyways mate. Cheers.


Best Regards.

Lord Odin

Reason: