Time Modeling

Time Zones

Introduction

LEAN is an international trading engine, so it can operate across time zones. The exchange, your algorithm, and the data in your algorithm can all have different time zones.

Exchange Time Zone

Exchanges are located in different areas, so they can have different time zones. To get the time zone of the exchange that a Security trades on, use its Exchange.Hours.TimeZone property.

var timeZone = Securities[_symbol].Exchange.Hours.TimeZone;
time_zone = self.Securities[self.symbol].Exchange.Hours.TimeZone

Algorithm Time Zone

LEAN supports international trading across multiple time zones and markets, so it needs a reference time zone for the algorithm to set the Time. The default time zone is Eastern Time (ET), which is UTC-4 in summer and UTC-5 in winter. To set a different time zone, call the SetTimeZone method. This method accepts either a string following the IANA Time Zone database convention or a NodaTime.DateTimeZone object. If you pass a string, the method converts it to a NodaTime.DateTimeZone object. The TimeZones class provides the following helper attributes to create NodaTime.DateTimeZone objects:

SetTimeZone("Europe/London");
SetTimeZone(NodaTime.DateTimeZone.Utc);
SetTimeZone(TimeZones.Chicago);
self.SetTimeZone("Europe/London")
self.SetTimeZone(NodaTime.DateTimeZone.Utc)
self.SetTimeZone(TimeZones.Chicago)

To get the time zone of your algorithm, use the TimeZone property.

time_zone = self.TimeZone
var timeZone = TimeZone;

To get the algorithm time in Coordinated Universal Time (UTC), use the UtcTime property.

utc_time = self.UtcTime
var utcTime = UtcTime;

The Time and UtcTime objects have no time zone. LEAN maintains their state to be consistent.

Data Time Zone

Datasets can have different time zones because the time zone is up to the data provider, where they are located, and where they collect the data. LEAN tracks the time zone of each dataset so that you receive the correct data at the right point in time in your algorithm. If you have multiple datasets in your algorithm from different time zones, LEAN synchronizes them to your algorithm time. Furthermore, if you set up time period consolidators, LEAN consolidates the data based on the data time zone.

To get the time zone of a dataset, view the listing page in the Dataset Market or call the GetDataTimeZone method.

time_zone = self.MarketHoursDatabase.GetDataTimeZone(Market.USA, self.symbol, SecurityType.Equity)
var timeZone = MarketHoursDatabase.GetDataTimeZone(Market.USA, _symbol, SecurityType.Equity);

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: