The rule sheets you wrote in Level 3 said when, not just what. Only between 9:30 and 11:00 New York time, say, or only Monday to Friday. Today you'll put that into code. By the end of this lesson, you'll know how to check whether a candle sits inside a time window. You'll see why the time zone has to be in the script. You'll also limit a script to certain days.
1 of 8
What you'll learn
- Set a trading window. Use time() with your hours. Outside them it gives na, or no value.
- Write the time zone in. Without it, the script uses the exchange's time zone.
- Read the window edges. It includes the start time but not the end time.
- Pick the days. Add day numbers to the window, or ask for one day by name.
- Filter signals by session. Work out the cross on its own line, then join it with and.
Exercise
- Add the Trading window script to a 5-minute MES1! chart.
- Then delete ", "America/New_York"" from the time() line, save, and see where the shading moves.
- Put it back, and change the window to the hours on your own rule sheet.
Quick check
1. You leave the time zone out of time() on an MES chart. Which clock does the window use?
2. In the session string "0930-1100:23456", what does 2 mean?
3. Is the 11:00 candle inside "0930-1100"?
0 of 3 answered.
Finished this lesson?
Tick it off and keep your place
Read the full notes
To limit a Pine Script to certain hours, use not na(time(timeframe.period, "0930-1100", "America/New_York")), and always write the time zone in, because the script otherwise uses the exchange's own time zone.
- inWindow = not na(time(timeframe.period, "0930-1100", "America/New_York")) is true on One bar on a price chart. It shows where price opened and closed, and the highest and lowest it reached, in one stretch of time.More in the glossary that open at or after 9:30 and before 11:00 New York time. time() gives na outside the window.
- Always write the time zone in. Without it, TradingView uses the An organised market with one order book for each thing it lists, where everyone’s orders are matched by the same rules. CME Group is a futures exchange.More in the glossary time zone: for The company that runs the Chicago Mercantile Exchange and other US futures exchanges, where contracts like the Micro S&P 500 trade.More in the glossary futures such as the A small futures contract on the S&P 500, traded at CME Group. Each point is worth $5, and its smallest step of 0.25 is worth $1.25.More in the glossary, that's Chicago, an hour behind New York. Your chart clock doesn't change what a script uses. Place names such as America/New_York follow Moving the clocks forward an hour for summer. The US and UK change on different dates, so for a few weeks a year the gap between New York and London time is 4 hours instead of 5.More in the glossary. See Time zones and setting your chart clock.
- The window includes its start time but not its end: the 11:00 candle is outside.
- Days: "0930-1100:23456" adds Monday to Friday (1 is Sunday). Or ask directly: dayofweek == dayofweek.friday.
- Use it as a A rule about time added to a setup, so it only counts in the sessions, days or hours you choose.More in the glossary: work out the The moment a shorter moving average crosses a longer one: above it for a cross up, below it for a cross down.More in the glossary on its own line, then signal = crossUp and inWindow. Pine stops reading an and once the first part is false, so ta. tools go on their own line. See Fitting your hours and Session and day filters.
Worked example
Made-up numbersWhich 5-minute candles are inside 0930-1100?
Made-up example · 5-minute chart · New York time
- First inside: the 9:30 candle (opens at the start time)
- Last inside: the 10:55 candle
- The 11:00 candle opens at the end time: outside
- 9:30 to 11:00 is 90 minutes; 90 ÷ 5 = 18 candles
The window includes its start but not its end.
Your turn
DrillMade-up example · 15-minute chart · window "0930-1100" · New York time
Trading is risky. You can lose money. Test results are hypothetical, and a good test doesn't mean future profits. This lesson explains how Pine Script works. It isn't advice.