Forcing input variables for first pass in fast genetic optimisation.

 

Hello, everyone!

Today, my question concerns fast genetic optimisation and whether or not it is possible to specify a set of initial parameters.

Let me explain:

Suppose I want to run a quick genetic optimisation. Random parameters are used, and the better the optimisation results, the more the parameters are geared towards those that generated good results.

Now, suppose I already know a set of parameters that give good results. In order to ‘guide’ the optimisation, I would like to “force” the use of the parameters in the inputs (in the ‘Input’ column).

Please note that this should not be confused with the ‘start’ column. In fact, if x is a parameter, the “start” and ‘end’ columns only ensure that x belongs to [start; end]. Here, I would like my value x to be the first value tested. By setting start = x, I am only ensuring that it is part of the test interval.


I have tested several solutions for this:

  • First, I tried playing around with the ParameterSetRange() and ParameterGetRange() functions. But these do not allow me to do this. I had thought, for example, of calling ParameterSetRange() so that the test interval would be [x; x] on the first optimisation pass, then [start; end] afterwards, but as the function can only be called in OnTesterInit(), it did not work.
  • Secondly, I wanted to test with global variables. The problem here is obviously that global variables cannot be optimised in the same way as inputs. Furthermore, the code would be be polluted with GlobalVariableSet() calls and wouldn't be readable.

The problem is in some ways related to the fact that input variables cannot be changed. (The solution of ‘mapping’ the input variables does not work, since I need to do it the other way around).

  • So, in a third step, I thought about putting the variables as extern... The problem is that extern no longer has the same functionality in MT5 (as it did in MT4, where extern variables were basically modifiable inputs).

In short, I basically need to be able to do something like that:

input int x = 1; // In MT4, could have been extern instead of input.

void OnTesterInit()
{
   // First optimisation pass, force test_var to be whatever I want.
   x = ORIENTATION_VARIABLE;
}

There are two big problems I see. On the one hand, you can't change input variables, and on the second hand, even if you did want to change the input variable, the thing you would change it to would change with optimisation. The second problem is less of an issue, as you could set your "orientation" set file as global variables or even retrieve it from a text file.


So I turn to you: do you have a solution to ensure that the first pass of genetic optimisation necessarily uses the input variables (‘force the variables once’) to guide the fast genetic optimisation?

 

I don't think you can do that with the MT5 integrated generic algorithm, as it's a closed system and you don't have access to the internal settings of the genetic algo.

You can't deal with parameters internally (which is technically doable), as to optimize them they need to be 'inputs', if you set other values internally, the genetic algorithm will continue to optimize with the known 'wrong' values from the inputs. The genetic algorithm will be misled.

Possibly you could try to play with the fitness function (with a custom formula to return the value in OnTester). Though it will be far from trivial, if doable at all (I didn't think about it further).

The best way to do what you want would be to create a custom genetic algorithm yourself, that's for sure not trivial either. 

 
Zaky Hamdoun:


So I turn to you: do you have a solution to ensure that the first pass of genetic optimisation necessarily uses the input variables (‘force the variables once’) to guide the fast genetic optimisation?

Your requirement is not clear and ideas are not well fit into the platform (as it's designed and supposed to be used).

The "quick genetic optimisation" is intended to probe entire range of specified inputs ([startA;stepA;stopA][startB;stepB;stopB]...) quickly. That means that even if you probe a specific initial point in the space first, the algorithm will continue probing many other random places to get enough population. So it makes no sense to insist on probing a specific point first - the algorithm will not converge faster (than at least one generation).

If you already know (somehow) the subregion of good parameters - then just specify it in the settings.

Other interesting question is how did you know these good parameters. If you found them by running the genetic optimization before, then just remember that you can run genetics many times, again and again, and it will pick up all previous good findings to speed up the process in next generations.

FYI, global variables can't be used because their storages are independent for the terminal and every tester agent.

Even if you could edit inputs in MT5, changing variables in OnTesterInit makes no effect on the tester agents, because EA instance with OnTesterInit runs in the terminal itself whereas the trading EA instances run on agents - they are all different processes.