Click here to Login

                                                   Custom Multi-Dimensional Fitness Formula (CMDFF) for Optimizer

Back to threads - Tags: --

2012-03-23 05:25:37

Probably you have made similar experiences while optimizing your trading system: the optimizer throws excellent results (e.g. on the fitness function "AnnualReturn") but the system drawdown is too bad or the number of trades is below statistical significance and so on.

I was thinking of a customizable fitness function that allows more than one parameter, because what we are all looking for is a trading system with e.g. high annual return, low drawdown and high Sharpe Ratio.

Below I will share a C#-script with you, you can just copy it to your fitness formula editor (don't forget to set the compiler to C#). I would appreciate your feedback once you have tested it.

This is the idea behind and the assumptions for the example:

You would weigh the output of your trading system by Annual Return, Maximum System Drawdown in % and by the Sharpe Ratio.
You would weigh Annual Return and Maximum System Drawdown in % equally and Sharpe Ratio is only half important for you.
You expect a minimum annual return of 0% from your trading system and a value above 30% would be perfect.
Maximum System Drawdown shall not be above 30% and anything below 10% would be your optimum.
The Sharpe Ratio shall not be below 0.5 and you do not expect more than 2.
And finally the minimum number of trades generated by your system shall be 100.

In the script it looks like:

bool ok = NumberOfTrades >= 100;
only if the ok variable is true, the function will calculate the other parameters, you can change to whatever rule you need, if you don't need it, just change to "bool ok = true"

double[] parameters = {AnnualReturn, MaximumSystemDrawdownInpercentage, SharpeRatio};
list the parameters you want to include in your fitness function here

double[] weight = {10, 10, 5};
specify your weighing factors, the total does not have to be 100

double[] worst_value = {0, -30, 0.5};
list the worst values you expect from your parameters here, note that the output of MaximumSystemDrawdownInpercentage has a negative sign

double[] best_value = {30, -10, 2};
list the best values you expect from your parameters here, note that the output of MaximumSystemDrawdownInpercentage has a negative sign

The output of the fitness function will be normalized between 0 and 100 and can be interpreted as follows:
A result of 0 means that either the number of trades is below 100 or none of the 3 minimum parameters is fulfilled.
A result of 100 means, that the number of trades is above or equal 100 and all of the 3 parameters are above maximum value.
A number between 0 and 100 indicates the comfort level with your parameters and weighing factors.

Note that the number of dimensions of the fitness formula is not limited, you can add or remove according to your needs, but in any case make sure that the number of dimensions in the arrays "parameters", "weight", "worst_value" and "best_value" is always the same.

And here is the script, enjoy!

// -------------- C# ------------------

// ---------- define your fitness parameters here ----------

bool ok = NumberOfTrades >= 100;
double[] parameters = {AnnualReturn, MaximumSystemDrawdownInpercentage, SharpeRatio};
double[] weight = {10, 10, 5};
double[] worst_value = {0, -30, 0.5};
double[] best_value = {30, -10, 2};

// ---------- end of parameterization ----------

double fit_param = 0;
double total_weight = 0;
if (ok)
for (int i = 0; i < parameters.Length; i++)
total_weight = total_weight + weight[i];
if (best_value[i] != worst_value[i])
fit_param = fit_param + weight[i] *
(Math.Max(Math.Min(parameters[i], best_value[i]), worst_value[i]) - worst_value[i]) /
(best_value[i] - worst_value[i]);
if (total_weight != 0) Fitness = 100 * fit_param / total_weight; else Fitness = 0;
Fitness = 0;

2012-03-23 13:18:23


Excellent, thank you!

I was using this:

double factor1 = 1.5*SharpeRatio;
double factor2 = 0.5*AnnualReturn;
double factor = (factor1*factor2);
Fitness = factor;

or this:

double factor = 1;
if(NumberOfTrades > 10)
factor = 1.1;
else if(NumberOfTrades > 20)
factor = 1.2;
else if(NumberOfTrades > 50)
factor = 1.3;

Fitness = factor * SharpeRatio;

2012-03-26 06:09:54


Well done. I have to say that this a very powerful script.

Any improvements you have seen in the simulation results produced by the AI Optimizer?

To all:
With few modifications, this script can also be used with the list of rules, ranking system and prediction model optimizers.

2012-03-30 02:36:05


@ Mike

Thank you! Yes, I have seen improvements, but according to the nature of the formula, not so much with the absolute return but with the stability of the return over time.

2013-07-09 23:16:52


While developing a trading system it will happen quite quickly that you will have a few hundred thousand or even million variations that you cannot analyze individually in an appropriate time frame.

Artificial Intelligence

This is when artificial intelligence comes into place. QuantShare offers two algorithms to perform optimizations, the so called Genetic Algorithm and PBIL (Population-Based Incremental Learning). Those algorithms have in common that they randomly select variations to get analyzed and try to learn from these results rather than analyzing all possible variations. So the upside is, that these algorithms find solutions much quicker, but the downside is, they might not find the optimal solution. That's the trade-off, but does it matter? In many cases probably not. Say you have a million variations in a system you want to optimize and if you would analyze them all you would find a solution that promises 50% CAR. Keep in mind, when you start artificial intelligence, you do not know this solution and you even do not know whether there is any solution promising a positive CAR. After a relatively short (compared with full analysis) period of learning the algorithm might present you a solution with 30% CAR. Good or bad? I would say "good" if the mindset is to find good solutions in due time rather than spending forever to find the optimum.

