How to prioritize 'applystop' before 'sell' signal if both occur on entry bar?

Hi guys,
Ive been at this for well over a week now and cant seem to crack it or find anything on the forum that answers my questions.
Short question – how do I set the backtester to prioritize the applystop exit signal over the sell exit signal if both are triggered on the entry bar?
Long description -
I am working on a simple strategy to start with that is pretty much a momo trend following strategy which has a tight stop.
The idea is to enter on the open of the buy signal and if the price pulls back more than 1.5% from the open then exit the position intraday on the same bar (the stop). If not, then the position keeps running until either a) price eventually goes 1.5% below the entry price and hits the stop or b) price closes 2% under the 50dma. Pretty simple idea.
The problem im having is that if it enters on a bar that ends up being a huge red candle and slices through the 50dma, the exits are executed on the close of that bar which is the sell signal, not at the MAX stop from entry price which is set at 1.5%.
So how can I rectify this. I have tried so many changes that I cant even remember where I began.
Ive only recently purchased Amibroker and started this so im definitely a newb but im learning as much as I can, have read Bandys manual and have tried everything within my knowledge before coming here for help.
Not looking for a fish, just someone to show me how to cast a line. Appreciate the help in advance.
This is the simple version of the code with all the buy parameters removed.

SetBacktestMode(backtestRegularRaw);
setoption( "AllowSameBarExit", True );
//I have tried using different sell trade delays but it delays the stop too i think
SetTradeDelays(0,0,0,0);
SetOption("ActivateStopsImmediately", True ); 

//EntryParams = //(Basically buys strong open)

Filter = Entryparams;

Buy = Entryparams;
BuyPrice = Open;
//I have tried exit types 0,1 and 2. 
ApplyStop( stopTypeLoss, stopModePercent, 1.5, 1);

Sell = Close < 0.98*MA(C, 50);
SellPrice = Close;
//Sell = 0;

//I have tried to use applystop for the Sell trigger too but it doesnt work either (also doesnt allow using a variable to exit applystop on backtestregularraw)
//SellStop = C < 0.98*MA(C, 50);
//ApplyStop( stopTypeLoss, stopModePoint, SellStop, 1);


SetPositionSize( 20, spsPercentOfEquity );

Begin by simplifying the problem. Do you ever see a stop loss exit on the day of entry? Make your EOD exit something like 50% below the MA(50) so that it seldom gets hit. Then run a back test to see if any trades have a duration of 1 bar.

1 Like

First of all, when you are posting code, make sure it works after straight copy-paste to editor, without need to correct it/add anything first.
Make it easy for people to help you and don't put extra work on their heads.

I modified your formula just to buy every bar:

EntryParams  = 1;

and it works perfectly, i.e. max loss 1.5% is triggered the same bar as it should. Bigger loses only occur on multiple day trades when OPEN price is lower than your max loss.

See this:

image

2 Likes

Hi @Tomasz , thanks for the replies and my apologies about the code not being fully functional from copy&paste. Duly noted for next time mate.

As for the results, I agree that your example is working fine and I understand that if the exit is on another bar then you cannot help some slippage past the 1.5% if the bar gaps down etc. As per my original post, the issue I am having is with exit on the initial entry bar. Everything else is fine.

As you can see below, I am getting same bar exits that are bring triggered by the sell signal (at the close price as per the sell instructions) and not on the applystop signals.

20180720%20NCK%20Amibroker%20Forum%20Example

Amibroker%20Forum%20Analysis%20Example

However, if i change setbacktestmode to BacktestRegular as opposed to BacktestRegularRaw it seems to work as intended.

If my goal was to just get this single strategy working I'd take this as a win, but this is just an exercise so that I can slowly develop my understanding of Amibroker for better projects down the track. So I'm more interested in understanding why this is the case.

@Mradtke Thanks for the reply and the guidance, yeah I tried lots of different things with the sell signal last week similar to what you suggested. I get lots of single bar trades haha (The strategy has a low win rate) where the stop is executed perfectly. The only time there is an issue is if both the stop and sell are triggered on the same bar in which case the sell overrides.

Cheers