amibroker

May 19, 2008

Recently on the AmiBroker mailing list some users expressed wish to have access to some of portfolio backtest metrics available in “historical” form (i.e. as date series, as opposed to scalars), so they can be plotted as an indicator.

Implementing such functionality is actually easy with existing tools and does not require any OLE scripts. Everything you need is small custom-backtester procedure that just reads built-in stats every bar and puts them into composite ticker.In the accompanying indicator code all you need to do is simply use Foreign() function to access the historical metrics data generated during backtest.

////////////////////////////////////////// BELOW IS ACTUAL CUSTOM BACKTESTER PART// that can read any built-in metric (in this example UlcerIndex)// and store it into composite ticker for further// retrieval as data series

SetOption("UseCustomBacktestProc", True );

if( Status("action") == actionPortfolio ){bo = GetBacktesterObject();

bo.PreProcess(); // Initialize backtester

// initialize with null // you can have as many historical metrics as you want // (just duplicate line below for many metrics you want)MyHistStat1 = Null;MyHistStat2 = Null; // add your own

// the line below reads the metric and stores it as array element // you can add many lines for each metric of your choiceMyHistStat1[ bar ] = stats.GetValue("UlcerIndex"); // get ulcer index value calculated this barMyHistStat2[ bar ] = stats.GetValue("WinnersPercent"); // add your own

}

bo.PostProcess(); // Finalize backtester

// now STORE the historical data series representing the metric of your choice // duplicate the line below for as many metrics as you wantAddToComposite( MyHistStat1, "~~~UI_HISTORICAL", "X", atcFlagEnableInPortfolio | atcFlagDefaults );

In the code above, for illustration purposes, we are exporting UlcerIndex and Winners Percent metrics as data series. They are stored in composite tickers for easy retrieval from indicator level.You can easily extend code to include ANY number of metrics you want.

This site uses cookies. By browsing this site you agree to our privacy & cookies policy
Amibroker.com is a software development company and does not provide any kind of investment or brokerage services in financial markets.