Need help: MT4 Backtesting does not work

Hi friends,

i managed to program an expert advisor that seems to be actually opening and closing trades. I tested it on a demo account for a couple of months so I know it opens and closes trades at regular times. The problem I have is that I can’t get it to work with the MT4 strategy tester. So every improvement I make takes ages to test on a demo account.

I have downloaded detailed data (M1 and tick) and imported it into MT4. Other EA’s, like the examples that come with MT4, seem to be working fine in the strategy tester, but when I start my own program in the tester it executes just one order and then stops. There are no error messages or anything. It just stops.

I was wondering if someone could point out some of the common mistakes that programmers make that prevents an EA from working in the strategy tester. Other tips & tricks would be very welcome too.

Thanks.

I made this thread a few years back but much of it still applies today. Hopefully this helps you out on the right track.

http://forums.babypips.com/expert-advisors-and-automated-trading/52287-tutorial-complete-backtesting-analysis-setup-100-free.html

A few things you can try :

Make sure that your actually using the tick data, check the History Center and the options .

Check the date range in the strategy tester , the data from brokers is usually limited and sometimes you might only see a few hundred days of data.

Try using the tick data provided by your broker first, then import the new tick data.

You can always post your EA code here and someone can look it over quickly to see any glaring issues.

Thank you for the replies,

@ClarkFX
It will take me a couple of days to go through the tutorial. I think I followed a similar approach already but I will do exactly what you suggest. I will let you guys know about the results.

@alldayevery306
I am afraid that posting the whole source-code is not possible. It’s not public domain. I am only allowed to share bits for explicit discussions about certain techniques that I used.

That can be, but most likely the strategy tester is not working due to an inconsistent parameter or a incorrect assumption in the code. We cannot help you on that when we can’t see the code. It is you that asked for help. It was not us ‘begging’ you to show us the code.

If you like I can take a look at it, but for that I need to understand the whole script.

No worries, check my statements above. You saying you programmed it led me to believe you had the source code, which is my assumption that you programmed it.

If this is some program you downloaded or bought, that could very well be the reason why it doesn’t work. It could be retrieving signals from a server so back testing wouldn’t work.

Besides your tick data being correct, and if you can back test another EA with the same data, and your getting no errors when running the back test, it’s probably something with your code, but if you can’t post snippets ( which i understand ) we can’t really help.

Good luck, let us know how it turns out.

Do you get any error messages or is there anything in the logs (MT4 folder ester\logs)?

Hello from my First Post at these Forums. Although - candidly - personally, I’m fairly new to the MetaTrader trading platform and inasmuch, I’m new to the albeit remarkably C-like MQL programming language, I believe that there may be a manner of a logical model to it, overall. I’d seen this forum thread referred to via the BabyPips channel at Twitter. I thought it could to serve to make for a useful keynote toward expanding my own reference base about the MQL language - in looking at Technical Analysis, too. I don’t know if anything that I found might be of any exacting help, though - and of course, it can be a little difficult to debug a black box? LoL.

Broadly, maybe a reference to the documentation could serve to elucidate the matter, and as without asking any further detail?

This is my first post to the forums - and so, I can’t post links here, yet. If it may serve to help elucidate the matter - without asking any detail about the software being developed - there’s a whole MQL 4 book full of documentation at the MQL4 dot-com web site, I notice. In beginning to study MQL myself, I’d noticed that there’s a section specifically about Program Running in the documentation.

As with so many diagnosis tasks, perhaps it might help to diagnose the issue from the beginning? e.g the OnInit() call, and so on. Not asking any further detail about the software being developed, maybe a reference to the documentation could serve to help?

Sometimes the older EA’s don’t work with the new builds of MT4 but I don’t think this is the case here.It might be a setting that is wrong.

Thank you all for your valuable information. I think I found the reason why my program was not running in the Strategy Tester. What I did is the following: When a trade closes I keep track of the reason why it closed. If it closed because of a stoploss was triggered or a breakeven was detected or simply that the requested target has been reached. Per reason I like to insert some delay time before the program is allowed to open another trade.

The delay timer that I programmed was based on GetTickCount(). This function turns out is not compatible with the strategy tester. Reason: The delay is always in real-time. Hence after the first order was closed it simply waited 5 min, but by then the simulation was already finished.
The new version of my delay is based on TimeCurrent(). This function uses the time from the last bar or tick and is therefore also able to work with the Strategy Tester.

Both versions are shown below…

/*
 * ---- ntr_check_delay  << OLD >>
 * If for some reason a TradeDelay is in place then this function will 
 * gradually decrease gTradeDelay until the wait time has passed.
 */

void ntr_check_delay() {

   static uint time_from = 0;
   static uint time_to;
   uint real_time;
      
   if (gTradeDelay != 0) {
      real_time = GetTickCount() / 1000;
      gTradeDelay = MathMin(7200,gTradeDelay); // Max 2 hours
      
      if (time_from == 0) {
         time_from = real_time;
         time_to   = time_from + gTradeDelay;
      }
      else {
         if (real_time > time_to)
            gTradeDelay = 0;
         else
            gTradeDelay = time_to - real_time;
            
         if (gTradeDelay <= 0) {
            gTradeDelay = 0;
            time_from   = 0;
         }         
      }
   }
}
/*
 * ---- ntr_check_delay  << NEW >>
 * If for some reason a TradeDelay is in place then this function will 
 * gradually decrease gTradeDelay until the wait time has passed.
 *
 * Inputs
 * - gTradeDelay  Number of seconds of delay
 */

void ntr_check_delay() {

   static datetime time_from = 0;
   static datetime time_to   = 0;
   datetime real_time;
   
   if (gTradeDelay > 0) {
      gTradeDelay = MathMin(7200,gTradeDelay); // Max 2 hours
      real_time = TimeCurrent(); // Timer approx
      
      if (time_from == 0) {
         time_from = real_time;
         time_to   = real_time + gTradeDelay;
      } else {
         if (real_time > time_to) gTradeDelay = 0;
         else gTradeDelay = (int)(time_to - real_time);
         
         if (gTradeDelay <= 0) {
            time_from   = 0;
            time_to     = 0;
            gTradeDelay = 0;
         }
      }      
   } 
}

@ClarkFX,
Ok I followed the TUTORIAL as far as I could, but I couldn’t get the Python scripts working. After some research I found another tutorial using the tool TickStory Light. This tool does the downloading and then also the conversion for MT4. No Python required.

Achieving 99% Quality Backtests in MetaTrader4 - Back Bay Markets

Please let me clarify: I did program the EA myself, but I have chosen not to donate the code to Public Domain. At least not yet. I have spend too many hours on it to just give it away. :smiley:

Yes that is correct, glad you figured it out.

Also, you can always program the EA to check X number of bars that has passed instead of a set time interval.

So if your trading on a 5 min chart and want to wait 15 mins before opening another trade, you can just make sure 3 new bars have formed since your last trade.