Access formula or array from backtester

I am trying to access array values from backtester but it works for some and not for others.
For eg. I can lookup BarIndex() but cannot lookup RSI formula.
I tried this example in 5 min charts for aud.usd
In addition, i tried to send static arrays to CBT but it did not work. I can however access static values in the CBT

SetCustomBacktestProc(""); 

period = 20; // number of averaging periods 
m = EMA( Close, period ); // exponential moving average
Buy = Cross( Close, m ); // buy when close crosses ABOVE moving average
Sell = Cross( m, Close ); // sell when closes crosses BELOW moving average
RSIVal=ValueWhen(Buy ,RSI(14));
Short=Cover=False;
/* Now custom-backtest procedure follows */ 
if( Status("action") == actionPortfolio ) 
{ 
    bo = GetBacktesterObject(); 

    bo.Backtest(1); // run default backtest procedure 

    // iterate through closed trades first 
    for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() ) 
    { 
      entryBar = Lookup( BarIndex(), trade.entrydatetime, 0 );
      entryRSI = Lookup( RSIVal, trade.entrydatetime, 0 );
       entryRSIAlternative = Lookup( RSI(14), trade.entrydatetime, 0 );
      
        // do your calculations involving trade objects (list of closed trades)
        yourmetric = 1; // your value here
        trade.AddCustomMetric(" entryBar",  entryBar  ); 
        trade.AddCustomMetric(" entryBar entryRSI", entryRSI  ); 
          trade.AddCustomMetric(" entryBar  entryRSIAlternative",  entryRSIAlternative  ); 
    } 

    bo.ListTrades(); 
}

You have to pass arrays (such as RSI()) from first phase to second phase (CBT) via static variables. There exist examples in this forum already.

Also look up knowledge base
http://www.amibroker.com/kb/2014/11/20/how-to-show-indicator-values-in-backtest-trade-list/

1 Like