Using the ~~~EQUITY symbol as part of strategy rules

Hello everyone,

I’m experimenting with using the ~~~EQUITY array as a factor in the rules of a trading system during a back test.
I’m finding that when I include the rules that reference ~~~EQUITY, my back test generates different results during consecutive runs. After a few runs though, the back test results seem to settle down and then stop changing. That issue does not happen if I comment out the rules that look at ~~~EQUITY.

I’m guessing that perhaps the equity symbol retains its values after each back test and then the next back test is seeing old values. I had expected that ~~~EQUITY would be re-initialized to the initial equity set when starting a back test.

So my question is:

  • Is there a way to reset the values in the ~~~EQUITY symbol to reflect the starting equity at the beginning of a back test?

The right way to use equity in your formula is in the phase 2 custom backtest. The backtest object exposes initial equity and current equity during the backtest. The complete equity array is also available, but is not complete until the backtest runs to completion. Refer to the documentation.

1 Like

After reading through the custom backtester interface docs, I’m thinking I’ll have to resort to coding in the low level in order to use in-progress equity to affect buy/sell operations.

Not necessarily. If all you want to do is prevent or allow entries based on your equity criteria, you can use the mid-level CBT.

If you process the signals conditionally based on your equity criteria, you can prevent an entry simply by setting sig.Price = -1;

eg

SetCustomBacktestProc("");
if (Status("action") == actionPortfolio)
{
    bo = GetBacktesterObject();    //  Get backtester object
    bo.PreProcess();    //  Do pre-processing
    for (i = 0; i < BarCount; i++)    //  Loop through all bars
    {
		PreventEntryBasedOnEquity = bo.Equity … // whatever you're using
        
        if (PreventEntryBasedOnEquity)
        {
			for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i))
			{
				if (sig.IsEntry() && sig.IsLong()) // long entry this bar
					sig.Price = -1; // prevent entry
			}
		}
		bo.ProcessTradeSignals(i);    //  Process trades at this bar (mid-level CBT)
    }    //  End of for loop over bars
    bo.PostProcess();    //  Do post-processing
}
4 Likes

Generally ~~~EQUITY is post-factum equity for display of equity/drawdown charts. You should not use it for position sizing. There are other methods to do equity-based position sizing.

First and simplest is described in the manual:
http://www.amibroker.com/guide/h_portfolio.html

Almost never you really need dollar value of portfolio equity to perform equity-based position sizing. The fundamental thing to do is to open your mind. To do so read the article that presents Van-Tharp-style position sizing
http://www.amibroker.com/kb/2014/10/12/position-sizing-based-on-risk/

But if you really insist that you need dollar value (believe me you don’t), see this sample
http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/

that shows how to properly use current portfolio equity while doing backtest.

Sometimes you may need/want single-symbol equity, see http://www.amibroker.com/guide/a_equity.html

3 Likes