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

CTradeStatistics - library for MetaTrader 5

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

votes: 32

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


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

//| Expert deinitialization function                                 |
void OnDeinit(const int reason)


//| Expert tick function                                             |
void OnTick()


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;

//--- update statistics


Translated from Russian by MetaQuotes Software Corp.
Original code:

BidAskChannel BidAskChannel

The BidAskChannel indicator is designed to account for spread in the shadows of candles.

Squize_MA Squize_MA

The Squize_MA indicator displays the intersection of two Moving Averages with different averaging periods. The chart also features conventional flat limits.

TradeStatisticsPanel TradeStatisticsPanel

The panel for the display of statistical parameters calculated based on the trade history.

CSelectFile CSelectFile

The file selection graphical interface class.