0

Consider this simple Pine script

//@version=4
level1 = 3050
study("FutureLine", overlay=true)
line.new(timestamp(year,month,dayofmonth,08,30), level1, timestamp(year,month,dayofmonth,13,30), level1, xloc=xloc.bar_time)

It's supposed to draw a line from 08:30 to 13:30 at the day of the last bar.
However, it draws that line on the day of the last bar AND on the day before that.
Any idea why that is?

Example is on 15min bars of SPX
enter image description here

Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42

1 Answers1

0

year is the year of the bar the script is running on. year(timenow) is the current year. What was happening is that you were drawing a line on each bar using that bar's year, month and day, and garbage collection was only keeping the last ones. Only 2 showed but there were many more, superimposed.

This code only creates a line on the dataset's first bar and then only modifies it when the script reaches the last bar, so it uses that bar's date:

//@version=4
study("FutureLine", overlay=true)
level1 = 3050
start = timestamp(year,month,dayofmonth,08,30)
stop  = timestamp(year,month,dayofmonth,13,30)
var line ln = line.new(start, level1, stop, level1, xloc=xloc.bar_time)
if barstate.islast
    line.set_x1(ln, start)
    line.set_x2(ln, stop)

It's more efficient to modify an existing line than to delete and create a new one, which is what would be required to keep only the last one.

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21