Русский 中文 Español Deutsch 日本語 Português
Risk Evaluation in the Sequence of Deals with One Asset. Continued

Risk Evaluation in the Sequence of Deals with One Asset. Continued

MetaTrader 5Statistics and analysis | 24 January 2018, 14:56
19 758 0
Aleksey Nikolayev
Aleksey Nikolayev

Table of Contents

Foreword

Let us continue studying the theory of risk estimation that started in the previous article. It will be brought to a logical conclusion on two examples. Also, other possible applications of the probability theory and mathematical statistics will be briefly outlined.

Assumptions about prices and trading systems

Can the methods considered in the previous article be used for analyzing trade results of any system? For this, we must be certain that the sequence of its deals is a sequence of independent and identically distributed variables. Unfortunately, however, practice shows that this is not always the case.

Both conditions may be violated: both being independent and identically distributed. In the book “The Mathematics of Money Management”, Ralph Vince outlines ways to solve these problems. He suggests splitting the systems into several parts (subsystems), which meet the required conditions. Vince gives two examples. The first one discusses a system that has a dependence between subsequent deals. In this case, the sequence is divided into two parts. Deals with even and odd numbers are treated as received from different systems. The second example considers a system with pyramiding — stepwise increase of the position volume. Each step of such increment is considered as a separate system. This results in a collection of multiple systems: the first one consists of the first steps, the second one contains the second steps, and so on.

Once such system "pieces" are studied, it is necessary to find a way to reassemble them, to create a "microportfolio" that takes the dependencies between them into account. This will require something similar to the Markowitz's Modern Portfolio Theory.

This looks quite complicated already, but there is still more to go. Consider a system which exits each position at one of the predetermined price levels: pstoploss or ptakeprofit. The ratio k=|ptakeprofit-penter|/|pstoploss-penter| varies from deal to deal depending on the specific indicator. The yield distribution in each deal will be very simple — a discrete distribution with two values -1 and k, taken with certain probabilities. But since the value of k is specific to each deal, different deals will have different distributions as well. Thus, when attempting to split the system into "pieces", their number will be equal to the number of deals, which is obviously meaningless. In practice, the used value of k is most likely to be the same for all deals (some average, characteristic value). This allows performing the analysis with a simplified version of the system used in real trading.

Summarize all the possible options for using the theory of risk for real trading systems:

  1. Split the trading system into several parts.
  2. Study the simplified analog of the system.
  3. Analyze the sequence of deals without checking for independence and identicalness of the distributions. At the same time, it is possible to slightly adjust the obtained risk value using other analysis methods.

This approach to studying systems is obviously not universal and is not always applicable. Let us try to make the method more systematic. This will require making several assumptions about the prices and systems.

  1. A price series is close to random walk. We will not attempt to mathematically formulate the definition of this closeness, but only assume that if certain calculations are performed on a random walk and on real prices, the results of the former can be considered as the zero approximation of the results of the latter. For example, this is how the assumptions about distributions of random variables obtained from a price series will be made.
  2. The system can be fed the quotes generated by a random walk. In this case, the random prices must be close to real ones "on average". This means a similar volatility on the higher timeframes, a similar distribution of price spikes on ticks, etc.
  3. The structures of dependence between the deals of the system (real prices and random walks) are close in both cases, while the yield distributions may differ greatly. For example, if all yields increase by the same number, the distributions will change (shift to the right). But the coefficients of correlation between the yields will remain the same.
  4. The system is arranged in such a way that its deals are identically distributed on a random walk and can be significantly interdependent only if they greatly overlap in time (the second one is opened before the first one is closed).

