How to manage multiple exit conditions in PineScript?

I want to exit a certain positions when certain conditions are met. My conditions are following:

  1. TP1 – When price passes a calculated %. This is not a static value, but for simplicity, let’s make this 5%.
  2. TP2 – Same as TP1 but when it passes 10%.
  3. SL – When price dips below certain %.
  4. SL to BE – When price passes a certain threshold e.g. 3%.

Now I tried everything I guess. strategy.exit() and strategy.close() and their combinations and I always face a problem, where one thing cancels or ignores another and it’s just a hell of a mess to handle.

CASE1: Using strategy.close(), it never triggers TP2; TP1 and SL work fine.

Code for case1:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// TP1 and SL BE
if (tp1_cond and not tp1_taken) //and not tp1_taken)
strategy.close(tradeId, qty_percent = tp1perc, comment="TP1: "+str.tostring(tradeId))
//strategy.exit("TP1: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp1perc) // one exit order somehow cancels another, despite different IDs
//strategy.exit("SL BE "+str.tostring(tradeId), from_entry=tradeId, qty_percent = 100-tp1perc, stop=entryPrice) // TODO: Figure out why this is decreasing strategy performance - smth to do with one order canceling another I guess
array.set(tp1_taken_arr, tp1_index, true)
// TP2
if (tp2_cond and not tp2_taken)
strategy.close(tradeId, qty_percent = tp2perc, comment="TP2: "+str.tostring(tradeId))
//strategy.exit("TP2: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp2perc)
array.set(tp2_taken_arr, tp2_index, true)
// Stop-loss condition
if (stopLossCond)
strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true)
//strategy.exit("SL", from_entry = tradeId, stop = entryPrice)
</code>
<code>// TP1 and SL BE if (tp1_cond and not tp1_taken) //and not tp1_taken) strategy.close(tradeId, qty_percent = tp1perc, comment="TP1: "+str.tostring(tradeId)) //strategy.exit("TP1: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp1perc) // one exit order somehow cancels another, despite different IDs //strategy.exit("SL BE "+str.tostring(tradeId), from_entry=tradeId, qty_percent = 100-tp1perc, stop=entryPrice) // TODO: Figure out why this is decreasing strategy performance - smth to do with one order canceling another I guess array.set(tp1_taken_arr, tp1_index, true) // TP2 if (tp2_cond and not tp2_taken) strategy.close(tradeId, qty_percent = tp2perc, comment="TP2: "+str.tostring(tradeId)) //strategy.exit("TP2: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp2perc) array.set(tp2_taken_arr, tp2_index, true) // Stop-loss condition if (stopLossCond) strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true) //strategy.exit("SL", from_entry = tradeId, stop = entryPrice) </code>
// TP1 and SL BE
if (tp1_cond and not tp1_taken) //and not tp1_taken)
    strategy.close(tradeId, qty_percent = tp1perc, comment="TP1: "+str.tostring(tradeId)) 
    //strategy.exit("TP1: "+str.tostring(tradeId), from_entry=tradeId, profit = 1,  qty_percent = tp1perc) // one exit order somehow cancels another, despite different IDs
    //strategy.exit("SL BE "+str.tostring(tradeId), from_entry=tradeId, qty_percent = 100-tp1perc, stop=entryPrice) // TODO: Figure out why this is decreasing strategy performance - smth to do with one order canceling another I guess
    array.set(tp1_taken_arr, tp1_index, true)

// TP2
if (tp2_cond and not tp2_taken)
    strategy.close(tradeId, qty_percent = tp2perc, comment="TP2: "+str.tostring(tradeId)) 
    //strategy.exit("TP2: "+str.tostring(tradeId), from_entry=tradeId, profit = 1,  qty_percent = tp2perc)
    array.set(tp2_taken_arr, tp2_index, true)
    
// Stop-loss condition
if (stopLossCond)
    strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true)
    //strategy.exit("SL", from_entry = tradeId, stop = entryPrice)

CASE2: If I switch to using strategy.exit(), only POP – condition #4 and SL – condition #3 work.

