MQL Code for Controlling of MT4 Strategy Tester - page 2

 
mqleurope: Hi Fernando, thank you for your answer. I tried the solution you suggested. After I called the terminal for the second time, it really gained a focus but nothing happened afterwards. It does not seem that the already running terminal is able to accept new commands this way: I really appreciate your willingness to help!
I currently have some obligations I need to take care of, but later tonight I will try to recreate the situation and see if I can come up with a solution for you!
 
Fernando Carreiro:
I currently have some obligations I need to take care of, but later tonight I will try to recreate the situation and see if I can come up with a solution for you!
Excellent, thanks so much!!
 
mqleurope: Excellent, thanks so much!!

Ok, I have done some tests and these are my findings as it pertains to my own setup.

EDIT: Please note that later posts confirm my misjudgments that I express in this post. Some of the information in this post is just wrong. Keep reading the following posts.

  1. Firstly, if the instance is already open it does not reuse it, just as the OP reported. I was wrong to assume that it would.
  2. When doing a fresh start, just as the OP reported, the test did in fact commence before waiting for the login process of the trader server or even the MQL5 login:
    [Terminal Journal Log]
    2021.06.06 01:41:13.679 MetaTrader 4 build 1335 started (MetaQuotes Software Corp.)
    2021.06.06 01:41:13.679 Windows 10 Pro x64, IE 11, UAC, 8 x Intel Core i7-4790T  @ 2.70GHz, Memory: 8661 / 16274 Mb, Disk: 158 / 893 Gb, GMT+0
    2021.06.06 01:41:13.679 Data Folder: C:\Trading\MetaTrader\Roboforex\MT4D2119309T
    2021.06.06 01:41:22.646 Started with configuration file 'test.ini'
    2021.06.06 01:41:25.792 '8657754': login on RoboForex-Demo through DataCenter Europe #1 (ping: 41.86 ms)
    2021.06.06 01:41:26.390 '8657754': login datacenter on RoboForex-Demo through DataCenter Europe #1 (ping: 41.86 ms)
    2021.06.06 01:41:27.393 MQL5.community: activated for 'fmic', balance: 10.00
    2021.06.06 01:41:27.678 MQL5.chats: activated for 'FMIC'
    
    [Strategy Tester Journal Log]
    2021.06.06 01:41:25.212 Expert Moving Average.ex4 EURUSD,H1: loaded successfully
    2021.06.06 01:41:25.227 Tester: start with configuration file
    2021.06.06 01:41:25.420 Tester: template 'C:\Trading\MetaTrader\Roboforex\MT4D2119309T\templates\tester.tpl' applied
    2021.06.06 01:41:27.848 TestGenerator: spread set to 15
    2021.06.06 01:41:27.848 2018.02.05 00:00:00  Moving Average.ex4 inputs: Lots=0.1; MaximumRisk=0.02; DecreaseFactor=3; MovingPeriod=12; MovingShift=6; 
    2021.06.06 01:41:28.380 2018.02.05 05:00:00  Moving Average.ex4 EURUSD,H1: open #1 buy 0.20 EURUSD at 1.24625 ok
    2021.06.06 01:41:28.481 2018.02.05 06:00:00  Moving Average.ex4 EURUSD,H1: close #1 buy 0.20 EURUSD at 1.24625 at price 1.24547
    ...
    2021.06.06 01:41:41.528 2018.02.12 17:00:00  Tester: stop button pressed
    2021.06.06 01:41:41.566 2018.02.12 17:00:00  Tester: order #11 is closed
    2021.06.06 01:41:41.566 EURUSD,H1: 138 tick events (138 bars, 38449 bar states) processed in 0:00:13.703 (total time 0:00:16.140)
  3. I then tried setting the login information explicitly in the ".ini" configuration file, in the hopes that it would first carry out the login before doing the test, but it did not. It still started the test before completing the login process.
  4. I then deleted all history data, both ".hst" and ".fxt" files, in the hopes that it would need to wait for the the login to proceed in order to obtain the quote data, but still it started before the login and this time gave a data generation error and halted the test.
    2021.06.06 02:18:09.100 TestGenerator: deficient data 'EURUSD60' (10 rate records)

In essence, it sucked! Not only does it not make any sense for it to start the testing without being connected to the trader server, but it offers no robust way around it, especially if it depends on the trade server to generate the ".fxt" data files for the test and requires the contract information for the testing environment. It just does not make any sense. As far as I am concerned, its a major bug, that will obviously never get fixed.

I see two possible work-arounds, which I have yet to test.

  1. The first, is to alter the EA being tested to wait for the trade connection to be made and enters a loop to prevent the EA from leaving the OnInit() until a connection is confirmed. However, this assumes that one has access to the source code which is not always the case. Another requirement is that we first start the terminal up and download all necessary data first, so that it will be available during the test and not generate an error even before the OnInit() handler is even called.
  2. The second option that I can think of, is to create or alter the "tester.tpl" template that is usually loaded during tests, or the template specific for the EA being tested, and include in it, an indicator specifically coded that would also cause a delay via a loop that would wait for the trade server to be connected and the account logged on. This would also have the requirement of having data available before running the test.

