0

I am trying to create a simple inside bar breakout strategy that finds an inside bar and then enters long on the high of the inside bar and takes profit at the high of the candle prior to the inside bar. The stop loss is at the low of the inside bar. The problem is that the strategy orders should cancel if the bar right after the inside bar does not trigger the entry (does not hit the inside bar high). Instead the entry is activated multiple candles after the inside bar was created which makes the trade obsolete. example

Here is a snippet of my code: (i am expecting the strategy to cancel all orders if the there is more than 1 candle or bar past the inside bar condition)

strategy("Inisde Bar Breakout", overlay=true, initial_capital=10000, default_qty_value=10, default_qty_type=strategy.percent_of_equity, calc_on_every_tick=true, process_orders_on_close=false)

LongCondition = high < high[1] and low > low[1]
BarsPast = ta.barssince(LongCondition)
timePeriod = time >= timestamp(syminfo.timezone, 2022, 1, 1, 0, 0)
if (timePeriod and LongCondition and BarsPast<2)
    strategy.entry("long", strategy.long, stop=high, limit=high)
    strategy.exit("exit", "long", stop=low, limit=high[1])
    if(BarsPast>1)
        strategy.cancel_all()

rigatoni4
  • 3
  • 1

1 Answers1

0

In order for your strategy.cancel_all() to be called, first (timePeriod and LongCondition and BarsPast<2) must be true and then (BarsPast>1) must be true.

You see, in order to reach if(BarsPast>1), LongCondition must be true.

You want to move

if(BarsPast>1)
        strategy.cancel_all()

outside of that parent if block.

vitruvius
  • 15,740
  • 3
  • 16
  • 26