I have been using a JScript to run the AA optimizer for each stock in a
list and store the optimum parameters in a file. The script is below. I
use a custom backtester that records the parameters that perform the
best so far, and after each optimization run, then I run another script
that saves the best parameters.
This works fine in some cases and it runs through all of a 100 stocks,
but sometimes the application gets an error in one of a couple different
places. I'd like to find out why this fails sometimes and how I can
avoid it. More details on the failures below.
Here is the custom backtester code, embedded in my afl script.
isOptimizeSetup = Status("ActionEx") == actionExOptimizeSetup;
if (isOptimizeSetup)
{
maxObjective = -999999999;
StaticVarSet("maxObjective", maxObjective);
}
else
{
maxObjective = StaticVarGet("maxObjective");
}
SetOption("UseCustomBacktestProc", True );
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject( );
bo.Backtest( ); // run default backtest procedure
st = bo.GetPerformanceStats(0); // get stats for all trades
// Compute Objective value to be maximized.
// To minimize, maximize the negative.
// e.g. Objective = - ( abs( 100 - X * Y ) + abs( X - Y ) );
NP = st.GetValue("NetProfit");
LTL = st.GetValue("LosersTotalLoss");
objective = NP - sqrt(-LTL);
// defaultParams is a string with all the parameter values assigned
by Optimize calls.
_Trace( "max: " + maxObjective + " objective: " + objective + "
params: " + defaultParams);
if ( objective > maxObjective )
{
maxObjective = objective;
_Trace("new max: " + maxObjective + " params: " + defaultParams );
StaticVarSet("maxObjective", maxObjective );
StaticVarSetText("maxParams", defaultParams);
}
// Add "objective" column.
// REMEMBER - Set the Optimization target to "objective" in AA
Settings / Walk-Forward
bo.AddCustomMetric( "objective", objective );
}
In my afl script for saving the optimum parameters I do the following:
maxParams = StaticVarGetText("maxParams");
_Trace("Save params " + maxParams);
saveParams(maxParams);
I am leaving out a few details which should be irrelevant. The
saveParams function ultimately calls fputs to write the parameters.
And here is my script for running the AA Optimize and calling the save
script for each stock:
// Loop through all stocks in a watchlist, or (if negative) all stocks.
// For each one, run the optimizer, and then run the saverFormula, which
should do the right thing to save parameters.
// I use a custom backtester that remembers the optimum set of
parameters.
database = "C:\\Program Files\\Amibroker\\IB";
iWatchList = -1; // negative means all symbols - see below
formula = "Formulas\\Systems\\Mine\\HLMA.afl";
saverFormula = "Formulas\\Utilities\\SaveOptParams.afl";
settingsFile = "path to settings file";
AB = new ActiveXObject( "Broker.Application" );
AB.LoadDatabase( database );
AB.Visible = false; // Maybe visible mode causes problems?
AA = AB.Analysis;
Qty = AB.Stocks.Count;
//WScript.echo("Total number of stocks: " + Qty);
for ( i = 0; i < Qty; i++ )
{
Stk = AB.Stocks( i );
if ( iWatchList < 0 ||
( iWatchList < 32
? ( Stk.WatchListBits & ( 1 << iWatchList ) )
: ( Stk.WatchListBits2 & ( 1 << ( iWatchList - 32 ) ) ) )
)
{
//WScript.echo("Optimize Stock: " + Stk.Ticker);
WScript.Sleep( 4000 ); // without this, the script will crash
occasionally.
try
{
Doc = AB.Documents.Open( Stk.Ticker );
WScript.Sleep( 4000 ); // 4 seconds delay. Why?
/* load formula from external file */
AA.LoadFormula( formula );
/* optional: load settings */
// AA.LoadSettings( settingsFile );
/* set apply to and range */
AA.ApplyTo = 1; // use current stock
AA.RangeMode = 2; // use last n days of quotes
AA.RangeN = 100;
/* run optimize for the portfolio, which is just one stock
*/
AA.Optimize( 0 ); // 0 == portfolio opt
//AA.Export( "C:\\" + Stk.Ticker + ".csv" );
AA.LoadFormula( saverFormula );
AA.Backtest();
}
catch ( err )
{
}
finally
{
Doc.Close();
}
WScript.Sleep( 1000 );
}
}
Regarding the failures, the lastest failure is inside the custom
backtester code, on the line that says bo.Backtest(). The error message
alert says:
Error 19.
COM method/function 'Backtest' call failed.
Internal application error.
Earlier, I was getting occasional errors in the jscript on the line that
says Doc = AB.Documents.Open( Stk.Ticker ); .
I don't recall the error message. My stocks have not changed and I was
running the same script with errors occurring at different times. I
added the Sleep calls to maybe avoid the problem, but it kept happening.
I wrapped a try ... catch around the whole body of the loop to hopefully
continue even if an error occurs, but now I am getting the error
described above inside.
Any suggestions appreciated.
dan
[EMAIL PROTECTED]