Click here to Login

How to Optimize a Trading System with Thousands of Billions of Combinations

Updated on 2014-02-08

You can use it to optimize your list of rules, your ranking systems, your neural network models and even your trading systems. It is called AI Optimizer and we recently improved it so that it can handle thousands of billions of combinations easily and quickly.

Let me walk you through the AI optimizer with a simple trading system example.

- Select "AI" then "Optimize"
- In "Optimize" form, click on "Create"
- Select the optimization method or algorithm to use (PBIL or Genetic Algorithm)

Note here that PBIL has the enormous advantage of supporting multithreading and thus can run much faster.

- We will select PBIL in this article
- Select "Trading System" under "What do you want to optimize?"
- Click on "Next"
- Fill PBIL settings

Number of generations: Each generation has a specific number of members (population). Each member represents a trading system. After the members of a generation are backtested, only the best ones + random ones are passed to the next generation.
Population size: This is the number of members in a generation.
Learning rate: Specifies the percentage of knowledge that is passed to the next generation
Number of best solutions to use in learning: The number of best performing members to be used in the learning process
Enable multithreading: Check this to enable multithreading (several trading systems will be backtested at the same time, the number of simultaneous trading system depends on the number of CPU cores you have in your computer)

Take a look at this table and how each setting influences our optimization research:

For more info regarding the PBIL and the Genetic Algorithm, please check these links:

- Click on "Next"
- Click on "Using optimize variables in a formula" radio box
- Click on "Update Trading System"
- Type the following formula in the formula editor:

Optimize("a", 10, 100, 10);
Optimize("b", 10, 100, 10);
Optimize("c", 2, 22, 5);
Optimize("d", 10, 90, 10);
Optimize("e", 1, 5, 1);
Optimize("f", 0, 5, 1);
Optimize("g", 5, 40, 5);

Optimize("s1", 10, 100, 10);
Optimize("s2", 10, 100, 10);
Optimize("s3", 10, 400, 20);

Optimize("m", 1, 20, 2);

SetSimSetting(_NbPositions, m);

rule1 = sma(a) > sma(b);
rule2 = rsi(c) > d;
rule3 = close > e;
rule4 = volume > f * sma(volume, g);

buy = rule1 and rule2 and rule3 and rule4;
sell = !rule1 or !rule2;

SetSimStop(_StopLoss, _Percent, s1, 0);
SetSimStop(_StopProfit, _Percent, s2, 0);
SetSimStop(_StopNBar, _Percent, s3, 0);

The trading system works as follow:
- Buy when the security first moving average is above second moving average and relative strength index is higher than a specific threshold and close above a certain price and volume higher than N-Times the average volume.
- Sell if either the first or second rule is no longer valid
- Add 3 stops (Stop Loss, Profit Stop and N-Bar Stop)

As you can see in the formula, there are many optimizable variables.
The first ones ("a" to "g") are used to optimize different indicators "moving average, relative strength index...)
The second ones (s1, s2 and s3) are used to optimize stop thresholds
The last one is used to optimize the maximum number of positions allowed in the portfolio. That value varies from 1 to 20 and is incremented by 2.
This means that you can get one the following number of positions: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19

- Now, click on "Update Trading System" to save your strategy and get back to the AI optimizer (You have now created 21.6 billion optimizations)
- Type your fitness formula

The fitness formula is used to compare trading systems and to select the best ones. By default, the annual return is used as fitness.
But if you are looking for high Sharpe systems, you can use instead:

Fitness = SharpeRatio;

Here are some other examples:

Annual Return above 10% and lowest drawdown:
if(AnnualReturn > 10)
Fitness = MaximumSystemDrawdown;
Fitness = -100;

Sharpe Ratio and total number of trades above 100:
if(NumberOfTrades > 100)
Fitness = SharpeRatio;
Fitness = -100;

Percent winners:
Fitness = PercentOfWinners;

The fitness function is very powerful and it allows you to get exactly the kind of system you are looking for.

Another example would be to get trading systems that have the maximum number of positive monthly performance:
int count = 0;
for(int i=0;i < MonthlyReturn.Count;i++)
if(MonthlyReturn[i] > 0)
Fitness = count;

Note: The above formulas are C# based. Under the fitness formula editor, make sure you choose "C#" instead of "JScript".

- Click on "Next"
- Click on "Symbols & Dates" to specify the securities to include in the trading system, the simulation/backtesting start/end period and the period (time frame and EOD/Intraday)
- Click on "Define Notifications" to specify how you want to be notified when the optimization ends
- Click on "Next" to save your optimize item
- In the optimizer form, select the optimize item we have just created then click on "Run"

Note that there are two run options:
Run once: Run the optimizer once
Run indefinitely: This means that when the optimization ends, the optimizer will start again searching for more profitable systems


It is very important to define in-sample and out-of-sample periods.
Basically, you should optimize your trading system using a specific period. Let us say that the period you are using in the optimizer spans from 2000 to 2010.
Once the optimization is completed, you should use the optimization report to backtest the best performing strategies using a different period (out-of-sample). That period for example could span from 2010 to 2014.
You can also backtest these strategies using different symbols to really assess the strength of each strategy.

NB: The system we have used in this post is simply an example. It is your turn now to create your trading systems and optimize them with the AI optimizer.

2 comments (Log in)

QuantShare Blog
Search Posts

Recent Posts

Create Graphs using the Grid Tool
Posted 1288 days ago

Profile Graphs
Posted 1393 days ago

Previous Posts

Troubleshooting a Trading System
Posted 3806 days ago

More Posts


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.