3

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/

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
Juan Antonio Tubío
  • 1,172
  • 14
  • 28

1 Answers1

6

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/

Jos
  • 1,732
  • 3
  • 19
  • 39
Juan Antonio Tubío
  • 1,172
  • 14
  • 28