If you are using JScript inside AFL (by the way I don't see the reason to use 
JScript anymore since
looping, functions and all that stuff is in AFL), and call any external ActiveX 
- they may change
the current working directory for current process.

Also individual optimization for each symbol is built in feature - don't see 
the reason to make it hard way as
you do.  It is called individual optimization and available  via Optimize( 1 )

see http://www.amibroker.com/guide/objects.html#Analysis

Analysis.Optimize(Type = 2 ); - runs optimization
Type parameter can be one of the following values:
0 : portfolio backtest/optimize
1 : individual backtest/optimize
2 : old backtest/optimize
3 : walk-forward test (AmiBroker version 5.11.0 or higher)

Best regards,
Tomasz Janeczko
amibroker.com
  ----- Original Message ----- 
  From: liberte721 
  To: [email protected] 
  Sent: Wednesday, October 29, 2008 6:11 AM
  Subject: [amibroker] Re: Auto optimize all stocks


  Thanks for the feedback.  I did a full symantec virus scan today, and
  three different rootkit scans, and all came up with nothing (except for
  some supposedly innocuous entries and cookies).  This computer is only
  a couple months old, and not used for much on the internet (behind
  firewalls), so I doubt I have a virus or rootkit, but I'll keep
  checking.

  I use #include_once for all includes, all relative to the 
  "Standard include path", which is just "Formulas\Include".
  Nothing in my script is trying to change the current working directory
  for AmiBroker, so it must be some external program.  But if programs
  are running outside of AmiBroker, could any of them change the current
  working directory for AmiBroker without being a virus or rootkit?  I
  don't know how windows (XP) works well enough to know, but that would
  be a bad situation if it were possible.

  Over a couple years of using AmiBroker, on three different computers, 
  I have frequently seen script error messages reported relative to the wrong
  files.  But these are legitimate errors, just reported incorrectly, which I 
  deal with just by editing carefully in only making modest code changes
  so I have a chance of figuring out what has gone wrong.

  I wonder if using EnableScript("jscript"); has something to do with these
  file mixups.  I am not doing any file manipulation in the jscript code, but
  I am doing AFL file operations.  I can send you my code if you want to take
  a closer look.

  I will try running my script with minimal other external programs to see
  if that helps.  Thanks again.

  dan


  --- In [email protected], "Tomasz Janeczko" <[EMAIL PROTECTED]> wrote:
  >
  > The error message you get is clearly because the include can no longer be 
found. Apparently you are using relative
  > paths, and something in your script or some external program (virus?) has 
changed current working directory for AmiBroker.
  > I suggest deep virus and rootkit scan using different scanners, as rootkits 
can hide so ordinary scanners don't detect anything.
  > 
  > Best regards,
  > Tomasz Janeczko
  > amibroker.com
  > ----- Original Message ----- 
  > From: liberte721 
  > To: [email protected] 
  > Sent: Tuesday, October 28, 2008 5:18 AM
  > Subject: [amibroker] Re: Auto optimize all stocks
  > 
  > 
  > I've made some progress figuring out the crashes in my auto automize 
jscript. 
  > 
  > First, I tried using the Commentary window like this:
  > 
  > AB.Commentary.LoadFormula(saverFormula);
  > AB.Commentary.Apply(); 
  > AB.Commentary.close();
  > 
  > But this had an undesirable side effect in that the saverFormula was 
executed 4 times. (The first time appeared to be using a cache of the previous 
version of the saverFormula, in fact.) Why there are 4 executions, I don't know 
(I don't have AmiBroker open in another window, and I start my auto-optimize 
script with no AmiBroker running), and I don't see any easy way to detect when 
a particular execution is one of the extras. So I gave up on that.
  > 
  > So using the Backtest call as before, I added another Sleep call after 
loading the formula and before the backtest, like so:
  > 
  > AA.LoadFormula( saverFormula );
  > WScript.Sleep( 1000 );
  > AA.Backtest();
  > WScript.Sleep( 1000 );
  > 
  > And this seems to work much more reliably. I made it through one whole run 
without incident.
  > 
  > On to the next crash... Gaining confidence, I was simultaneously running a 
few browser windows (Firefox and IE) but I was not using the computer 
otherwise, when, after 40 minutes of running, I got the following interesting 
error.
  > 
  > DBVOn = ParamToggle("DebugView","OFF|ON", 0);
  > 
  > function T
  > ---------^
  > 
  > Error 31.
  > Syntax error, expecting IDENTIFIER
  > 
  > This is bizarre because this same bit of script has been executing many 
