Assignment of ~~~EQUITY

@Tomasz and other AB experts: I am trying to use the ~~~EQUITY variable inside a low-level CBT so that I can retrieve an array of the cash levels. A broad outline of my code is shown below, and yes I know that I could use bo.cash to store the current cash value in an array as each bar completes, but I have reasons for not wanting to do that.

The problem I'm experiencing is that when I call Foreign("~~~EQUITY","L"), I am getting the cash values from the previous back test, not the current one. Based on the documentation, I expected that the ~~~EQUITY symbol would be valid through the current bar after I called bo.UpdateStats(bar,2);, or at the very worst after calling bo.PostProcess(). However, that doesn't seem to be the case.

Any guidance would be appreciated.

SetOption("usecustombacktestproc", True);
SetCustomBacktestProc("");
SetBacktestMode(backtestRegularRaw);

<<Buy and Sell Logic>>

if (Status("action") == actionPortfolio)
{
	// Get backtester object
	bo = GetBacktesterObject(); 
	bo.PreProcess();	

	for (bar=0; bar < BarCount; ++bar)
	{		
	
		<< My Signal Processing>>
		
		bo.UpdateStats(bar,2);
	}
		
	bo.PostProcess();
		
	stats = bo.GetPerformanceStats( 0 );
	cash = Foreign("~~~EQUITY","L");
}
1 Like

Inside Custom Backtest, you should not use Foreign("~~~EQUITY", ...). This special ticker is written once backtest is completed. Remember there are multiple threads and multiple Analysis windows.
Multitheaded program like AmiBroker holds working data set for every running Analysis window as thread-private storage and global symbol (like ~~~EQUITY) is only written just once when everything is done.

Current Equity and Cash is property of backtester object (bo) and should be accessed directly as explained in manual: http://www.amibroker.com/guide/a_custombacktest.html see properties of Backtester object:

  • bo.EquityArray - holds equity as array (updated upto current bar)
  • bo.Cash - holds current cash (can store it bar by bar)
  • bo.Equity - holds current equity value
2 Likes

Thanks @Tomasz. I was hoping there was a way to get the entire array of Cash values so that I could use them to generate metrics from a #include file. So yes, I can store bar-by-bar values retrieved with bo.Cash, but then every AFL that #include's the metrics will need to have that extra assignment in it.

You said "global symbol (like ~~~EQUITY) is only written just once when everything is done". Since some of the Report Charts use "C" to refer to the equity curve, that must mean that ~~~EQUITY is written and set as the current symbol after the CBT exits but before any of the Report Charts AFL is called, right?

1 Like

Report Charts are executed after backtest is completed. They can use ~~~EQUITY as it is already updated at the time when they are run.

1 Like