single entry point with multiple take-profit levels and pulling the stop loss to the entry point

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)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật