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:
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.
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 : ) )
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 ?