Search code examples
pine-scriptpine-script-v5

Splitting a string and drawing hline for its individual values without encountering series array error in pine script


here is the instructions:

1.script gets the input string 2.separates that by | 3.then separate each part again by "," 4.then draws a line for each value.

so far I have come to this mess:


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


paste_values_here = input(defval = "GEXFLIPS,147, 148, 150, 153, 156, 159, 161, 165, 166, 180, 181, 182, 183, 184, 186, 187, 188, 189|GEXLEVELSP,147, 157, 200|GEXLEVELSN,192, 205|GEXLEVELSHP,147, 200, 157|GEXLEVELSHN,205, 192|VOLLEVELSC,195|VOLLEVELSP,174, 186|VOLLEVELSHC,195|VOLLEVELSHP,174, 186")

parts = array.new_string(0)
if bar_index == 0
    parts := str.split(paste_values_here, "|")


// Assign each part to a new variable
gexFlips = array.new_string(0)
gexLevelsP = array.new_string(0)
gexLevelsN = array.new_string(0)
gexLevelsHP = array.new_string(0)
gexLevelsHN = array.new_string(0)
volLevelsC = array.new_string(0)
volLevelsP = array.new_string(0)
volLevelsHC = array.new_string(0)
volLevelsHP = array.new_string(0)

if bar_index == 0
    array.push(gexFlips, array.get(parts, 0))
    array.push(gexLevelsP, array.get(parts, 1))
    array.push(gexLevelsN, array.get(parts, 2))
    array.push(gexLevelsHP, array.get(parts, 3))
    array.push(gexLevelsHN, array.get(parts, 4))
    array.push(volLevelsC, array.get(parts, 5))
    array.push(volLevelsP, array.get(parts, 6))
    array.push(volLevelsHC, array.get(parts, 7))
    array.push(volLevelsHP, array.get(parts, 8))



if bar_index == 0
    log.info(array.get(gexFlips, 0))


if bar_index == 0
    gexFlipsStr = array.get(gexFlips, 0)
    gexFlipsParts = str.split(gexFlipsStr, ",")
    
    
    for i = 1 to array.size(gexFlipsParts) - 1
        levelStr = str.trim(array.get(gexFlipsParts, i))
        level = str.tonumber(levelStr)
        hline(level, "GEXFLIPS " + levelStr, color=color.blue)

I have tried many other ways but still cannot find the right method to achieve this.

apparently the problem with the code above is that : Cannot call 'hline' with argument 'price'='level'. An argument of 'series float' type was used but a 'input float' is expected.


Solution

  • You need to split it with | and get each group within your string. Then loop through that array and split each item with ",". You need to loop through that array to get each number.

    //@version=5
    indicator("Split Example", overlay=true)
    
    
    // paste_values_here = input(defval = "GEXFLIPS,147, 148, 150, 153, 156, 159, 161, 165, 166, 180, 181, 182, 183, 184, 186, 187, 188, 189|GEXLEVELSP,147, 157, 200|GEXLEVELSN,192, 205|GEXLEVELSHP,147, 200, 157|GEXLEVELSHN,205, 192|VOLLEVELSC,195|VOLLEVELSP,174, 186|VOLLEVELSHC,195|VOLLEVELSHP,174, 186")
    
    paste_values_here = input(defval = "GEXFLIPS,1, 2, 3|GEXLEVELSP,5, 6, 7|GEXLEVELSN,8")
    
    f_parse(p_txt) =>
        str_split_gr_arr = str.split(p_txt, "|")                    // Split by | to get each group
        len_split_gr_arr = array.size(str_split_gr_arr)
    
        if (len_split_gr_arr > 0)
            for i = 0 to len_split_gr_arr - 1
                string str = array.get(str_split_gr_arr, i)
                str_split_ind_arr = str.split(str, ',')             // Split by , to get each individual string within a group
                len_split_ind_arr = array.size(str_split_ind_arr)
    
                if (len_split_ind_arr > 1)                          // > 1 because the first string is just some text and not a number
                    for k = 1 to len_split_ind_arr - 1
                        str_num = array.get(str_split_ind_arr, k)   // Get the number
                        num = str.tonumber(str_num)
                        line.new(bar_index, num, bar_index + 1, num, color=color.yellow, extend=extend.both)
    
    if (barstate.isfirst)                                           // Run it just once
        f_parse(paste_values_here)
    

    enter image description here

    Note: I changed your input to have a quick and easy test.