Code for recording balance and equity charts and calculating additional optimization criteria - MetaTrader 5 Library - MT4/MT5 Resources









I decided to share this code from a big project designed in a separation library .
If you have access to the Expert Advisor code, you can save balance and equity charts and calculate other optimization criteria by adding other codes from this library.
Download the Advanced Optimization Report Saver.mqh file from the attached link and save it to the terminal folder \MQL5\includes\
Add the code to connect to the downloaded file: (You can add this below the last line of Expert Advisor code or anywhere else)
#Include <Advanced Optimization Report Saver.mqh>You then need to add calls to the exported functions to the existing functions in your Expert Advisor: (if there are no functions, add them)
Blank check (){ //...your code //Save balance and net worth charts_OnTick( time current (), account information double ( account balance ), account information double ( account assets )); } Blank tester initialization () { //...your code save_OnTesterInit (); //Print to chart "Do not close this window until optimization is complete!" } BlankOnTesterDeinit (){ //...your code save_OnTesterDeinit(); // get frame } double tester () { //...your code // Save any custom values: double avg pips = 0 ; save_Add(AvgPips, 2 , «custom1» , true ); //values, numbers, names, sort_min_to_max save_add( teststat ( statprofit ), 2 , «custom2» ); double custom = 0 ; //your custom fitness save chart (custom); //save final equity, all trades... return custom; }
The save_Add function allows you to add what you want included to your report :
save_Add(AvgPips, 2 , "Customization 1" , true ); //Value, number, name, sort_min_to_max save_add ( test statistics ( statistical profit ), 2 , "Customization 2" );
For example, we add this code to the Moving Average Expert Advisor from the examples folder (...\MQL5\Experts\Examples\Moving Average\ Moving Average.mq5 ). First, copy the original Expert Advisor and name it Moving Average Chart.mq5 . Now add the code from the above instructions to it.
This change is only at the very end of the code, starting from the OnTick() function after the check on() ; line:
Blank check ( blank ) { //--- if (select position()) checkClose(); Other checks open(); //Save balance and net worth charts Save_OnTick ( time current (), account information double ( account balance ), account information double ( account assets )); } BlankTesterInitialization ( ) { //...your code save_OnTesterInit (); //Print to chart "Do not close this window until optimization is complete!" } BlankOnTesterDeinit (){ //...your code save_OnTesterDeinit(); // get frame } double tester (){ //...your code //save any custom values: double average points = 0 ; save_Add(AvgPips, 2 , "Customization 1" , true ); //Value, number, name, sort_min_to_max save_add ( test statistics ( statistical profit ), 2 , "Customization 2" ); double custom = 0 ; //your custom fitness save chart(custom); //save final equity, all trades... return custom; } #Include <Advanced Optimization Report Saver.mqh>
As you can see, it's very easy and only takes a few minutes.
Afterwards you can run the optimization:


The last screenshot shows that the connected code adds 2 parameters:
Once the optimization is complete, you can create a report (using a program that parses the saved frames) and view the following:
100 row movement period

10 lines with mobile shift

If there are more than 20 rows, only 3 rows of averages are displayed: 3 rows at the top and 3 rows at the bottom.
100 lines are difficult to perceive, so you need to reduce their number.
Instead of using input variables for the same step, you can scale via an enumerator, for example: 1,2,3,5,7,10,15,20,30,50,70,100 - only 12 rows instead of 100.
Enumeration nums1 {__0= 0 , __1= 1 , __2= 2 , __3= 3 , __5= 5 , __7= 7 , __10= 10 , __15= 15 , __20= 20 , __30= 30 , __50= 50 , __70= 70 , __100= 100 , __150= 150 , __200= 200 , __300= 300 , __500= 500 }; Enter nums1 moving period = 10 ; // Moving average periodYou can download the Expert Advisor information in the enumeration version attachment.
The number of optimizations will no longer be 1000, but 120, the calculation time will be about 8 times faster, and the file size will be about 8 times smaller.
The results are even more obvious:

The result with MovingPeriod = 15 immediately stands out, with the highest average and highest maximum.
However, as is often the case, the best results from backtesting are not the best results from forward testing.
A program that parses saved frames can display a graph like this:

There are additional standards like this:
If you have code for other interesting standards, you can suggest adding them to the library.
Attachment download
📎 Moving_Average_Charts_Enum.mq5 (17.49 KB)
📎 Advanced_Optimization_Report_Saver.mqh (98.54 KB)
Source: MQL5 #68706
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •