'How to cancel orders if they are not triggered within 1 bar?

How do i prevent orders from remaining open and executing later.

For long entries i have a buystop = high, so if the next candle the price doesn't hit the high it'll remain open until x number of candles print and finally price hits the valid candle high price.

Do we have the ability to only execute one bar after valid long/short entry?



Solution 1:[1]

Let's say you are normally doing something like this:

long_cond = something

if (long_cond)
    strategy.entry("Long", strategy.long)

You can then check if your condition was true one bar ago and if you are not in a long position at the moment.

is_long = strategy.position_size > 0
cancel_long_order = barstate.isconfirmed and long_cond[1] and not is_long

if (cancel_long_order)
    strategy.cancel("Long")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vitruvius