Lean  $LEAN_TAG$
QuantConnect.Time Class Reference

Time helper class collection for working with trading dates More...

Classes

struct  DateTimeWithZone
 Live charting is sensitive to timezone so need to convert the local system time to a UTC and display in browser as UTC. More...
 
class  MonthYearJsonConverter
 Helper method to deserialize month/year More...
 

Static Public Member Functions

static TimeSpan GetNextLiveAuxiliaryDataDueTime ()
 Helper method to get the new live auxiliary data due time More...
 
static TimeSpan GetNextLiveAuxiliaryDataDueTime (DateTime utcNow)
 Helper method to get the new live auxiliary data due time More...
 
static int GetSecondUnevenWait (int waitTimeMillis)
 Helper method to adjust a waiting time, in milliseconds, so it's uneven with the second turn around More...
 
static int GetSecondUnevenWait (this DateTime now, int waitTimeMillis)
 Helper method to adjust a waiting time, in milliseconds, so it's uneven with the second turn around More...
 
static DateTime UnixTimeStampToDateTime (double unixTimeStamp)
 Create a C# DateTime from a UnixTimestamp More...
 
static DateTime UnixTimeStampToDateTime (decimal unixTimeStamp)
 Create a C# DateTime from a UnixTimestamp More...
 
static DateTime UnixTimeStampToDateTime (long unixTimeStamp)
 Create a C# DateTime from a UnixTimestamp More...
 
static DateTime UnixMillisecondTimeStampToDateTime (decimal unixTimeStamp)
 Create a C# DateTime from a UnixTimestamp More...
 
static DateTime UnixNanosecondTimeStampToDateTime (long unixTimeStamp)
 Create a C# DateTime from a UnixTimestamp More...
 
static double DateTimeToUnixTimeStamp (DateTime time)
 Convert a Datetime to Unix Timestamp More...
 
static double DateTimeToUnixTimeStampMilliseconds (DateTime time)
 Convert a Datetime to Unix Timestamp More...
 
static long DateTimeToUnixTimeStampNanoseconds (DateTime time)
 Convert a Datetime to Unix Timestamp More...
 
static double TimeStamp ()
 Get the current time as a unix timestamp More...
 
static TimeSpan Max (TimeSpan one, TimeSpan two)
 Returns the timespan with the larger value More...
 
static TimeSpan Min (TimeSpan one, TimeSpan two)
 Returns the timespan with the smaller value More...
 
static DateTime Max (DateTime one, DateTime two)
 Returns the larger of two date times More...
 
static DateTime Min (DateTime one, DateTime two)
 Returns the smaller of two date times More...
 
static TimeSpan Multiply (this TimeSpan interval, double multiplier)
 Multiplies the specified interval by the multiplier More...
 
static DateTime ParseDate (string dateToParse)
 Parse a standard YY MM DD date into a DateTime. Attempt common date formats More...
 
static DateTime ParseFIXUtcTimestamp (string dateToParse)
 Parse a standard YY MM DD date into a DateTime. Attempt common date formats More...
 
static IEnumerable< DateTime > EachDay (DateTime from, DateTime thru)
 Define an enumerable date range and return each date as a datetime object in the date range More...
 
static IEnumerable< DateTime > EachTradeableDay (ICollection< Security > securities, DateTime from, DateTime thru)
 Define an enumerable date range of tradeable dates - skip the holidays and weekends when securities in this algorithm don't trade. More...
 
static IEnumerable< DateTime > EachTradeableDay (Security security, DateTime from, DateTime thru)
 Define an enumerable date range of tradeable dates - skip the holidays and weekends when securities in this algorithm don't trade. More...
 
static IEnumerable< DateTime > EachTradeableDay (SecurityExchangeHours exchange, DateTime from, DateTime thru)
 Define an enumerable date range of tradeable dates - skip the holidays and weekends when securities in this algorithm don't trade. More...
 
