I'm trying to have two exits on my strategy, one as a partial take profit and the other a trailing stop loss. I found some codes on the internet and I'm trying to put them together, but the trailing stop loss isn't working. It seams is something related to the order of the code. If I put the execution of the trailing stop before the partial take profit, the trailing stop works but the partial TP doesn't. I would appreciate some help. Thank you.
//@version=5
strategy("BASE", overlay=true, initial_capital = 1000)
//######################################################################################## TIME RANGE ######################################################################################
FromDay=input.int(defval=18,title="FromDay",minval=1,maxval=31)
FromMonth=input.int(defval=8,title="FromMonth",minval=1,maxval=12)
FromYear=input.int(defval=2021,title="FromYear",minval=2016)
dateCond = (time >= timestamp('GMT+10', FromYear,FromMonth, FromDay, 00, 00))
//######################################################################################## TIME RANGE ######################################################################################
//######################################################################################## STRATEGY ########################################################################################
// 3 rsi strategy , when all of them are overbought we sell, and vice versa
if ta.crossover(ta.ema(close,20),ta.ema(close,100)) and dateCond
strategy.entry("BUY", strategy.long)
if ta.crossunder(ta.ema(close,20),ta.ema(close,100)) and dateCond
strategy.entry("SELL", strategy.short)
//######################################################################################## STRATEGY ########################################################################################
//######################################################################################## TRAILING STOP AND TAKE PROFIT #################################################################
//Trailing Stop
longTrailPerc = input.float(title='Trailing Long Loss (%)', minval=0.0, step=0.1, defval=1) * 0.01
shortTrailPerc = input.float(title='Trailing Short Loss (%)', minval=0.0, step=0.1, defval=1) * 0.01
longStopPrice = 0.0
shortStopPrice = 0.0
longStopPrice := if strategy.position_size > 0
stopValue = close * (1 - longTrailPerc)
math.max(stopValue, longStopPrice[1])
else
0
shortStopPrice := if strategy.position_size < 0
stopValue = close * (1 + shortTrailPerc)
math.min(stopValue, shortStopPrice[1])
else
999999
//################################################################### Partial Take Profit ############################
percentAsPoints(pcnt) =>
strategy.position_size != 0 ? math.round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)
lossPnt = percentAsPoints(2)
strategy.exit("x1", qty_percent = 50, profit = percentAsPoints(5), comment = "Loss")//, loss = lossPnt)
strategy.exit("x2", qty_percent = 25, profit = percentAsPoints(20), comment = "Loss")//, loss = lossPnt)
strategy.exit("x3", qty_percent = 25, profit = percentAsPoints(30), comment = "Loss")//, loss = lossPnt)
strategy.exit("x4", profit = percentAsPoints(4), comment = "Loss")//, loss = lossPnt)
profitPercent(price) =>
posSign = strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0
(price - strategy.position_avg_price) / strategy.position_avg_price * posSign * 100
p1 = plot(profitPercent(high), style=plot.style_linebr, title = "open profit % upper bound")
p2 = plot(profitPercent(low), style=plot.style_linebr, title = "open profit % lower bound")
fill(p1, p2, color = color.red)
//####################################################################### Execution ################################
if strategy.position_size > 0
strategy.exit(id='Trailing', stop=longStopPrice, alert_message="close BTCUSDT a=usdm")
if strategy.position_size < 0
strategy.exit(id='Trailing', stop=shortStopPrice, alert_message="close BTCUSDT a=usdm")