[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] Re: Auto optimize all stocks



PureBytes Links

Trading Reference Links

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@xxxxxxxxx> 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 amibroker@xxxxxxxxxxxxxxx, "liberte721" <daniel.laliberte@xxx>
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@xxx
>




--
Daniel LaLiberte
liberte@xxxxxxxxxxxxx
daniel.laliberte@xxxxxxxxx

__._,_.___

**** IMPORTANT ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

*********************
TO GET TECHNICAL SUPPORT from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com
*********************

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html

*********************************




Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe

__,_._,___