static IEnumerable< DateTime > EachTradeableDayInTimeZone (SecurityExchangeHours exchange, DateTime from, DateTime thru, DateTimeZone timeZone, bool includeExtendedMarketHours=true)
 Define an enumerable date range of tradeable dates but expressed in a different time zone. More...
 
static bool TradableDate (IEnumerable< Security > securities, DateTime day)
 Make sure this date is not a holiday, or weekend for the securities in this algorithm. More...
 
static int TradeableDates (ICollection< Security > securities, DateTime start, DateTime finish)
 Could of the number of tradeable dates within this period. More...
 
static DateTime GetStartTimeForTradeBars (SecurityExchangeHours exchangeHours, DateTime end, TimeSpan barSize, int barCount, bool extendedMarketHours, DateTimeZone dataTimeZone)
 Determines the start time required to produce the requested number of bars and the given size More...
 
static DateTime GetEndTimeForTradeBars (SecurityExchangeHours exchangeHours, DateTime start, TimeSpan barSize, int barCount, bool extendedMarketHours)
 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 barSize units like is done in GetStartTimeForTradeBars More...
 
static int GetNumberOfTradeBarsInInterval (SecurityExchangeHours exchangeHours, DateTime start, DateTime end, TimeSpan barSize)
 Gets the number of trade bars of the specified barSize that fit between the start and end More...
 
static double NormalizeInstantWithinRange (DateTime start, DateTime current, TimeSpan period)
 Normalizes the current time within the specified period time = start => 0 time = start + period => 1 More...
 
static double NormalizeTimeStep (TimeSpan period, TimeSpan stepSize)
 Normalizes the step size as a percentage of the period. More...
 
static TimeSpan Abs (this TimeSpan timeSpan)
 Gets the absolute value of the specified time span More...
 

Static Public Attributes

static readonly DateTime EndOfTime = new DateTime(2050, 12, 31)
 Provides a value far enough in the future the current computer hardware will have decayed :) More...
 
static TimeSpan EndOfTimeTimeSpan = new TimeSpan(EndOfTime.Ticks)
 Provides a time span based on EndOfTime More...
 
static readonly DateTime Start = new DateTime(1998, 1, 2)
 Provides a common and normalized start time for Lean data More...
 
static readonly DateTime BeginningOfTime = DateTime.FromOADate(0)
 Provides a value far enough in the past that can be used as a lower bound on dates, 12/30/1899 More...
 
static readonly TimeSpan MaxTimeSpan = TimeSpan.FromDays(1000*365)
 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 More...
 
static readonly TimeSpan OneYear = TimeSpan.FromDays(365)
 One Year TimeSpan Period Constant More...
 
static readonly TimeSpan OneDay = TimeSpan.FromDays(1)
 One Day TimeSpan Period Constant More...
 
static readonly TimeSpan OneHour = TimeSpan.FromHours(1)
 One Hour TimeSpan Period Constant More...
 
static readonly TimeSpan OneMinute = TimeSpan.FromMinutes(1)
 One Minute TimeSpan Period Constant More...
 
static readonly TimeSpan OneSecond = TimeSpan.FromSeconds(1)
 One Second TimeSpan Period Constant More...
 
static readonly TimeSpan OneMillisecond = TimeSpan.FromMilliseconds(1)
 One Millisecond TimeSpan Period Constant More...
 

Properties

static TimeSpan LiveAuxiliaryDataOffset = TimeSpan.FromHours(8) [get, set]
 Allows specifying an offset to trigger the tradable date event More...
 

Detailed Description

Time helper class collection for working with trading dates

Definition at line 30 of file Time.cs.

Member Function Documentation

◆ GetNextLiveAuxiliaryDataDueTime() [1/2]

static TimeSpan QuantConnect.Time.GetNextLiveAuxiliaryDataDueTime ( )
static

Helper method to get the new live auxiliary data due time

Returns
The due time for the new auxiliary data emission