thousands of times without problem. This is in my debug.afl that is the first 
#include in my main afl. Here it is:
  > 
  > 
  > 
  > ClearDBV = ParamTrigger("Clear DebugView","CLEAR DBV"); 
  > if( ClearDBV ) _TRACE("# DBGVIEWCLEAR"); 
  > 
  > //traceIfScanning = Status("action") == actionBacktest; 
  > 
  > DBVOn = ParamToggle("DebugView","OFF|ON", 0); 
  > 
  > function TRACE( DBVString ) 
  > { 
  > global DBVOn; 
  > global symbol; 
  > statusActionList = "ZERO,INDICATOR,COMMENTARY,SCAN,EXPLORATION,BACKTEST / 
OPTIMIZE"; 
  > statusAction = StrExtract( statusActionList, Status( "action" ) ); 
  > 
  > if ( DBVOn ) 
  > _TRACE( "# " + symbol + 
  > " " + statusAction + 
  > " " + DBVString 
  > ); 
  > } 
  > 
  > 
  > So is AmiBroker totally confused at this point? Why does it think there is 
a syntax error? It's pointing at what looks like a perfectly fine identifier 
even. Is it maybe looking in the wrong file? 
  > 
  > I believe this AB OLE interface needs to be made much more robust before we 
can expect mere mortals to use it. It needs a lot more detailed documentation 
as well. I find I am guessing half the time what things are supposed to be 
doing. 
  > 
  > Thanks for any help.
  > 
  > 
  > 
  > dan
  > 
  > 
  > --- In [email protected], "Daniel LaLiberte" daniel.laliberte@ 
wrote:
  > >
  > > Thanks for your reply and ideas.
  > > 
  > > 1. I'm not sure the delays are necessary, but adding the first delay 
before
  > > the AB.documents.open call appeared to help. The second delay was in the
  > > code that I had copied to start with, and I didn't see any explanation why
  > > it was there. It is disturbing to think that the delays would help,
  > > actually, because there is no amount of delay time that would be correct 
all
  > > the time. There should be a way to call things safely. I would be happy to
  > > add a busy wait loop if necessary, or a call to some function that doesn't
  > > return until it is safe to proceed.
  > > 
  > > 2. Making the problem worse might be a good strategy to figure it out.
  > > There are many more ways things could go wrong than right, however, so I
  > > could easily get distracted pursing the wrong things. Is there a list of
  > > known causes of backtester failure? I doubt I have any obscure corner
  > > cases in my afl script that cause runtime errors, e.g. divide by zero and
  > > such, but it is possible. An "internal application error" doesn't sound
  > > like that anyway.
  > > 
  > > 3. I saw that use of the Commentary mode previously. Thanks for reminding
  > > me. I'll try it. It would be handy if the optimizer would run one more
  > > time after completing a run and set Status("ActionEx") ==
  > > actionExOptimizeShutdown.
  > > 
  > > dan
  > > 
  > > On Sat, Oct 25, 2008 at 10:27 PM, Steve Davis <_sdavis@ wrote:
  > > 
  > > > Thanks for posting this. I intended to write something like this
  > > > myself and never got around to it. Some thoughts:
  > > >
  > > > 1. Are you sure the delays are needed? I've had good luck using AutoIt
  > > > to watch for windows to open or close.
  > > >
  > > 
  > > 
  > > > 2. Instead of trying to fix the problem, try to make it happen more
  > > > often. What would happen if you never set the buy/sell arrays? Would
  > > > this cause the backtester to fail?
  > > >
  > > > 3. The parameter save script is invoking the backtester. There is
  > > > another way to execute general purpose afl from a script without using
  > > > the backtester. See this message:
  > > > http://finance.groups.yahoo.com/group/amibroker/message/131038
  > > >
  > > > Cheers,
  > > > Steve
  > > >
  > > > --- In [email protected] <amibroker%40yahoogroups.com>,
  > > > "liberte721" daniel.laliberte@
  > > > wrote:
  > > >
  > > > >
  > > > > 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
  > > > > daniel.laliberte@
  > > > >
  > > >
  > > > 
  > > >
  > > 
  > > 
  > > 
  > > -- 
  > > Daniel LaLiberte
  > > liberte@
  > > daniel.laliberte@
  > >
  >
   

Reply via email to