When the price reaches the first take-profit level, I’m using strategy.close to partially close the position. However, strategy.close closes the trade at the close of the candle. I tried using strategy.exit, but I couldn’t achieve the desired outcome. What I actually want is to take profit at the first TP level and move the stop level to the entry level. Then, at the second TP level, I want to take profit again and start a trailing stop. Thank you in advance for your assistance.
//@version=5
strategy("MAYKKK", overlay=true, pyramiding=4, initial_capital=100, default_qty_type = strategy.cash, calc_on_order_fills=false, default_qty_value = 300, commission_type=strategy.commission.percent, commission_value=0.02, calc_on_every_tick=false)
rewardMultiplier = input.float(1.2, title="R.R Ratio", minval=1, step = 0.1)
intermediateRewardMultiplier = input.float(0.75, title="Intermediate TP Ratio", minval=0.1, step = 0.1)
lowestLowPeriod = input.int(7, title="Number of Bars for Long Stop")
highestHighPeriod = input.int(5, title="Number of Bars for Short Stop")
var int candle_count = input(1, title="Number of Bars to Wait")
riskAmount = input.float(5, title="Risk Amount per Trade")
longCondition = ta.change(co) and co and maConditionLong and volumeConditionLong
shortCondition = ta.change(cu) and cu and maConditionShort and volumeConditionShort
var bool gg_conf_long = false
var bool gg_conf_short = false
var int bsb_long = 0
var int bsb_short = 0
var float long_stop_loss = na
var float long_take_profit = na
var float short_stop_loss = na
var float short_take_profit = na
lowestLow = ta.lowest(low, lowestLowPeriod)
highestHigh = ta.highest(high, highestHighPeriod)
positionSizeLong = riskAmount / (close - lowestLow)
positionSizeShort = riskAmount / (highestHigh - close)
var float long_intermediate_tp = na
var float short_intermediate_tp = na
// Long position intermediate TP and stop adjustment
if longCondition
long_intermediate_tp := na
long_stop_loss := na
long_take_profit := na
gg_conf_long := true
if gg_conf_long
bsb_long := bsb_long + 1
if bsb_long > candle_count and gg_conf_long
if close > coPrice and window()
long_stop_loss := lowestLow
long_take_profit := close + (close - long_stop_loss) * rewardMultiplier
long_intermediate_tp := close + (close - long_stop_loss) * intermediateRewardMultiplier
strategy.entry("Long", strategy.long, qty=positionSizeLong, comment="Entered Long: ")
gg_conf_long := false
bsb_long := 0
// Short position intermediate TP and stop adjustment
if shortCondition
short_intermediate_tp := na
short_stop_loss := na
short_take_profit := na
gg_conf_short := true
if gg_conf_short
bsb_short := bsb_short + 1
if bsb_short > candle_count and gg_conf_short
if close < cuPrice and window()
short_stop_loss := highestHigh
short_take_profit := close - (short_stop_loss - close) * rewardMultiplier
short_intermediate_tp := close - (short_stop_loss - close) * intermediateRewardMultiplier
strategy.entry("Short", strategy.short, qty=positionSizeShort, comment="Entered Short: ")
gg_conf_short := false
bsb_short := 0
// Check if intermediate TP levels are triggered for long positions
if strategy.position_size > 0 and window()
if na(long_intermediate_tp) == false and high >= long_intermediate_tp
strategy.close("Long", "Intermediate TP", qty=strategy.position_size * 0.5, immediately = true)
long_stop_loss := strategy.position_avg_price // Move the stop to the entry price
long_intermediate_tp := na
// Check if intermediate TP levels are triggered for short positions
if strategy.position_size < 0 and window()
if na(short_intermediate_tp) == false and low <= short_intermediate_tp
strategy.close("Short", "Intermediate TP", qty=strategy.position_size * 0.5, immediately = true)
short_stop_loss := strategy.position_avg_price // Move the stop to the entry price
short_intermediate_tp := na
if strategy.position_size > 0 and window()
strategy.exit("Take Profit/Stop Loss", stop=long_stop_loss, limit=long_take_profit)
if strategy.position_size < 0 and window()
strategy.exit("Take Profit/Stop Loss", stop=short_stop_loss, limit=short_take_profit)
plot(ma, title="SMA", color=color.yellow, linewidth=3)
plot(strategy.position_size > 0 ? long_stop_loss : na, title="Long Stop Loss", color=color.red, linewidth=3, style=plot.style_linebr, transp=1)
plot(strategy.position_size > 0 ? long_take_profit : na, title="Long Take Profit", color=color.green, linewidth=3, style=plot.style_linebr, transp=1)
plot(strategy.position_size < 0 ? short_stop_loss : na, title="Short Stop Loss", color=color.red, linewidth=3, style=plot.style_linebr, transp=1)
plot(strategy.position_size < 0 ? short_take_profit : na, title="Short Take Profit", color=color.green, linewidth=3, style=plot.style_linebr, transp=1)
plot(strategy.position_size < 0 ? short_intermediate_tp : na, title="Short Intermediate TP", color=color.blue, linewidth=3, style=plot.style_linebr, transp=1)
plot(strategy.position_size > 0 ? long_intermediate_tp : na, title="Long Intermediate TP", color=color.purple, linewidth=3, style=plot.style_linebr, transp=1)