Definition at line 152 of file Time.cs.

◆ GetNextLiveAuxiliaryDataDueTime() [2/2]

static TimeSpan QuantConnect.Time.GetNextLiveAuxiliaryDataDueTime ( DateTime  utcNow)
static

Helper method to get the new live auxiliary data due time

Parameters
utcNowThe current utc time
Returns
The due time for the new auxiliary data emission

Definition at line 162 of file Time.cs.

◆ GetSecondUnevenWait() [1/2]

static int QuantConnect.Time.GetSecondUnevenWait ( int  waitTimeMillis)
static

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

Parameters
waitTimeMillisThe desired wait time

This is useful for real time performance in live trading. We want to avoid adding unnecessary cpu usage, during periods where we know there will be cpu time demand, like a second turn around where data is emitted.

Returns
The adjusted wait time

Definition at line 179 of file Time.cs.

Here is the caller graph for this function:

◆ GetSecondUnevenWait() [2/2]

static int QuantConnect.Time.GetSecondUnevenWait ( this DateTime  now,
int  waitTimeMillis 
)
static

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

Parameters
nowThe current time
waitTimeMillisThe desired wait time

This is useful for real time performance in live trading. We want to avoid adding unnecessary cpu usage, during periods where we know there will be cpu time demand, like a second turn around where data is emitted.

Returns
The adjusted wait time

Definition at line 192 of file Time.cs.

◆ UnixTimeStampToDateTime() [1/3]

static DateTime QuantConnect.Time.UnixTimeStampToDateTime ( double  unixTimeStamp)
static

Create a C# DateTime from a UnixTimestamp

Parameters
unixTimeStampDouble unix timestamp (Time since Midnight Jan 1 1970)
Returns
C# date timeobject

Definition at line 209 of file Time.cs.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UnixTimeStampToDateTime() [2/3]

static DateTime QuantConnect.Time.UnixTimeStampToDateTime ( decimal  unixTimeStamp)
static

Create a C# DateTime from a UnixTimestamp

Parameters
unixTimeStampDecimal unix timestamp (Time since Midnight Jan 1 1970)
Returns
C# date time object

Definition at line 230 of file Time.cs.

Here is the call graph for this function:

◆ UnixTimeStampToDateTime() [3/3]

static DateTime QuantConnect.Time.UnixTimeStampToDateTime ( long  unixTimeStamp)
static

Create a C# DateTime from a UnixTimestamp

Parameters
unixTimeStampLong unix timestamp (Time since Midnight Jan 1 1970)
Returns
C# date time object

Definition at line 240 of file Time.cs.

Here is the call graph for this function:

◆ UnixMillisecondTimeStampToDateTime()

static DateTime QuantConnect.Time.UnixMillisecondTimeStampToDateTime ( decimal  unixTimeStamp)
static

Create a C# DateTime from a UnixTimestamp

Parameters
unixTimeStampDecimal unix timestamp (Time since Midnight Jan 1 1970) in milliseconds
Returns
C# date time object

Definition at line 250 of file Time.cs.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UnixNanosecondTimeStampToDateTime()

static DateTime QuantConnect.Time.UnixNanosecondTimeStampToDateTime ( long  unixTimeStamp)
static

Create a C# DateTime from a UnixTimestamp

Parameters
unixTimeStampInt64 unix timestamp (Time since Midnight Jan 1 1970) in nanoseconds
Returns
C# date time object

Definition at line 274 of file Time.cs.

Here is the call graph for this function:

◆ DateTimeToUnixTimeStamp()

static double QuantConnect.Time.DateTimeToUnixTimeStamp ( DateTime  time)
static

Convert a Datetime to Unix Timestamp

Parameters
timeC# datetime object
Returns
Double unix timestamp

Definition at line 295 of file Time.cs.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DateTimeToUnixTimeStampMilliseconds()

static double QuantConnect.Time.DateTimeToUnixTimeStampMilliseconds ( DateTime  time)
static

