Clarification on displaying indicator values in backtest trade list

Source code below is extracted from the link below;
http://www.amibroker.com/kb/2014/11/20/how-to-show-indicator-values-in-backtest-trade-list/

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 ) )

The sample source code works fine. What puzzles me is when I comment away the code section under // iterate through open positions, I don't see see any difference in the rows at the backtest trade list. It seems like redundant code.

What is the purpose of adding custom metric in open positions when they don't seem to have any effect in the rows displayed in backtest trade list? When is adding custom metric in open positions relevant?

It ADDS custom metrics in trade list.
First loop adds them for CLOSED trades. Second for OPEN POSITIONS.

If and only if you don't have open positions, second loop that you commented out, won't make difference.

2 Likes

Thank you for the clarification. It is clear now.

My backtest results happen to have no open positions, that's why I didn't see any difference.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.