Please note, that both methods described above, have not been tested, and the ideas are only theoretical. In practice, they may not save the situation.

If anyone else has experience with this and a solution, I am sure the OP will appreciate it.

 

After reading over my previous post looking for errors to correct, I noticed something that I overlook the first time.

Even though the test started before the login process, I see now that the setting of the spread and actual start of the test only took place after the login process.

[Terminal Journal Log]
2021.06.06 01:41:22.646 Started with configuration file 'test.ini'
2021.06.06 01:41:25.792 '8657754': login on RoboForex-Demo through DataCenter Europe #1 (ping: 41.86 ms)
2021.06.06 01:41:26.390 '8657754': login datacenter on RoboForex-Demo through DataCenter Europe #1 (ping: 41.86 ms)
2021.06.06 01:41:27.393 MQL5.community: activated for 'fmic', balance: 10.00
2021.06.06 01:41:27.678 MQL5.chats: activated for 'FMIC'

[Strategy Tester Journal Log]
2021.06.06 01:41:25.212 Expert Moving Average.ex4 EURUSD,H1: loaded successfully
2021.06.06 01:41:25.227 Tester: start with configuration file
2021.06.06 01:41:25.420 Tester: template 'C:\Trading\MetaTrader\Roboforex\MT4D2119309T\templates\tester.tpl' applied
2021.06.06 01:41:27.848 TestGenerator: spread set to 15
2021.06.06 01:41:27.848 2018.02.05 00:00:00  Moving Average.ex4 inputs: Lots=0.1; MaximumRisk=0.02; DecreaseFactor=3; MovingPeriod=12; MovingShift=6; 
2021.06.06 01:41:28.380 2018.02.05 05:00:00  Moving Average.ex4 EURUSD,H1: open #1 buy 0.20 EURUSD at 1.24625 ok
2021.06.06 01:41:28.481 2018.02.05 06:00:00  Moving Average.ex4 EURUSD,H1: close #1 buy 0.20 EURUSD at 1.24625 at price 1.24547

So, I'm left wondering If I am not maybe letting the OP's experience cloud my judgment, and it is in fact waiting for the download, but only in the case where data is already available.

I am going to redo the tests again in a different way and see if I am not analysing it incorrectly.

 

Ok, I redid the test, but with data preloaded (there was no way around this issue), but this time I set the spread to floating (instead of a fixed value). I did this to see if it would actually collect the trade server's currently reported spread (which it did). These were the timing results:

[Terminal Journal Log]
2021.06.06 03:05:21.380 MetaTrader 4 build 1335 started (MetaQuotes Software Corp.)
2021.06.06 03:05:21.380 Windows 10 Pro x64, IE 11, UAC, 8 x Intel Core i7-4790T  @ 2.70GHz, Memory: 8853 / 16274 Mb, Disk: 158 / 893 Gb, GMT+0
2021.06.06 03:05:21.380 Data Folder: C:\Trading\MetaTrader\Roboforex\MT4D2119309T
2021.06.06 03:05:21.900 Started with configuration file 'test.ini'
2021.06.06 03:05:25.134 '500242399': login on RoboForex-Demo through DataCenter Europe #5 (ping: 71.27 ms)
2021.06.06 03:05:26.315 '500242399': login datacenter on RoboForex-Demo through DataCenter Europe #5 (ping: 71.27 ms)
2021.06.06 03:05:27.340 MQL5.community: activated for 'fmic', balance: 10.00
2021.06.06 03:05:27.616 MQL5.chats: activated for 'FMIC'

[Strategy Tester Journal Log]
2021.06.06 03:05:24.386 Expert Moving Average.ex4 GBPUSD,H1: loaded successfully
2021.06.06 03:05:24.386 Tester: start with configuration file
2021.06.06 03:05:24.595 Tester: template 'C:\Trading\MetaTrader\Roboforex\MT4D2119309T\templates\tester.tpl' applied
2021.06.06 03:05:27.177 TestGenerator: current spread 43 used
2021.06.06 03:05:27.177 2018.02.05 00:00:00  Moving Average.ex4 inputs: Lots=0.1; MaximumRisk=0.02; DecreaseFactor=3; MovingPeriod=12; MovingShift=6; 
2021.06.06 03:05:28.151 2018.02.05 11:00:00  Moving Average.ex4 GBPUSD,H1: open #1 buy 0.20 GBPUSD at 1.41356 ok

Viewing the logs with new eyes, it does look like that the test is paused until the trade server connects before setting the spread and continuing the test. However, it does not seem to wait for the MQL5 website login to complete, only the login to the trade server.