Let us describe these assumptions in more detail. A random walk is an extensive class of random processes. It can be discrete or continuous. Both types are used in analytical calculations, while modeling on a computer uses only the first. These types are related — a random walk with continuous time can be obtained from a discrete one by passage to the limit as the time step tends to zero. Also, the random walk can be either symmetric (no trend, flat) or have a positive/negative shift (up/downtrend). The symmetric random walk is considered the most often. In this case, the assumption about the prices leads to the following:

  • Trade results cannot be predicted by any indicators. This greatly simplifies the modeling. It turns out that it is necessary to study only the part of the system responsible for exiting deals. Where and how the deal had been opened is completely irrelevant.
  • On average, the profit is zero. This does not prevent any single deal from obtaining any amount of profit. But the more deals are opened, the more the aggregate of their results corresponds to a certain probability distribution. The specific form of this distribution depends on the market exit algorithm, but its expected value is always zero.

These corollaries must be taken into account when testing (and especially optimizing) real systems using real prices. There is a need for criteria showing that the results are obtained not for a symmetric random walk with sufficient reliability. This applies both to distribution of deal yields and the degree of utility of technical indicators. In addition, the search for deviations from a symmetric random walk can also be useful when searching for ideas for trading systems. As an example, we will later consider the possibility of using the statement "gaps always get filled".

Aforementioned assumptions impose restrictions on trading systems. In general, they require simplicity, uniformity and transparency in the market entry and exit rules. These rules should not additionally strengthen the relationship between deals (for example, refer to the equity curve), should not arbitrarily change the exiting methods from deal to deal (for example, randomly change the take profit/stop loss ratio), the deals themselves should not be too frequent, etc. The approximate fulfillment of these conditions can be either checked theoretically or modeled on a computer using the Monte Carlo method.

Yield distributions. General remarks

We begin with a remark on the general form of distributions. Whenever the concept of probability distribution function is introduced, the discrete and continuous types are usually implied. It turns out that in addition to those, their mixtures are also possible. That is, distribution functions in the form P(x)=γcPc(x)+γdPd(x) are possible, where γc≥0, γd≥0, γcd=1, while Pc(x) and Pd(x) are continuous (having a density) and discrete (stepwise) distribution functions, respectively.

As an example, consider a simple exit method — fixed stop loss and take profit at a ratio of k, supplemented by a limit on the position holding time of texit. Simply put, deals with the exit based on take profit/stop loss have a discrete distribution, and those with the exit by time have a continuous distribution. The overall distribution is their mixture. Naturally, this complicates further discussion. Let us make a simplifying assumption: suppose that texit is large enough for γc to be considered negligible. Assume that P(x)=Pd(x).

Exiting a trade using a fixed trailing stop is an example of the case γd=0. It is also possible that γd≠0 and γс≠0 at the same time. For example, this can be a combination of both examples considered. With this method, the stop loss is immovable during the initial period of the deal, but once a certain condition is met, it starts moving similar to trailing stop.

Let us concretize the risk theory for both examples considered above (the combined variant will not be considered). Scripts for calculating the optimal risk value ropt for each specific case of yield distribution will be provided below. But sometimes other tasks are of interest. For example, the risk is set, and it is necessary to calculate δ — the corresponding level of reliability (or the maximum available value of the average yield G0, as an option). In this case, it is necessary to either modify the script, or to run it with different input data, achieving the desired risk value.

Fixed stop loss and take profit

As it was mentioned above, the distribution will be considered to be discrete. The trade yields can take two values: -1 and k, with probabilities P-1 and Pk, respectively. By the formula of total probability, P-1+Pk=1. The distribution function has a stepwise form with two jumps at points -1 and k, with magnitudes P-1 and Pk. The expected value of the distribution is M=-1*P-1+k*Pk. The notation θ=Pk is introduced, leading to P-1=1-θ, M=(1+k)θ-1. Thus, we are dealing with a parametric family of distributions, defined by parameters k>0 and 0≤θ≤1. In case of a symmetrical random walk, M=0, leading to θ=θ0=1/(1+k).

Let there be ai — yields of n deals, with each equal to either -1, or k.

  1. Calculate the estimate for the expected value M≈Mapr=(a1+a2+...+an)/n=(1+k)nk/n-1, where nk is the number of profitable trades. If Mapr≤0, then reject the trading system as unprofitable. There are no risk management tricks that can turn an unprofitable system into a profitable one. If Mapr>0, proceed to the next step.
  2. It is necessary to show that the trades are not a result of trading on a symmetrical random walk. For this, the estimate Mapr must be significantly greater than zero (with reliability δ). Take the assertion that M=0 as the null hypothesis. Test statistic will be the variable S=n(Mapr+1)/(1+k)=nk, which has a standard binomial distribution B(n,θ0) when the null hypothesis is satisfied. Denote its distribution function by P(x). To accept the null hypothesis, it is necessary to make sure that P(S)>P(nθ0)+δ/2. If this is successful, proceed to the next step. If not, reject the trading system.
  3. Check if the required condition is met for the selected value of the minimum average yield G0. For this, G0 should not be greater than the δ-quantile of qδ distribution of random variable Λ, introduced in the first part (arithmetic mean of yields). The variable Λ coincides with Mapr, expressed though S. Now the variable S is considered to be distributed according to the binomial law with parameters n and θapr=nk/n. If the condition is satisfied, proceed to the next step. If not, determine the minimum of deals n=nmin required for the condition to be met (incrementally increase n for a fixed θapr up to the value at which the required inequality is satisfied).
  4. Using Monte Carlo method, obtain an empirical distribution ρopt. To do this, simulate a large number of sets with n trade yields in each. Each yield is equal to k with probability Pk, and is equal to -1 with probability 1-Pk. For each set, determine the value ropt,i using the method discussed in the introduction to the first part. In total, they determine the empirical distribution of the random variable ρopt. To estimate the risk ropt, use the sample δ quantile of this distribution.

#include <Math\Stat\Binomial.mqh> #property script_show_inputs input uint na=50; // number of deals in the series input double kt=3; // ratio of take profit to stop loss input double pk=0.5; // probability of profit #define NX 300 // the maximum number of deals in a series (to search for nmin) #define NB 10000 // the number of generated samples for the Monte Carlo method #define NR 1000 // the number of interval partitions for the risk double G0=0.15; // the lowest average yield double D0=0.9; // the lowest minimal gain double dlt=0.05; // significance level void OnStart()   {    if (D0<=0||D0>=1) {Print("D0 must be positive and less than one"); return;}    double m=(1+kt)*pk-1; // expected value    if(m<=0) {Print("expected value must be positive"); return;}    double s=na*pk; // statistics (average number of profitable deals)    double s0=na/(1+kt); // average number of profitable deals at zero expected value    int ner;    double p=MathCumulativeDistributionBinomial(s,na,1/(1+kt),ner);    if(!MathIsValidNumber(p)) {Print("MathIsValidNumber(p) error ",ner); return;}    double p0=MathCumulativeDistributionBinomial(s0,na,1/(1+kt),ner);    if(!MathIsValidNumber(p0)) {Print("MathIsValidNumber(p0) error ",ner); return;}    if(p<=p0+dlt/2) {Print("expected value is close to zero"); return;}    double q=MathQuantileBinomial(dlt,na,pk,ner),q0=na*(G0+1)/(kt+1);    if(!MathIsValidNumber(q)) {Print("MathIsValidNumber(q) error ",ner); return;}    if(q0>q)      {       Print("G0 is greater than dlt quantile of the arithmetic mean of the yield");       uint nmin=na+1;       for(;nmin<=NX;++nmin)         {          q=MathQuantileBinomial(dlt,nmin,pk,ner); q0=nmin*(G0+1)/(kt+1);          if(!MathIsValidNumber(q)) {Print("MathIsValidNumber(q) error ",ner); return;}          if(q0<=q) break;         }       if(nmin<=NX) Print("nmin = ",nmin);       else Print("nmin is greater than ",NX);       return;      } // the Monte Carlo method    MathSrand(GetTickCount());    double a[],c[],ropt[NB],A0,A,r,g,d,cn1,cn2,stp=1.0/NR;    uint ir;    ArrayResize(a,na);    ArrayResize(c,na);    for(uint ib=0; ib<NB;++ib)      {       for(uint i=0; i<na;++i) if(MathRandomNonZero()<=pk) a[i]=kt; else a[i]=-1;       A=MathMean(a); A0=a[ArrayMinimum(a)];       if(A<G0) {ropt[ib]=0; continue;}       if(A0>0) {ropt[ib]=1-stp; continue;}       cn1=1;       for(ir=1;ir<NR;++ir)         {          r=ir*stp;          cn2=1; for(uint i=0; i<na;++i) cn2*=1+r*a[i];          if(cn2<cn1) {ropt[ib]=r-stp; break;}          cn1=cn2;          g=(MathPow(cn2,1.0/na)-1)/r;          if(g<G0) {ropt[ib]=r-stp; break;}          c[0]=1+r*a[0]; for(uint i=1; i<na;++i) c[i]=c[i-1]*(1+r*a[i]);          d=dcalc(c);          if(d<D0) {ropt[ib]=r-stp; break;}         }      }      ArraySort(ropt);      uint nr=(uint)(dlt*NB);      Print("ropt = ",ropt[nr]);   } // the dcalc() function accepts the array of c1, c2, ... cN values and // returns the minimal gain d. Assume that c0==1 double dcalc(double &c[])   {    if(c[0]<=0) return 0;    double d=c[0], mx=c[0], mn=c[0];    for(uint i=1; i<na;++i)      {       if(c[i]<=0) return 0;       if(c[i]<mn) {mn=c[i]; d=MathMin(d,mn/mx);}       else {if(c[i]>mx) mx=mn=c[i];}      }    return d;   } //+------------------------------------------------------------------+

With the default arguments, the script calculates the following risk value:

  • ropt = 0.011

This corresponds to 1.1% of the capital allocated to the system risked in each deal.


Fixed trailing stop

Assume that there is a buy deal (the changes necessary for the sell deals will be intuitive). Denote the difference between the entry price and the initial stop loss by Δp=penter-pstop. The deal will be closed once the price reaches the value pmaxp, where pmax is the maximum price since the deal has been opened. In case the price series is modeled by a random walk with continuous time and linear shift, the yield distribution can be found in explicit form. It will be an exponential distribution shifted to the left by one. The distribution function is more accurately described by the formula P(x)=1-exp(-θ(x+1)) if x≥-1, and P(x)=0 if x<-1. With a positive shift (uptrend) the parameter θ satisfies the inequality 0<θ<1, with a negative one (downtrend): θ>1 and at zero (flat): θ=1. The expected value is defined by the formula M=1/θ-1.

Since we assumed that the real prices are close to a random walk, the yields distribution of deals in a series will also be considered as such, with a certain value of the θ parameter. This gives a one-parameter family of continuous distributions. A specific value of the θ parameter during a trend depends on the ratio between the strength of the trend and trailing stop. Let us calculate it using methods of parametric statistics for the exponential distribution for the available history of deals (in this case, using the maximum likelihood estimation). For the further calculations, it is also important that the sum (and the arithmetic mean) of a finite number of such variables with the same parameter has a well-studied gamma distribution.

