Discussion of article "Creating Custom Criteria of Optimization of Expert Advisors" - page 3

 

With much experimenting, I have come to similar conclusions. I don't know why it took me so long to find this article and discussion.

  I started with a problem where my break out and trend riding strategies tended to fit themselves to a few super profitable trades. So much profit from these few that the optimizer only "cared" about these few trades and used wide stops and far away take profit and making sure its signal caught and milked these few trades to the maximum. this occurred even on 20 year samples with 100's of trades. Even when optimizing with a dd to profit ratio, it still went after the runs with these trades at all costs cause the runs were 90% of its profit. I wanted to limit this behavior in some way without cutting the head off of the breakout/trend ride strat. I found that optimizing for relative dd percent (NOT dd ratio to profit, just dd relative percent only and nothing much else) gives good results going forward as long as minimum trades and minimum profit factor (about 1.3) are met. 


//............... 

sinput double mint; //minimum trades for optimization purposes. Will impose a penalty on runs with fewer trades. 

sinput double minpf; //minimum profit factor for optimization purposes. Will impose a penalty on runs with less pf.

//...............

double OnTester()

{ 

	double dd=TesterStatistics(STAT_BALANCE_DDREL_PERCENT);//equity instead of balance also worked well going forward

	double pf=TesterStatistics(STAT_PROFIT_FACTOR);

	int    tt=TesterStatistics(STAT_TRADES); 

	double custom=(100-dd);

	if (mint>t) custom=custom*(tt/mint); // imposes a penalty if minimum trades not met

	if (minpf>minpf) custom=custom*((pf-1)/(minpf-1)) // this line also causes losing runs to be negative as well as imposing a penalty lesser pf.

	return(custom); 

}

 

 

