Optimization and exploration together ?

Is it posssible to find the best moving average crossover for 35 stocks I have on amibroker through optimization and then run the optimized moving average in exploration mode to check it each day if a setup has triggered or not.
The purpose is to get to know if today, the best moving average crossover has happened on any stock or not.

This is recipe for overfitting but you can use batch to run optimization and exploration right afterwards. To get optimized values of parameters you can use a feature that is not widely-known. Below is a copy from ReleaseNotes.html that describes this functionality (AmiBroker 6.30 required)

Auto-optimization framework

AFL: SetOption("OptimizeSaveParams", True );

- turns on generation of AFL file that contains values of optimization parameters producing best result. The generated file has the same name as formula run but has .opt.afl extension

This functionality is provided to simplify creation of auto-optimization schemes

How to use:

  1. Create your main formula, say it's file name is "Formulas/Custom/MainFormula.afl"
  2. Create your opt param formula: "Formulas/Custom/MainFormula.opt.afl"
    In the opt param formula type:
// This file will be automatically overwritten by optimizer with best values
first_param = 6;
second_param = 12;
  1. In the main formula type:
#include "Formulas/Custom/MainFormula.opt.afl"

x = Optimize("first_param", first_param, 3, 10, 1 );
y = Optimize("second_param", second_param, 11, 30, 1 );

// causes automatic generation of MainFormula.opt.afl
SetOption("OptimizeSaveParams", 1 );

// dummy system using two params
Buy = Cross( C, MA( C, x ) );
Sell = Cross( MA( C, y ), C ); 

// exploration code added for you
Filter = Status("lastbarinrange");
AddColumn( Buy, "Buy" );
AddColumn( Sell, "Buy" );

17 Likes

When I copy the main code given by you, I get following message

Error 42. #include failed because the file does not exist: <filename> (current working directory is '...')

Occurs when specified include file does not exist.

If file does not exist in Custom folder then create empty file "MainFormula.opt.afl" using formula editor.

Can someone guide me, I can't do the above ?
Please help

Open formula editor (Analysis - Formula editor).
In the white formula name field replace given formula name by MainFormula.opt.
Press keybord's enter key to update name.
Save formula.

To sum up,
I have created 2 AFL's one named "mainformula.opt" and second one named "Test".
in the first one I have written this

first_param = 6;
second_param = 12;

In the second code I have written this

#include "Formulas/Custom/MainFormula.opt.afl"

x = Optimize("first_param", first_param, 3, 10, 1 );
y = Optimize("second_param", second_param, 11, 30, 1 );

// causes automatic generation of MainFormula.opt.afl
SetOption("OptimizeSaveParams", 1 );

// dummy system using two params
Buy = Cross( C, MA( C, x ) );
Sell = Cross( MA( C, y ), C ); 

// exploration code added for you
Filter = Status("lastbarinrange");
AddColumn( Buy, "Buy" );
AddColumn( Sell, "Buy" );

However in the second formula, when I check the code I am getting this error - unsupported field in SetOptions

Please assist what should I do ?

You need to upgrade to AmiBroker 6.30.

Even I am interested in this .Isn't there any other method by which we can first do optimization and then run exploration on the basis of optimization results ?

If you are interested then just purchase AmiBroker. :wink:

wonderful...
But I have a doubt. how can i make it to auto optimize each day and use the result for the next day.
Iam a novice with Amibroker. so any help would be much appreciated.
example: optimize and find the best MA for N no. of days at the end of each day and use that MA for the next day. The same should be used for back testing too.
TIA.