Skip to content

FuturesExpiryUtilityFunctions

QuantConnect.Securities.Future.FuturesExpiryUtilityFunctions

Bases: Object

Class to implement common functions used in FuturesExpiryFunctions

get_future_expiration_from_contract_month

get_future_expiration_from_contract_month(
    symbol: str,
    market: str,
    contract_month: Union[datetime, date],
) -> datetime
get_future_expiration_from_contract_month(
    future: Union[Symbol, str, BaseContract],
    contract_month: Union[datetime, date],
) -> datetime

Helper method to resolve a future expiration from it's contract month

third_friday

third_friday(time: Union[datetime, date]) -> datetime
third_friday(
    time: Union[datetime, date],
    contract: Union[Symbol, str, BaseContract],
) -> datetime

Signature descriptions:

  • Method to retrieve the 3rd Friday of the given month

  • This function returns the third Friday of the month, adjusted for holidays and weekends.

Parameters:

Name Type Description Default
time Union[datetime, date]

Date from the given month

required

Returns:

Type Description
datetime

3rd Friday of given month.

add_business_days

add_business_days(
    time: Union[datetime, date],
    n: int,
    holidays: HashSet[datetime],
) -> datetime

Method to retrieve n^th succeeding/preceding business day for a given day

Parameters:

Name Type Description Default
time Union[datetime, date]

The current Time

required
n int

Number of business days succeeding current time. Use negative value for preceding business days

required
holidays HashSet[datetime]

Set of holidays to exclude. These should be sourced from the MarketHoursDatabase

required

Returns:

Type Description
datetime

The date-time after adding n business days.

add_business_days_if_holiday

add_business_days_if_holiday(
    time: Union[datetime, date],
    n: int,
    holiday_list: HashSet[datetime],
) -> datetime

Method to retrieve n^th succeeding/preceding business day for a given day if there was a holiday on that day

Parameters:

Name Type Description Default
time Union[datetime, date]

The current Time

required
n int

Number of business days succeeding current time. Use negative value for preceding business days

required
holiday_list HashSet[datetime]

Enumerable of holidays to exclude. These should be sourced from the MarketHoursDatabase

required

Returns:

Type Description
datetime

The date-time after adding n business days.

get_delta_between_contract_month_and_contract_expiry

get_delta_between_contract_month_and_contract_expiry(
    underlying: str,
    future_expiry_date: Optional[datetime] = None,
) -> int

Gets the number of months between the contract month and the expiry date.

Parameters:

Name Type Description Default
underlying str

The future symbol ticker

required
future_expiry_date Optional[datetime]

Expiry date to use to look up contract month delta. Only used for dairy, since we need to lookup its contract month in a pre-defined table.

None

Returns:

Type Description
int

The number of months between the contract month and the contract expiry.

get_future_contract_month

get_future_contract_month(
    symbol: Union[Symbol, str, BaseContract],
) -> datetime

Helper method to retrieve the futures contract month

last_friday

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

Method to retrieve the last Friday of any month

Parameters:

Name Type Description Default
time Union[datetime, date]

Date from the given month

required

Returns:

Type Description
datetime

Last Friday of the given month.

last_thursday

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

Method to retrieve the last Thursday of any month

Parameters:

Name Type Description Default
time Union[datetime, date]

Date from the given month

required

Returns:

Type Description
datetime

Last Thursday of the given month.

last_weekday

last_weekday(
    time: Union[datetime, date], day_of_week: DayOfWeek
) -> datetime

Method to retrieve the last weekday of any month

Parameters:

Name Type Description Default
time Union[datetime, date]

Date from the given month

required
day_of_week DayOfWeek

the last weekday to be found

required

Returns:

Type Description
datetime

Last day of the we.

not_holiday

not_holiday(
    time: Union[datetime, date],
    holiday_list: List[datetime],
) -> bool

Method to check whether a given time is holiday or not

Parameters:

Name Type Description Default
time Union[datetime, date]

The DateTime for consideration

required
holiday_list List[datetime]

Enumerable of holidays to exclude. These should be sourced from the MarketHoursDatabase

required

Returns:

Type Description
bool

True if the time is not a holidays, otherwise returns false.

not_preceded_by_holiday

not_preceded_by_holiday(
    thursday: Union[datetime, date],
    holiday_list: List[datetime],
) -> bool

This function takes Thursday as input and returns true if four weekdays preceding it are not Holidays

Parameters:

Name Type Description Default
thursday Union[datetime, date]

DateTime of a given Thursday

required
holiday_list List[datetime]

Enumerable of holidays to exclude. These should be sourced from the MarketHoursDatabase

required

Returns:

Type Description
bool

False if DayOfWeek is not Thursday or is not preceded by four weekdays,Otherwise returns True.

nth_business_day

nth_business_day(
    time: Union[datetime, date],
    nth_business_day: int,
    holiday_list: List[datetime],
) -> datetime

Calculates the n^th business day of the month (includes checking for holidays)

Parameters:

Name Type Description Default
time Union[datetime, date]

Month to calculate business day for

required
nth_business_day int

n^th business day to get

required
holiday_list List[datetime]

Holidays to not count as business days

required

Returns:

Type Description
datetime

Nth business day of the month.

nth_friday

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

Method to retrieve the Nth Friday of the given month

Parameters:

Name Type Description Default
time Union[datetime, date]

Date from the given month

required
n int

The order of the Friday in the period

required

Returns:

Type Description
datetime

Nth Friday of given month.

nth_last_business_day

nth_last_business_day(
    time: Union[datetime, date],
    n: int,
    holiday_list: List[datetime],
) -> datetime

Method to retrieve the n^th last business day of the delivery month.

Parameters:

Name Type Description Default
time Union[datetime, date]

DateTime for delivery month

required
n int

Number of days

required
holiday_list List[datetime]

Holidays to use while calculating n^th business day. Useful for MHDB entries

required

Returns:

Type Description
datetime

Nth Last Business day of the month.

nth_weekday

nth_weekday(
    time: Union[datetime, date],
    n: int,
    day_of_week: DayOfWeek,
) -> datetime

Method to retrieve the Nth Weekday of the given month

Parameters:

Name Type Description Default
time Union[datetime, date]

Date from the given month

required
n int

The order of the Weekday in the period

required
day_of_week DayOfWeek

The day of the week

required

Returns:

Type Description
datetime

Nth Weekday of given month.

second_friday

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

Method to retrieve the 2nd Friday of the given month

Parameters:

Name Type Description Default
time Union[datetime, date]

Date from the given month

required

Returns:

Type Description
datetime

2nd Friday of given month.

third_wednesday

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

Method to retrieve third Wednesday of the given month (usually Monday).

Parameters:

Name Type Description Default
time Union[datetime, date]

Date from the given month

required

Returns:

Type Description
datetime

Third Wednesday of the given month.