-2

I'm very new to coding and made the following parameters to automate my trading strategy. My stop loss just isn't working and I have no explanation as to why.

Also I think my TP and SL are flipped, but even if flipped the TP works just the SL doesn't. Here's where the script mentions SL and TP. There must be an issue somewhere.

p=input.float(2.0,"Desired Profit %")
sl= input.float(1,"Desired Stop loss %")
TP  = strategy.position_avg_price * (1 + (p* 0.01))
SL = strategy.position_avg_price * (1 - (sl* 0.01))

if long
    strategy.entry("Long", strategy.long)
if short
    strategy.entry("Short", strategy.short)


strategy.exit("Exit", "Long",limit=TP)
strategy.exit("Exit", "Long",limit=SL)

strategy.exit("Exit", "Short",limit=TP)
strategy.exit("Exit", "Short",limit=SL)
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

You need to use the stop argument of the strategy.exit() for your stop loss. And you can and should use both limit and stop in the same call unless you want to do partial close.

strategy.exit("Long Exit", "Long", limit=TP, stop=SL)
strategy.exit("Short Exit", "Short", limit=TP, stop=SL)
vitruvius
  • 15,740
  • 3
  • 16
  • 26