Skip to content

Time

QuantConnect.Time

Bases: Object

Time helper class collection for working with trading dates

live_auxiliary_data_offset

live_auxiliary_data_offset: timedelta

Allows specifying an offset to trigger the tradable date event

END_OF_TIME

END_OF_TIME: datetime = ...

Provides a value far enough in the future the current computer hardware will have decayed :)

end_of_time_time_span

end_of_time_time_span: timedelta = ...

Provides a time span based on END_OF_TIME

START

START: datetime = ...

Provides a common and normalized start time for Lean data

BEGINNING_OF_TIME

BEGINNING_OF_TIME: datetime = ...

Provides a value far enough in the past that can be used as a lower bound on dates, 12/30/1899

MAX_TIME_SPAN

MAX_TIME_SPAN: timedelta = ...

Provides a value large enough that we won't hit the limit, while small enough we can still do math against it without checking everywhere for TimeSpan.MaxValue

ONE_YEAR

ONE_YEAR: timedelta = ...

One Year TimeSpan Period Constant

ONE_DAY

ONE_DAY: timedelta = ...

One Day TimeSpan Period Constant

ONE_HOUR

ONE_HOUR: timedelta = ...

One Hour TimeSpan Period Constant

ONE_MINUTE

ONE_MINUTE: timedelta = ...

One Minute TimeSpan Period Constant

ONE_SECOND

ONE_SECOND: timedelta = ...

One Second TimeSpan Period Constant

ONE_MILLISECOND

ONE_MILLISECOND: timedelta = ...

One Millisecond TimeSpan Period Constant

DateTimeWithZone

DateTimeWithZone(
    date_time: Union[datetime, date],
    time_zone: TimeZoneInfo,
)

Live charting is sensitive to timezone so need to convert the local system time to a UTC and display in browser as UTC.

Initializes a new instance of the QuantConnect.Time.DateTimeWithZone struct.

Parameters:

Name Type Description Default
date_time Union[datetime, date]

Date time.

required
time_zone TimeZoneInfo

Time zone.

required

universal_time

universal_time: datetime

Gets the universal time.

time_zone

time_zone: TimeZoneInfo

Gets the time zone.

local_time

local_time: datetime

Gets the local time.

MonthYearJsonConverter

MonthYearJsonConverter()

Bases: IsoDateTimeConverter

Helper method to deserialize month/year

Creates a new instance

each_tradeable_day

each_tradeable_day(
    securities: ICollection[Security],
    _from: Union[datetime, date],
    thru: Union[datetime, date],
) -> Iterable[datetime]
each_tradeable_day(
    security: Security,
    _from: Union[datetime, date],
    thru: Union[datetime, date],
    extended_market_hours: bool = False,
) -> Iterable[datetime]
each_tradeable_day(
    exchange: SecurityExchangeHours,
    _from: Union[datetime, date],
    thru: Union[datetime, date],
    extended_market_hours: bool = False,
) -> Iterable[datetime]

Define an enumerable date range of tradeable dates - skip the holidays and weekends when securities in this algorithm don't trade.

Parameters:

Name Type Description Default
securities Optional[ICollection[Security]]

Securities we have in portfolio

None
_from Union[datetime, date]

Start date

required
thru Union[datetime, date]

End date

required
security Optional[Security]

The security to get tradeable dates for

None
extended_market_hours Optional[bool]

True to include days with extended market hours only, like sunday for futures

False
exchange Optional[SecurityExchangeHours]

The security to get tradeable dates for

None

Returns:

Type Description
Iterable[datetime]

Enumerable date range.

get_next_live_auxiliary_data_due_time

get_next_live_auxiliary_data_due_time() -> timedelta
get_next_live_auxiliary_data_due_time(
    utc_now: Union[datetime, date],
) -> timedelta

Helper method to get the new live auxiliary data due time

Parameters:

Name Type Description Default
utc_now Optional[Union[datetime, date]]

The current utc time

None

Returns:

Type Description
timedelta

The due time for the new auxiliary data emission.

get_second_uneven_wait

get_second_uneven_wait(wait_time_millis: int) -> int
get_second_uneven_wait(
    now: Union[datetime, date], wait_time_millis: int
) -> int

Helper method to adjust a waiting time, in milliseconds, so it's uneven with the second turn around

Parameters:

Name Type Description Default
wait_time_millis int

The desired wait time

required
now Optional[Union[datetime, date]]

The current time

None

Returns:

Type Description
int

The adjusted wait time.

max

max(one: timedelta, two: timedelta) -> timedelta
max(
    one: Union[datetime, date], two: Union[datetime, date]
) -> datetime

Signature descriptions:

  • Returns the timespan with the larger value

  • Returns the larger of two date times

min

min(one: timedelta, two: timedelta) -> timedelta
min(
    one: Union[datetime, date], two: Union[datetime, date]
) -> datetime

