ExpertHistoryExample

Download script
Author: Copyright 2021, Yuriy Bykov

This script serves as a dedicated logging and record-keeping tool designed for MetaTrader users who want to maintain a structured history of their automated trading activities. Its primary purpose is to automatically archive specific data points from your trading environment into a central folder on your computer.

Core Functionality

The logic operates by capturing two distinct types of information: the specific settings you use to run your trading robot (Expert Advisor) and the actual results of those trades. Instead of relying on manual note-taking, this script systematically gathers data so you can review past performance or verify which configurations yielded the best results.

How the Logic Works

The script follows a simple three-step process to manage your data:

  1. Configuration Identification: When you start the trading robot, the script immediately identifies its key identity markers. It captures essential "input parameters"—such as the specific financial instrument (Symbol) being traded, as well as your pre-set profit targets (TP) and stop-loss limits (SL).

  2. Data Registration: During the initialization phase, the script registers these parameters into a dedicated history object. This ensures that every unique setup you use is correctly labeled before any actual trading begins.

  3. Automated Exporting: The most critical logic occurs during the testing phase (Backtesting). The script is designed to detect whether you are running a standard backtest or a large-scale optimization. If it recognizes a standard run, it triggers an "Export" command. This command writes all the collected data—the parameters you chose and the history of the deals executed—into a permanent file in your terminal's common folder.

Key Benefits for the User

  • Centralized Organization: By saving information to a common folder, it creates a consistent repository where different trading tests can be compared side-by-side.
  • Audit Trail: It provides a reliable way to track exactly what settings were active during a specific test run, which is vital for refining trading strategies and eliminating guesswork.
  • Optimization Awareness: The script is intelligent enough to distinguish between a single test and an optimization process, ensuring your records remain clean and organized without being cluttered by unnecessary data from massive automated sweeps.
2 Views
0 Downloads
0 Favorites
ExpertHistoryExample
//+------------------------------------------------------------------+
//|                                         ExpertHistoryExample.mq4 |
//|                                      Copyright 2021, Yuriy Bykov |
//|                               https://www.mql5.com/ru/code/37444 |
//|                               https://www.mql5.com/ru/code/38981 |
//|                               https://www.mql5.com/en/code/38938 |
//|                               https://www.mql5.com/en/code/38980 |
//+------------------------------------------------------------------+
#property description   "Save deals history and/or parameters of expert runs to common folder of terminal"
#property copyright     "Copyright 2021, Yuriy Bykov"
#property link          "https://www.mql5.com/ru/code/37444"
#property link          "https://www.mql5.com/ru/code/38981"
#property link          "https://www.mql5.com/en/code/38938"
#property link          "https://www.mql5.com/en/code/38980"
#property version       "1.77"
#property strict

// 0. Include library with CExpertHistory class
#include <ExpertHistory.mqh>

// 1. Create object instance in global scope
string expertName = "SomeExpert";
string expertVersion = "1.00";     // Not required

CExpertHistory expertHistory(expertName, expertVersion);



//--- input parameters
input double   SL = 500.0;
input double   TP = 1000.0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit() {
//---
   expertHistory.AddParam("Symbol", Symbol());
   expertHistory.AddParam("TP", TP);
   expertHistory.AddParam("SL", SL);
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
//---

}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {
//---

}
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester() {
//---
   double ret = 0.0;
//---
   // 3. Add in OnTester() function call of Export() method:
   
   // If you want save history only in single tester run
   // do it only if it was not optimization run 
   if(!MQLInfoInteger(MQL_OPTIMIZATION)) { 
      expertHistory.Export();
   }
//---
   return(ret);
}
//+------------------------------------------------------------------+

Comments