Lenar, great,
this drop-down menu looks cute. BUT, this is not the best way to select the fitness function. This resembles the predefined, Tradestation-like style of work too much. And you want to do better than Tradestation does, right?
Obviously, there should be a number of predefined fitness functions, like:
- NetProfit/MaxDrawDown
- NetProfit/Margin
- VanTharp Expectancy
- PROM (Pessimistic Return on Margin)
- Profit Factor
- Equity Curve Linear Regression Max Error/StdDev
- Sharpe Ratio
etc.
But to lift MetaTrader to higher grounds and smash the competition flat

, you have to give your user the freedom to calculate
his own fitness funtion.
If you present the user with only a list of several available options in that cute drop-down menu, it is like going to the supermarket and being able to only choose between:
- package 1: cheese, milk, bread, rice
- package 2: cornflakes, 4 apples, wine, sugar
- package 3: potatoes, beef, ice cream, bacon
Now you're smiling, aren't you? But you offer your user precisely these predefined packages without the option the choose milk and ice cream only.
Fortunately, the solution is quite trivial: instead of offering user the entire formulas as a fitness function, give him the ability to use the
building blocks to calculate the fitness instead.
What are building blocks?
- TotalProfit
- MaximumDrawDown
- ProfitFactor
- NumberOfLosers
- NumberOfWinners
etc. etc.
This way, if you want to arrive at a set of input values which produces the most trades and best drawdown to profit ratio, you would enter as a fitness function this simple equation:
FitnessValue = (NumberOfLosers + NumberOfWinners)*TotalProfit/MaximumDrawDown
Genetic algorithm-powered optimizer will then try to maximize the result of this equation.
You may implement this
FitnessValue variable assignment either right in the code of the strategy (preferably) or you could offer the user the opportunity to enter the formula before the optimization will be run.
Why it is best to implement the
FitnessValue variable assignment right in the code? Since the strategy developer may then use
any arbitrary value or command within the confines of MetaTrader4 programming language to calculate his own preferred fitness function. Also, implementing this assignment right into the code will actually
save you money because there would be absolutely no need to code any extra new dialogues etc.
For this assignment to work, all the tested variables and values during the testing run should be available for calculation within the code.
Let's say that we would like to modify our fitness function from the above example so that we are interested in arriving at a set of input values that produces the most traders, the best profit/drawdown ratio and also the least StopLoss parameter (we don't want to risk much on any single trade

.
The equation might then look like this:
FitnessValue = (NumberOfLosers + NumberOfWinners)*TotalProfit/MaximumDrawDown/StopLossValue
In order to be able to use this construction, the value of the parameter "StopLossValue" needs to be available to the programmer. With every test run, the parameter value will change (the optimizer will pick a new value) and this value needs to be available for the programmer in order to be able to use it in the calculation.
Michal