DateUtils

Calendar Definition

class Calendar(filename=None)

Bases: object

This module provides the interface for determining whether a date is business day or holiday via specifying a txt file with holiday list.

__init__(filename=None)

Specify holiday list with a txt file. If cannot find file given filename or filename is none, use remote url instead. The remote url is https://gitee.com/luluUCD/optshare/raw/master/src/optshare/dateutils/China.txt.

Parameters:

filename (str or None) – a txt file with holiday list, use remote url instead if cannot find file or filename is None.

business_days_between(start_date, end_date, include_start_date=False, include_end_date=True)

Count the number of business days between given start date and end date, specifying whether to include start date or end date

Parameters:
  • start_date (datetime.date) – start date

  • end_date (datetime.date) – end date

  • include_start_date (bool) – determine whether to include start date

  • include_end_date (bool) – determine whether to include end date

Returns:

the number of business days between start date and end date

Return type:

bool

is_business_day(d)

A boolean value that determines whether d is business day

Parameters:

d (datetime.date) – input date

Returns:

True or False. True if d is business day, and vice versa.

Return type:

bool

is_holiday(d)

A boolean value that determines whether d is holiday

Parameters:

d (datetime.date) – input date

Returns:

True or False. True if d is holiday, and vice versa.

Return type:

bool

next_business_day(d)

Returns the following business day given input date d

Parameters:

d (datetime.date) – input date

Returns:

next business day

Return type:

datetime.date

previous_business_day(d)

Returns previous business day given input date d

Parameters:

d (datetime.date) – input date

Returns:

previous business day

Type:

datetime.date

Date Utility Functions

This module offers useful date utility functions in the Chinese financial market for more convenient research purpose.

add_months(input_date, months)
count_business_days(start_date_str, end_date_str, cal, include_start_date=False, include_end_date=True)

Count the number of business days given string-like start date and date, with the inclusion of start date and end date represented by the specified boolean respectively

Parameters:
  • start_date_str (str, 'yyyymmdd') – start date string

  • end_date_str (str, 'yyyymmdd') – end date string

  • cal (optshare.Calendar) – calendar

  • include_start_date – determine whether to include the start date

  • include_end_date (bool) – determine whether to include the end date

Returns:

the number of natural days between start date and end date

Return type:

int

count_natural_days(start_date_str, end_date_str, include_start_date=False, include_end_date=True)

Count the number of the natural days given string-like start date and date, with the inclusion of start date and end date represented by the specified boolean respectively

Parameters:
  • start_date_str (str, 'yyyymmdd') – start date string

  • end_date_str (str, 'yyyymmdd') – end date string

  • include_start_date – determine whether to include the start date

  • include_end_date (bool) – determine whether to include the end date

Returns:

the number of natural days between start date and end date

Return type:

int

date_from_string(date_str)

Get datetime.date object from ‘yyyymmdd’ date string

Parameters:

date_str (str, 'yyyymmdd') – date string, e.g. “20220101”

Returns:

date object

Return type:

datetime.date

get_lastnth_trading_day(n, yyyy, mm, cal)

Return the last nth trading day in month

Parameters:
  • n (int) – nth trading day

  • yyyy (int) – 4-digit year number

  • mm (int) – month number, 1-12

  • cal (optshare.Calendar) – calendar object

Returns:

the last nth trading day in month

Return type:

datetime.date

get_market_time(now)

Adjusted current datetime function.

Parameters:

now (datetime.datetime) – current datetime, e.g. datetime.datetime.now()

Returns:

corresponding market time, adjusting the dates that are not in the transaction

Return type:

datetime.datetime

get_nth_trading_day(n, yyyy, mm, cal)

Return the nth trading day in month

Parameters:
  • n (int) – nth trading day

  • yyyy (int) – 4-digit year number

  • mm (int) – month number, 1-12

  • cal (optshare.Calendar) – calendar object

Returns:

the nth trading day in month

Return type:

datetime.date

get_weekday_in_month(year, month, number_of_week, weekday)

get the nth weekday in month

Parameters:
  • year (int) – year number, ‘yyyy’

  • month (int) – month number, 1-12

  • number_of_week (int) – number of the week

  • weekday – weekday, 1-7. 1 represents monday, 7 represents sunday

Returns:

nth weekday in month

Return type:

datetime.date