@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");
}
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.
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?