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. Two-variable optimization (suitable for 3D charting)
per = Optimize("per", 2, 5, 50, 1 );
Level = Optimize("level", 2, 2, 150, 4 );
Buy=Cross( CCI(per),
-Level );
Sell = Cross(
Level, CCI(per) );
3. 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).
Displaying 3D animated optimization charts
To display 3D optimization chart, you need to run two-variable optimization first. Two variable optimization needs a formula that has 2 Optimize() function calls. An example two-variable optimization formula looks like this:
After entering the formula you need to click "Optimize" button. Once optimization is complete you should click on the drop down arrow on Optimize button and choose View 3D optimization graph. In a few seconds a colorful three-dimensional surface plot will appear in a 3D chart viewer window. An example 3D chart generated using above formula is shown below. |
![]() |

By default the 3D charts display values of Net profit against optimization variables. You can however plot 3D surface chart for any column in the optimization result table. Just click on the column header to sort it (blue arrow will appear indicating that optimization results are sorted by selected column) and then choose View 3D optimization graph again.
By visualizing how your system's parameters affect trading performance, you can more readily decide which parameter values produce "fragile" and which produce "robust" system performance. Robust settings are regions in the 3D graph that show gradual rather than abrupt changes in the surface plot. 3D optimization charts are great tool to prevent curve-fitting. Curve-fitting (or over-optimization) occurs when the system is more complex than it needs to be, and all that complexity was focused on market conditions that may never happen again. Radical changes (or spikes) in the 3D optimization charts show clearly over-optimization areas. You should choose parameter region that produces a broad and wide plateau on 3D chart for your real life trading. Parameter sets producing profit spikes will not work reliably in real trading.
3D chart viewer controls
AmiBroker's 3D chart viewer offers total viewing capabilities with full graph rotation and animation. Now you can view your system results from every conceivable perspective. You can control the position and other parameters of the chart using the mouse, toolbar and keyboard shortcuts, whatever you find easier for you. Below you will find the list.
Mouse controls:
- to Rotate - hold down LEFT mouse button and move in X/Y directions
- to Zoom-in, zoom-out - hold down RIGHT mouse button and move in X/Y directions
- to Move (translate) - hold down LEFT mouse button and CTRL key and move in
X/Y directions
- to Animate - hold down LEFT mouse button, drag quickly and release button
while dragging
Keyboard controls:
SPACE - animate (auto-rotate)
LEFT ARROW KEY - rotate vert. left
RIGHT ARROW KEY - rotate vert. right
UP ARROW KEY - rotate horiz. up
DOWN ARROW KEY - rotate horiz. down
NUMPAD + (PLUS) - Near (zoom in)
NUMPAD - (MINUS) - Far (zoom out)
NUMPAD 4 - move left
NUMPAD 6 - move right
NUMPAD 8 - move up
NUMPAD 2 - move down
PAGE UP - water level up
PAGE DOWN - water level down