Time Modeling
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
exchange.hours.time_zone
property.
// Get the time zone of the exchange for a specific asset. var timeZone = Securities[_symbol].Exchange.Hours.TimeZone;
# Get the time zone of the exchange for a specific asset. time_zone = self.securities[self._symbol].exchange.hours.time_zone
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
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
set_time_zone
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.set_time_zone("Europe/London") self.set_time_zone(TimeZones.CHICAGO)
To get the time zone of your algorithm, use theTimeZone
time_zone
property.
# The algorithm timezone property can assist with multi-asset trading. time_zone = self.time_zone
// The algorithm timezone property can assist with multi-asset trading. var timeZone = TimeZone;
To get the algorithm time in Coordinated Universal Time (UTC), use the UtcTime
utc_time
property.
# Access the current UTC time to coordinate multi-timezone events or improve logging. utc_time = self.utc_time
// Access the current UTC time to coordinate multi-timezone events or improve logging. var utcTime = UtcTime;
The Time
time
and UtcTime
utc_time
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.
# Get the timezone of an asset. time_zone = self.market_hours_database.get_data_time_zone(Market.USA, self._symbol, SecurityType.EQUITY)
// Get the timezone of an asset. var timeZone = MarketHoursDatabase.GetDataTimeZone(Market.USA, _symbol, SecurityType.Equity);