# MQL4: Calculation of statistical coefficients and their use for optimization of advisers Greetings, friends!

There are quite a few different statistical coefficients that reflect one or another aspect of the quality of the trading system. And what will happen if we optimize the TS based on the values ​​of any of these coefficients? Today we will deal with this issue in a new programming lesson.

## Custom optimization criterion Fortunately, MetaTrader provides the ability to test advisors using custom optimization criteria. You can find this function in the "Expert Advisor Properties" on the "Testing" tab: The parameter itself, with respect to which the optimization takes place, is calculated in the body of the expert using the special onTester () method.

## Squid ratio Here is a calculation of the classic squid coefficient:

double OnTester () {double AvProfit = 0; double Kalmar = 0; if (TesterStatistics (STAT_PROFIT_TRADES)> 0) {AvProfit = NormalizeDouble (TesterStatistics (STAT_GROSS_PROFIT) / TesterStatistics (STAT_TRADES), Digits); Kalmar = NormalizeDouble (AvProfit / TesterStatistics (STAT_BALANCE_DD), Digits); } return (Kalmar); }

This coefficient was specially invented to evaluate the effectiveness of a particular trading strategy of a trader. According to many investors, it solves rather complex problems when choosing an investment object.

First squid coefficient was presented in one of the most famous futures exchange magazines by the author of the trust and investment in hedge funds column, Terry Young. This indicator is based on a concept that is well known to traders as drawdown.

The main disadvantage of this indicator is that the risk is determined by only one single event (maximum drawdown), thus reducing its statistical significance and representativeness. Using maximum drawdown as a single risk assessment can lead to bias in evaluating results due to emissions. Therefore, you can slightly modify the formula by entering some additional data into it:

double OnTester () {double AvProfit = 0; double Kalmar = 0; if (TesterStatistics (STAT_PROFIT_TRADES)> 0) {AvProfit = NormalizeDouble (TesterStatistics (STAT_GROSS_PROFIT) / TesterStatistics (STAT_TRADES), Digits); } Kalmar = NormalizeDouble (-AvProfit * (TesterStatistics (STAT_CONPROFITMAX_TRADES) / TesterStatistics (STAT_TRADES)) / (TesterStatistics (STAT_MAX_LOSSTRADE) * (TesterStatistics (STAT_CONLOSSMAX_TRADES)); return (Kalmar); }

## Sortino coefficient Many different metrics are used to evaluate trading systems. Each of them is aimed at identifying one or another factor and one of such indicators is the Sortino coefficient.

Sortino coefficient it is customary to use in cases where we are interested in the spread of negative values ​​of returns. The calculation method is very similar to the calculation of the Sharpe ratio. If both positive and negative returns are used for the Sharpe ratio, then only negative values ​​are used for the Sortino coefficient.

It is worth noting that Harry Markowitz, who developed the modern theory of the portfolio, noted the importance of using negative deviations as a measure of risk. A positive return always has a positive effect for an investor, but a negative one represents a negative impact and needs to be studied.

Let's write a code that will calculate and return the value of the Sortino coefficient:

double OnTester () {double AvProfit = 0; double Sortino = 0; double AvLoss = 0; double MaxLoss = 0; if (TesterStatistics (STAT_PROFIT_TRADES)> 0) AvProfit = NormalizeDouble (TesterStatistics (STAT_GROSS_PROFIT) / TesterStatistics (STAT_PROFIT_TRADES), Digits); AvLoss = NormalizeDouble (TesterStatistics (STAT_GROSS_LOSS) / TesterStatistics (STAT_LOSS_TRADES), Digits); MaxLoss = NormalizeDouble (TesterStatistics (STAT_MAX_LOSSTRADE), Digits); Sortino = NormalizeDouble (((AvProfit-MaxLoss) / - AvLoss), Digits); return (Sortino); }

## Trainor coefficient Trainor coefficient (Treynor 1965) is also called the reward to volatility ratio and represents the ratio of excess returns to market risk. In contrast to the Sharpe ratio, in this indicator, profitability does not correlate with general risk, but only with systematic (non-diversifiable).

The higher the values ​​of the Trainor indicator, the more efficiently the investment portfolio is managed, therefore, the strategies that have the highest values ​​of the Trainor indicator are selected. Typically, this indicator is used to build portfolio ratings.

Let's take a look at the code:

double OnTester () {double AvProfit = 0; double Treynor = 0; double AvLoss = 0; double MaxProfit = 0; if (TesterStatistics (STAT_PROFIT_TRADES)> 0) AvProfit = NormalizeDouble (TesterStatistics (STAT_GROSS_PROFIT) / TesterStatistics (STAT_PROFIT_TRADES), Digits); AvLoss = NormalizeDouble (TesterStatistics (STAT_GROSS_LOSS) / TesterStatistics (STAT_LOSS_TRADES), Digits); MaxProfit = NormalizeDouble (TesterStatistics (STAT_MAX_PROFITTRADE), Digits); Treynor = NormalizeDouble (((AvProfit-MaxProfit) / - AvLoss), Digits); return (Treynor); }

## Sharpe Ratio Sharpe Ratio came up with the famous American economist - William Sharp. Today it is one of the most commonly used indicators of the risk to return ratio. You can read more about the Sharpe ratio in a separate article. Well, we look at the code:

double OnTester () {double AvProfit = 0; double Sharp = 0; double ObLoss = 0; double MaxProfit = 0; if (TesterStatistics (STAT_PROFIT_TRADES)> 0) AvProfit = NormalizeDouble (TesterStatistics (STAT_GROSS_PROFIT) / TesterStatistics (STAT_PROFIT_TRADES), Digits); MaxProfit = NormalizeDouble (TesterStatistics (STAT_MAX_PROFITTRADE), Digits); ObLoss = NormalizeDouble (TesterStatistics (STAT_GROSS_LOSS), Digits); Sharp = NormalizeDouble (((AvProfit-MaxProfit) / - ObLoss), Digits); return (Sharp); }

## Conclusion Thus, you can optimize any statistics of your trading experts, using formulas of various generally accepted statistical coefficients, as well as writing your own.