Stop values when using ApplyStop

I'm attempting to use ApplyStop for profit and loss stops but I'm not getting the values I would expect to see.

Here's the entirety of the code I'm using:

SetOption( "ActivateStopsImmediately", True );

period = 10; // 10 bars 
ChannelBreakUp = Close > Ref( HHV( High, period ), -1 );

Buy = ChannelBreakUp;
Sell = 0;

ApplyStop(stopTypeProfit, stopModePoint, 2.0, 1);
ApplyStop(stopTypeLoss, stopModePoint, 5.0, 1);

So profit is set to 2 points - however in the backtest, some of the exits are not 2 points:

Backtest

In the highlighted trade the exit price is 110 but I would expect it to be 109.90 which would be 2 points above the entry of 107.90.

In the Analysis settings I have the trades set to "Trade NEXT BAR on OPEN, the stops exit INTRADAY"

I know it has to be something simple but I'm just not seeing it. Any help would be greatly appreciated.

To me this looks like you have a few up/down gaps in there in your price data because in your picture it always exits with higher difference than being set in ApplyStop.

I tried with SPY adjusted and non adjusted prices and no where am I seeing your differences (exceeding 2 and 5 points all the time). What data source do you use? Yahoo?

SetOption( "ActivateStopsImmediately", True );

SetTradeDelays(1, 1, 1, 1 );
SetPositionSize(1, spsShares );

period = 10; // 10 bars 
ChannelBreakUp = Close > Ref( HHV( High, period ), -1 );

BuyPrice = SellPrice = Open;

Buy = ChannelBreakUp;
Sell = 0;

Short = Cover = 0;

ApplyStop(stopTypeProfit, stopModePoint, 2.0, 1);
ApplyStop(stopTypeLoss, stopModePoint, 5.0, 1);

So again if I do backtest with dividend&split adjusted prices then I am getting nowhere your entry and exit prices (as well as I am not getting exceeding set targets all the time). See,

130

The only larger difference of more than 5 points loss on Feb 26th is due to down gap. See below picture

54

But as for March 25th I am getting exact 2 points profit contrary to your result list.

So to me it looks like you have lots of gaps in there indeed (more than I have for same time period). So check your trades on chart to back up whether you have gaps in there. Later you might re-download SPY data from Yahoo via AmiQuote.


Now, if you want to try non adjusted price data then go to Formats folder of AmiBroker directory on HDD and open aqh.format file with Notepad and replace AdjClose by Skip. Then save and re-download SPY from Yahoo via AmiQuote. Then re-do test with upper code.

130

You should get similar entry exits if not the same as below

13

If you look at upper picture then you can see exit to entry differences are in line with what has been set in ApplyStop.

Now, March 25th is different here. The reason for that one is shown in picture below. It is because of gap (again) -> up gap this time. So that's why more than 2 points profit.

36

4 Likes

Thanks fxshrat I appreciate the detailed reply. I suspected gaps at first as well but it doesn't appear that's the case. I'm using Norgate data but I went ahead and downloaded data from Yahoo just to see if I could get the same values. So here's the backtest with unadjusted Yahoo data:

image

I'm getting the same value for trade open but I get 137.67 for the exit instead of 137.05 which it should be since that is the open and above the 2 point threshold. In the chart I see this:

image

The open is 137.05 and the high is 137.67 which seems to be what it's picking as the exit.

I also tried Norgate data with no adjustment and ended up with the same thing:

image

image

I'm using version 6.30 of Amibroker if that makes any difference.

The exit prices on Aug, Sept, Nov 2008 and Jan 2009 of your first picture do not exist as OHLC in Yahoo price data. It makes no sense to me exiting at integer values.

Have you tried Detailed log setting of Analysis settings - Report tab and re-run test? Check what log says.

Have you included any Custom backtest procedure in Analysis settings - Portfolio tab at bottom?

Please upload Analysis project file (*.apx file) because I'm not really into playing wheel of fortune.

1 Like

Geez, I knew it was something silly - I went back through each of the Analysis settings and lo and behold I had "Tick Size" set to "1" for some reason. I set it to "0" and everything works as it should.

Sorry for the wild goose chase fxshrat but I do really appreciate your time and response.

1 Like