Code for case2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// TP1 and SL BE
if (tp1_cond and not tp1_taken) //and not tp1_taken)
//strategy.close(tradeId, qty_percent = tp1perc, comment="TP1: "+str.tostring(tradeId))
strategy.exit("TP1: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp1perc) // one exit order somehow cancels another, despite different IDs
//strategy.exit("SL BE "+str.tostring(tradeId), from_entry=tradeId, qty_percent = 100-tp1perc, stop=entryPrice) // TODO: Figure out why this is decreasing strategy performance - smth to do with one order canceling another I guess
array.set(tp1_taken_arr, tp1_index, true)
// TP2
if (tp2_cond and not tp2_taken)
//strategy.close(tradeId, qty_percent = tp2perc, comment="TP2: "+str.tostring(tradeId))
strategy.exit("TP2: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp2perc)
array.set(tp2_taken_arr, tp2_index, true)
// Stop-loss condition
if (stopLossCond)
//strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true)
strategy.exit("SL", from_entry = tradeId, stop = entryPrice)
// Check if price has popped by the break-even threshold and move stop loss to break-even - increased peformance of the strategy and decreased the drawdown - if set specifically at 3%, needs some experimenting
if (currentProfit >= slBeThreshold)
strategy.exit("POP - Move to BE: "+str.tostring(tradeId), from_entry=tradeId, stop=entryPrice)
enter code here
</code>
<code>// TP1 and SL BE if (tp1_cond and not tp1_taken) //and not tp1_taken) //strategy.close(tradeId, qty_percent = tp1perc, comment="TP1: "+str.tostring(tradeId)) strategy.exit("TP1: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp1perc) // one exit order somehow cancels another, despite different IDs //strategy.exit("SL BE "+str.tostring(tradeId), from_entry=tradeId, qty_percent = 100-tp1perc, stop=entryPrice) // TODO: Figure out why this is decreasing strategy performance - smth to do with one order canceling another I guess array.set(tp1_taken_arr, tp1_index, true) // TP2 if (tp2_cond and not tp2_taken) //strategy.close(tradeId, qty_percent = tp2perc, comment="TP2: "+str.tostring(tradeId)) strategy.exit("TP2: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp2perc) array.set(tp2_taken_arr, tp2_index, true) // Stop-loss condition if (stopLossCond) //strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true) strategy.exit("SL", from_entry = tradeId, stop = entryPrice) // Check if price has popped by the break-even threshold and move stop loss to break-even - increased peformance of the strategy and decreased the drawdown - if set specifically at 3%, needs some experimenting if (currentProfit >= slBeThreshold) strategy.exit("POP - Move to BE: "+str.tostring(tradeId), from_entry=tradeId, stop=entryPrice) enter code here </code>
// TP1 and SL BE
if (tp1_cond and not tp1_taken) //and not tp1_taken)
    //strategy.close(tradeId, qty_percent = tp1perc, comment="TP1: "+str.tostring(tradeId)) 
    strategy.exit("TP1: "+str.tostring(tradeId), from_entry=tradeId, profit = 1,  qty_percent = tp1perc) // one exit order somehow cancels another, despite different IDs
    //strategy.exit("SL BE "+str.tostring(tradeId), from_entry=tradeId, qty_percent = 100-tp1perc, stop=entryPrice) // TODO: Figure out why this is decreasing strategy performance - smth to do with one order canceling another I guess
    array.set(tp1_taken_arr, tp1_index, true)

// TP2
if (tp2_cond and not tp2_taken)
    //strategy.close(tradeId, qty_percent = tp2perc, comment="TP2: "+str.tostring(tradeId)) 
    strategy.exit("TP2: "+str.tostring(tradeId), from_entry=tradeId, profit = 1,  qty_percent = tp2perc)
    array.set(tp2_taken_arr, tp2_index, true)
    
// Stop-loss condition
if (stopLossCond)
    //strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true)
    strategy.exit("SL", from_entry = tradeId, stop = entryPrice)
    
// Check if price has popped by the break-even threshold and move stop loss to break-even - increased peformance of the strategy and decreased the drawdown - if set specifically at 3%, needs some experimenting
if (currentProfit >= slBeThreshold)
    strategy.exit("POP - Move to BE: "+str.tostring(tradeId), from_entry=tradeId, stop=entryPrice)
enter code here

CASE3: If I comment out the condition #4 and use strategy.exit() all 3 things work TP1, TP2 and SL work fine.

Code for case3 (same as case 2, with added condition for #4):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// TP1 and SL BE
if (tp1_cond and not tp1_taken) //and not tp1_taken)
//strategy.close(tradeId, qty_percent = tp1perc, comment="TP1: "+str.tostring(tradeId))
strategy.exit("TP1: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp1perc) // one exit order somehow cancels another, despite different IDs
//strategy.exit("SL BE "+str.tostring(tradeId), from_entry=tradeId, qty_percent = 100-tp1perc, stop=entryPrice) // TODO: Figure out why this is decreasing strategy performance - smth to do with one order canceling another I guess
array.set(tp1_taken_arr, tp1_index, true)
// TP2
if (tp2_cond and not tp2_taken)
//strategy.close(tradeId, qty_percent = tp2perc, comment="TP2: "+str.tostring(tradeId))
strategy.exit("TP2: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp2perc)
array.set(tp2_taken_arr, tp2_index, true)
// Stop-loss condition
if (stopLossCond)
//strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true)
strategy.exit("SL", from_entry = tradeId, stop = entryPrice)
</code>
<code>// TP1 and SL BE if (tp1_cond and not tp1_taken) //and not tp1_taken) //strategy.close(tradeId, qty_percent = tp1perc, comment="TP1: "+str.tostring(tradeId)) strategy.exit("TP1: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp1perc) // one exit order somehow cancels another, despite different IDs //strategy.exit("SL BE "+str.tostring(tradeId), from_entry=tradeId, qty_percent = 100-tp1perc, stop=entryPrice) // TODO: Figure out why this is decreasing strategy performance - smth to do with one order canceling another I guess array.set(tp1_taken_arr, tp1_index, true) // TP2 if (tp2_cond and not tp2_taken) //strategy.close(tradeId, qty_percent = tp2perc, comment="TP2: "+str.tostring(tradeId)) strategy.exit("TP2: "+str.tostring(tradeId), from_entry=tradeId, profit = 1, qty_percent = tp2perc) array.set(tp2_taken_arr, tp2_index, true) // Stop-loss condition if (stopLossCond) //strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true) strategy.exit("SL", from_entry = tradeId, stop = entryPrice) </code>
// TP1 and SL BE
    if (tp1_cond and not tp1_taken) //and not tp1_taken)
        //strategy.close(tradeId, qty_percent = tp1perc, comment="TP1: "+str.tostring(tradeId)) 
        strategy.exit("TP1: "+str.tostring(tradeId), from_entry=tradeId, profit = 1,  qty_percent = tp1perc) // one exit order somehow cancels another, despite different IDs
        //strategy.exit("SL BE "+str.tostring(tradeId), from_entry=tradeId, qty_percent = 100-tp1perc, stop=entryPrice) // TODO: Figure out why this is decreasing strategy performance - smth to do with one order canceling another I guess
        array.set(tp1_taken_arr, tp1_index, true)

// TP2
if (tp2_cond and not tp2_taken)
    //strategy.close(tradeId, qty_percent = tp2perc, comment="TP2: "+str.tostring(tradeId)) 
    strategy.exit("TP2: "+str.tostring(tradeId), from_entry=tradeId, profit = 1,  qty_percent = tp2perc)
    array.set(tp2_taken_arr, tp2_index, true)
    
// Stop-loss condition
if (stopLossCond)
    //strategy.close(tradeId, comment="SL: "+str.tostring(tradeId), immediately = true)
    strategy.exit("SL", from_entry = tradeId, stop = entryPrice)

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