November 20, 2014
How to display indicator values in the backtest trade list
Backtesting engine in AmiBroker allows to add custom metrics to the report, both in the summary report and in the trade list. This is possible with Custom Backtester Interface, which allows to modify the execution of portfolio-level phase of the test and (among many other features) adjust report generation.
Due to the fact that the report generation occurs in 2nd phase of the test, when the backtester works on ~~~EQUITY ticker, we can not refer directly to given indicators. For example, to display ATR values – calling ATR() function directly is not enough, because we want to see ATR values of the traded symbol, while in portfolio-phase of the test we are no longer working on that symbol’s quotes.
So, we need to:
- store the values of indicators in static variables in the 1st phase of the test (when individual symbols are processed). This can be done with static variables, creating separate static variable for each symbol
- read stored values once the backtester reaches the portfolio phase of the test.
The following formula shows how this can be coded. The formula below displays the value of ATR indicator for the entry bar of given trade:
SetCustomBacktestProc( "" );
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
// run default backtest procedure without generating the trade list
bo.Backtest( True );
// iterate through closed trades
for ( trade = bo.GetFirstTrade( ); trade; trade = bo.GetNextTrade( ) )
{
// read ATR values and display as custom metric
symbolATR = StaticVarGet( trade.Symbol + "ATR" );
trade.AddCustomMetric( "Entry ATR", Lookup( symbolATR, trade.EntryDateTime ) );
}
// iterate through open positions
for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos( ) )
{
// read ATR values and display as custom metric
symbolATR = StaticVarGet( trade.Symbol + "ATR" );
trade.AddCustomMetric( "Entry ATR", Lookup( symbolATR, trade.EntryDateTime ) );
}
// generate trade list
bo.ListTrades( );
}
// your trading system here
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );
// assign indicator values to ticker-specific variables
StaticVarSet( Name() + "ATR", ATR( 15 ) )
Filed by Tomasz Janeczko at 6:22 am under Custom Backtest
Comments Off on How to display indicator values in the backtest trade list