Convert a Datetime to Unix Timestamp

Parameters
timeC# datetime object
Returns
Double unix timestamp

Definition at line 314 of file Time.cs.

Here is the call graph for this function:

◆ DateTimeToUnixTimeStampNanoseconds()

static long QuantConnect.Time.DateTimeToUnixTimeStampNanoseconds ( DateTime  time)
static

Convert a Datetime to Unix Timestamp

Parameters
timeC# datetime object
Returns
Int64 unix timestamp

Definition at line 333 of file Time.cs.

Here is the call graph for this function:

◆ TimeStamp()

static double QuantConnect.Time.TimeStamp ( )
static

Get the current time as a unix timestamp

Returns
Double value of the unix as UTC timestamp

Definition at line 351 of file Time.cs.

Here is the call graph for this function:

◆ Max() [1/2]

static TimeSpan QuantConnect.Time.Max ( TimeSpan  one,
TimeSpan  two 
)
static

Returns the timespan with the larger value

Definition at line 359 of file Time.cs.

◆ Min() [1/2]

static TimeSpan QuantConnect.Time.Min ( TimeSpan  one,
TimeSpan  two 
)
static

Returns the timespan with the smaller value

Definition at line 367 of file Time.cs.

◆ Max() [2/2]

static DateTime QuantConnect.Time.Max ( DateTime  one,
DateTime  two 
)
static

Returns the larger of two date times

Definition at line 375 of file Time.cs.

◆ Min() [2/2]

static DateTime QuantConnect.Time.Min ( DateTime  one,
DateTime  two 
)
static

Returns the smaller of two date times

Definition at line 383 of file Time.cs.

◆ Multiply()

static TimeSpan QuantConnect.Time.Multiply ( this TimeSpan  interval,
double  multiplier 
)
static

Multiplies the specified interval by the multiplier

Parameters
intervalThe interval to be multiplied, such as TimeSpan.FromSeconds(1)
multiplierThe number of times to multiply the interval
Returns
The multiplied interval, such as 1s*5 = 5s

Definition at line 394 of file Time.cs.

◆ ParseDate()

static DateTime QuantConnect.Time.ParseDate ( string  dateToParse)
static

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

Parameters
dateToParseString date time to parse
Returns
Date time

Definition at line 404 of file Time.cs.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseFIXUtcTimestamp()

static DateTime QuantConnect.Time.ParseFIXUtcTimestamp ( string  dateToParse)
static

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

Parameters
dateToParseString date time to parse
Returns
Date time

Definition at line 460 of file Time.cs.

Here is the call graph for this function:

◆ EachDay()

static IEnumerable<DateTime> QuantConnect.Time.EachDay ( DateTime  from,
DateTime  thru 
)
static

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

Parameters
fromDateTime start date
thruDateTime end date
Returns
Enumerable date range

Definition at line 489 of file Time.cs.

Here is the caller graph for this function:

◆ EachTradeableDay() [1/3]

static IEnumerable<DateTime> QuantConnect.Time.EachTradeableDay ( ICollection< Security securities,
DateTime  from,
DateTime  thru 
)
static

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

Parameters
securitiesSecurities we have in portfolio
fromStart date
thruEnd date
Returns
Enumerable date range

Definition at line 503 of file Time.cs.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ EachTradeableDay() [2/3]

static IEnumerable<DateTime> QuantConnect.Time.EachTradeableDay ( Security  security,
DateTime  from,
DateTime  thru 
)
static

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

Parameters
securityThe security to get tradeable dates for
fromStart date
thruEnd date
Returns
Enumerable date range

Definition at line 522 of file Time.cs.

Here is the call graph for this function:

◆ EachTradeableDay() [3/3]

static IEnumerable<DateTime> QuantConnect.Time.EachTradeableDay ( SecurityExchangeHours  exchange,
DateTime  from,
DateTime  thru 
)
static

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

