Skipped Bars for Entry/Exit Signals

Hi All,
Appreciate your help here, I am baffled with this one instance that one of the strategy I was testing seems to skip the exit. It appears the backtester is not recognising that bar. In my debugger, I am able to see the Entry and Exits accurately.

Code for the strategy is below

cond1 = DayOfWeek()==1;
Cond2 = C<Ref(C,-1);
Cond3 = Ref(C,-1)<Ref(C,-2);
SetTradeDelays (0,0,0,0);
Buy = cond1 AND cond2 AND cond3; 
BuyPrice = SellPrice = C;
Sell = BarsSince(Buy)==5 ;
SellPrice = C;
Short = 0;
Cover = 0;


The strategy is simple, it enters on Monday close (if the condition satisfied) and holds the position for 5 bars which in most case also result on a Monday. i.e., in some case the position may have to enter on the same day as the exit.

The issue I am facing is that some exits/entries are skipped.


As you can see in the screenshot there are trades which stayed open for 11 days. I have tried to resolve it through N-Bar stop as well and I have exact same result. So I am not certain why it is happening.

It could be very obvious which my eyes are not catching at the moment, but I have tried hours trying to resolve this and gone through the forum reading about same bar exits and Exrems but none of them seems to be relevant to my issue.

Appreciate your guidance.

Your code is incorrect. To exit after 5-bars you should use ApplyStop function with N-bar stop mode. See manual: AFL Function Reference - APPLYSTOP

So replace your code with

cond1 = DayOfWeek()==1;
Cond2 = C<Ref(C,-1);
Cond3 = Ref(C,-1)<Ref(C,-2);
SetTradeDelays (0,0,0,0);
Buy = cond1 AND cond2 AND cond3; 
BuyPrice = SellPrice = C;
Sell = 0;

ApplyStop( stopTypeNBar, stopModeBars, 5 );

BarsSince that you used is not going to work in case of repeated signals (because every repeated signal resets the count from the start).

Hi Tomasz
I originally tried using the applystop but I noticed that re-entry on the same day as exit (please see screenshot below) was not happening that was my reason to try Barssince.


I have even set the reentry delay to 0 and also tried the option of setting "Allow Same bar Exit" but nothing seems to make the entry occur on the same day.

Not sure what I am missing.

  1. There are NO re-entries on same day on your chart.
  2. There is a difference between raw signal and actual trade (use "show arrows for actual trades")
  3. Re-entry delay should be set to value GREATER than zero to prevent re-entry in given number of bars. Say you can set it to 10 to prevent re-entry for 10 bars after exit.

I am not trying to delay re-entry but I am trying to take the entry on the same day as my previous position was exited.

I checked the raw signals and there are valid raw signals for entry on the day my previous position exited.

For example in the screenshot I gave .
23/09/2013 - Buy signal appeared - System entered a long at closing price of the day.
30/09/2013 - Long position exited on close based on N-Bar exit. On the same day there was another buy signal so ideally there should be a re-entry on that day. But that re-entry is not happening.

I understand this is essentially very similar to leaving the position open but I am testing different position sizing approach on the back of final equity of the day. So in real life I would reenter with a recalculated position size.

First, if you are exiting position ON CLOSE (as you do), you can't reasonably expect to re-open position same bar.

But then if really you want to re-open positions the same day after stop you have to use proper settings, namely you have to turn off "activate stops immediately" .

Thank you! I must have misunderstood the purpose of "activate stops immediately" this is doing exactly what I was expecting.

Thank you again Tomasz for your time.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.