'How do I plot a horizontal line on the high and low of a specific time in Pinescript?

My question is really two parts.

  1. How do I plot a horizontal line (that extends to the right but not the left) starting at a specific time of the day, outside of trading hours. I want to plot two horizontal lines actually, one on the high and one on the low of the 8am candle of a 10 minute chart. I would like for it to be plotted historically as well.

  2. How do I make it so that the lines that get plotted only extend to the right until a new bar makes a higher high or a lower low. example horizontal line on the high that I manually drew on the chart

I don't really even know where to start with this. I have some experience writing in Pinescript but not with drawing lines in the way I want. Any help would be greatly appreciated.



Solution 1:[1]

  • Find out new session
  • Save the high value in a var
  • Start drawing until there is a new high
  • Reset everything when it is a new session

Here is an example for the high:

//@version=5
indicator("My script", overlay=true, max_lines_count=500)
h = input.int(9, "Hour", minval=0, maxval=59)
m = input.int(30, "Hour", minval=0, maxval=59)

var float first_high = na
var bool can_draw = false
var line l = na

is_new_session = (hour == h) and (minute == m)
bgcolor(is_new_session ? color.new(color.green, 85) : na)

if (is_new_session)     // New session
    first_high := high  // Reset session high
    can_draw := true    // We are allowed to draw a line
    l := line.new(bar_index, first_high, bar_index, first_high, color=color.red)    // Get new line id
else
    if (can_draw)
        if (high > first_high)  // New high is made
            can_draw := false   // Stop drawing
        else
            line.set_x2(l, bar_index)   // Update x2 position of the line (move it to the right)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vitruvius