Search code examples
pine-scriptpine-script-v5

How to remove value from array A if A contains value from array B


getting Error on bar 10514:

In 'array.get()' function. Index 46 is out of bounds, array size is 45.

I am unable to resolve the reason. The purpose of this code is to delete set of values from arrays (numbersBIG and numbersBIGhalf) from the arrays (numbersWHOLE and numbersHALF).

The code the problem stands from:

//@version=5
indicator("snippet", overlay=true, max_lines_count = 500, max_bars_back = 500)


TRange = 1.2700
BRange =  1.2205
ArrFreq = 100*syminfo.mintick
BRange00 = 1.2200
bigArrFreq = 1000*syminfo.mintick
BRangeBigHALF = 1.2250

var numbersWHOLE=array.new_float(0)
var numbersHALF=array.new_float(0)
var numbersBIG=array.new_float(0)
var numbersBIGhalf=array.new_float(0)

if barstate.islast and timeframe.isintraday
    for i = BRange00 to TRange by bigArrFreq
        array.push(numbersBIG,i)
    for i = BRangeBigHALF to TRange by bigArrFreq
        array.push(numbersBIGhalf,i)
    for i = BRange to TRange by ArrFreq
        array.push(numbersHALF,i)

    for i = BRange00 to TRange by ArrFreq
        //if array.indexof(numbersBIG,i) != array.get(numbersWHOLE,i)
        array.push(numbersWHOLE,i)

    for i = 0 to (array.size(numbersWHOLE) == 0 ? na : array.size(numbersWHOLE) - 1)
        ju = numbersWHOLE.get(i)   
        if numbersBIG.includes(ju)            
            //label.new(bar_index-i*2,low,str.tostring(array.get(numbersHALF,i)))
            numbersWHOLE.remove(i)
    for i = 0 to array.size(numbersHALF) - 1        
        line.new(bar_index - 20, array.get(numbersHALF,i),bar_index + 20,  array.get(numbersHALF,i), style = line.style_dashed, color = color.red, width = 1) //, extend = extend.both
    for i = 0 to array.size(numbersWHOLE) - 1        
        line.new(bar_index - 20, array.get(numbersWHOLE,i),bar_index + 20,  array.get(numbersWHOLE,i), style = line.style_dashed, color = color.red, width = 1) //, extend = extend.both
    
    for i = 0 to array.size(numbersBIGhalf) - 1
        line.new(bar_index - 400, array.get(numbersBIGhalf,i),bar_index + 100,  array.get(numbersBIGhalf,i), style = line.style_dashed, color = color.white, width = 1, extend = extend.both) //, extend = extend.both
    for i = 0 to array.size(numbersBIG) - 1
        line.new(bar_index - 400, array.get(numbersBIG,i),bar_index + 100,  array.get(numbersBIG,i), style = line.style_dashed, color = color.white, width = 1, extend = extend.both) //, extend = extend.both

I tried many expressions variations for ... in , while, stil unable to get the idea working.


Solution

  • Here is an example for you that uses array.includes(), array.indexof() and array.remove() functions.

    //@version=5
    indicator("snippet", overlay=true)
    
    test_arr_1 = array.from(1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0)
    test_arr_2 = array.from(1.0, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.35, 1.4, 1.45, 1.5, 1.55, 1.6, 1.65, 1.7, 1.75, 1.8, 1.85, 1.9, 1.95, 2.0)
    num_to_remove_arr = array.from(1.1, 1.8)
    
    s1 = "------   Before   ------" + "\n" +
         "test_arr_1: " + str.tostring(test_arr_1) + "\n" + 
         "test_arr_2: " + str.tostring(test_arr_2) + "\n" + 
         "num_to_remove_arr: " + str.tostring(num_to_remove_arr) + "\n"
    
    len = array.size(num_to_remove_arr)
    
    if (len > 0)
        for i = 0 to len - 1
            num_to_remove = array.get(num_to_remove_arr, i)
    
            if (array.includes(test_arr_1, num_to_remove))
                idx = array.indexof(test_arr_1, num_to_remove)
                array.remove(test_arr_1, idx)
    
            if (array.includes(test_arr_2, num_to_remove))
                idx = array.indexof(test_arr_2, num_to_remove)
                array.remove(test_arr_2, idx)
    
    s2 = "------   After   ------" + "\n" +
         "test_arr_1: " + str.tostring(test_arr_1) + "\n" + 
         "test_arr_2: " + str.tostring(test_arr_2) + "\n" + 
         "num_to_remove_arr: " + str.tostring(num_to_remove_arr) + "\n"
    
    s = s1 + s2
    
    if barstate.islast
        label.new(bar_index, high, s, textcolor=color.white)
    

    enter image description here