0

On a 1 minute chart (or any timeframe), I'm using the request.security function to get the open price, open time and closing time from the daily timeframe. What I'm looking to do is draw a horizontal line where y=day's open price, x1 = day's open time and x2 = day's closing time. I've tried several iterations that all work and print a separate horizontal line for each of the previous days, however for some reason the current day (session that has not closed yet) does not print a line. I'm assuming it has something to do with my use of "time_close" and the fact that the session hasn't actually closed yet.

Here is my code...

//@version=5
indicator("myTest", overlay=true)

//get open price, open time and close time from daily
[d_open, d_time_open, d_time_close] = request.security(syminfo.tickerid,"D", [open,time,time_close])

//check for first bar of the day
is_first_bar = ta.change(time("D"))
if (is_first_bar)
    //print line from beginning of day to end of day at the opening price of the day if first bar of day
    line.new(d_time_open, d_open, d_time_close, d_open, xloc.bar_time, extend.none, color.yellow, line.style_solid, 1)

Screen shot showing a yellow line everyday except for the current/last session...

enter image description here

broncomz1
  • 23
  • 3

1 Answers1

0

No need to use the request security for this. You can use the first bar_index and build in open variable since you are checking if a new day.

//@version=5
indicator("My script", overlay = true)

newDay = ta.change(time('D'))
var line openLine = na
if newDay
    openLine := line.new(bar_index, open, bar_index+1, open)
openLine.set_x2(bar_index)
smashapalooza
  • 932
  • 2
  • 2
  • 12
  • Thanks @smashapalooza, it looks like this might work, however I need to wait until the next trading session to verify for sure. If you don't mind, I'm having trouble understanding why the "openLine := line.new..." line doesn't print the horizontal line by itself and you need the additional "openLine.set_x2(bar_index)" line? – broncomz1 Jul 15 '23 at 21:43
  • No problem, because we are using a conditional statement it will only plot the line if it’s a new day. When the line is created it’s X2 is set to the bar_index + 1, so if we don’t use the openLine.set_x2 the line will stay in the same position all day, that line makes sure it continues to extend as the day progresses – smashapalooza Jul 15 '23 at 23:43