Loop takes too long to execute

I am not completely new to Pinescript but facing an issue. I understand this error indicates infinite loop but I dont think I have any or maybe I am failing to recognize even after 100th attempt.

I strongly believe the root cause might be related to garbage value or incorrect data in the variable or assignment.

Can you please help?

  • Function
calcTrailingStopLoss(entryType = "None") =>
    var float slMult = 0
    var float stopLossValueTSL = na
    var float slPointsTSL = na
    var float entryValueTSL = na
    var bool enableLongTSL = false
    var bool enableShortTSL = false
    var int rRTSL = na
    var float prevStopLossValue = na
    rRTSL := str.endswith(strategy.opentrades.entry_id(0),"SL") ? 2 : varRRTSL
    if entryType == "Long"
        stopLossValueTSL := stopLossValueLong
        slPointsTSL := slPointsLong
        entryValueTSL := entryValueLong
    else if entryType == "Short"
        stopLossValueTSL := stopLossValueShort
        slPointsTSL := slPointsShort
        entryValueTSL := entryValueShort
    slMult :=  math.floor(math.abs(close-stopLossValueTSL)/slPointsTSL)
    prevStopLossValue := stopLossValueTSL
    //[currentCandleHealthPer, currentCandleBody] = getCandleHealthPer(0)
    if  bar_index >= debugBarIndex-50 and bar_index <=debugBarIndex+50
        log.warning("calcTrailingStopLoss - slMult Before:{0}",slMult)
    //Checking if system current candle is elligible for trailling
    if isGreen and entryType == "Long" and (candleHealthPer >= varCandleHealthPer) and ((close > (entryValueTSL+(rRTSL*slPointsTSL)) and (stopLossValueTSL < close) and (strategy.position_size[1] > 0 and strategy.position_size > 0)) or (math.abs(close - entryValueTSL) >= (entryValueTSL*0.005) or dailyPoints >= entryValueTSL*0.005))
        enableLongTSL := true
    else if isRed and entryType == "Short" and (candleHealthPer >= varCandleHealthPer) and ((close < (entryValueTSL-(rRTSL*slPointsTSL)) and stopLossValueTSL < close and isRed and (strategy.position_size[1] < 0 and strategy.position_size < 0)) or (math.abs(close - entryValueTSL) >= entryValueTSL*0.005 or dailyPoints >= entryValueTSL*0.005))
        enableShortTSL := true
        
    //Trailing logic
    //Trailing logic
    if enableLongTSL or enableShortTSL
        //for i=0 to slMult//-1
        while slMult>=1 and ((entryType == "Long" and stopLossValueTSL+(slPointsTSL) <= low) or (entryType == "Short" and stopLossValueTSL-(slPointsTSL) >= high))
            if entryType == "Long"
                stopLossValueTSL := slMult ==1 ? (stopLossValueTSL + slPointsTSL) - tSLMarginPer : stopLossValueTSL + slPointsTSL
            else if entryType == "Short"
                stopLossValueTSL := slMult ==1 ? (stopLossValueTSL - slPointsTSL) + tSLMarginPer : stopLossValueTSL - slPointsTSL

            slMult := slMult-1
            //if  bar_index >= debugBarIndex-50 and bar_index <=debugBarIndex+50
            //    log.warning("calcTrailingStopLoss - slMult After:{0}",slMult)
        if entryType == "Long" and (prevStopLossValue < stopLossValueTSL)
            //log.info("calcTrailingStopLoss - inside label printing")
            label.new(bar_index, high, text="SL="+ str.tostring(stopLossValueTSL)+"|slP="+str.tostring(slPointsTSL), color=color.new(color.green, 80))
        else if entryType == "Short" and (prevStopLossValue > stopLossValueTSL)
            label.new(bar_index, low, text="SL="+ str.tostring(stopLossValueTSL)+"|slP="+str.tostring(slPointsTSL), color=color.new(color.red, 80),style = label.style_label_up)
        //log.info("calcTrailingStopLoss - enableLongTSL:{0}|enableShortTSL:{1}|prevStopLossValue:{2}|stopLossValueTSL:{3}|slMult:{4}",enableLongTSL,enableShortTSL,prevStopLossValue,stopLossValueTSL,slMult)
        enableLongTSL := false
        enableShortTSL := false
    [stopLossValueTSL, enableLongTSL, enableShortTSL]

I get error on line (slMult := slMult-1) at different candles, on specific conditions, not always. Can you advise what is the exact problem here?

I tried putting logs and manual calculations to understand why it is failing but no luck. There is no way my calculations can take this long time, there has to be some leaks or garbage value that is cause this issue.

New contributor

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

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