Signature descriptions:

  • Returns the timespan with the smaller value

  • Returns the smaller of two date times

unix_time_stamp_to_date_time

unix_time_stamp_to_date_time(
    unix_time_stamp: float,
) -> datetime
unix_time_stamp_to_date_time(
    unix_time_stamp: int,
) -> datetime

Create a C# DateTime from a UnixTimestamp

Parameters:

Name Type Description Default
unix_time_stamp float | int

Double unix timestamp (Time since Midnight Jan 1 1970)

required

Returns:

Type Description
datetime

Depends on the signature used. Case 1: [C# date timeobject.]; Case 2: [C# date time object.]

abs

abs(time_span: timedelta) -> timedelta

Gets the absolute value of the specified time span

Parameters:

Name Type Description Default
time_span timedelta

Time span whose absolute value we seek

required

Returns:

Type Description
timedelta

The absolute value of the specified time span.

date_time_range

date_time_range(
    _from: Union[datetime, date],
    thru: Union[datetime, date],
    step: timedelta,
) -> Iterable[datetime]

Define an enumerable date time range using the given time step

Parameters:

Name Type Description Default
_from Union[datetime, date]

DateTime start date time

required
thru Union[datetime, date]

DateTime end date time

required

Returns:

Type Description
Iterable[datetime]

Enumerable date time range.

date_time_to_unix_time_stamp

date_time_to_unix_time_stamp(
    time: Union[datetime, date],
) -> float

Convert a Datetime to Unix Timestamp

Parameters:

Name Type Description Default
time Union[datetime, date]

C# datetime object

required

Returns:

Type Description
float

Double unix timestamp.

date_time_to_unix_time_stamp_milliseconds

date_time_to_unix_time_stamp_milliseconds(
    time: Union[datetime, date],
) -> float

Convert a Datetime to Unix Timestamp

Parameters:

Name Type Description Default
time Union[datetime, date]

C# datetime object

required

Returns:

Type Description
float

Double unix timestamp.

date_time_to_unix_time_stamp_nanoseconds

date_time_to_unix_time_stamp_nanoseconds(
    time: Union[datetime, date],
) -> int

Convert a Datetime to Unix Timestamp

Parameters:

Name Type Description Default
time Union[datetime, date]

C# datetime object

required

Returns:

Type Description
int

Int64 unix timestamp.

each_day

each_day(
    _from: Union[datetime, date],
    thru: Union[datetime, date],
) -> Iterable[datetime]

Define an enumerable date range and return each date as a datetime object in the date range

Parameters:

Name Type Description Default
_from Union[datetime, date]

DateTime start date

required
thru Union[datetime, date]

DateTime end date

required

Returns:

Type Description
Iterable[datetime]

Enumerable date range.

each_tradeable_day_in_time_zone

each_tradeable_day_in_time_zone(
    exchange: SecurityExchangeHours,
    _from: Union[datetime, date],
    thru: Union[datetime, date],
    time_zone: Any,
    include_extended_market_hours: bool = True,
) -> Iterable[datetime]

Define an enumerable date range of tradeable dates but expressed in a different time zone.

Parameters:

Name Type Description Default
exchange SecurityExchangeHours

The exchange hours

required
_from Union[datetime, date]

The start time in the exchange time zone

required
thru Union[datetime, date]

The end time in the exchange time zone (inclusive of the final day)

required
time_zone Any

The timezone to project the dates into (inclusive of the final day)

required
include_extended_market_hours bool

True to include extended market hours trading in the search, false otherwise

True

get_end_time_for_trade_bars

get_end_time_for_trade_bars(
    exchange_hours: SecurityExchangeHours,
    start: Union[datetime, date],
    bar_size: timedelta,
    bar_count: int,
    extended_market_hours: bool,
) -> datetime

Determines the end time at which the requested number of bars of the given will have elapsed. NOTE: The start time is not discretized by bar_size units like is done in get_start_time_for_trade_bars

Parameters:

Name Type Description Default
exchange_hours SecurityExchangeHours

The exchange hours used to test for market open hours

required
start Union[datetime, date]

The end time of the last bar over the requested period

required
bar_size timedelta

The length of each bar

required
bar_count int

The number of bars requested

required
extended_market_hours bool

True to allow extended market hours bars, otherwise false for only normal market hours

required

Returns:

Type Description
datetime

The start time that would provide the specified number of bars ending at the specified end time, rounded down by the requested bar size.

get_number_of_trade_bars_in_interval

get_number_of_trade_bars_in_interval(
    exchange_hours: SecurityExchangeHours,
    start: Union[datetime, date],
    end: Union[datetime, date],
    bar_size: timedelta,
) -> int

Gets the number of trade bars of the specified bar_size that fit between the start and end

Parameters:

Name Type Description Default
exchange_hours SecurityExchangeHours

The exchange used to test for market open hours

required
start Union[datetime, date]

The start time of the interval in the exchange time zone

required
end Union[datetime, date]

The end time of the interval in the exchange time zone

required
bar_size timedelta

The step size used to count number of bars between start and end

required

Returns:

Type Description
int

The number of bars of the specified size between start and end times.

get_start_time_for_trade_bars

get_start_time_for_trade_bars(
    exchange_hours: SecurityExchangeHours,
    end: Union[datetime, date],
    bar_size: timedelta,
    bar_count: int,
    extended_market_hours: bool,
    data_time_zone: Any,
    daily_precise_end_time: bool = False,
) -> datetime

Determines the start time required to produce the requested number of bars and the given size

Parameters:

Name Type Description Default
exchange_hours SecurityExchangeHours

The exchange hours used to test for market open hours

required
end Union[datetime, date]

The end time of the last bar over the requested period

required
bar_size timedelta

The length of each bar

required
bar_count int

The number of bars requested

required
extended_market_hours bool

True to allow extended market hours bars, otherwise false for only normal market hours

required
data_time_zone Any

Timezone for this data

required
daily_precise_end_time bool

True if daily strict end times are enabled

False

Returns:

Type Description
datetime

The start time that would provide the specified number of bars ending at the specified end time, rounded down by the requested bar size.

multiply

multiply(
    interval: timedelta, multiplier: float
) -> timedelta

Multiplies the specified interval by the multiplier

Parameters:

Name Type Description Default
interval timedelta

The interval to be multiplied, such as TimeSpan.FromSeconds(1)

required
multiplier float

The number of times to multiply the interval

required

Returns:

Type Description
timedelta

The multiplied interval, such as 1s*5 = 5s.

normalize_instant_within_range

normalize_instant_within_range(
    start: Union[datetime, date],
    current: Union[datetime, date],
    period: timedelta,
) -> float

Normalizes the current time within the specified period time = start => 0 time = start + period => 1

Parameters:

Name Type Description Default
start Union[datetime, date]

The start time of the range

required
current Union[datetime, date]

The current time we seek to normalize

required
period timedelta

The time span of the range

required

Returns:

Type Description
float

The normalized time.

normalize_time_step

normalize_time_step(
    period: timedelta, step_size: timedelta
) -> float

Normalizes the step size as a percentage of the period.

Parameters:

Name Type Description Default
period timedelta

The period to normalize against

required
step_size timedelta

The step size to be normaized

required

Returns:

Type Description
float

The normalized step size as a percentage of the period.

parse_date

parse_date(date_to_parse: str) -> datetime

Parse a standard YY MM DD date into a DateTime. Attempt common date formats

Parameters:

Name Type Description Default
date_to_parse str

String date time to parse

required

Returns:

Type Description
datetime

Date time.

parse_fix_utc_timestamp

parse_fix_utc_timestamp(date_to_parse: str) -> datetime

Parse a standard YY MM DD date into a DateTime. Attempt common date formats

Parameters:

Name Type Description Default
date_to_parse str

String date time to parse

required

Returns:

Type Description
datetime

Date time.

time_stamp

time_stamp() -> float

Get the current time as a unix timestamp

Returns:

Type Description
float

Double value of the unix as UTC timestamp.

tradable_date

tradable_date(
    securities: List[Security], day: Union[datetime, date]
) -> bool

Make sure this date is not a holiday, or weekend for the securities in this algorithm.

Parameters:

Name Type Description Default
securities List[Security]

Security manager from the algorithm

required
day Union[datetime, date]

DateTime to check if trade-able.

required

Returns:

Type Description
bool

True if tradeable date.

tradeable_dates

tradeable_dates(
    securities: ICollection[Security],
    start: Union[datetime, date],
    finish: Union[datetime, date],
) -> int

Could of the number of tradeable dates within this period.

Parameters:

Name Type Description Default
securities ICollection[Security]

Securities we're trading

required
start Union[datetime, date]

Start of Date Loop

required
finish Union[datetime, date]

End of Date Loop

required

Returns:

Type Description
int

Number of dates.

unix_millisecond_time_stamp_to_date_time

unix_millisecond_time_stamp_to_date_time(
    unix_time_stamp: float,
) -> datetime

Create a C# DateTime from a UnixTimestamp

Parameters:

Name Type Description Default
unix_time_stamp float

Decimal unix timestamp (Time since Midnight Jan 1 1970) in milliseconds

required

Returns:

Type Description
datetime

C# date time object.

unix_nanosecond_time_stamp_to_date_time

unix_nanosecond_time_stamp_to_date_time(
    unix_time_stamp: int,
) -> datetime

Create a C# DateTime from a UnixTimestamp

Parameters:

Name Type Description Default
unix_time_stamp int

Int64 unix timestamp (Time since Midnight Jan 1 1970) in nanoseconds

required

Returns:

Type Description
datetime

C# date time object.