I also redid the test with a fixed spread and the timing values were equivalent. So, even in my previous posts, the log posted does show that the spread was only set after the trader server connection.

So, my question to the OP is this:

  • Can you please supply your own logs for both the Terminal Journal as well as the Tester Journal s can see you timing of the test run as well as the errors that are reported by the EA being tested. This should help us narrow down the problem and maybe come up with a solution.
 
Fernando Carreiro:

Ok, I redid the test, but with data preloaded (there was no way around this issue), but this time I set the spread to floating (instead of a fixed value). I did this to see if it would actually collect the trade server's currently reported spread (which it did). These were the timing results:

Viewing the logs with new eyes, it does look like that the test is paused until the trade server connects before setting the spread and continuing the test. However, it does not seem to wait for the MQL5 website login to complete, only the login to the trade server.

I also redid the test with a fixed spread and the timing values were equivalent. So, even in my previous posts, the log posted does show that the spread was only set after the trader server connection.

So, my question to the OP is this:

  • Can you please supply your own logs for both the Terminal Journal as well as the Tester Journal s can see you timing of the test run as well as the errors that are reported by the EA being tested. This should help us narrow down the problem and maybe come up with a solution.

Hello Fernando,

I really admire your willingness to help, your way of solving problems and your style, so THANK YOU!

I switched to Moving Average.ex4 for now as well. My conclusions are the same as yours. If the .fxt file generated by MT4 itself is used, the backtesting starts after MT4 is connected to the trade account. But it seems that I have to be a troublemaker. The .fxt file I need to use is produced by a third-party application. It contains tick price data and also the fixed spread number. The thing is that this spread number is NOT used for the backtest (MT4 always uses the spread written in the Tester window) but in spite of this fact, the tester does not wait for the connection to the trade account in this case and starts running before the connecting is finished. Where the tester finds out the value of the current (floating) spread, remains a miracle for me. I think I deleted all the files where MT4 could store prices.

Thank you once again for all the research you did for us!

 
mqleurope: I really admire your willingness to help, your way of solving problems and your style, so THANK YOU! I switched to Moving Average.ex4 for now as well. My conclusions are the same as yours. If the .fxt file generated by MT4 itself is used, the backtesting starts after MT4 is connected to the trade account. But it seems that I have to be a troublemaker. The .fxt file I need to use is produced by a third-party application. It contains tick price data and also the fixed spread number. The thing is that this spread number is NOT used for the backtest (MT4 always uses the spread written in the Tester window) but in spite of this fact, the tester does not wait for the connection to the trade account in this case and starts running before the connecting is finished. Where the tester finds out the value of the current (floating) spread, remains a miracle for me. I think I deleted all the files where MT4 could store prices. Thank you once again for all the research you did for us!

Don’t worry, you are not a troublemaker. I am well aware of TDS. With TDS (or files produced by TickStory), the spread is in the “.fxt” file itself using a special encoding and TDS calls the MetaTrader terminal in a way that “hacks” its internal functioning to “override” it and read the spread from the files instead of the standard way. That is probably also why it does not wait for the trade server connection, since they have always recommended running tests with the trade-server disconnected and with all contract and server data preloaded.

Since I have not used Birt’s TDS for a very long time, I don’t know if it currently has any parameters that could alter its functionality and force it to wait for the connection, so please look into that first.

If not, then the only alternative I believe should function, is the special indicator via the template file I mentioned before.

 
Also, as a side note, try contacting Birt (TDS Author) and ask about this situation. Maybe they can offer a solution or even consider adding the functionality to future versions of TDS to have an option to force it to wait for a trade-server connection.
 
Fernando Carreiro:

Don’t worry, you are not a troublemaker. I am well aware of TDS. With TDS (or files produced by TickStory), the spread is in the “.fxt” file itself using a special encoding and TDS calls the MetaTrader terminal in a way that “hacks” its internal functioning to “override” it and read the spread from the files instead of the standard way. That is probably also why it does not wait for the trade server connection, since they have always recommended running tests with the trade-server disconnected and with all contract and server data preloaded.

Since I have not used Birt’s TDS for a very long time, I don’t know if it currently has any parameters that could alter its functionality and force it to wait for the connection, so please look into that first.

If not, then the only alternative I believe should function, is the special indicator via the template file I mentioned before.

Hello Fernando,

you are a genius! I inserted a loop into the initialization section of some indicator, placed this indicator into tester.tpl and IT WORKS!! Thank you so much, I do not know how would I continue without you. I am going to order a coding of EA from you one day for sure!

 
mqleurope: you are a genius! I inserted a loop into the initialization section of some indicator, placed this indicator into tester.tpl and IT WORKS!! Thank you so much, I do not know how would I continue without you. I am going to order a coding of EA from you one day for sure!

You are welcome! Good to know you resolved the issue.