Visualization of real-time optimization results - MetaTrader 5 Expert | MT5 EA Download - MetaTrader 5 Resources






MetaTrader 5 strategy tester allows obtaining statistical parameters of the trading system after each test pass.
Statistical parameters can be obtained using the test statistics() function, which calls the OnTester() and OnDeinit() functions internally.
Functions for handling optimization results were introduced ( FrameFirst() , FrameFilter() , NextFrame() , FrameInput() and FrameAdd() ) and traders are now able to perform visual optimizations. Necessary data can now be processed and displayed during Expert Advisor optimization or mathematical problem solving.
We will consider the details of the example of visualization of Expert Advisor test results during optimization described in the article "Visualizing strategies in the MetaTrader 5 tester" .
1. Visualization of equilibrium dynamics during optimization
1.1.Download files
Moving Averages_With_Frames.mq5 Expert Advisor should be copied to terminal_data_folder\MQL5\Experts.
They should be copied to terminal_data_folder\MQL5\Include.
Library programs can be downloaded automatically through the MetaTrader 5 terminal (Library tab of the "Toolbox" window). To do this, select Download. All code will be downloaded and placed in its folder.

Figure 1. Download the CodeBase program through the MetaTrader 5 terminal
1.2. Test EA trading
Moving Averages_With_Frames.mq5 Expert Advisor should be launched in the strategy tester.

Figure 2. Moving Averages_With_Frames.mq5 test setup
Optimization options:

Figure 3. Moving Averages_With_Frames.mq5 test setup
During the optimization process, the Expert Advisor will display balance dynamics and optimization process statistics:

Figure 4. Moving Averages_With_Frames.mq5 optimization
Optimization process:
1.3. Principles of EA trading
Moving Averages_With_Frames.mq5 Expert Advisor is created based on Moving Averages.mq5 included in the standard MetaTrader 5 terminal package (MQL5\Experts\Examples\Moving Average).
//--- Connect the code used to process the optimization results #include//--- Frame Generator CFrameGenerator fg; //+----------------------------------------------------------------------------------+ //|Test function | //+------------------------------------------------------------------+ Double OnTester() { // - - Insert your optimization criteria calculation function here double tester criteria = math AB ( teststat ( STAT_SHARPE_RATIO )* teststat ( stat_profit )); Tester Criteria = Test Statistics ( Statistical Profit ) > 0 ?TesterCritetia:(-TesterCritetia); //--- Call and submit the optimization criteria as parameters after each test fg.OnTester(TesterCritetia); //--- Return (TesterCritetia); } //+------------------------------------------------------------------+ //| TesterInit function | //+------------------------------------------------------------------+ blankOnTesterInit () { //--- Prepare the chart to display multiple balance lines fg.OnTesterInit( 3 ); // Parameter sets the number of balance lines on the chart } //+----------------------------------------------------------------------------------+ //| TesterPass function | //+------------------------------------------------------------------+ blankOnTesterPass () { //--- Process the obtained test results and display the graphics fg.OnTesterPass(); } //+------------------------------------------------------------------+ //| TesterDeinit function | //+------------------------------------------------------------------+ BlankOnTesterDeinit () { //--- End of optimization fg.OnTesterDeinit(); } //+------------------------------------------------------------------+ //| Chart event handling | //+------------------------------------------------------------------+ Blank chart event ( constant integer ID, constant long &l parameter, constant double &d parameter, constant string &s parameter) { //--- Start the frame display after the optimization is completed when the title is clicked . chart event (id, lparam, dparam, sparam, 100 ); // 100 is the pause between shots (in milliseconds) } //+----------------------------------------------------------------------------------+
This code can be used similarly in any Expert Advisor.
The CFrameGenerator class is used to process optimization results. This class contains the following methods for handling strategy tester events ( OnTester() , OnTesterInit() , OnTesterPass() , OnTesterDeinit() ) and chart events ( OnChartEvent() ). The graphics object used is prepared in the OnTesterInit() method of the CFrameGenerator class.
Each time a test passes, the OnTester() method of the CFrameGenerator class is called on the test agent. This method provides dynamic calculation of the balance curve based on transaction results. The balance graph is a one-dimensional array whose length depends on the number of executed transactions.
Then, the teststatistics() function is used to request statistics (net profit, profit factor, recovery factor, number of trades, number of trades, maximum drawdown percentage, custom optimization criteria values) and generate a frame (in our case an array of test pass data) that is sent (from the test agent) to the terminal frame add() function.
Frames are received and processed in the OnTesterPass() method of the CFrameGenerator class. Frame reading operations are performed by the NextFrame() function. The request for the EA input parameters of the generated frame is executed using the FrameInput() function.
After getting the data, use CSimpleTable, CColorProgressBar and CSpecialChart class methods to display and update the data.
It should be noted that the OnTester() handler function is executed on the test agent after completing another current optimization process. This allows using the MQL5 Cloud Network distributed network to be able to perform complex mathematical calculations. To do this, place the calculation part into the OnTester event handler.
An example of using optimization to speed up mathematical calculations (continuous wavelet transform Weierstrass function ) is shown in the topic "Manage optimization processes in real time and transfer large amounts of data from brokers to MetaTrader 5" .
2. Examples using CSimpleTable, CColorProgressBar and CSpecialChart classes
Attached Test_SimpleTable.mq5 and Test_CSpecialChart.mq5 scripts contain examples of working with CSimpleTable, CColorProgressBar and CSpecialChart classes. They should be copied to terminal_data_folder\MQL5\Scripts.

Figure 5. Test_SimpleTable.mq5 script running results

Figure 6. Test_CSpecialChart.mq5 script running results
Attachment download
📎 specialchart.mqh (7.71 KB)
📎 simpletable.mqh (10.78 KB)
📎 framegenerator.mqh (15.08 KB)
📎 colorprogressbar.mqh (4.89 KB)
📎 test_simpletable.mq5 (2.52 KB)
📎 test_cspecialchart.mq5 (2.19 KB)
📎 moving_average_with_frames.mq5 (8.67 KB)
Source: MQL5 #914
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •