Join our fan page
- Views:
- 8439
- Rating:
- Published:
- 2012.11.15 12:14
- Updated:
- 2014.02.03 09:06
-
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance
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 Ltd.
Original code: https://www.mql5.com/ru/code/1081
![BidAskChannel](https://c.mql5.com/i/code/indicator.png)
The BidAskChannel indicator is designed to account for spread in the shadows of candles.
![Squize_MA](https://c.mql5.com/i/code/indicator.png)
The Squize_MA indicator displays the intersection of two Moving Averages with different averaging periods. The chart also features conventional flat limits.
![TradeStatisticsPanel](https://c.mql5.com/i/code/indicator.png)
The panel for the display of statistical parameters calculated based on the trade history.
![CSelectFile](https://c.mql5.com/i/code/library.png)
The file selection graphical interface class.