Fitness Function in General

What is the Fitness function needed for? This function tells the computer how to rank the variations that are analyzed. There is not the one-and-only or the correct fitness function, but the fitness function basically is the description of an individual risk-reward preference. In my experiences, single-parameter fitness functions (e.g. "Fitness = AnnualReturn;") did not lead to appropriate optimization results. For instance I had a system that showed 54% CAR over 10 years, but when I looked into it, only 2 or 3 years were positive. Is this a surprise? I would say no, because when you judge the output of a trading system personally, I guess, you would never look at AnnualReturn alone but look for Drawdown, Volatility, Stability as well. So we need to find a way to tell the computer what we are looking at and how we perform our own judgement.

How to develop a Fitness Function

1. Decide on the elements you include in your judgement. Keep in mind, there is no right or wrong. For simplification purposes lets just assume you would judge a system just by "AnnualReturn" and "MaximumSystemDrawdownInpercentage".
2. Apply rating points on your elements. This of course requires some experience to know what output for your chosen elements you can expect in your system with your individual portfolio and timeframe. Say you would expect an AnnualReturn between 10% and 30% and a MaximumSystemDrawdownInpercentage between -50% and -20% (note the negative sign). When your system has an AnnualReturn of 15%, you could give this element 25 fitness points ((15% - 10%) / (30% - 10%) * 100). And if MaxDD is -40% you could give this element 33.33 fitness points ((-40% + 50%) / (-20% + 50%) * 100).
3. Weigh the fitness of the elements. How should you weigh? Do not easily say 50:50, because you think AnnualReturn and MaxDD are equally important to you.
As stated above, Fitness is about your individual risk-reward preference.
3a. Define your individual substitution rate. What does it mean? Say you have a system with 15% AnnualReturn and -40% MaxDD. And another one with also 15% Return but only -39% MaxDD. It is clear, that you would give the second one a higher fitness value, whatever the formula is. But what about a system that promises 16% Return and -41% MaxDD. One element has improved, the other element is worse. How do you rate that compared to the initial system? The answer is, it depends ... on your individual risk-reward preference. You should ask yourself the question, what am I willing to give in terms of MaxDD if I have the chance to increase the AnnualReturn by 1%. Lets further assume, you would judge both systems (15%/-40% vs 16%/-41%) equally, meaning you will have a substitution rate of 1, they will have the same fitness value.
3b. Determine weighing factor. Now it becomes a little algebraic. The calculation of the fitness points (25 and 33.33) above basically is a linear equation. The slope of this equations is (100 / (30% - 10%)) = 5 for AnnualReturn and (100 / (-20% + 50%)) = 3.33 for MaxDD. If both systems (15%/-40% vs 16%/-41%) shall have equal fitness, weighing should be done according to the slopes, which means (5 / (5 + 3.33)) = 60% and (3.33 / (5 + 3.33)) = 40%.
The proof: Return of 15% gets 25 fitness points (see above), 16% gets 30 fitness points. Max DD of -40% gets 33.33 fitness points (see above), -41% gets 30 fitness points. Fitness of system 1: 25 pts * 40% + 33.33 pts * 60% = 30 pts. Fitness of system 2: 30 pts * 40% + 30 pts * 60% = 30 pts.
The example shows that you would weigh AnnualReturn and MaxDD 60:40 rather than 50:50 in case you have a substitution rate of 1. In case you would consider additional elements in your fitness function, you would have to think about additional substitution rates between AnnualReturn and your additional parameters.

Any questions, just let me know ...

No more messages


No html code. URLs turn into links automatically.

Type in the trading objects you want to include: - Add Objects
To add a trading object in your message, type in the object name, select it and then click on "Add Objects"


Trading Items
Custom Weight for Individual Securities
History of dividend payment for stocks in the US stock market
Black-Scholes Option Pricing Model formula
Lagged Data From a Custom Database
Advanced Quotes and Custom Databases Function

How-to Lessons
How to add custom position-based metrics to your trading system
How to create a custom trendline
How to download earnings calendar data for various stocks
How to get fundamental data for U.S. Stocks
How to use a custom drawing tool

Related Forum Threads
Custom Fitness Functions for Walk Forward Testing
Custom Fitness function in Ranking System Manager
Best way to precompute a custom formula and store it
How do I find the close price for an entry matching a custom DB
Monthly/weekly summation for a custom database field

Blog Posts
Create custom metrics for the statistical data analysis tool
How to Create Custom Metrics for Your Trading Positions
Custom Shortcuts - Move a Chart to Display the Last Quotes/Data
How to Backtest Your Trading System for Each Industry
Trading System Analysis: Backtesting report and custom measures

Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright 2024
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Follow us on Google+
RSS Trading Items

Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.