CTradeStatistics – library MetaTrader 5

The CTradeStatistics class contains all ENUM_STATISTICS enumeration parameter calculations.

All parameters, except for equity drawdown, can in this class be calculated based on the trade history.

The main purpose of the class is to remove the TesterStatistics() function restriction, i.e. to be able to get the necessary statistical data at any time during testing, as well as outside of the strategy tester.

An example of using outside of the tester

All parameters are calculated by calling the CTradeStatistics::Calculate() function. After the successful execution of this function, the results are available through the calls of numerous methods, their names being similar to the names of statistical parameters. 

Code example:

CTradeStatistics m_stat;

if(m_stat.Calculate()) PrintFormat("LR Correlation: %.2f",m_stat.LRCorrelation());
else Print(m_stat.GetLastErrorString());

Result:

2012.09.13 08:52:19 TradeStatistics (EURUSD,H1) LR Correlation: 0.97

 

The CTradeStatistics::PrintStatistics() function can be used to print all parameters to the log.

if(m_stat.Calculate()) PrintStatistics();

A simple example of working with the class can be found in the TradeStatistics.mq5 script.

The TradeStatisticsPanel panel is designed for better visualization of results. 

An example of using in the tester

Alternative:   20PRExp-3 - EA MetaTrader 5

To correctly calculate equity drawdown, the CTradeStatistics::CalculateEquityDD() function should be used.

Code example:

#include <CTradeStatistics.mqh>
CTradeStatistics m_stat;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   m_stat.CalculateEquityDD(CALC_INIT);

   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   m_stat.CalculateEquityDD(CALC_DEINIT);

   if(m_stat.Calculate())m_stat.PrintStatistics();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   m_stat.CalculateEquityDD(CALC_TICK);

  }

It should be noted that reliable values of statistical indicators can be obtained at any point in the program and not only at the end of testing using the OnTester() or OnDeinit() events. Calculations can be updated using the OnTrade() event as shown in the following example:

void OnTrade()
  {
//--- block repeated requests at same sec.
   static datetime time_on_trade;
   if(time_on_trade==TimeTradeServer())return;
   time_on_trade=TimeTradeServer();

//--- update statistics
   if(!m_stat.Calculate())Print(m_stat.GetLastErrorString());

  }


📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks