Jona Rodrigues
Jona Rodrigues

Reputation: 980

How to check if future hour is a valid regular market session in pinescript?

I need to plot a box in the future using box.new(), for example, starting 3h from the last bar and extending for 10 periods.

Some markets, like FOREX, have breaks during weekends, for example from Friday 4pm to Sunday 4pm the market is closed. On TradingView charts, these breaks are automatically taken into account, and the next candle "placeholder" (empty slot which will be filled with next upcoming candle) already reflects this break jumping from Friday 4pm directly to Sunday 4pm.

How can I programatically anticipate these breaks using Pine-Script, for any market (not just for this specific example) ? In other words, how can I check if next candle will be a valid trading hour ?

I can already check that for the current bar using session.ismarket. However, I cannot see any ways to check this for future bars. Ideally I would need some way to check if a custom timestamp is valid, like session.ismarket(timestamp). Or is there any other ways to deal with this situation?

NOTE: As a side-effect, I found a bug related to this issue. In fact, when drawing a box in the future using box.new(), if the corresponding box is drawn during a break, only the left edge of the box is plotted on the chart. For example, say the last 1h candle is on friday 4pm, and next empty candle slot jumps to sunday 4pm: if I draw a box using box.new() from Saturday 8am to Saturday 6 pm, I expect to see nothing plotted on the chart since these hours literally don't exist for this specific market. However, for some reasons I still don't get, only the left edge of this box will be plotted on the empty candle slot of Sunday 4pm... Very confusing. In that example, I would need a way to determine if next timestamp WILL be a regular market hour and if not, I could displace my box or just prevent from plotting it.

Upvotes: 0

Views: 742

Answers (1)

John Baron
John Baron

Reputation: 291

as you know there is no function to check a time against session. I am not entirely sure why you need to do it using time vs bars, regardless, if I had to do this I probably would try to save the time/session information as the bars work through the historical data, using the existing session calls. start_session_time:= session.ismarket? time:start_session_time and u can get more granular by checking day of week and basically build your own internal market calendar

Upvotes: 0

Related Questions