my stop loss isnt working on my pine script for tradingview help me solve it please

Viewed 19

im very new to coding and made the following parameters to automate my trading strategy. my stop loss just isnt 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 doesnt. help me solve it please. heres where the script mentions SL and TP. there must be an issue somewhere. thank u in advance:

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)
1 Answers

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)
Related