Join our fan page
TesterBenchmark - library for MetaTrader 5
- Views:
- 3578
- Rating:
- Published:
- 2017.11.03 11:53
- Need a robot or indicator based on this code? Order it on Freelance Go to Freelance
When writing different code versions, you may need to measure their impact on the overall performance of the Expert Advisor in the tester. This allows you to understand how optimal the code is, compared to other programs, and provides prerequisites for further fast optimization of Expert Advisors. This approach allows us to identify the "bottleneck" in the EA performance.
The MetaTrader 5 provides an excellent function for profiling Expert Advisors on historical data. But it has some disadvantages: it is slow (in the visual mode), and the final result is presented in relative units, i.e. it is not possible to compare performance in absolute terms.
Also, in MetaTrader 5, the EA is executed on a local Agent even during a single run without visualization. Sometimes a large part of testing time is taken to synchronize the terminal with a corresponding local agent. Therefore, time data from the following lines
Core 1 EURUSD,M1: 3387955 ticks, 52608 bars generated. Environment synchronized in 0:00:01.389. Test passed in 0:00:09.422 (including ticks preprocessing 0:00:00.187). Core 1 EURUSD,M1: total time from login to stop testing 0:00:10.811 (including 0:00:01.389 for history data synchronization)
are a poor representation of the net EA/tester performance, and can differ much in different runs.
The net operation time of the Strategy Tester is the time from the first Tick event (the first OnTick) till the last tick in the tested interval. Immediately after this interval, the tester calls OnTester (followed by OnDeinit).
The net time of a testing run in the Strategy Tester
The library allows adding to the EA code one line
#include <TesterBenchmark.mqh>
and to obtain in logs the data on the tester's net performance.
Core 1 2017.07.21 23:59:59 Interval = 8.842 s., Count = 3387955, 383166.1 unit/sec Core 1 EURUSD,M1: 3387955 ticks, 52608 bars generated. Environment synchronized in 0:00:01.389. Test passed in 0:00:09.422 (including ticks preprocessing 0:00:00.187). Core 1 EURUSD,M1: total time from login to stop testing 0:00:10.811 (including 0:00:01.389 for history data synchronization)
Moreover, if in the tester you switch from a single run to an optimization mode (there is no need to specify optimization ranges for MetaTrader 5), the library will run the EA for the specified number of times and will generate the tester performance statistics, based on which you can understand the pure performance of the tester.
------ OnTesterInit i = 0 Pass = 0 OnTester = 8.687 s.: Count = 3387955, 390002.9 unit/sec, Agent = C:\Program Files\Alpari Limited MT5\Tester\Agent-127.0.0.1-3000 i = 1 Pass = 1 OnTester = 8.702 s.: Count = 3387955, 389330.6 unit/sec, Agent = C:\Program Files\Alpari Limited MT5\Tester\Agent-127.0.0.1-3000 iMin = 0 Results[iMin] = 8.687s. iMax = 1 Results[iMax] = 8.702s. Amount = 2 Mean = 8.694 s. - 83.89% OnTesterDeinit ------ Interval = 20.729 s., Count = 0, 0.0 unit/sec
In this case, it is clear that there were two starts on the same local Agent. The minimum, maximum and average calculation time is shown. The total time of optimization (Interval) is also shown. The percent characteristics (83.89%) shows how much of the total optimizer time is taken on the average by the pure operation of the tester (time required for synchronization with Agents is taken into account).
OnTick profiling
By adding one more line
#define PROFILER_OnTick // Measures the net time of all OnTick executions, may slightly slows down the total operation #include <TesterBenchmark.mqh>
you can see time spent on the execution of OnTick, not taking into account time taken to simulate trading environment, performance, etc.
------ OnTesterInit i = 0 Pass = 0 OnTester = 9.540 s.: OnTick Profiler: Count = 3387955, Interval = 8.079 s., 419359.4 unit/sec , Agent = C:\Program Files\Alpari Limited MT5\Tester\Agent-127.0.0.1-3000 i = 1 Pass = 1 OnTester = 9.471 s.: OnTick Profiler: Count = 3387955, Interval = 8.029 s., 421956.9 unit/sec , Agent = C:\Program Files\Alpari Limited MT5\Tester\Agent-127.0.0.1-3000 iMin = 1 Results[iMin] = 9.471s. iMax = 0 Results[iMax] = 9.540s. Amount = 2 Mean = 9.505 s. - 98.86% OnTesterDeinit ------ Interval = 19.231 s., Count = 0, 0.0 unit/sec
Example
The library is provided with an example EA (MQL4/5) with a header, which helps to understand one of the practical applications of the library.
#include <TesterBenchmark.mqh> // If both include lines below are commented, then the trade logic is written in pure MQL5 - trade API // Otherwise, an uncommented line is the API wrapper that is used. // TesterBench shows the performance of each of the three APIs. // #include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006 // #include <Trade\Trade.mqh>
Translated from Russian by MetaQuotes Ltd.
Original code: https://www.mql5.com/ru/code/18804
The script saves current chart settings to a template with the specified name.
ReportThe MetaTrader 4/5 library allows generating reports based on the trading history.
The indicator displays trend power and direction based on four moving averages.
Mikahekin_HTFThe Mikahekin indicator with the timeframe selection option available in input parameters.