Search code examples
pine-scriptpine-script-v5

How to sell 50% at profit and let 50% run


I am trying to have 2 sell orders but I want to have a profit and stop as well based on pips not price. If I use strategy.exit it would look like this for my stop and profit but I can't figure out how to split the sells.

strategy.exit("exit", "long", stop = 100, profit = 100)

Is this possible?

shortCondition = close < lowerBand and close[1] > lowerBand

if (shortCondition)

strategy.entry("short", strategy.short, qty=pos_size)

strategy.order("short1", strategy.long, qty=size_trim, comment="closefirst50%")

strategy.order("short2", strategy.long, qty=size_trim, comment="closesecond50%")


longCondition = close > upperBand and close[1] < upperBand

if (longCondition)

strategy.entry("long", strategy.long, qty=pos_size)

strategy.order("long1", strategy.short, qty=size_trim, comment="closefirst50%")

strategy.order("long2", strategy.short, qty=size_trim, comment="closesecond50%")

Solution

  • The solution is to use 2 strategy.exit indeed

    if (shortCondition)
        
        strategy.entry("short", strategy.short, qty=pos_size)
        
    if strategy.position_size < 0
    
        strategy.exit("exit short TP", qty=size_trim, profit = 100, comment="closefirst50%")
        
        strategy.exit("exit short SL", loss = 100, comment="close the remaining 50%")