Let there be ai — yields of n deals, with each deal satisfying ai>-1 (for this, neglect the slippage). As in the previous chapter, propose a plan for further action.

  1. Calculate the estimate for the expected value M≈Mapr=(a1+a2+...+an)/n. If Mapr≤0, then reject the trading system as unprofitable. If Mapr>0, then proceed to the next step.
  2. Try to refute the M=0 hypothesis. Test statistic will be the variable S=Mapr+1, which has a gamma distribution Γ(n,1/n) (if the M=0 null hypothesis is satisfied). Denote its distribution by P(x). Then, to accept the alternative hypothesis, the P(S)≥P(1)+δ/2 inequality must be fulfilled. If the null hypothesis was successfully refuted, proceed to the next step, and if not — reject the system.
  3. Check the essential condition for G0. First, it is necessary to obtain the estimate of the parameter θ=θapr. Using the maximum likelihood estimation, we obtain θapr=1/(1+Mapr). The value Λ will be distributed according to the gamma distribution Γ(n,θapr/n) shifted to the left by one. The required condition will be satisfied if the δ quantile of this gamma distribution is no smaller than G0+1. If it is satisfied, proceed further. If it is not satisfied, it is possible to determine the minimum number of deals to fulfill this condition. To do this, incrementally increase the value of n and set nmin equal to the one, at which the required condition is satisfied.
  4. It is executed in the same way as in the previous chapter of the article. Changes concern only the distribution used for random generation of yields. This time we apply the exponential distribution with the parameter θ=θapr shifted to the left by one. The statistical library in MQL5 uses a slightly different form of parametrization for the exponential distribution — parameter μ (expected value), expressed through θ or M as μ=1/θ or μ=M+1.

#include <Math\Stat\Gamma.mqh> #include <Math\Stat\Exponential.mqh> #property script_show_inputs input uint na=50; // number of deals in the series input double tht=0.65; // parameter of the exponential distribution 0<tht<1 #define NX 300 // the maximum number of deals in a series (to search for nmin) #define NB 10000 // the number of generated samples for the Monte Carlo method #define NR 1000 // the number of interval partitions for the risk double G0=0.15; // the lowest average yield double D0=0.9; // the lowest minimal gain double dlt=0.05; // significance level void OnStart()   {    if(D0<=0 || D0>=1) {Print("D0 must be positive and less than one"); return;}    double m=1/tht-1; // expected value    if(m<=0) {Print("expected value must be positive"); return;}    int ner;      double p=MathCumulativeDistributionGamma(m+1,na,1.0/na,ner);    if(!MathIsValidNumber(p)) {Print("MathIsValidNumber(p) error ",ner); return;}    double p0=MathCumulativeDistributionGamma(1,na,1.0/na,ner);    if(!MathIsValidNumber(p0)) {Print("MathIsValidNumber(p0) error ",ner); return;}    if(p<=p0+dlt/2) {Print("expected value is close to zero"); return;}    double q=MathQuantileGamma(dlt,na,1/tht/na,ner),q0=G0+1;    if(!MathIsValidNumber(q)) {Print("MathIsValidNumber(q) error ",ner); return;}    if(q0>q)      {       Print("G0 is greater than dlt quantile of the arithmetic mean of the yield");       uint nmin=na+1;       for(;nmin<=NX;++nmin)         {          q=MathQuantileGamma(dlt,nmin,1/tht/nmin,ner); q0=G0+1;          if(!MathIsValidNumber(q)) {Print("MathIsValidNumber(q) error ",ner); return;}          if(q0<=q) break;         }       if(nmin<=NX) Print("nmin = ",nmin);       else Print("nmin is greater than ",NX);       return;      }     // the Monte Carlo method    MathSrand(GetTickCount());    double a[],c[],ropt[NB],A0,A,r,g,d,cn1,cn2,stp=1.0/NR;    uint ir;    ArrayResize(a,na);    ArrayResize(c,na);    for(uint ib=0; ib<NB;++ib)      {       for(uint i=0; i<na;++i)         {          a[i]=MathRandomExponential(m+1,ner);          if(!MathIsValidNumber(a[i])) {Print("Error MathRandomExponential()",ner); return;}          a[i]-=1.0;         }       A=MathMean(a); A0=a[ArrayMinimum(a)];       if(A<G0) {ropt[ib]=0; continue;}       if(A0>0) {ropt[ib]=1-stp; continue;}       cn1=1;       for(ir=1;ir<NR;++ir)         {          r=ir*stp;          cn2=1; for(uint i=0; i<na;++i) cn2*=1+r*a[i];          if(cn2<cn1) {ropt[ib]=r-stp; break;}          cn1=cn2;          g=(MathPow(cn2,1.0/na)-1)/r;          if(g<G0) {ropt[ib]=r-stp; break;}          c[0]=1+r*a[0]; for(uint i=1; i<na;++i) c[i]=c[i-1]*(1+r*a[i]);          d=dcalc(c);          if(d<D0) {ropt[ib]=r-stp; break;}         }      }    ArraySort(ropt);    uint nr=(uint)(dlt*NB);    Print("ropt = ",ropt[nr]);   } // the dcalc() function accepts the array of c1, c2, ... cN values and // returns the minimal gain d. Assume that c0==1 double dcalc(double &c[])   {    if(c[0]<=0) return 0;    double d=c[0], mx=c[0], mn=c[0];    for(uint i=1; i<na;++i)      {       if(c[i]<=0) return 0;       if(c[i]<mn) {mn=c[i]; d=MathMin(d,mn/mx);}       else {if(c[i]>mx) mx=mn=c[i];}      }    return d;   } //+------------------------------------------------------------------+

