Percentage Take Profit and Trailing Stop Loss TradingView strategy issue

Viewed 2370

This is a simple strategy based on EMA cross. The issue comes when trying to exit a % at a certain level and letting the remaining position to be filled by the trailing stop.

Let's set a target of 3%. When the price hits the target the strategy should close 50% of the order and letting the Trailing Stop continue until it is hit and then exit the order.

Issue: if the price doesn't reach the target, exit 100% at my trailing stop

The code:

if(strategy.position_size > 0)
  strategy.exit("XL", qty_percent=50, limit=longExitPrice)
  strategy.exit("EL", stop=longStopPrice)
if(strategy.position_size < 0)
  strategy.exit("XS", qty_percent=50, limit=shortExitPrice)
  strategy.exit("ES", stop=shortStopPrice)

When the target is reached, it works without any issue. One order (XL) at the target and another one (EL) at the trailing stop. When the price doesn't reach the specific target, the strategy exit only 50% of the order (EL). When there isn't any percentage set. Strategy screenshot

1 Answers

Perhaps the 50% carries over from previous use. Try adding qty_percent=100% to the EL as well.

Related