Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TradingView Pine script's time() function for weekends?

I'm developing an indicator that shows custom time zone sessions by changing chart background.

For that I use this function to check if one bar is inside one of my defined time sessions:

InSession(sess) => na(time(period, sess)) == false

Where sess it's something similar to "0130-0800".

But it's not drawing anything during the weekend. It seems that the time() function only checks its input on workdays. But I use this indicator in cryptocurrencies that are open every day.

Is there any way to extend the time() check to the weekends? If not, can you think of another method to check if a bar is within a timeframe?

PS: This is the full indicator code: https://es.tradingview.com/script/NMjZ2616/

like image 572
Juan Antonio Tubío Avatar asked Oct 28 '25 19:10

Juan Antonio Tubío


1 Answers

You only have to add the string ":1234567" to the session hours to include weekends and ":12345" to exclude weekends.

Sample code:

// InSession() determines if a price bar falls inside the specified session
InSession(sess) => na(time(period, sess)) == false

// === INPUTS ===
sessionHours=input(defval="0800-1400", type = session, title="Session Hours")
inputIncludeWeekends=input(title="Include Weekends?", type=bool, defval=true)

weekendsStr = inputIncludeWeekends ? ":1234567" : ":12345"

// === /INPUTS ===

bgcolor(color=InSession(sessionHours + weekendsStr)[1] ? red : na, title="Session Hours", transp=85)

Thanks to https://www.tradingview.com/u/pequet/

like image 121
Juan Antonio Tubío Avatar answered Oct 31 '25 03:10

Juan Antonio Tubío