Slippage on Applystop()

I would like to buy/short once the H/L goes beyond the ATR(5), and using 0.01% stopTypeTrailing to exit.

Buy = (H-Open) > ATR(5);
BuyPrice = H;

Short = (Open - L) > ATR(5);
ShortPrice = L;

Sell = Short;

Cover = Buy;

ApplyStop(stopTypeTrailing, stopModePercent, 0.01);

Slippage = TickSize;
BuyPrice = BuyPrice + Slippage; 
SellPrice = SellPrice - Slippage; 
CoverPrice = CoverPrice + Slippage; 
ShortPrice = ShortPrice - Slippage; 
 
Equity(1);  

After backtest, the entry price adds the slippage correctly. However, the exit price which triggered by the trailing stop doesn't have the slippage added. May I know how to do that?

Thanks!

I haven't run the code but plainly looking at it, you order of statements is not sequentially correct.

Slippage = TickSize;
BuyPrice = BuyPrice + Slippage; 
SellPrice = SellPrice - Slippage; 
CoverPrice = CoverPrice + Slippage; 
ShortPrice = ShortPrice - Slippage;

This whole block should be before ApplyStop()

@travick - the location of ApplyStop doesn't matter. ApplyStop is an instruction for the backtester not a function and it doesn't really matter where you put them as long as they are before Equity() call as it is Equity() which actually runs backtests and evaluates stops.

@wtchung - did you read the manual? If you want to use "trade" price instead of stop price you have to specify exitAtStop argument = 0

http://www.amibroker.com/guide/afl/applystop.html

Quote:
ExitAtStop

ExitAtStop = 0 - means check stops using only trade price and exit at regular trade price(1)
(if you are trading on close it means that only close price will be checked for exits and exit will be done at close price)
ExitAtStop = 1 - check High-Low prices and exit intraday on price equal to stop level on the same bar when stop was triggered
ExitAtStop = 2 - check High-Low prices but exit NEXT BAR on regular trade price.

Also, BuyPrice and all other **Price arrays are user input arrays. They are intended to set the trade price not read. They are initialized with values from Backtester Settings, so if you write

BuyPrice = BuyPrice + Slippage; 

It takes the value from settings (say you have set buy price to Open) and will be just equivalent to:

BuyPrice = Open + Slippage; 

Nothing more. It is intended to set trade price not to read it from who-knows-where.

If you want your stops to be executed intra-bar (not at trade price) and simulate slippage, you should use commissions instead (add slippage amount to commission).

3 Likes