Pinescript v5 Indicator Removing Lines Under Certain Circumstances

I am writing a TradingView Indicator that I think is pretty simple. I am creating an array of price levels, than passing it to a function to print them called “draw_levels” that loops through the data array and uses line.new() with each price in the array.

That works fine.

I want to draw zone’s around those levels like +/- 1% then fill that space. So I added this code into the draw_levels() function with a conditional to only draw the zones if the input var is true.

The problem I am seeing is when I toggle the input to show/hide the zones. For some reason, when I turn the option on, some of the price levels are simply removed from the chart. Toggle it off and the levels come back.

Any idea why?

Here’s the code:

//@version=5
indicator("Levels", overlay = true)

zones_groupname = "Zone Options"
zone_band_size_l   = input.float(1, "Level Zone +/- %", group = zones_groupname)
zone_band_size_a   = input.float(0.09, "Zone for Alerts +/- %", group = zones_groupname)

visuals_groupname  = "Visual Options"
supercycle_color   = input.color(color.yellow, "", group = visuals_groupname, inline = "vis_1")
show_supercycles   = input.bool(true, "Show Super Cycle Levels?", group = visuals_groupname, inline = "vis_1")

level_color        = input.color(color.red, "", group = visuals_groupname, inline = "vis_2")
show_levels        = input.bool(true, "Show Levels?", group = visuals_groupname, inline = "vis_2")

zone_band_color_l  = input.color(color.blue, "", group = visuals_groupname, inline = "vis_4")
show_zone_bands_l  = input.bool(false, "Show Zone around Levels?", group = visuals_groupname, inline = "vis_4")

// This function will draw the levels and super cycle levels on the chart. 
draw_levels(levels, labels) =>
    // Loop through the levels in the array and draw them 
    for i = 0 to array.size(levels) - 1
        
        // get prices and labels 
        price = array.get(levels, i)
        level_label = array.get(labels, i)

        if level_label == "level"
            line.new(bar_index, price, bar_index + 1, price, width=2, color=level_color, extend=extend.both)
        
        if show_zone_bands_l and level_label == "level"
            // calculate zone around level 
            upper_band_price = price + (price * (zone_band_size_l/100))
            lower_band_price = price - (price * (zone_band_size_l/100))
            
            // plot upper and lower band
            upper_line = line.new(bar_index, upper_band_price, bar_index + 1, upper_band_price, width=2, color=zone_band_color_l, extend=extend.both)
            lower_line = line.new(bar_index, lower_band_price, bar_index + 1, lower_band_price, width=2, color=zone_band_color_l, extend=extend.both)

            // fill the zone between lines
            linefill.new(upper_line, lower_line, color.new(zone_band_color_l, 75))
        
draw_sc_levels(levels, labels) =>
    // Loop through the levels in the array and draw them 
    for i = 0 to array.size(levels) - 1
        
        // get prices and labels 
        price = array.get(levels, i)
        level_label = array.get(labels, i)

        if level_label == "supercycle"
            line.new(bar_index, price, bar_index + 1, price, width=2, color=supercycle_color, extend=extend.both)   
        
        if show_zone_bands_sc and level_label == "supercycle"
            // calculate zone around level 
            upper_band_price = price + (price * (zone_band_size_l/100))
            lower_band_price = price - (price * (zone_band_size_l/100))
            
            // plot upper and lower band
            upper_line = line.new(bar_index, upper_band_price, bar_index + 1, upper_band_price, width=2, color=zone_band_color_sc, extend=extend.both)
            lower_line = line.new(bar_index, lower_band_price, bar_index + 1, lower_band_price, width=2, color=zone_band_color_sc, extend=extend.both)

            // fill the zone between lines
            linefill.new(upper_line, lower_line, color.new(zone_band_color_sc, 75))  
        
        

// get the symbole the chart is on
symbol = syminfo.ticker

// declare arrays to hold level data 
var float[] levels = na
var string[] labels = na

// symbol data
if symbol == "USDJPY"
    levels := array.new_float(18)
    array.set(levels , 0, 162.437)
    array.set(levels , 1, 156.313) 
    array.set(levels , 2, 151.434) 
    array.set(levels , 3, 146.31) 
    array.set(levels , 4, 141.186) 
    array.set(levels , 5, 136.409) 
    array.set(levels , 6, 131.249) 
    array.set(levels , 7, 126.829) 
    array.set(levels , 8, 122.608) 
    array.set(levels , 9, 116.5) 
    array.set(levels , 10, 111.829) 
    array.set(levels , 11, 106.296) 
    array.set(levels , 12, 101.769) 
    array.set(levels , 13, 96.811) 
    array.set(levels , 14, 91.961) 
    array.set(levels , 15, 86.464)
    array.set(levels , 16, 81.434) 
    array.set(levels , 17, 76.547) 


    labels := array.new_string(18)
    array.set(labels, 0, 'level')
    array.set(labels, 1, 'level')
    array.set(labels, 2, 'supercycle')
    array.set(labels, 3, 'level')
    array.set(labels, 4, 'level')
    array.set(labels, 5, 'level')
    array.set(labels, 6, 'level')
    array.set(labels, 7, 'supercycle')
    array.set(labels, 8, 'level')
    array.set(labels, 9, 'level')
    array.set(labels, 10, 'level')
    array.set(labels, 11, 'level')
    array.set(labels, 12, 'supercycle')
    array.set(labels, 13, 'level')
    array.set(labels, 14, 'level')
    array.set(labels, 15, 'level')
    array.set(labels, 16, 'level')
    array.set(labels, 17, 'supercycle')

// conditionally show the different types of levels 
if show_levels 
    draw_levels(levels, labels)
if show_supercycles
    draw_sc_levels(levels, labels)

I have tried changing the data, limiting the data, re-inputting the data, re-typing the data arrays to ensure no mistakes, changed around the logic for toggling the zones on/off.

New contributor

Bryan Goldstein 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