Dinuka W
Dinuka W

Reputation: 45

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)

Upvotes: 1

Views: 2857

Answers (2)

Vivek Deshpande
Vivek Deshpande

Reputation: 11

I am providing below the code that should work for you by adjusting the GMT time zone (I used GMT+5:30 for India 9:15am to 15:30pm) and then defining the exact time of your location while calculating t1 and t2

//@version=5

indicator('Session Lines', overlay=true, max_lines_count=500, max_bars_back=4999)

t1 = timestamp("GMT+5:30", year, month, dayofmonth, 05, 30, 00)

t2 = timestamp("GMT+5:30", year, month, dayofmonth, 17, 30, 00)

if time == t1

    line.new(t1, low, t1, high,  xloc.bar_time, extend.both, color.rgb(255, 255, 0), line.style_dashed, 1)

if time == t2

    line.new(t2, low, t2, high, xloc.bar_time, extend.both, color.rgb(0, 255, 255), line.style_dashed, 1)

Upvotes: 1

G.Lebret
G.Lebret

Reputation: 3108

Your code works, but you should replace your condition :

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

By

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

Upvotes: 2

Related Questions