{"id":73,"date":"2008-05-19T04:22:54","date_gmt":"2008-05-19T09:22:54","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=73"},"modified":"2014-12-06T06:54:05","modified_gmt":"2014-12-06T11:54:05","slug":"historical-portfolio-backtest-metrics","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2008\/05\/19\/historical-portfolio-backtest-metrics\/","title":{"rendered":"Historical portfolio backtest metrics"},"content":{"rendered":"
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.<\/p>
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. <\/p>
The code below shows the BACKTEST formula with custom backtester part:<\/p> 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. Now in order to Plot metrics as indicators, use this simple formula:<\/p> As you can see with one Foreign function call you can read the historical value of any metric generated by the backtester.<\/p> NOTE: when running backtest please setup a filter in AA that EXCLUDES composites (group 253) from backtest set.<\/p>","protected":false},"excerpt":{"rendered":" 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 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[43,53,14,15,21],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/73"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":1,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":797,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/73\/revisions\/797"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<\/span>\/\/ Replace lines below with YOUR TRADING SYSTEM
<\/span>EnableRotationalTrading<\/span>();
<\/span>PositionScore <\/span>= <\/span>1<\/span>\/<\/span>RSI<\/span>(<\/span>14<\/span>);
<\/span>PositionSize <\/span>= -<\/span>25<\/span>;
<\/span>SetOption<\/span>(<\/span>"WorstRankHeld"<\/span>, <\/span>7 <\/span>);
<\/span>SetOption<\/span>(<\/span>"MaxOpenPositions"<\/span>, <\/span>4 <\/span>);
<\/span>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
\/\/ 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
<\/span>SetOption<\/span>(<\/span>"UseCustomBacktestProc"<\/span>, <\/span>True <\/span>);
if( <\/span>Status<\/span>(<\/span>"action"<\/span>) == <\/span>actionPortfolio <\/span>)
{
<\/span>bo <\/span>= <\/span>GetBacktesterObject<\/span>();
<\/span>bo<\/span>.<\/span>PreProcess<\/span>(); <\/span>\/\/ Initialize backtester
\/\/ initialize with null
\/\/ you can have as many historical metrics as you want
\/\/ (just duplicate line below for many metrics you want)
<\/span>MyHistStat1 <\/span>= <\/span>Null<\/span>;
<\/span>MyHistStat2 <\/span>= <\/span>Null<\/span>; <\/span>\/\/ add your own
<\/span>for(<\/span>bar<\/span>=<\/span>0<\/span>; <\/span>bar <\/span>< <\/span>BarCount<\/span>; <\/span>bar<\/span>++)
{
<\/span>bo<\/span>.<\/span>ProcessTradeSignals<\/span>( <\/span>bar <\/span>);
<\/span>\/\/ recalculate built-in stats on EACH BAR
<\/span>stats <\/span>= <\/span>bo<\/span>.<\/span>GetPerformanceStats<\/span>( <\/span>0 <\/span>);
<\/span>\/\/ the line below reads the metric and stores it as array element
\/\/ you can add many lines for each metric of your choice
<\/span>MyHistStat1<\/span>[ <\/span>bar <\/span>] = <\/span>stats<\/span>.<\/span>GetValue<\/span>(<\/span>"UlcerIndex"<\/span>); <\/span>\/\/ get ulcer index value calculated this bar
<\/span>MyHistStat2<\/span>[ <\/span>bar <\/span>] = <\/span>stats<\/span>.<\/span>GetValue<\/span>(<\/span>"WinnersPercent"<\/span>); <\/span>\/\/ add your own
<\/span>}
<\/span>bo<\/span>.<\/span>PostProcess<\/span>(); <\/span>\/\/ Finalize backtester
\/\/ now STORE the historical data series representing the metric of your choice
\/\/ duplicate the line below for as many metrics as you want
<\/span>AddToComposite<\/span>( <\/span>MyHistStat1<\/span>, <\/span>"~~~UI_HISTORICAL"<\/span>, <\/span>"X"<\/span>, <\/span>atcFlagEnableInPortfolio <\/span>| <\/span>atcFlagDefaults <\/span>);
<\/span>\/\/ you can add your own as shown below
<\/span>AddToComposite<\/span>( <\/span>MyHistStat2<\/span>, <\/span>"~~~WP_HISTORICAL"<\/span>, <\/span>"X"<\/span>, <\/span>atcFlagEnableInPortfolio <\/span>| <\/span>atcFlagDefaults <\/span>);
<\/code>
You can easily extend code to include ANY number of metrics you want.<\/p>PlotForeign<\/span>(<\/span>"~~~UI_HISTORICAL"<\/span>, <\/span>"UlcerIndex Historical"<\/span>, <\/span>colorRed<\/span>, <\/span>styleLine <\/span>);
<\/span>PlotForeign<\/span>(<\/span>"~~~WP_HISTORICAL"<\/span>, <\/span>"Winners Percent"<\/span>, <\/span>colorBlue<\/span>, <\/span>styleLine <\/span>| <\/span>styleOwnScale <\/span>)<\/code>