With the default arguments, the following risk value is obtained:

  • ropt = 0.015

This corresponds to 1.1% of the capital allocated to the system risked in each deal.

The sequence of thirty deals, considered in the previous part as an example, was obtained specifically for a system with exits by trailing stop. Therefore, our method can be applied to it. Similar to the method considered previously, it shows that the sequence is unfit for trading under given conditions (n, δ, G0, D0). This result will not change even after removing the slippage, by adding a small positive number to all ai, so that for all i=1,...,n the condition ai>-1 would be satisfied.

Note: if the script generates the error number 4 for the default arguments, then the necessary updates are required for the terminal. In this case, a part of the script (lines 21 through 42) must be commented out. This part contains calculations related to gamma distributions. For them, you can use the following script in R.

na=50; tht=0.65; dlt=0.05; G0=0.15; nx=300
m=1/tht-1; l1=l2=l3=FALSE
l1 = m > 0
if (l1) l2 = pgamma(m+1,na,scale = 1/na) > pgamma(1,na,scale = 1/na)+dlt/2
if (l2) l3 = G0+1 <= qgamma(dlt,na,scale = 1/tht/na)

if(!l1) cat("m must be greater 0\n")

if(l1 & l3) cat("G0 and na are ok\n") else cat("na too small or G0 too big\n")

if(l1 & l2 & !l3)
{for (nmin in (na+1):(nx+1)) {if(G0+1 <= qgamma(dlt,nmin,scale = 1/tht/nmin)) break()}
 if (nmin<=nx) {cat("nmin =",nmin)} else {cat("nmin >",nx)}
}


What's next?

Clearly, the methods above cannot be considered complete and final. For the sake of simplicity, the deals were taken as independent and distributed identically. But this requires a more in-depth research and may turn out not to be so. It may be useful for the distribution of yields to belong or be close to a specific parametric family. But this membership might also require additional research.

We settled for point estimates of the distribution parameters and did not seek confidence intervals for them. But once they are constructed, the question of stability of the system's profitability will come up as these parameters fluctuate.

A multitude of possible methods, of course, does not mean that they need to be used constantly and all at once. But it is better to know about them and to be able to use them when necessary, rather than to be "fooled by randomness".

Several other possible directions for applying the probability theory will be briefly outlined in the remainder of the article.

Statistics and gaps

A gap is a large jump between the Close and Open prices of two consecutive bars. These bars are also required to have a significant interval between them. It is believed that "gaps always get filled." Filling of a gap means that after it occurs, the price eventually returns to the Close price of the bar preceding the gap. Sometimes traders try to build a trading system based on this regularity. Is it possible?