Parameters
exchangeThe security to get tradeable dates for
fromStart date
thruEnd date
Returns
Enumerable date range

Definition at line 535 of file Time.cs.

Here is the call graph for this function:

◆ EachTradeableDayInTimeZone()

static IEnumerable<DateTime> QuantConnect.Time.EachTradeableDayInTimeZone ( SecurityExchangeHours  exchange,
DateTime  from,
DateTime  thru,
DateTimeZone  timeZone,
bool  includeExtendedMarketHours = true 
)
static

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

This is mainly used to bridge the gap between exchange time zone and data time zone for file written to disk. The returned enumerable of dates is guaranteed to be the same size or longer than those generated via EachTradeableDay(ICollection<Security>,DateTime,DateTime)

Parameters
exchangeThe exchange hours
fromThe start time in the exchange time zone
thruThe end time in the exchange time zone (inclusive of the final day)
timeZoneThe timezone to project the dates into (inclusive of the final day)
includeExtendedMarketHoursTrue to include extended market hours trading in the search, false otherwise
Returns

Definition at line 559 of file Time.cs.

Here is the call graph for this function:

◆ TradableDate()

static bool QuantConnect.Time.TradableDate ( IEnumerable< Security securities,
DateTime  day 
)
static

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

Parameters
securitiesSecurity manager from the algorithm
dayDateTime to check if trade-able.
Returns
True if tradeable date

Definition at line 593 of file Time.cs.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TradeableDates()

static int QuantConnect.Time.TradeableDates ( ICollection< Security securities,
DateTime  start,
DateTime  finish 
)
static

Could of the number of tradeable dates within this period.

Parameters
securitiesSecurities we're trading
startStart of Date Loop
finishEnd of Date Loop
Returns
Number of dates

Definition at line 617 of file Time.cs.

Here is the call graph for this function:

◆ GetStartTimeForTradeBars()

static DateTime QuantConnect.Time.GetStartTimeForTradeBars ( SecurityExchangeHours  exchangeHours,
DateTime  end,
TimeSpan  barSize,
int  barCount,
bool  extendedMarketHours,
DateTimeZone  dataTimeZone 
)
static

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

Parameters
exchangeHoursThe exchange hours used to test for market open hours
endThe end time of the last bar over the requested period
barSizeThe length of each bar
barCountThe number of bars requested
extendedMarketHoursTrue to allow extended market hours bars, otherwise false for only normal market hours
dataTimeZoneTimezone for this data
Returns
The start time that would provide the specified number of bars ending at the specified end time, rounded down by the requested bar size

Definition at line 648 of file Time.cs.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetEndTimeForTradeBars()

static DateTime QuantConnect.Time.GetEndTimeForTradeBars ( SecurityExchangeHours  exchangeHours,
DateTime  start,
TimeSpan  barSize,
int  barCount,
bool  extendedMarketHours 
)
static

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 barSize units like is done in GetStartTimeForTradeBars

Parameters
exchangeHoursThe exchange hours used to test for market open hours
startThe end time of the last bar over the requested period
barSizeThe length of each bar
barCountThe number of bars requested
extendedMarketHoursTrue to allow extended market hours bars, otherwise false for only normal market hours
Returns
The start time that would provide the specified number of bars ending at the specified end time, rounded down by the requested bar size

Definition at line 679 of file Time.cs.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetNumberOfTradeBarsInInterval()

static int QuantConnect.Time.GetNumberOfTradeBarsInInterval ( SecurityExchangeHours  exchangeHours,
DateTime  start,
DateTime  end,
TimeSpan  barSize 
)
static

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

Parameters
exchangeHoursThe exchange used to test for market open hours
startThe start time of the interval in the exchange time zone
endThe end time of the interval in the exchange time zone
barSizeThe step size used to count number of bars between start and end
Returns
The number of bars of the specified size between start and end times

Definition at line 721 of file Time.cs.

