How do configure a stop loss at the high of the candle that is prior to the entry candle?

I'm having two problems:
#1 I'm having problems configuring a stop loss for back testing where the stop loss should go at the high of the candle that is prior to the entry candle.

#2 When i output my ShortPrice and CoverPrice the values are identical in the _Trace output. I would like to see my entry and exit price in the log as well as the ATR(14) when price exists how would I see that using _Trace()?

Here is my code.

Short = 
Volume > 500000
&& Volume > MA(Volume,10) 
&& Close < 20
&& Cross(EMA(Close,8),Close)
&&1;

Cover = False;
Buy = False;
Sell = False;

ApplyStop(stopTypeProfit, stopModePercent, 5 ); // 5% profit target from short/buy price
ApplyStop(stopTypeLoss, stopModePoint, Ref(High,-1), True);  // should stop-out in prior candle high of the entry candle

_TRACE("ShortPrice: " + ShortPrice ); 
_TRACE("CoverPrice: "+ CoverPrice);
_TRACE("prior high: "+ Ref(High,-1));

Thanks!

@justintime you are making a simple mistake by using

Ref(High,-1)

That value is the value of the High, one bar ago and will change every new bar. If I understand you correctly, you are only interested in that value on the day you enter your trade. (i.e. the High on the bar before you enter your trade). An example of how to code that is in this Official Knowledge Base article.

http://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/

And by the way, what is the point of this in your Short order?

&& 1;

That will always be true.

I tried the example but i don't think the example is working. I keep stopping out on the same entry candle and it has not hit the stop of prior candle yet.

For example using code from: http://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/

I do a back test and I find symbol AB. December 28th 2017 has a sell signal; due to the 1 day delay it enters at the open of December 29th. but the stop is at the high of December 27th. Then I get stopped out on December 29th for no reason. I had seen the example before and It didn't seem to work for me. The example also uses prior day high with the Ref() which is where I got the idea from initially. ie.

stopLevelShort = Ref( H, -1 ); // use previous bar high

P.S.I was using the always true for easily adding addition of "AND" statements for my testing.

@justintime no that is not what the article coded. It progressed to create stopAmountShort which is what is eventually fed into the ApplyStop

Re-read carefully and modify the article for your purposes. All the information you need is there.

// define stop level
stopLevelLong = Ref( L, -1 ); // use previous bar low
stopLevelShort = Ref( H, -1 ); // use previous bar high

// calculate stop amount
stopAmountLong = BuyPrice - stopLevelLong;
stopAmountShort = stopLevelShort - ShortPrice;

I think I have posted this in the past, but here is a list of resources I refer to when looking up info on stops and Applystop

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

AB Knowledge Base
http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/
http://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/
https://www.amibroker.com/kb/2016/01/28/how-does-risk-mode-trailing-stop-work/
http://www.amibroker.com/kb/2014/10/12/position-sizing-based-on-risk/

afl code from a nice series of 3 articles in TASC on stops.
http://www.amibroker.com/members/traders/05-2009.html
http://www.amibroker.com/members/traders/06-2009.html
http://www.amibroker.com/members/traders/07-2009.html

another article with a loop trailing stop code
http://www.amibroker.com/members/traders/01-2008.html

another article with a loop trailing stop code
http://www.amibroker.com/members/traders/02-2008.html

And forum info on ApplyStop:

Some more info on forum,

Once you have read and practiced with all of that material you will be a stop expert!

4 Likes

When i follow an article I expect it to work as advertised.

http://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/

I'm trying to apply a stop at prior candle high and it doesn't work. I'm trying to get the example working before implementing it in my own code. If i can't get the example to work there's no point trying to even integrating it into my own code.

As i stated before when i run the example I enter and get stopped out on the same day. I enter on the close and some how stop intraday on the same candle. I tried setting the stop amount to be a hard number ie 1 dollar above the buy price and it still exits on the same candle. Do you have a simple example of ApplyStop working for stopTypeLoss?

I'm spent a lot of time trying to debug this and still can't figure out how to set the stop loss. I can set a trailing stop fine but stoploss at a set target just doesn't seem to work.

I've reduced the above example in the article to:

// Use stock AB only when doing backtesting
// Date is Dec 29th 2017. Stop amount is forced to be 1 dollar.
// Entered on close  @ 25.05 exited @ 24.95. The code only exits on stop loss but stop loss should be above the current price but it exited lower than the entry price when this is a shorting strategy 
Filter = 1;

//TradeDelay = 1; // set it to 0 for no delays
SetPositionSize( 100, spsValue );

// sample entry rules
Short = Cross( Signal(), MACD() );
Buy = Sell = Cover = 0; // no other exit conditions, just stops

BuyPrice = SellPrice = ShortPrice = CoverPrice = Close;

// define stop level
stopLevelShort = Ref( H, -1 ); // use previous bar high

// calculate stop amount
stopAmountShort = stopLevelShort - ShortPrice;

stopAmount = stopAmountShort;
ApplyStop( stopTypeLoss, stopModePoint, 1, ExitAtStop = 1 ); // Forcing stop amount to be 1. 

AddColumn( C, "Close" );
AddColumn(stopAmountShort, "stopAmountShort");
AddColumn(BuyPrice, "BuyPrice");
AddColumn(High, "High");
AddColumn(low, "low");
AddColumn(Close, "Close");