Exit not working
Posted: Mon Jul 12, 2021 7:21 am
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")
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")