Constraints for input parameters during optimization

 

I've 3 different input parameters : P1 [1..10], P2 [1..10] and P3 [3..10]. The constraints are the following : P1 < P2 < P3.

Some potential solutions should be discarded right away when the constraints are not respected. For instance when P1 = 2, P2 = 1 and P3 = 4.

Is there a way to accomplish such thing with MetaTrader optimization ? (eventually using OnTesterPass function ?)

I'm losing a lot of time during my optimization because of that and I'd like to find a way to discard or skip directly the potential solution when the constraints are not respected.

Thanks for your help

 
Fr4nz78:

I've 3 different input parameters : P1 [1..10], P2 [1..10] and P3 [3..10]. The constraints are the following : P1 < P2 < P3.

Some potential solutions should be discarded right away when the constraints are not respected. For instance when P1 = 2, P2 = 1 and P3 = 4.

Is there a way to accomplish such thing with MetaTrader optimization ? (eventually using OnTesterPass function ?)

I'm losing a lot of time during my optimization because of that and I'd like to find a way to discard or skip directly the potential solution when the constraints are not respected.

Thanks for your help

Not possible inside MetaTrader.

You can build .set files with the exact parameters you need then call MT using a config file from the command line to run the tester.

See section "Running with a Custom Configuration File" sub-section "[Tester]."

https://www.metatrader5.com/en/terminal/help/start_advanced/start

Platform Start - For Advanced Users - MetaTrader 5
Platform Start - For Advanced Users - MetaTrader 5
  • www.metatrader5.com
After installation, a group of programs of the trading platform is added to the Start menu, and the program shortcut is created on the desktop. Use them to run the platform. Two copies of the platform cannot run from the same directory. If you need to run multiple copies at the same time, install the appropriate number of programs in different...
 
Anthony Garot:

Not possible inside MetaTrader.

You can build .set files with the exact parameters you need then call MT using a config file from the command line to run the tester.

See section "Running with a Custom Configuration File" sub-section "[Tester]."

https://www.metatrader5.com/en/terminal/help/start_advanced/start

I don't see how it helps for the OP problem about an optimization ?
 
Fr4nz78:

I've 3 different input parameters : P1 [1..10], P2 [1..10] and P3 [3..10]. The constraints are the following : P1 < P2 < P3.

Some potential solutions should be discarded right away when the constraints are not respected. For instance when P1 = 2, P2 = 1 and P3 = 4.

Is there a way to accomplish such thing with MetaTrader optimization ? (eventually using OnTesterPass function ?)

I'm losing a lot of time during my optimization because of that and I'd like to find a way to discard or skip directly the potential solution when the constraints are not respected.

Thanks for your help

Forum on trading, automated trading systems and testing trading strategies

Stop running EA on invalid inputs (Especialy on tester)

Alain Verleyen, 2013.11.23 16:57

Place your test in OnInit() and return INIT_FAILED or INIT_PARAMETERS_INCORRECT. Like :

if (fastema>=slowema)
   return(INIT_PARAMETERS_INCORRECT);
However, if you are using Genetic algorithm, it probably has a serious impact on the results.
 
Fr4nz78:

I've 3 different input parameters : P1 [1..10], P2 [1..10] and P3 [3..10]. The constraints are the following : P1 < P2 < P3.

Some potential solutions should be discarded right away when the constraints are not respected. For instance when P1 = 2, P2 = 1 and P3 = 4.

Is there a way to accomplish such thing with MetaTrader optimization ? (eventually using OnTesterPass function ?)

I'm losing a lot of time during my optimization because of that and I'd like to find a way to discard or skip directly the potential solution when the constraints are not respected.

Thanks for your help

I'm not really sure about it (I've never tested it) but I think you can use return value INIT_PARAMETERS_INCORRECT in your OnInit(). Something like this:
int OnInit()
  {
//--- check for input
   if(P1>=P2 || P2>=P3) return(INIT_PARAMETERS_INCORRECT);
...

As the documentation says:

The result string containing this return code (INIT_PARAMETERS_INCORRECT) is highlighted in red in the general optimization table. Testing for the given set of parameters of the Expert Advisor will not be executed.

UPDATE: Alain were faster :D

 
Anthony Garot:

Not possible inside MetaTrader.

You can build .set files with the exact parameters you need then call MT using a config file from the command line to run the tester.

See section "Running with a Custom Configuration File" sub-section "[Tester]."

https://www.metatrader5.com/en/terminal/help/start_advanced/start

So if I understand correctly I set my own parameters inside "MQL5\Profiles\Tester\MyEA.set".

But then this means I can only have one correct set of parameters per run (because if I use start, step and stop I will end up with the same problem as before).

And so I need to somehow write my own optimization algorithm from outside MetaTrader ?

 
Petr Nosek:
I'm not really sure about it (I've never tested it) but I think you can use return value INIT_PARAMETERS_INCORRECT in your OnInit(). Something like this:

