Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2012.11.15 12:14
Libraries

CTradeStatistics - library for MetaTrader 5

| English Русский 中文 Español Deutsch 日本語 Português

Views:
3082
Rating:
votes: 28
\MQL5\Scripts\\MQL5\Include\

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

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());

  }

Translated from Russian by MetaQuotes Software Corp.
Original code: https://www.mql5.com/ru/code/1081

Accelerator Oscillator (AC) Accelerator Oscillator (AC)

The Acceleration/Deceleration Indicator (AC) measures acceleration and deceleration of the current driving force.

Average Directional Movement Index (ADX) Average Directional Movement Index (ADX)

The Average Directional Movement Index Indicator (ADX) helps to determine if there is a price trend.

Holt's double exponential smoothing trend Holt's double exponential smoothing trend

Holt's double exponential smoothing - the trend component (mt4 version).

Holt's double exponential smoothing (mt4 version) Holt's double exponential smoothing (mt4 version)

Holt's double exponential smoothing with linear forecasting.