Indicator is supposed to draw three lines but it draws a lot more

47 Views Asked by At

Hi my indicator is supposed to only draw three lines starting at bar_index[15] and ending at bar-index[14] but somehow it draws fewer lines on every bar going back. I think it has something to do with the bar_index but I can't figure it out.

enter image description here

//@version=5 
indicator("My Fan", overlay=true, max_bars_back = 400)


drawLines(origX, origY, adjX, adjY) =>
    // Calculate key values
    dx = adjX - origX
    dy = adjY - origY
    priceUnit = dy / dx


    draw1x1 = input(true, "Draw 1 Line")
    draw1x2 = input(true, "Draw 2 Line")
    draw2x1 = input(true, "Draw 3 Line")


    adjPriceUnit = 1 / 8  // 1/8 line
    adjYnew = adjY - (adjPriceUnit * dx)

    origYnew = origY
    origXnew = origX
    adjXnew = adjX
    dxnew = adjXnew - origXnew
    dynew = adjYnew - origYnew
    priceUnitnew = dynew / dxnew

    // 1 line
    if draw1x1
        line.new(origX, origY, adjX, adjYnew, extend=extend.right, color=color.blue, width=2)

    // 2 line
    if draw1x2
        y2 = origYnew + priceUnitnew * 2
        line.new(origX, origY, adjX, y2, extend=extend.right, color=color.blue, width=1)

    // 3line
    if draw2x1
        x2 = origXnew + dxnew * 2
        line.new(origX, origY, x2, adjY, extend=extend.right, color=color.blue, width=1)

// User inputs
origX = bar_index[15]
origY = input.float(448, "Origin Y")
adjX = bar_index[14]
adjY = input.float(446, "Adjustment Y")


drawLines(origX, origY, adjX, adjY)

I tried changing the bar_index and it just drew more lines...

1

There are 1 best solutions below

0
vitruvius On

Your function drawLines() draws three lines and it is called on every bar. Therefore, you will get many lines. If you only want to keep the last three lines, then you need to delete the old ones.

// 1 line
if draw1x1
    l1 = line.new(origX, origY, adjX, adjYnew, extend=extend.right, color=color.blue, width=2)
    line.delete(l1[1])

// 2 line
if draw1x2
    y2 = origYnew + priceUnitnew * 2
    l2 = line.new(origX, origY, adjX, y2, extend=extend.right, color=color.blue, width=1)
    line.delete(l2[1])

// 3line
if draw2x1
    x2 = origXnew + dxnew * 2
    l3 = line.new(origX, origY, x2, adjY, extend=extend.right, color=color.blue, width=1)
    line.delete(l3[1])