As the documentation says:

The result string containing this return code (INIT_PARAMETERS_INCORRECT) is highlighted in red in the general optimization table. Testing for the given set of parameters of the Expert Advisor will not be executed.

UPDATE: Alain were faster :D

Thanks Alain and Petr, I think that's exatly what I'm looking for.

 
Fr4nz78:

So if I understand correctly I set my own parameters inside "MQL5\Profiles\Tester\MyEA.set".

But then this means I can only have one correct set of parameters per run (because if I use start, step and stop I will end up with the same problem as before).

To break this down:

1. You have specific variable combinations that are valid based upon:

P1 [1..10], P2 [1..10] and P3 [3..10]

You can generate these values external to MT5. For example, you might do this in python.

2. Build a .set file with P1,P2,P3 that fit your constraints.

3. Run the strategy tester (in tester mode, not optimization mode, with visualization turned off) and append the values you want to a CSV file.

It's fairly straightforward to calculate the same statistics that the optimizer supplies, i.e. Recovery factor, Sharpe ratio, etc. You can also any add additional statistical metrics you desire.

4. Once you have generated all the statistics for your combinations of P1, P2, P3, you can run your analysis upon the CSV file. Use Excel or gnuplot or gnumeric or whichever tools you desire to determine which combination of P1,P2,P3 delivered the optimum result.

And so I need to somehow write my own optimization algorithm from outside MetaTrader ?

Granted, it's a long-term solution.

Fr4nz78:

Thanks Alain and Petr, I think that's exatly what I'm looking for.

Well, then, your current problem is solved with a simpler solution. That's cool.

 
Anthony Garot:

To break this down:

1. You have specific variable combinations that are valid based upon:

P1 [1..10], P2 [1..10] and P3 [3..10]

You can generate these values external to MT5. For example, you might do this in python.

2. Build a .set file with P1,P2,P3 that fit your constraints.

3. Run the strategy tester (in tester mode, not optimization mode, with visualization turned off) and append the values you want to a CSV file.

It's fairly straightforward to calculate the same statistics that the optimizer supplies, i.e. Recovery factor, Sharpe ratio, etc. You can also any add additional statistical metrics you desire.

4. Once you have generated all the statistics for your combinations of P1, P2, P3, you can run your analysis upon the CSV file. Use Excel or gnuplot or gnumeric or whichever tools you desire to determine which combination of P1,P2,P3 delivered the optimum result.

Granted, it's a long-term solution.

Well, then, your current problem is solved with a simpler solution. That's cool.

It's what I thought, thanks for the detailed explanation. But that's not a little work, at least the first time you code it. And by the way ALL of that could be done with MT5 and mql5, a matter of preference I guess ;-)
 
Alain Verleyen:
But that's not a little work, at least the first time you code it.
Very true. It took many weeks to get it right. But it is extensible for other uses, e.g. building a sliding time window for walk-forward analysis that shows the results graphically.
Alain Verleyen:
And by the way ALL of that could be done with MT5 and mql5, a matter of preference I guess ;-)

This is also true.

While I like MT5 very much, I didn't want to become overly invested in one platform. Having some of my tools outside MT5 gives me options for the future if, for example MetaQuotes goes out of business or if a new platform emerges that is far superior in features. My gamble is that that python will remain viable in the future.

 
Fr4nz78:

Thanks Alain and Petr, I think that's exatly what I'm looking for.

So after implementing the constraints in OnInit I now face another challenge.

I'm trying to optimize my current EA that contains approximately 40 different inputs with MetaTrader genetic algorithm.

The inputs have constraints such as I1 < I2 < I3, I24 > 0, ... For total of about 20 constraints.

The problem is then the following : no viable solutions are found after the first 512 iterations and the optimization stops (same happens with the non genetic optimizer).

If I remove the constraints the algorithm will run and optimize the solutions but then those solutions will not respect the constraints.

Has anyone already faced similar issues ? Currently I think I'll have to use an external tool to optimize but this does not feel right