How to draw a support and resistance line that ends when crossed

93 Views Asked by At

I'm new to coding/pine. For the last week I have been trying to code what I believe to be a fairly simple bit of code to no avail. Any help is appreciated.

I have a bit of code that identifies a fractal. It then draws a horizontal line based on the fractal. What I want it to do is to extend this line until price crosses it again.

What I have tried:

  1. Creating a FOR loop that cycles through al the bars since the fractal and counts each bar for which the low of the fractal was not crossed. This is what I have shown in the current code.

  2. A WHILE loop to no avail.

// © Wizzle

//@version=5
indicator('Fractal Test with CV Count', overlay=true)

// ** VARIABLES **
body = math.abs(open - close)
fractalperiods = input.int(defval=1, title='Fractal Periods', minval=1, maxval=5)
up_fractal = ta.pivotlow(fractalperiods, fractalperiods)
down_fractal = ta.pivothigh(fractalperiods, fractalperiods)

// ** Plot Fractals ** 
plotshape(up_fractal, title='Up Fractal', style=shape.square, location=location.belowbar, color=color.new(#0cc006, 0), offset=-1 * fractalperiods, size=size.small)
plotshape(down_fractal, title='Down Fractal', style=shape.square, location=location.abovebar, color=color.new(#ff0000, 0), offset=-1 * fractalperiods, size=size.small)


var line fractalline = na
if up_fractal //and bars_since_fractal < 50
    
    int bars_since_fractal = ta.barssince(up_fractal)
    upf_crossed = (low[0] < low[up_fractal]) == true

    fractalline := line.new(bar_index[fractalperiods], low[fractalperiods], bar_index + 1, low[fractalperiods])
    
// ** Loop ** 

bool cv = ta.crossunder(low, low[up_fractal]) == false //determines if price is above the low of the fractal
int count_cv = 0
for counter = 0 to ta.barssince(up_fractal) by 1
    if cv[counter] 
        count_cv += 1
        line.set_x2(fractalline, bar_index+count_cv)


Any help or pointers are appreciated : ) )

1

There are 1 best solutions below

1
On
// © Wizzle

//@version=5
indicator("My Fractal Array Test", overlay = true)

var float [] float_line  = array.new_float()
var line [] one_line = array.new_line()
fractalperiods = input.int(defval=1, title='Fractal Periods', minval=1, maxval=5)
up_fractal = ta.pivotlow(fractalperiods, fractalperiods)

// TEST 4 IS THE BEST VERSION
//  Test 4  //

if up_fractal
    array.push(float_line,low)
    array.push(one_line,line.new(bar_index -1,low[up_fractal],bar_index+10,low[up_fractal]))


if array.size(one_line) > 0
    //last_array = array.last(float_line)
    for i = 0 to array.size(one_line) -1
        if low < array.last(float_line) //last_array
            line.set_x2(array.last(one_line),bar_index)
            array.pop(one_line)
            array.pop(float_line)

So I am nearly there, finally. I have created an array of lines: the last element in the array is always the first line to be crossed. So using the array.last I can check if price is below it. It works nicely 90% of the time.

There is still one bug, if price crosses more than 1 line on the same bar, it somehow doesnt work. Any ideas ?