The theory of a symmetric random walk on a line states that any price value will be reached over a time large enough, with a probability almost equal to one. That is, "gaps always get filled" almost surely under these conditions as well. But as we know, no profitable system is possible with a symmetrical random walk. Does this mean that a gap-based system has no chance of existence?

Not necessarily. Not only the fact of gap closure is important, but also the way the gap get filled. This does not refer to closure of every specific gap, but their certain aggregate. It is then possible to consider empirical probability distributions of random variables associated with them. There may be a difference between real prices and a symmetric random walk model. The differences can vary greatly. This can be caused by insufficient movement in the direction of the gap, the time until the closure being less than for a random walk, and much more. It is only important that the presence and significance of these deviations can be determined using the methods of probability theory and mathematical statistics. After verifying these deviations, trading systems can be built on their basis. You can also try to test a theory that classified gaps.

Let us consider the simplest version of searching for deviations from a random walk. This will be a measure of how much the price moves in the gap direction until it gets filled. It is also possible to obtain an empirical distribution of this variable by the price history and compare it with the theoretical one. Some goodness-of-fit criterion can be used for comparing these two distributions.

Consider a model of a symmetric random walk with discrete time. Let large gaps be rare in it (as in real prices), and let the model be close to a walk with continuous time (Wiener process) in the intervals between the gaps. Introduce the notation: g — the gap size, m — the maximum movement in the direction of the gap before it gets filled. Then the random variable x=m/g will have a distribution function close to the function P(x) — such that P(x)=1-1/x if x≥1, and P(x)=0 if x<1. The Figure illustrates the introduced values:

Gap directed down.

The topic brought up requires a more detailed and extensive study. Therefore, we confine ourselves to the following remarks:

  • The problem of gap research lies in their rarity. This is especially true when attempting to study the distributions associated with them, depending on their parameters (similar to the way described in the next chapter).
  • To check the theory of gap classification (for example, here), a more complex model will have to be built, which not only considers the prices from the gap until it gets filled, but also the previous and subsequent ones. This will lead to an increase in the number of considered random variables. This, in its turn, can cause problems due to the lack of data specified above.

Statistical machine learning

Consider the scheme of the trading system. For each deal, a set of parameters h is known, the values of which must uniquely define the deal volume and the exiting algorithm (not the price!). The value of h must be reliably known at the time of entry. It can be some technical indicator value, etc. In terms of machine learning, h is a set of features.

Just in case, make the following refinement. Generally, when speaking of a trading system, not one specific system is meant, but their parametric family. The task of testing and optimization is to select a specific system out of this family by determining and fixing a specific parameter value. In order not to complicate the presentation (do not confuse h with parameters that define the selection of a system from the family), this chapter considers a particular system, and not their family. The problems of the optimization will be briefly discussed in the next chapter.

After fixing a specific value of h, obtain a fixed algorithm for entering/exiting the market. This gives a fixed distribution of probabilities. Please note that for the same h there may be different yields. The distribution of yields is fixed, not their specific value.

This brings us to the problem of restoring the dependence of the yield distribution law on the features h. The initial data are the history of deals as a set of pairs of parameter values and the corresponding yields (hi,ai). Such problems are solved using the methods of statistical machine learning. It turns out that there is no fundamental difference between these methods and those used in machine learning when solving problems of restoring a deterministic (non-random) dependence.

Let us slightly clarify the formulation of the problem, assuming that distribution of yields belongs to a certain family. A set of (hi,ai) pairs is given. Distribution of yields ai belongs to the parametric family of distributions depending on the parameter θ. It is necessary to restore the dependence θ(h). This dependence is not sought among arbitrary functions — the initial data are not sufficient for this. It is usually sought among a certain parametric family of functions: θ(h)=θ(h,β), where the parameter β is determined by the set of (hi,ai) pairs, taking into account the requirement of some optimality. A wide range of function families for finding dependencies, optimality conditions for the parameter and methods to search for this optimal value, defines the enormous variety of the machine learning methods.