Here is the call graph for this function:

◆ NormalizeInstantWithinRange()

static double QuantConnect.Time.NormalizeInstantWithinRange ( DateTime  start,
DateTime  current,
TimeSpan  period 
)
static

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

Parameters
startThe start time of the range
currentThe current time we seek to normalize
periodThe time span of the range
Returns
The normalized time

Definition at line 767 of file Time.cs.

◆ NormalizeTimeStep()

static double QuantConnect.Time.NormalizeTimeStep ( TimeSpan  period,
TimeSpan  stepSize 
)
static

Normalizes the step size as a percentage of the period.

Parameters
periodThe period to normalize against
stepSizeThe step size to be normaized
Returns
The normalized step size as a percentage of the period

Definition at line 785 of file Time.cs.

◆ Abs()

static TimeSpan QuantConnect.Time.Abs ( this TimeSpan  timeSpan)
static

Gets the absolute value of the specified time span

Parameters
timeSpanTime span whose absolute value we seek
Returns
The absolute value of the specified time span

Definition at line 801 of file Time.cs.

Member Data Documentation

◆ EndOfTime

readonly DateTime QuantConnect.Time.EndOfTime = new DateTime(2050, 12, 31)
static

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

new DateTime(2050, 12, 31)

Definition at line 44 of file Time.cs.

◆ EndOfTimeTimeSpan

TimeSpan QuantConnect.Time.EndOfTimeTimeSpan = new TimeSpan(EndOfTime.Ticks)
static

Provides a time span based on EndOfTime

Definition at line 49 of file Time.cs.

◆ Start

readonly DateTime QuantConnect.Time.Start = new DateTime(1998, 1, 2)
static

Provides a common and normalized start time for Lean data

Definition at line 54 of file Time.cs.

◆ BeginningOfTime

readonly DateTime QuantConnect.Time.BeginningOfTime = DateTime.FromOADate(0)
static

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

DateTime.FromOADate(0)

Definition at line 62 of file Time.cs.

◆ MaxTimeSpan

readonly TimeSpan QuantConnect.Time.MaxTimeSpan = TimeSpan.FromDays(1000*365)
static

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

Definition at line 68 of file Time.cs.

◆ OneYear

readonly TimeSpan QuantConnect.Time.OneYear = TimeSpan.FromDays(365)
static

One Year TimeSpan Period Constant

365 days

Definition at line 74 of file Time.cs.

◆ OneDay

readonly TimeSpan QuantConnect.Time.OneDay = TimeSpan.FromDays(1)
static

One Day TimeSpan Period Constant

Definition at line 79 of file Time.cs.

◆ OneHour

readonly TimeSpan QuantConnect.Time.OneHour = TimeSpan.FromHours(1)
static

One Hour TimeSpan Period Constant

Definition at line 84 of file Time.cs.

◆ OneMinute

readonly TimeSpan QuantConnect.Time.OneMinute = TimeSpan.FromMinutes(1)
static

One Minute TimeSpan Period Constant

Definition at line 89 of file Time.cs.

◆ OneSecond

readonly TimeSpan QuantConnect.Time.OneSecond = TimeSpan.FromSeconds(1)
static

One Second TimeSpan Period Constant

Definition at line 94 of file Time.cs.

◆ OneMillisecond

readonly TimeSpan QuantConnect.Time.OneMillisecond = TimeSpan.FromMilliseconds(1)
static

One Millisecond TimeSpan Period Constant

Definition at line 99 of file Time.cs.

Property Documentation

◆ LiveAuxiliaryDataOffset

TimeSpan QuantConnect.Time.LiveAuxiliaryDataOffset = TimeSpan.FromHours(8)
staticgetset

Allows specifying an offset to trigger the tradable date event

Useful for delaying the tradable date event until new auxiliary data is available to refresh map and factor files

Definition at line 36 of file Time.cs.


The documentation for this class was generated from the following file: