pine-scriptpine-script-v5tradingview-api

interesting project Ignoring exit condition on first candle


In a strategy with entry and exit conditions, is it possible to set a conditional expression that ignores the exit condition in the first candle where the entry occurs?

That is, the exit condition is activated from the second candle

I'm new to PineScript, so I'm learning this and that. I'd appreciate it if you could teach me your knowledge


Solution

  • You can use strategy.opentrades.entry_bar_index to get the entry bar index.

    Below code will give you the number of bars since the entry.

    bars_since_entry = bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades-1)
    

    Then use bars_since_entry as condition for your exit.