From this variety of methods, it is necessary to select the ones that are meaningful from a trader's point of view (for example, one that helps increase the profit or decrease the drawdown). This is especially important when selecting the optimality condition for selecting the parameter β. When this condition is changed, the resulting dependence θ(h,β) can vary greatly.

Suppose that for the given parametric family of yield distributions ai, the risk estimation problem was solved similar to the two cases considered above. Since the dependence θ=θ(h) has been built, and it is known how to select the risk value ropt for each given θ, the dependence ropt=ropt(h) is also known. Construction of the system can be deemed complete.

Statistics and optimization of the trading system

Consider the following simple variant of optimization. Make N runs of the system with n trades in each. Choose a small number εN of the best results out of them (with the highest average yield), where 0<ε<<1. Denote the smallest average among these selected runs by mopt, and consider it an indicator of how good the optimal system will be.

Now let us consider a system that will serve as an example of that the optimization results should be treated with caution. The example, albeit artificial, is enough to see the essence of the problem. Consider a system with a zero expected value of yields and a random dependence on parameters.

This system will make entries in random directions at fixed times. The direction will be selected using a random number generator with equal probability. The seed value for this generator before each test run will be defined by a function of the parameters. Thus, the same sequence of deals will always be obtained for each set of parameters, while changing these parameters will change the sequence in a completely random, unpredictable manner. Market exits will be carried out at fixed stop loss / take profit levels with a constant ratio k. Also, consider the test interval to have no trends.

Obviously, the lack of a trend would cause the expected value of this system's yields to be close to zero (a small negative with the spread taken into account). Denote the number of deals in a series by n. Then the arithmetic mean m of the yields in the series of n trades will be a random variable. It is expressed in terms of a random variable nk — the number of profitable trades in the series: m=(1+k)nk/n-1. The variable nk has a binomial distribution with the parameters 1/(1+k) and n.

At a sufficiently large N, the value mopt will be close to (1-ε) quantile of the m variable distribution. Let us show that despite the obvious futility of this "trading system" (due to zero expected value), the value of mopt will be quite high. For clarity, set: k=3, n=50, ε=0.1 and obtain mopt=2.52.

It is obvious that everything is arranged differently and is much more complex in real, meaningful systems during real testing. Nevertheless, in this case, it is possible and necessary to understand whether the advantage of the optimized system is random or not.

Attachments

 No. NameType
 Description
 1 bn.mq5 scriptcalculation of risk for exits with fixed stop loss and take profit
2
exp.mq5scriptcalculation of risk for exits with a fixed trailing stop


Translated from Russian by MetaQuotes Ltd.
Original article: https://www.mql5.com/ru/articles/3973

Attached files |
exp.mq5 (7.79 KB)
bn.mq5 (7.84 KB)
Automatic Selection of Promising Signals Automatic Selection of Promising Signals
The article is devoted to the analysis of trading signals for the MetaTrader 5 platform, which enable the automated execution of trading operations on subscribers' accounts. Also, the article considers the development of tools, which help search for potentially promising trading signals straight from the terminal.
Night trading during the Asian session: How to stay profitable Night trading during the Asian session: How to stay profitable
The article deals with the concept of night trading, as well as trading strategies and their implementation in MQL5. We perform tests and make appropriate conclusions.
How to reduce trader's risks How to reduce trader's risks
Trading in financial markets is associated with a whole range of risks that should be taken into account in the algorithms of trading systems. Reducing such risks is the most important task to make a profit when trading.
Momentum Pinball trading strategy Momentum Pinball trading strategy
In this article, we continue to consider writing the code to trading systems described in a book by Linda B. Raschke and Laurence A. Connors “Street Smarts: High Probability Short-Term Trading Strategies”. This time we study Momentum Pinball system: there is described creation of two indicators, trade robot and signal block on it.