Page 1 of 1

Exit not working

Posted: Mon Jul 12, 2021 7:21 am
by Django
Hi folks, after a ridiculous amount of attempts seems that I have managed to find the right order for the entry and exits, however, I still have an issue with the strategy.order.

When backtesting, if a trade is closed with a SL order, even though that should bring the strategy.position_size to 0 and nullify the requisite to open a strategy.order, seems like TradingView ignores this part and places a starategy.order anyway, which at that point becomes a short trade instead of an exit as there was no position to close in the first place.

So my idea was to cancel the strategy.order whenever the SL exit is triggered, but after trying both strategy.cancel, or OCA this doesnìt seem to work.

Can any expert please take a look and tell me what's is off with this code?

//Orders placement
if long_cond and strategy.position_size==0
strategy.entry(id="long", long=true, qty=ordersize)

if strategy.position_size>0
strategy.exit(id="SL", from_entry="long", stop=SL)

if strategy.position_size>0
strategy.order(id="long", long=false, qty=abs(strategy.position_size), stop=longStop, comment="ATR")

Re: Exit not working

Posted: Fri Jul 16, 2021 3:08 am
by Django
There's an update on this, I am running this strategy that includes 2 types of exits, a stop loss and a trailing stop.
I am trying to take advantage of the FIFO execution order which is the default in TradingView, however, under certain circumstances both the stop loss and the trailing stop will be executed at the same time (I know that is due to how Pine Script manages the orders) resulting in a net short position instead of a flat position.

I have tried using strategy.exit or strategy.close for the trailing stop, but for some reason, they don't seem to work and the only option is to go with strategy.order:

if long_cond and strategy.position_size==0
strategy.entry(id="long", long=true, qty=ordersize)

if strategy.position_size>0
strategy.exit(id="SL", from_entry="long", stop=SL)

else if strategy.position_size==0
strategy.cancel(id="SL")

if strategy.position_size>0
strategy.order(id="TS", long=false, qty=abs(strategy.position_size), stop=longStop)

else if strategy.position_size==0
strategy.cancel(id="TS")


For now I have put this snippet at the end of that code:

if strategy.position_size<=0
strategy.close_all()

But obviously this is a workaround and not the best way to do this.

Any idea of how to avoid the script from reversing the position, but in a better and more efficient way?

Help is highly appreciated, thanks guys

Re: Exit not working

Posted: Fri Jun 17, 2022 2:14 pm
by Chris_lucky
Hi, did you find the solution ? I have same issue and orders are not trigerred. Thanks