pine-scripttradingview-api

Pine script draw vertical lines at a certain time


[Pine script] I would like to draw two vertical lines(dashed) every weekday at certain times (e.g. 11:30 GMT and 15:30 GMT). But I could not do what I want. It doesn't draw lines beyond certain time and doesn't draw dashed lines. Below is the code which I have tried.

Please help..

enter image description here

//@version=5
indicator('Timegap', overlay=true, max_lines_count=500, max_bars_back=4999)

weekday = (dayofweek != dayofweek.saturday and dayofweek != dayofweek.sunday)

t1 = timestamp("GMT", year, month, dayofmonth, 11, 30, 00)
t2 = timestamp("GMT", year, month, dayofmonth, 15, 30, 00)

timeIsOk = (time >= t1) and (time <= t2)

if timeIsOk and weekday
    line.new(t1, low, t1, high, xloc.bar_time, extend.both, color.rgb(255, 255, 0), line.style_dashed, 1)
    line.new(t2, low, t2, high, xloc.bar_time, extend.both, color.rgb(255, 255, 0), line.style_dashed, 1)

Solution

  • Your code works, but you should replace your condition :

    timeIsOk = (time >= t1) and (time <= t2)
    

    By

    timeIsOk = (time == t1) or (time == t2)