Skip to content
Wildflower
Join free
Coding strategies in Pine Script

Free Trading School · Level 7 · Course 7.4

Sessions and time

Lesson 9 of 16 in Coding strategies in Pine ScriptModule 2: Indicators and conditions

Advanced · Coding strategies in Pine Script

Sessions and time

Wildflower Trading School · Lesson 7.4.2.4

Wildflower

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

  1. Add the Trading window script to a 5-minute MES1! chart.
  2. Then delete ", "America/New_York"" from the time() line, save, and see where the shading moves.
  3. Put it back, and change the window to the hours on your own rule sheet.

Next: Strategy versus indicator.

Quick check

  1. 1. You leave the time zone out of time() on an MES chart. Which clock does the window use?

  2. 2. In the session string "0930-1100:23456", what does 2 mean?

  3. 3. Is the 11:00 candle inside "0930-1100"?

0 of 3 answered.

Finished this lesson?

Tick it off and keep your place

Make a free account and we'll remember where you are, so “Strategy versus indicator” is waiting next time.

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 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 time zone: for futures such as the , 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 . 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 : work out the 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 numbers

Which 5-minute candles are inside 0930-1100?

Made-up example · 5-minute chart · New York time

  1. First inside: the 9:30 candle (opens at the start time)
  2. Last inside: the 10:55 candle
  3. The 11:00 candle opens at the end time: outside
  4. 9:30 to 11:00 is 90 minutes; 90 ÷ 5 = 18 candles

The window includes its start but not its end.

Your turn

Drill

Made-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.