user6761052
user6761052

Reputation: 31

Study Error: Bar index value of the 'left' argument (-1.0000) in box.new() is too far from the current bar index. Try using 'time' instead

This code generates the very rare error mentioned above, in some charts only - seems more prevalent in charts with many bars. I cannot find any references or documentation that points to a possible issue. I've tried setting the left value of box.new() manually to various values, but nothing seems to solve it.

Any ideas would be highly appreciated?



//@version=5
indicator("Horizontal Support and Resistance zones", overlay=true, max_bars_back=75)

// Make floating-point array
var float[] all_ohlc =   array.new_float()
var float[] all_perc =   array.new_float()
var float[] box_array =   array.new_float()
var float[] allvalues =   array.new_float()

deviation_allowed_perc = input.float(5, "Percentage Deviation Allowed", 0.0, 100.0, 0.1)
//min_count = input.int(6, "Minimum touchpoints", 1, 50, 1)
min_count_perc = input.float(5, "Minimum touchpoints  as % of total bars", 1, 10, 0.1)
max_lookback = input.int(500, "Maximum bars lookback", 1, 1000, 1)
ignore_hl = input.bool(true, "Ignore High & Lows")
debug = input.bool(true, "Debug")
zone_col = input.color(color.new(color.aqua,0), 'Colors',inline='col')
trans  =  input.int(90,title="",inline='col',minval=0,maxval=100)



// Make one label to show the chart's bar count
var barsLabel = label.new(x=na, y=na, style=label.style_label_left,
     color=color.teal, textcolor=color.white, size=size.large)
     
var loopcount1 = 0
var loopcount2 = 0
var loopcount3 = 0
var loopcount4 = 0
     
var boxcount = 0
var boxindex = last_bar_index

var min_barindex = math.max(0, last_bar_index-max_lookback)
var min_count = (last_bar_index-min_barindex) * min_count_perc / 100

// Add the opens, highs, lows and closes to the array
if barstate.isconfirmed and bar_index >= min_barindex
    array.push(id=all_ohlc, value=open)
    array.push(id=all_ohlc, value=close)
    if not ignore_hl
        array.push(id=all_ohlc, value=high)
        array.push(id=all_ohlc, value=low)
    
var thearray = str.tostring(all_ohlc)

             
if barstate.islast
    
    // Sort the Array from highest to lowest values
    array.sort(all_ohlc, order.descending)
    
    // Get first value - possible boxtop
    var highvalue = array.shift(all_ohlc)
    boxtop = 0.0
    boxbottom = 0.0


    var count = 0
    
    for i = 0 to array.size(all_ohlc) - 1
        loopcount1 := loopcount1 + 1
        curr_value = array.shift(all_ohlc)
        array.push(id=allvalues, value=curr_value)
        if highvalue / curr_value <= (1+deviation_allowed_perc/100) //Check whether the two values are close to each other
            count := count + 1
            loopcount2 := loopcount2 + 1
            if count == 1
                boxtop := highvalue
                boxindex := math.min(array.indexof(all_ohlc, highvalue), boxindex)
            if count >= 1
                boxbottom := curr_value
                boxindex := math.min(array.indexof(all_ohlc, curr_value), boxindex)

        else //This executes when the two values are not close to each other
            loopcount3 := loopcount3 + 1
            if count >= min_count // draw a box
                loopcount4 := loopcount4 + 1
                boxcount := boxcount + 1
                if bar_index - 50 > boxindex
                    boxindex ==  bar_index - 50
                if boxindex < 1
                    boxindex ==  1
                array.push(id=box_array, value=boxtop)
                array.push(id=box_array, value=boxbottom)
                box.new(boxindex, boxtop, bar_index, boxbottom, xloc=xloc.bar_index, bgcolor=color.new(zone_col, trans), border_color=color.new(zone_col, trans), extend=extend.right)

                
            // Reset parameters to start over
            count := 0
            highvalue := curr_value
            
        

    



    labelText = "This " + syminfo.ticker + " chart has\n"
         + "Price bars: " + str.tostring(bar_index + 1, "##,###") + "\n"
         + "Min Bar Index: " + str.tostring(min_barindex) + "\n"
         + "Min Count: " + str.tostring(min_count) + "\n"
         + "Array size: " + str.tostring(array.size(all_ohlc)) + "\n"
         + "Loop1: " + str.tostring(loopcount1) + "\n"
         + "Loop2: " + str.tostring(loopcount2) + "\n"
         + "Loop3: " + str.tostring(loopcount3) + "\n"
         + "Loop4: " + str.tostring(loopcount4) + "\n"
         + "Boxcount: " + str.tostring(boxcount) + "\n"
         + "Array: " + str.tostring(str.substring(thearray, 0, 50)) +"\n"
         + "All Values: " + str.tostring(array.slice(allvalues, 0, 50)) + "\n"
         + "All Perc: " + str.tostring(str.substring(str.tostring(all_perc), 0, 50)) + "\n"
         + "Box Values: " + str.tostring(str.substring(str.tostring(box_array), 0, 50)) + "\n"
    label.set_text(id=barsLabel, text=labelText)
    if debug
        label.set_xy(id=barsLabel, x=bar_index + 2, y=close*2)

Upvotes: 2

Views: 1456

Answers (1)

Starr Lucky
Starr Lucky

Reputation: 1961

error lying somewhere here:

            if count >= 1
                boxbottom := curr_value
                boxindex := math.min(array.indexof(all_ohlc, curr_value), boxindex)

array.indexof

The function returns the index of the first occurrence of the value, or -1 if the value is not found.

Upvotes: 0

Related Questions