It seems wrong to not have profit involved but by having a pf minimum criteria, mostly quite profitable runs are appearing at the top. Cropping out dd from profitable runs is usually increasing profits. In any case, by selecting the more profitable trades and doing the final touch-ups carefully manually by hand (such as near the end of this excellent article https://www.mql5.com/en/articles/156), I can allow the optimizer to do a lot of the initial work while avoiding a lot of curve fitting. Ive experimented and had mixed results with code such as the following. (Experiments should include changing risk and starting balance too once you begin put additional weights in on top of a percent based score)

//.............

sinput double pfw //Profit factor weight 

//.............

        double custom=(100-dd)+(pf-1)*pfw;
//...........
Guide to Testing and Optimizing of Expert Advisors in MQL5
Guide to Testing and Optimizing of Expert Advisors in MQL5
  • 2010.10.12
  • Samuel
  • www.mql5.com
This article explains the step by step process of identifying and resolving code errors as well as the steps in testing and optimizing of the Expert Advisor input parameters. You will learn how to use Strategy Tester of MetaTrader 5 client terminal to find the best symbol and set of input parameters for your Expert Advisor.
 

Can anyone tell me if this article will be applicable for MT4? It seems that it has already adopted many properties from MT5.

In general, my task is to get just the values of LR Correlation and LR Standard Error in the MT4 tester, as it can be easily seen in the MT5 tester.

I just want to read these values in the deinit() function at the end of the test run and write them to a file together with the value of the optimised parameter.

Maybe someone has already done such things and will share with me the ready result (the necessary function for calculating LR Correlation and LR Standard Error values) so that I don't have to reinvent the wheel?

 
solandr:

Can anyone tell me if this article will be applicable for MT4? It seems that it has already adopted many properties from MT5.

In general, my task is to get just the values of LR Correlation and LR Standard Error in the MT4 tester, as it can be easily seen in the MT5 tester.

I just want to read these values in the deinit() function at the end of the test run and write them to a file together with the value of the optimised parameter.

Maybe someone has already done such things and will share with me the ready result (the necessary function for calculating LR Correlation and LR Standard Error values) so that I don't have to reinvent the wheel?

An example of calculating LR Correlation and LR Standard Error on trades in history is available in AlgLib (MQL4\Scripts\Alglib\UseAlglib.mq4).
 
Automated-Trading:
An example of calculating LR Correlation and LR Standard Error for trades in history is available in AlgLib (MQL4\Scripts\Alglib\UseAlglib.mq4).
Thank you! I will look into it.
 
solandr:
Thank you! I'll look into it.

I got it. The correlation seems to be calculated.

The only thing I had to think about is the fact that this point does not work in the MT4 Build 670 tester:

//--- obtaining the initial balance
      if(order_type==6) // OP_BALANCE=6
        {
         if(NormalizeDouble(OrderProfit()+OrderSwap(),2)>=0.0)
            if(balance==0.0)
               balance=OrderProfit();
        }

There are simply no orders with type 6 in the tester.

That is, when running in the MT4 tester and using the code from UseAlglib.mq4, which is included in the downloaded zip file, through a call from the deinit() function.

balance remains equal to 0. And then the error"Trading operations with zero balance" is printed.

I had to simply insert the necessary value of the initial balance in the MT4 tester into the code itself and then everything was counted perfectly.

Perhaps the developers can take this point into account in future versions of the library.

 

I have tested Kelly Criterion (Strategy), using the following code:


double OnTester(void)
  {
   //https://www.investopedia.com/articles/trading/04/091504.asp
   double w=((TesterStatistics(STAT_PROFIT_TRADES)+TesterStatistics(STAT_LOSS_TRADES))>0)?TesterStatistics(STAT_PROFIT_TRADES)/(TesterStatistics(STAT_PROFIT_TRADES)+TesterStatistics(STAT_LOSS_TRADES)):0; // winning probability
   double r=((TesterStatistics(STAT_GROSS_LOSS)!=0)&&(TesterStatistics(STAT_LOSS_TRADES)!=0)&&(TesterStatistics(STAT_PROFIT_TRADES)!=0))?(TesterStatistics(STAT_GROSS_PROFIT)/TesterStatistics(STAT_PROFIT_TRADES))/(-TesterStatistics(STAT_GROSS_LOSS)/TesterStatistics(STAT_LOSS_TRADES)):0; // Win/loss ratio;
   double Kelly=(r!=0)?w-((1-w)/r):0; // Kelly Criterion
   return(Kelly);
  }


I am not sure if Metatrader Strategy Tester computes 0 (zero) profit trades as profit trades. Anyone?

 
Ingvar Engelbrecht:

Well, here I am again, the lone wolf in this universe  :-)

I have been trying the straightness Custom Criteria trying to get the slope of the calculated straight line into the equation. As is it can give you a very hgh rating on a very feeble profit. Just adding the end profit

into the caculation does not make it any better  In an attempt to add the actual slope into the equation  I changed the code lilke seen below.

It is not a perfect solution but it is closer to what I want to see. Using result together wit balance or profit  works fine for me with this code


I know it's been so long since you posted this but in case you are still playing with this or someone else are looking for the same straightness criteria implementation.

I found a working public solution here https://community.darwinex.com/t/equity-curve-straigthness-optimization-with-metatrader/3976

Equity Curve Straigthness Optimization with Metatrader
Equity Curve Straigthness Optimization with Metatrader
  • 2018.05.16
  • KlondikeFX
  • community.darwinex.com
An underrated feature of Metatrader’s Backtester is its ability to define a custom fitness function for the genetic optimization process. Meaning that you no longer are limited to rather simple metrics like final balance or profit factor but can evaluate the quality of each test run with your own individual calculations. To give a quick...
 

The relating tutorial has too much info, such as specifically about programming an EA, which is in other tutorial, and non-applicable to average punter, who, will buy their EA and has minimal programming ability.

I find, that by default, the only useful criterions for MT5 in  genetic algo is the "balance max", then have to repeat that a few times, and fish through results til find low drawdown, as for use on multiple pairs.

What criterions I need :- Max balance with <20 Drawdown, MaxBalance with <10 Drawdown 

 
Great read, Lone wolf out here 😁