Classic Scott
Classic Scott

Reputation: 37

Why do these lines extend to the current bar instead of stopping like the boxes do?

enter image description here

As you can see, I have boxes drawn that when broken based on certain logic stop at that bar index. The lines should follow this same logic, yet do not. I've spent hours over the past week+ reading up on for loops and arrays in Tradingview's manual, yet still can't seem to figure out what I'm doing wrong.

Below is the logic for one of the two types of boxes.

res_style = input.string(defval = 'Dotted', options = ['Dotted', 'Dashed', 'Solid'], title = 'Border Style', group = 'Resistance Zones')
res_width = input.int(defval = 1, maxval = 4, title = 'Border Width', group = 'Resistance Zones')
res_color = input.color(color.new(color.red, 20), title = 'Border Color', group = 'Resistance Zones')
res_fill = input.color(color.new(color.red, 85), title = 'Fill Color', group = 'Resistance Zones')

res_left = prev_bar
res_top = high[1]
res_right = bar_index
res_bottom = low[1]
res_xloc = xloc.bar_index
res_border_style = res_style == 'Dotted' ? line.style_dotted : res_style == 'Dashed' ? line.style_dashed : res_style == 'Solid' ? line.style_solid : na

//----- ARRAYS
var resistance_box = array.new_box()
var resistance_high = array.new_float()

if _k_x_under_ob or _d_x_under_ob
    array.push(resistance_high, res_top)
    array.push(resistance_box, box.new(res_left, res_top, res_right, res_bottom, xloc = res_xloc, border_color = res_color, border_style = res_border_style, border_width = res_width, bgcolor = res_fill))

// Extend box and check for price break to stop box
if (array.size(resistance_box) > 0)
    for i = 0 to array.size(resistance_box) - 1
        activeBox = array.get(resistance_box, i)
        activeBox.set_right(bar_index)

if (array.size(resistance_high) > 0)
    for i = array.size(resistance_high) - 1 to 0
        boxHighPrice = array.get(resistance_high, i)
        if (close > boxHighPrice)
            brokenBox = array.get(resistance_box, i)
            brokenBox.set_right(bar_index)
            array.remove(resistance_high, i)
            array.remove(resistance_box, i)

And the logic for the lines, which I would think would be basically the same...

mid_style = input.string(defval = 'Dotted', options = ['Dotted', 'Dashed', 'Solid'], title = 'Line Style', group = 'Mid Line')
mid_width = input.int(defval = 1, maxval = 4, title = 'Line Width', group = 'Mid Line')
mid_color = input.color(color.white, title = 'Line Color', group = 'Mid Line')

x1 = prev_bar
y1 = zone_avg
x2 = bar_index
y2 = zone_avg
_mid_style = mid_style == 'Dotted' ? line.style_dotted : mid_style == 'Dashed' ? line.style_dashed : mid_style == 'Solid' ? line.style_solid : na

//----- ARRAYS
var mid_line = array.new_line()
var mid_price = array.new_float()

if _k_x_over_os or _d_x_over_os or _k_x_under_ob or _d_x_under_ob
    array.push(mid_price, y1)
    array.push(mid_line, line.new(x1, y1, x2, y2, xloc = xloc.bar_index, color = mid_color, style = _mid_style, width = mid_width))

// Extend line and check for price break to stop line
if (array.size(mid_line) > 0)
    for i = 0 to array.size(mid_line) - 1
        activeLine = array.get(mid_line, i)
        activeLine.set_x2(bar_index)

if (array.size(support_low) > 0)
    for i = array.size(support_low) -1 to 0
        boxLowPrice = array.get(support_low, i)
        if (close < boxLowPrice)
            brokenLine = array.get(mid_line, i)
            brokenLine.set_x2(bar_index)
            array.remove(mid_price, i)
            array.remove(mid_line, i)

if (array.size(resistance_high) > 0)
    for i = array.size(resistance_high) -1 to 0
        boxHighPrice = array.get(resistance_high, i)
        if (close > boxHighPrice)
            brokenLine = array.get(mid_line, i)
            brokenLine.set_x2(bar_index)
            array.remove(mid_price, i)
            array.remove(mid_line, i)

I have a feeling it has something to do with the nested if statements, and have tried numerous variations of substituting the mid_price id for resistance_high/support_low, but I just get out of bounds errors.

I tried nesting for loops like this:

    for i = array.size(support_low) -1 to 0
        for j = array.size(mid_price) -1 to 0
            boxLowPrice = array.get(support_low, i)
            if (close < boxLowPrice)
                brokenLine = array.get(mid_line, j)
                brokenLine.set_x2(bar_index)
                array.remove(mid_price, j)
                array.remove(mid_line, j)

and still the same issue.

Please help!

Upvotes: 0

Views: 28

Answers (0)

Related Questions