admin管理员组

文章数量:1356778

I want to draw a vertical orange line on the 14th March 2025 @ 06:54 hrs

(I have numerous lines on varying timestamps that also need to be added)

I am using the code below to do this.

However, the line is always offset by 1 bar to the right on every time frame (apart from the 1 min chart)

It's my understanding that because I am using ".xloc.bar_time" that if my chart does not have a bar exactly at that timestamp, the line will offset to the next bar along (to the right)

So, my line is set to 14th March 2025 @ 06:54 hrs BUT

on Daily chart = line drawn on 15th (as opposed to 14th)

on 4 hr chart = line drawn at 08:00 (as opposed to 04:00)

on 1 hr chart = line drawn at 07:00 (as opposed to 06:00)

Is it possible to offset the line 1 bar to the left on all time frames?

Is there another workaround?

would using ".xloc.bar_index work" as I need to use a timestamp?

// Function drawVerticalLine
drawVerticalLine(targetTime) =>
    line.new(
      x1=targetTime, 
      y1=low, 
      x2=targetTime, 
      y2=high, 
      xloc=xloc.bar_time, 
      extend=extend.both, 
      color=color.new(#f9961e, 10), 
      style=line.style_solid, 
      width=1)

// Lines to Plot
targetTime = timestamp(2025,03,14,06,54,00)
drawVerticalLine(targetTime)

Daily Chart 4hr Chart 1hr Chart

I want to draw a vertical orange line on the 14th March 2025 @ 06:54 hrs

(I have numerous lines on varying timestamps that also need to be added)

I am using the code below to do this.

However, the line is always offset by 1 bar to the right on every time frame (apart from the 1 min chart)

It's my understanding that because I am using ".xloc.bar_time" that if my chart does not have a bar exactly at that timestamp, the line will offset to the next bar along (to the right)

So, my line is set to 14th March 2025 @ 06:54 hrs BUT

on Daily chart = line drawn on 15th (as opposed to 14th)

on 4 hr chart = line drawn at 08:00 (as opposed to 04:00)

on 1 hr chart = line drawn at 07:00 (as opposed to 06:00)

Is it possible to offset the line 1 bar to the left on all time frames?

Is there another workaround?

would using ".xloc.bar_index work" as I need to use a timestamp?

// Function drawVerticalLine
drawVerticalLine(targetTime) =>
    line.new(
      x1=targetTime, 
      y1=low, 
      x2=targetTime, 
      y2=high, 
      xloc=xloc.bar_time, 
      extend=extend.both, 
      color=color.new(#f9961e, 10), 
      style=line.style_solid, 
      width=1)

// Lines to Plot
targetTime = timestamp(2025,03,14,06,54,00)
drawVerticalLine(targetTime)

Daily Chart 4hr Chart 1hr Chart

Share Improve this question edited Apr 1 at 8:24 user30113140 asked Mar 30 at 21:56 user30113140user30113140 12 bronze badges New contributor user30113140 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1
  • Hello, you can format code block using 3 backticks. – XouDo Commented Mar 31 at 15:20
Add a comment  | 

2 Answers 2

Reset to default 0

Try with const string in timestamp()

drawVerticalLine(targetTime) =>
    line.new(
      x1     = targetTime,
      y1     = low,
      x2     = targetTime,
      y2     = high,
      xloc   = xloc.bar_time,
      extend = extend.both,
      color  = color.new(#f9961e, 10),
      style  = line.style_solid,
      width  = 1)

// Lines to Plot//
targetTime = timestamp('2025-03-24 06:54 GMT+0')
drawVerticalLine(targetTime)

https://i.imgur/xcjOZua.png

I have found a solution to my original question.
How 'clean' it is I don't know but it works

Option 1 - Offset each // Lines to Plot

Original

// Lines to Plot
targetTime = timestamp(2025,03,14,06,54,00) 
drawVerticalLine(targetTime)

Solution

targetTime = timestamp(2025,03,14,06,54,00)
offsetTime = targetTime - (time - time[1])
drawVerticalLine(offsetTime)

Option 2 - Offset within // Function drawVerticalLine

// Function drawVerticalLine
drawVerticalLine(targetTime) =>
    line.new (
      x1=targetTime - (time - time[1]) , 
      y1=low, 
      x2=targetTime - (time - time[1]) , 
      y2=high, 
      xloc=xloc.bar_time, 
      extend=extend.both, 
      color=color.new(#f9961e, 10), 
      style=line.style_solid, 
      width=1)  

Logic

calculates the duration of 1 bar time - time[1]

calculates the duration of 2 bars time - time[2]

subtracts 1 bar - (time - time[1])

adds 1 bar + (time - time[1])

adds 2 bars + (time - time[2])

本文标签: pine scriptVertical line offset by 1 bar to the right on all timeframesStack Overflow