Horizontal Ray from all weekly closes

Viewed 88

I am trying to achieve a horizontal ray from the weekly close of the past 52 weeks that can be seen on all time frames. The below appears to achieve this on the weekly time frame.

//@version=4
study("Open Ray to right", overlay=true)
higherTF1 = input("W", type=input.resolution)

dailyopen = security(syminfo.tickerid, higherTF1, open)

line1 = line.new(x1=bar_index[1], y1=dailyopen, x2=bar_index, y2=dailyopen, 
xloc=xloc.bar_index, style=line.style_solid,extend=extend.right, color=color.green)

It produces the following:

enter image description here

However it doesn't seem to have the amount amount on lower time frames such as on the 1h:

enter image description here

I can get this to work with the following:

study("PCL", overlay=true)

prev_daily_close = security(tickerid, "W", close[1])
plot(prev_daily_close, title="PCL", trackprice=false, style=circles) 

However the lines are plotted with circles rather than horizontal rays. I would like them to run extended across the chart.

enter image description here

Thanks in advance.

1 Answers
//@version=5
indicator("Weekly closes", overlay = true, max_lines_count = 500)

num_weekly_closes = input.int(52, title = "Number of weekly closes", minval = 1, maxval = 500)

weekly_close = time_close == time_close("W") and barstate.isconfirmed

var line[] weekly_close_lines = array.new_line()

if weekly_close
    array.push(weekly_close_lines, line.new(x1 = bar_index, y1 = close, x2 = bar_index + 1, y2 = close, color = color.blue, width = 2))

if array.size(weekly_close_lines) > num_weekly_closes
    line.delete(array.shift(weekly_close_lines))

if array.size(weekly_close_lines) > 0
    for i = 0 to array.size(weekly_close_lines) - 1
        line.set_x2(array.get(weekly_close_lines, i), x = bar_index)
Related