My Pinescript strategy is closing trades immediatly after opening and i cannot understand why

doing my best to learn pinescript. im currently working with a Market Structure shift Strategy and CANNOT understan why these trades are closing immediatly after placing them.
at this stage i simply want to take trades in the direction of the identified MSS and close them on an opposing one.

I have previously grappled with trades opening and closing on every bar and solved it by adding a criteria reset after trade entry. im wondering if that is what has cause it.

code is outlined below.

//@version=5
strategy("Market Structure (working copy)", "Market Structure (working copy)", true, max_labels_count = 500, max_lines_count = 500)

pivot_strength = input.int(3, "Pivot Strength", 1)
show_pivots = input.bool(false, "Show Pivots", inline = "PIV")
highs = input.color(color.blue, "", inline = "PIV")
lows = input.color(#f23645, "", inline = "PIV")
show_bos = input.bool(false, "Show BOS/MSS")
style = input.string("Dashed", "Line Style", options = ['Solid', 'Dashed', 'Dotted'])
enableFilter = input(true,  "Enable Backtesting Range Filtering")
fromDate   = input.time(timestamp("20 Jul 2023 00:00 +0300"), "Start Date")
toDate       = input.time(timestamp("20 Jul 2099 00:00 +0300"), "End Date")
tradeDateIsAllowed = not enableFilter or (time >= fromDate and time <= toDate)
ticks_target = input.int(60, "Ticks Target")
// Times are in UTC


type swing
    int _index = na
    float _val = na

method set_val(swing s, int i, float v) =>
    s._index := i
    s._val := v

method set_na(swing s) =>
    s._index := na
    s._val := na

var swing_high = swing.new()
var swing_low = swing.new()
var temp_hi = swing.new()
var temp_lo = swing.new()

var bool bull = na

sh = false
sl = false

bull_bos = false
bull_mss = false
bear_bos = false
bear_mss = false

var line_style = switch style
    "Solid" => line.style_solid
    "Dashed" => line.style_dashed
    "Dotted" => line.style_dotted
// drawing boxes on the sh and sl's
if not na(ta.pivothigh(high, pivot_strength, pivot_strength))
    swing_high.set_val(bar_index - pivot_strength, high[pivot_strength])
    sh := true // Sh and SL are used for the shapes drawn above and below sl and sh's 
    
if not na(ta.pivotlow(low, pivot_strength, pivot_strength))
    swing_low.set_val(bar_index - pivot_strength, low[pivot_strength])
    sl := true // Sh and SL are used for the shapes drawn above and below sl and sh's 


// ID MSS section 
if not na(swing_high._val)
    if close > swing_high._val 
        mss = (bull == false or na(bull))
        if mss
            bull_mss := true
            temp_hi.set_val(swing_high._index, swing_high._val)
            bear_mss := false  // Reset bear_mss when bull_mss is true
        else
            bull_bos := true
            temp_hi.set_val(swing_high._index, swing_high._val)
        bull := true
        swing_high.set_na()
        
if not na(swing_low._val)
    if close < swing_low._val
        mss = (bull == true or na(bull))
        if mss
            bear_mss := true
            temp_lo.set_val(swing_low._index, swing_low._val)
            bull_mss := false  // Reset bull_mss when bear_mss is true
        else
            bear_bos := true
            temp_lo.set_val(swing_low._index, swing_low._val)
        bull := false
        swing_low.set_na()


// MSS direction ascertained in here. 


if show_bos
    if bull_mss[1] or bull_bos[1]
        line.new(temp_hi._index, temp_hi._val, bar_index - 1, temp_hi._val, style = line_style, color = highs)
        label.new(math.floor(math.avg(temp_hi._index, bar_index - 1)), temp_hi._val, bull_mss[1] ? "MSS" : "BOS", textcolor = highs, color = #ffffff00, style = label.style_label_down)

    if bear_mss[1] or bear_bos[1]
        line.new(temp_lo._index, temp_lo._val, bar_index - 1, temp_lo._val, style = line_style, color = lows)
        label.new(math.floor(math.avg(temp_lo._index, bar_index - 1)), temp_lo._val, bear_mss[1] ? "MSS" : "BOS", textcolor = lows, color = #ffffff00, style = label.style_label_up)

plotshape(show_pivots and sh, style = shape.triangledown, location = location.abovebar, color = highs, offset = -pivot_strength, size = size.tiny)
plotshape(show_pivots and sl, style = shape.triangleup, location = location.belowbar, color = lows, offset = -pivot_strength, size = size.tiny)


// Entry conditions
var entry_condition_long = false
var entry_condition_short = false

// Variables to reset MSS conditions one bar later
var bool bull_mss_reset = false
var bool bear_mss_reset = false

// Check for entry based on MSS 
if bull_mss and tradeDateIsAllowed and strategy.opentrades < 1
    entry_condition_long := true

if bear_mss and tradeDateIsAllowed and strategy.opentrades < 1
    entry_condition_short := true

// Entry 
if entry_condition_long and bull_mss
    strategy.entry("Buy", strategy.long, 1)

if entry_condition_short and bear_mss  
    strategy.entry("Sell", strategy.short, 1)

// Exit conditions
var exit_condition_long = false
var exit_condition_short = false

// Check for exit based on opposing MSS

if bear_mss and tradeDateIsAllowed and strategy.opentrades > 0
    exit_condition_long:= true

if bull_mss and tradeDateIsAllowed and strategy.opentrades > 0
    exit_condition_short:= true


// Exit
if exit_condition_long
    strategy.close("Buy")
if exit_condition_short
    strategy.close("Sell")

New contributor

Adam Seeber is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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