Same-bar entry + stoploss

Hi all,

I'm running a custom loop-based backtester (not using ApplyStop) for a breakout strategy on 15-minute bars. My data source is 1-minute bars.

When a breakout entry and its stoploss are both within the same bar's range, my AFL exits immediately (same-candle SL exit). For example, for a long entry:

if(H[k] > entryLevel) // entry triggers
{
    Buy[k] = 1;
    BuyPrice[k] = entryLevel;
    stoploss = entryLevel - entryLevel * 0.004; // 0.4% SL

    if(L[k] <= stoploss) // same-candle SL check
    {
        Sell[k] = 1;
        SellPrice[k] = stoploss;
    }
}

I verified using bar replay at 1-minute resolution that in many cases, the bar's Low was established early in the bar (before price rallied to trigger the entry). The entry only happened after the Low was made, so the SL was never actually hit. The trade stayed open through that bar in bar replay and the SL was hit on the next bar instead.

My workaround:

I added an Open-based check. If the bar opened below the entry price, the price had to rally upward to trigger entry, which means the Low was likely formed before the entry. So I skip the same-candle SL in that case:

// Only apply same-candle SL if Open confirms entry could have happened before the Low
if(L[k] <= stoploss AND (O[k] >= buyPriceVal))
{
    Sell[k] = 1;
    SellPrice[k] = stoploss;
}

For short entries, the mirror: skip same-candle SL if O[k] > shortPriceVal.

My questions:

  1. Is the Open-based heuristic a reasonable approach, or are there edge cases I'm not considering?
  2. Has anyone used TimeFrameSet(in1Minute) inside a custom loop to check the actual intra-bar sequence (which 1-min bar made the Low vs which triggered the entry)? Would that be more accurate than the Open heuristic?
  3. Any other approaches people have used for this same-bar ambiguity in custom loop backtesters?

Thanks in advance.

First Q, what stops you from BT in 1min itself?
Because from higher periodicity, you can't do TF set 1-min.
You can only access higher timeframes from your chosen TF by design.

If you are doing 15min strategy, you can set 15min TF and generate signals from 1min periodicity.

AB also has "Allow same bar exit" setting to check/uncheck.

2 Likes

The system operates in 15 mins time frame. I was just doing bar replay to check what is happening in lower time frame if the entry candle has really taken out the sl

@nsm51 is correct: if you want your CBT to only enforce stops that are actually triggered after entry, then you should consider modifying your backtest to use 1min bars. Before you do that, it might be worth comparing a backtest on 15min bars that enforces stops on the entry bar to one that does not. If the performance of those two backtests is acceptably close, then don't bother using 1min bars because you're probably not going to learn anything new.

Another possibility would be exit on the entry bar if the Close is below the long stop price, as this surely means price fell after you entered the trade. But again, I wouldn't complicate things if it's not going to have a significant impact on your results.

One other thing: based on the partial code you provided, it appears your entry may not be accounting for opening gaps past the entry level. For example, if your entry level is 100, the Open is 101 and High is 102, your backtest will enter at 100.

2 Likes

thanks @mradtke & @nsm51 for the suggestion. I have used closed based check.