How to optimize trading system

NOTE: This is fairly advanced topic. Please read previous AFL tutorials first.

Introduction

AmiBroker 3.70 brings a new feature to Automatic Analysis window called "Optimization".

The idea behind an optimization is simple. First you have to have a trading system, this may be a simple moving average crossover for example. In almost every system there are some parameters (as averaging period) that decide how given system behaves (i.e. is is well suited for long term or short term, how does is react on highly volatile stocks, etc). The optimization is the process of finding optimal values of those parameters (giving highest profit from the system) for a given symbol (or a portfolio of symbols). AmiBroker is one of the very few programs that allow you to optimize your system on multiple symbols at once.

To optimize your system you have to define from one upto ten parameters to be optimized. You decide what is a minimum and maximum allowable value of the parameter and in what increments this value should be updated. AmiBroker then performs multiple back tests the system using ALL possible combinations of parameters values. When this process is finished AmiBroker displays the list of results sorted by net profit. You are able to see the values of optimization parameters that give the best result.

Writing AFL formula

Optimization in back tester is supported via new function called optimize. The syntax of this function is as follows:

variable = optimize( "Description", default, min, max, step );

where:

variable - is normal AFL variable that gets assigned the value returned by optimize function.
With normal backtesting, scanning, exploration and comentary modes the optimize function returns default value, so the above function call is equivalent to: variable = default;

In optimization mode optimize function returns successive values from min to max (inclusively) with step stepping.

"Description" is a string that is used to identify the optimization variable and is displayed as a column name in the optimization result list.

default is a default value that optimize function returns in exploration, indicator, commentary, scan and normal back test modes

min is a minimum value of the variable being optimized

max is a maximum value of the variable being optimized

step is an interval used for increasing the value from min to max

Notes:

Examples

1. Single variable optimization:

sigavg = optimize( "Signal average", 9, 2, 20, 1 );

buy = cross( macd( 12, 26 ), signal( 12, 26, sigavg ) );
sell = cross( signal( 12, 26, sigavg ), macd( 12, 26 ) );

2. Multiple (3) variable optimization:

mfast = optimize( "MACD Fast", 12, 8, 16, 1 );
mslow = optimize("MACD Slow", 26, 17, 30, 1 );
sigavg = optimize( "Signal average", 9, 2, 20, 1 );

buy = cross( macd( mfast, mslow ) , signal( mfast, mslow, sigavg ) );
sell = cross( signal( mfast, mslow, sigavg ), macd( mfast, mslow ) );

After entering the formula just click on Optimize button in "Automatic Analysis" window. AmiBroker will start testing all possible combinations of optimization variables and report the results in the list. After optimization is done the list of result is presented sorted by the Net % profit. As you can sort the results by any column in the result list it is easy to get the optimal values of parameters for the lowest drawdown, lowest number of trades, largest profit factor, lowest market exposure and highest risk adjusted annual % return. The last columns of result list present the values of optimization variables for given test.

When you decide which combination of parameters suits your needs the best all you need to do is to replace the default values in optimize function calls with the optimal values. At current stage you need to type them by hand in the formula edit window (the second parameter of optimize function call).