I’m using a pinescript code for a dynamic trailing stop loss. The TSL seems to exit shortly after entering. Why?

This trailing stop loss code is suppose to be dynamic. I should use something similar to a supertrend to tighten the stop loss as it continues.

Entries an exits on chart

 This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
 ZenAndTheArtOfTrading / PineScriptMastery.com

 // @version=5
 strategy("www.PineScriptMastery.com", overlay=true)
 
 Get user input
 tradeType           = input.string(title="Trade Direction", defval="Long", options=["Long", "Short"], confirm=true)
 trailSource         = input.string(title="Trail Source", defval="Lows/Highs", options=["Lows/Highs", "Close", "Open"], confirm=true)
 trailMethod         = input.string(title="Trail Method", defval="ATR", options=["ATR", "Percent"], confirm=true)
 trailPercent        = input.float(title="Trail Percent", defval=10, minval=0.1, confirm=true)
 swingLookback       = input.int(title="Lookback", defval=7, confirm=true)
 atrPeriod           = input.int(title="ATR Period", defval=14, confirm=true)
 atrMultiplier       = input.float(title="ATR Multiplier", defval=1.0, confirm=true)
 
 MACD Calculation on 3-minute chart
 [macdLine, signalLine, _] = request.security(syminfo.tickerid, "3", ta.macd(close, 12, 26, 9))
 macdHist = macdLine - signalLine
 
 Condition: MACD goes from falling to rising
 macdRising = ta.crossover(macdHist, 0)
 
 Declare trailing price variable (stores our trail stop value)
 var float trailPrice    = na
 float next_trailPrice   = na
 
 Get required trailing stop variables
 atrValue                = ta.atr(atrPeriod) * atrMultiplier
 float swingLow          = ta.lowest(low, swingLookback)
 float swingHigh         = ta.highest(high, swingLookback)
 
 Get trailing stop price
 if trailMethod == "ATR" // Determine ATR trailing stop price based on price source
 next_trailPrice := switch trailSource
         "Close"     => strategy.position_size > 0 ? close - atrValue : close + atrValue
         "Open"      => strategy.position_size > 0 ? open - atrValue : open + atrValue
 strategy.position_size > 0 ? swingLow - atrValue : swingHigh + atrValue
 else if trailMethod == "Percent" // Determine percentage trailing stop price based on price source
 float percentMulti = strategy.position_size > 0 ? (100 - trailPercent) / 100 : (100 + trailPercent) / 100
 next_trailPrice := switch trailSource
         "Close"     => close * percentMulti
         "Open"      => open * percentMulti
 strategy.position_size > 0 ? swingLow * percentMulti : swingHigh * percentMulti
 
 Check for trailing stop update
 if strategy.position_size != 0 and barstate.isconfirmed
 Trail long stop ONLY IF temp trailPrice is not set or is a higher price
 if (next_trailPrice > trailPrice or na(trailPrice)) and strategy.position_size > 0
 trailPrice := next_trailPrice
 Trigger alert
 alert(message="Trailing Stop updated for " + syminfo.tickerid + ": " + str.tostring(trailPrice, "#.#####"), freq=alert.freq_once_per_bar_close)
 Trail short stop ONLY IF temp trailPrice is not set or is a lower price
 if (next_trailPrice < trailPrice or na(trailPrice)) and strategy.position_size < 0
 trailPrice := next_trailPrice
 Trigger alert
 alert(message="Trailing Stop updated for " + syminfo.tickerid + ": " + str.tostring(trailPrice, "#.#####"), freq=alert.freq_once_per_bar_close)
 
 Draw data to chart
 plot(strategy.position_size != 0 ? trailPrice : na, color=color.red, title="Trailing Stop")
 
 Entry condition: Enter a long position if MACD goes from falling to rising and on the 3-minute chart
 if macdRising and timeframe.multiplier == 3 and timeframe.isintraday
 strategy.entry(id="Long Entry", direction=strategy.long)
 
 Exit trade if stop is hit
 strategy.exit(id="Trail Exit", from_entry="Long Entry", limit=na, stop=trailPrice)

I was expecting the trailing stop loss to continue until the conditions as I understood them were met. I tried adjusting the sensitivity of the trailing stop loss but it did not help.

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