Search code examples
pine-scriptalgorithmic-tradingtradingpine-script-v4

PineScript - Setting Up % Stoploss


I found a simple percentage stoploss for my strategy:

SL = 0.5    

// calculating the exit stoploss     
longStop = strategy.position_avg_price * 1 - SL

// Strategy.Entry    
strategy.entry("LongA", strategy.long,1,stop=longStop, when= a and b)

// Strategy.close    
strategy.close("LongA", when=(x and y) or z)

(Note: a,b,x,y,z are predefined variables.)

but I still see -7%,-10% losses in my list of trades, meaning that this stoploss strategy is not working.Can you propose a better script when it comes to setting up stoploss? I just want to set a stoploss of 5% when strategy detects an entry, so when the market goes against me and goes than more than 5%, I want to close the trade. As a result, I want a simple stoploss, not trailing stoploss.

Thanks!


Solution

  • 0.5 is 50% or half your avg price for starters. 5% would be ,95 of your entry average. I think the losses you are seeing are from the close call, not your stop hitting. Stops must be entered in strategy,exit with the stop argument. The stop argument if the entry call is to enter on stop.

    You need another call strategy,exit() for a stop loss.

    Cheers.