A while ago I wrote about which exit reason is reported when normal exit
and n-bar stop are triggered on the same bar
(http://finance.groups.yahoo.com/group/amibroker/message/148242
<http://finance.groups.yahoo.com/group/amibroker/message/148242>  ).
Recently I returned to my pursuit and explored "raw" backtest modes. I
have found a few strange things:

1. In both raw" and "raw2" modes the exit signals corresponding to n-bar
stops are not available for user examination. That is, the backtester
duly acts upon them, but they are not to be found in
.GetFirstSignal()/.GetNextSignal() loop.

2. The selected backtest mode actually affects the execution of n-bar
stop - whether the exit is performed on Open or Close and even on which
bar! Note that I did nit attempt any custom signal processing. More on
this further below.

3.  SetBackTestMode description in both online manual 
(http://www.amibroker.com/guide/afl/afl_view.php?id=350
<http://www.amibroker.com/guide/afl/afl_view.php?id=350> ) and Amibroker
help is incomplete - it misses "raw2" modes which are described here: 
http://www.amibroker.com/guide/h_portfolio.html
<http://www.amibroker.com/guide/h_portfolio.html> .

Here's the bare bones formula to reproduce the problem, based on the
system example that comes with AmiBroker:

Buy = Cross(MACD(), Signal());
Sell = Cross(Signal(), MACD());

// trade on next bar open
SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = SellPrice = Open;

// trade size: 25% of current portfolio equity
SetPositionSize( 25, spsPercentOfEquity );

dt = DateTime();

SetOption("ActivateStopsImmediately", True);       // variant: False
SetBacktestMode(backtestRegular);                  // variant:
backtestRegularRaw

ApplyStop( stopTypeNBar, stopModeBars, 30, exitatstop = 1, volatile =
False, 1);

SetCustomBacktestProc("");

if (Status("actionEx") == actionPortfolio)
   {
    bo = GetBacktesterObject();
    _TRACE("===== START! =====");
    bo.PreProcess();
    for (i = 0; i < BarCount; i++)
      {
       for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i))
        {
          if (sig.isExit())
             _TRACE("Exit reason for " + sig.symbol + " is " + sig.reason
+
                    ", date: " + WriteVal(dt[i], formatDateTime));
        }
       bo.ProcessTradeSignals(i);
      }
    bo.PostProcess();
   }

Below please see what I'm getting when I backtest using this formula for
current symbol (DAN) only and the date range 1 Sept 2001 and 1 Mar 2010.
I provide the first columns of trade lists and the _TRACE output.

I. Backtest mode set to backtestRegular:

DAN Long 9/16/2009
6.59 9/22/2009
5.9
DAN Long 9/23/2009
7.17 9/30/2009
6.64
DAN Long (n-bar) 11/11/2009
7.06 12/24/2009
10.75
DAN Open Long 2/18/2010
10.77 3/1/2010
11.5

[1196] ===== START! =====
[1196] Exit reason for DAN is 1, date: 9/22/2009
[1196] Exit reason for DAN is 1, date: 9/30/2009
[1196] Exit reason for DAN is 5, date: 12/24/2009

II. Backtest mode set to either backtestRegularRaw or
backtestRegularRaw2:

DAN Long 9/16/2009
6.59 9/22/2009
5.9
DAN Long 9/23/2009
7.17 9/30/2009
6.64
DAN Long (n-bar) 11/11/2009
7.06 12/24/2009
10.9
DAN Open Long 2/18/2010
10.77 3/1/2010
11.5
[1196] ===== START! =====
[1196] Exit reason for DAN is 1, date: 9/22/2009
[1196] Exit reason for DAN is 1, date: 9/30/2009
[1196] Exit reason for DAN is 1, date: 1/13/2010

As you see, in both cases the n-bar stop is honored. But with the raw
mode selected there's no stop signal on that date! Here's how it looks
in the detailed log:

12/24/2009
     Entry signals(score):
     Exit signals:
     Exit Long, DAN, Price: 10.9, (Avg. exit pr. 10.9), Shares: 3382,
Commission: 16.91, (Total comm.: 33.82), Profit: 12953.1 (54.25 %),
Entry rank:1, Equity: 107958, Fx rate: 1
     0 Open Positions: , Equity: 108465, Cash: 108465

Weird, eh? Trade exited without a signal!

Secondly, you will notice the exit price differs. With regular mode
selected it's the Open price, with raw mode it's Close. As I understood
the manual, raw mode should only introduce back REDUNDANT signals, that
is, unless I'm doing some custom processing, the actual trades should be
the same, not? Not to mention that raw (as opposed to raw2) should not
affect exit signals at all!

If we set ActivateStopsImmediately to False, the effect of raw mode
becomes even more profound - the stop is actually performed one bar
later than in plain regular mode.

Any idea what's going on here?

Thanks and regards!

Reply via email to