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


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));
/* 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  ); 


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

1 Like