finpricing.utils#

Submodules#

Package Contents#

Classes#

TimeInterval

Date

Date

DayCountTypes

Generic enumeration.

DayCount

Date

BusDayAdjustTypes

Generic enumeration.

Holiday

CalendarTypes

Generic enumeration.

DateGenRuleTypes

Generic enumeration.

Calendar

FrequencyTypes

Generic enumeration.

BusDayAdjustTypes

Generic enumeration.

TimeInterval

FrequencyTypes

Generic enumeration.

BusDayAdjustTypes

Generic enumeration.

CalendarTypes

Generic enumeration.

DayCountTypes

Generic enumeration.

ClassUtil

CDSStubType

Generic enumeration.

CDSEffectiveDateStyle

Generic enumeration.

CDSTermStyle

Generic enumeration.

CDSAccruedStyle

Generic enumeration.

CDSStyle

CDSStyleBase

CDSStyleCorpNA

Date

ClassUtil

TimeInterval

Date

CDSStyle

CDSStubType

Generic enumeration.

CDSTermStyle

Generic enumeration.

Calendar

DateGenerator

Literal

Literal constants

Functions#

dict_to_obj_str(→ str)

Return the string representation of a dictionary

prettyTableByColumn(→ str)

Return the string representation of a dictionary in a pretty table format where

prettyTableByRow(→ str)

Return the string representation of a dictionary in a pretty table format

datetimeToDates(func)

datetimeToDates(func)

class finpricing.utils.TimeInterval(value: int, period: str)[source]#
classmethod from_string(interval_string)[source]#
__neg__()[source]#
__repr__() str[source]#

Return repr(self).

__mul__(other: int)[source]#
__rmul__(other: int)[source]#
class finpricing.utils.Date(year: int, month: int, day: int)[source]#
property is_weekend: bool#

Return True if the date is a weekend, False otherwise

Monday is 0 and Sunday is 6.

MON = 0#
TUE = 1#
WED = 2#
THU = 3#
FRI = 4#
SAT = 5#
SUN = 6#
to_tuple() tuple[source]#

Return a tuple of (year, month, day)

classmethod convert_from_datetime(date: datetime.date | Date) Date[source]#

Return a Date object from a datetime.date object

classmethod convert_from_datetimes(dates: list[datetime.date | Date]) list[Date][source]#

Return a list of Date objects from a list of datetime.date objects

classmethod from_datetime(date: datetime.date) Date[source]#

Return a Date object from a datetime.date object

classmethod from_tuple(date_tuple: tuple) Date[source]#

Return a Date object from a tuple of (year, month, day)

__sub__(other: object) int[source]#
__add__(days: int) Date[source]#
__eq__(other: object) bool[source]#

Return self==value.

__lt__(other: object) bool[source]#

Return self<value.

__repr__() str[source]#

Return repr(self).

add_days(days: int) Date[source]#

Return a new Date object by adding days to self

add_months(months: int) Date[source]#

Return a new Date object by adding months to self

add_weeks(weeks: int) Date[source]#

Return a new Date object by adding weeks to self

add_years(years: int) Date[source]#

Return a new Date object by adding years to self

add_tenor(tenor: str) Date[source]#

Return a new Date object by adding tenor to self

add_interval(time_interval: TimeInterval) Date[source]#
strftime(fmt: str) str[source]#

Return a string representing the date, controlled by an explicit format string

__hash__() int[source]#

Return hash(self).

class finpricing.utils.Date(year: int, month: int, day: int)[source]#
property is_weekend: bool#

Return True if the date is a weekend, False otherwise

Monday is 0 and Sunday is 6.

MON = 0#
TUE = 1#
WED = 2#
THU = 3#
FRI = 4#
SAT = 5#
SUN = 6#
to_tuple() tuple[source]#

Return a tuple of (year, month, day)

classmethod convert_from_datetime(date: datetime.date | Date) Date[source]#

Return a Date object from a datetime.date object

classmethod convert_from_datetimes(dates: list[datetime.date | Date]) list[Date][source]#

Return a list of Date objects from a list of datetime.date objects

classmethod from_datetime(date: datetime.date) Date[source]#

Return a Date object from a datetime.date object

classmethod from_tuple(date_tuple: tuple) Date[source]#

Return a Date object from a tuple of (year, month, day)

__sub__(other: object) int[source]#
__add__(days: int) Date[source]#
__eq__(other: object) bool[source]#

Return self==value.

__lt__(other: object) bool[source]#

Return self<value.

__repr__() str[source]#

Return repr(self).

add_days(days: int) Date[source]#

Return a new Date object by adding days to self

add_months(months: int) Date[source]#

Return a new Date object by adding months to self

add_weeks(weeks: int) Date[source]#

Return a new Date object by adding weeks to self

add_years(years: int) Date[source]#

Return a new Date object by adding years to self

add_tenor(tenor: str) Date[source]#

Return a new Date object by adding tenor to self

add_interval(time_interval: TimeInterval) Date[source]#
strftime(fmt: str) str[source]#

Return a string representing the date, controlled by an explicit format string

__hash__() int[source]#

Return hash(self).

exception finpricing.utils.NotSupportedError[source]#

Bases: Exception

Common base class for all non-exit exceptions.

class finpricing.utils.DayCountTypes[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

ACT_360 = 0#
ACT_365 = 1#
THIRTY_360 = 2#
ACT_ACT_ISDA = 3#
Thirty_E_360 = 4#
Thirty_E_360_ISDA = 5#
class finpricing.utils.DayCount(dccType: DayCountTypes)[source]#
year_frac#
days_between(start_date: finpricing.utils.date.Date, end_date: finpricing.utils.date.Date, include_end=False) tuple[source]#

Return the number of days and year fraction using a specific day count convention

Returns:

(days, fraction of year)

Return type:

tuple

year_fraction(start_date: finpricing.utils.date.Date | datetime.date, end_date: finpricing.utils.date.Date | datetime.date, next_coupon_date: finpricing.utils.date.Date = None) float[source]#

Return the fraction of year between two dates using a specific day count convention

The actual calculation is done in days_between() method.

Parameters:
  • start_date (Date) – start date of the accrual period

  • end_date (Date) – end date of the accrual period. For a bond trade, it is the settlement date of the trade.

  • next_coupon_date (Date) – next coupon date of the bond / maturity date of the bond if no more coupon payment

convert_dates_to_times(anchor_date: finpricing.utils.date.Date, dates: list[finpricing.utils.date.Date]) list[float][source]#

Return a list of time fraction (year fraction) between anchor date and each date in the list

Parameters:
  • anchor_date (Date) – anchor date

  • dates (list[Date]) – list of dates

Returns:

list of time fraction (year fraction)

Return type:

list[float]

__repr__() str[source]#

Return the string representation of the object

exception finpricing.utils.NotSupportedError[source]#

Bases: Exception

Common base class for all non-exit exceptions.

class finpricing.utils.Date(year: int, month: int, day: int)[source]#
property is_weekend: bool#

Return True if the date is a weekend, False otherwise

Monday is 0 and Sunday is 6.

MON = 0#
TUE = 1#
WED = 2#
THU = 3#
FRI = 4#
SAT = 5#
SUN = 6#
to_tuple() tuple[source]#

Return a tuple of (year, month, day)

classmethod convert_from_datetime(date: datetime.date | Date) Date[source]#

Return a Date object from a datetime.date object

classmethod convert_from_datetimes(dates: list[datetime.date | Date]) list[Date][source]#

Return a list of Date objects from a list of datetime.date objects

classmethod from_datetime(date: datetime.date) Date[source]#

Return a Date object from a datetime.date object

classmethod from_tuple(date_tuple: tuple) Date[source]#

Return a Date object from a tuple of (year, month, day)

__sub__(other: object) int[source]#
__add__(days: int) Date[source]#
__eq__(other: object) bool[source]#

Return self==value.

__lt__(other: object) bool[source]#

Return self<value.

__repr__() str[source]#

Return repr(self).

add_days(days: int) Date[source]#

Return a new Date object by adding days to self

add_months(months: int) Date[source]#

Return a new Date object by adding months to self

add_weeks(weeks: int) Date[source]#

Return a new Date object by adding weeks to self

add_years(years: int) Date[source]#

Return a new Date object by adding years to self

add_tenor(tenor: str) Date[source]#

Return a new Date object by adding tenor to self

add_interval(time_interval: TimeInterval) Date[source]#
strftime(fmt: str) str[source]#

Return a string representing the date, controlled by an explicit format string

__hash__() int[source]#

Return hash(self).

class finpricing.utils.BusDayAdjustTypes[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

NONE = 1#
FOLLOWING = 2#
MODIFIED_FOLLOWING = 3#
PREVIOUS = 4#
MODIFIED_PREVIOUS = 5#
class finpricing.utils.Holiday(calendarType: CalendarTypes)[source]#
is_holiday(date: finpricing.utils.date.Date) bool[source]#
holiday_united_states(date: finpricing.utils.date.Date) bool[source]#

Return True if the date is a holiday in the United States, False otherwise

Need to verify and enhance the dates. Reference: domokane/FinancePy

class finpricing.utils.CalendarTypes[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

NONE = 0#
WEEKEND = 1#
UNITED_STATES = 2#
class finpricing.utils.DateGenRuleTypes[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

FORWARD = 1#
BACKWARD = 2#
class finpricing.utils.Calendar(calendarType: finpricing.utils.holiday.CalendarTypes)[source]#
is_holiday(date: finpricing.utils.date.Date) bool[source]#
is_business_day(date: finpricing.utils.date.Date) bool[source]#

Return True if the date is a business day

Weekend or not is determined solely by the date itself. Holiday or not is determined by the calendar.

add_business_days(date: finpricing.utils.date.Date, num_days: int) finpricing.utils.date.Date[source]#

Return a new Date object by adding num_days business days to the date

adjust(date: finpricing.utils.date.Date, busDayAdjType: finpricing.utils.bus_day_adj.BusDayAdjustTypes) finpricing.utils.date.Date[source]#

Return a new Date object by adjusting the date according to the Business Day Convention

For details, https://jollycontrarian.com/index.php?title=Business_Day_Convention_-_ISDA_Definition

class finpricing.utils.FrequencyTypes[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

ANNUAL = 1#
SEMI_ANNUAL = 2#
QUARTERLY = 4#
MONTHLY = 12#
CONTINUOUS = 99#
class finpricing.utils.BusDayAdjustTypes[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

NONE = 1#
FOLLOWING = 2#
MODIFIED_FOLLOWING = 3#
PREVIOUS = 4#
MODIFIED_PREVIOUS = 5#
class finpricing.utils.TimeInterval(value: int, period: str)[source]#
classmethod from_string(interval_string)[source]#
__neg__()[source]#
__repr__() str[source]#

Return repr(self).

__mul__(other: int)[source]#
__rmul__(other: int)[source]#
class finpricing.utils.FrequencyTypes[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

ANNUAL = 1#
SEMI_ANNUAL = 2#
QUARTERLY = 4#
MONTHLY = 12#
CONTINUOUS = 99#
class finpricing.utils.BusDayAdjustTypes[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

NONE = 1#
FOLLOWING = 2#
MODIFIED_FOLLOWING = 3#
PREVIOUS = 4#
MODIFIED_PREVIOUS = 5#
class finpricing.utils.CalendarTypes[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

NONE = 0#
WEEKEND = 1#
UNITED_STATES = 2#
class finpricing.utils.DayCountTypes[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

ACT_360 = 0#
ACT_365 = 1#
THIRTY_360 = 2#
ACT_ACT_ISDA = 3#
Thirty_E_360 = 4#
Thirty_E_360_ISDA = 5#
class finpricing.utils.ClassUtil[source]#
save_attributes(ignore=[])[source]#
resolve_dates(start_date, maturity_date_or_tenor)[source]#
first_valid(*args)[source]#
class finpricing.utils.CDSStubType[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

NO_STUB = 'NO_STUB'#
class finpricing.utils.CDSEffectiveDateStyle[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

PCD = 'PCD'#
class finpricing.utils.CDSTermStyle[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

IMM_CORPORATE = 'IMM_CORPORATE'#
class finpricing.utils.CDSAccruedStyle[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

SNAC = 'SNAC'#
CONVENTIONAL = 'CONVENTIONAL'#
class finpricing.utils.CDSStyle[source]#

Bases: finpricing.utils.tools.ClassUtil

classmethod from_name(name)[source]#

Return a CDSStyle instance from a string of name

classmethod CORP_NA()[source]#
class finpricing.utils.CDSStyleBase(name, day_count_type, frequency_type, bus_day_adj_type, cds_term_style, cds_effective_date_style, cds_stub_length, minimal_stub_period: int, eom_adj: bool, calendar_type, accrued_style)[source]#

Bases: finpricing.utils.tools.ClassUtil

class finpricing.utils.CDSStyleCorpNA[source]#

Bases: CDSStyleBase

class finpricing.utils.Date(year: int, month: int, day: int)[source]#
property is_weekend: bool#

Return True if the date is a weekend, False otherwise

Monday is 0 and Sunday is 6.

MON = 0#
TUE = 1#
WED = 2#
THU = 3#
FRI = 4#
SAT = 5#
SUN = 6#
to_tuple() tuple[source]#

Return a tuple of (year, month, day)

classmethod convert_from_datetime(date: datetime.date | Date) Date[source]#

Return a Date object from a datetime.date object

classmethod convert_from_datetimes(dates: list[datetime.date | Date]) list[Date][source]#

Return a list of Date objects from a list of datetime.date objects

classmethod from_datetime(date: datetime.date) Date[source]#

Return a Date object from a datetime.date object

classmethod from_tuple(date_tuple: tuple) Date[source]#

Return a Date object from a tuple of (year, month, day)

__sub__(other: object) int[source]#
__add__(days: int) Date[source]#
__eq__(other: object) bool[source]#

Return self==value.

__lt__(other: object) bool[source]#

Return self<value.

__repr__() str[source]#

Return repr(self).

add_days(days: int) Date[source]#

Return a new Date object by adding days to self

add_months(months: int) Date[source]#

Return a new Date object by adding months to self

add_weeks(weeks: int) Date[source]#

Return a new Date object by adding weeks to self

add_years(years: int) Date[source]#

Return a new Date object by adding years to self

add_tenor(tenor: str) Date[source]#

Return a new Date object by adding tenor to self

add_interval(time_interval: TimeInterval) Date[source]#
strftime(fmt: str) str[source]#

Return a string representing the date, controlled by an explicit format string

__hash__() int[source]#

Return hash(self).

finpricing.utils.dict_to_obj_str(d: dict) str[source]#

Return the string representation of a dictionary

finpricing.utils.prettyTableByColumn(d: dict, align='') str[source]#

Return the string representation of a dictionary in a pretty table format where dictionary values can be either iterables for direct use or tuples where the first element is an iterable and the second is a format string.

Args: d (dict): Dictionary with data to be displayed by the table. align (str): String with column alignments. Use ‘l’ for left, ‘r’ for right, and ‘c’ for center.

Returns: str: A string representation of the table.

finpricing.utils.prettyTableByRow(d: dict) str[source]#

Return the string representation of a dictionary in a pretty table format

The dictionary contains the row names as keys and the list of values as values.

finpricing.utils.datetimeToDates(func)[source]#
class finpricing.utils.ClassUtil[source]#
save_attributes(ignore=[])[source]#
resolve_dates(start_date, maturity_date_or_tenor)[source]#
first_valid(*args)[source]#
class finpricing.utils.TimeInterval(value: int, period: str)[source]#
classmethod from_string(interval_string)[source]#
__neg__()[source]#
__repr__() str[source]#

Return repr(self).

__mul__(other: int)[source]#
__rmul__(other: int)[source]#
class finpricing.utils.Date(year: int, month: int, day: int)[source]#
property is_weekend: bool#

Return True if the date is a weekend, False otherwise

Monday is 0 and Sunday is 6.

MON = 0#
TUE = 1#
WED = 2#
THU = 3#
FRI = 4#
SAT = 5#
SUN = 6#
to_tuple() tuple[source]#

Return a tuple of (year, month, day)

classmethod convert_from_datetime(date: datetime.date | Date) Date[source]#

Return a Date object from a datetime.date object

classmethod convert_from_datetimes(dates: list[datetime.date | Date]) list[Date][source]#

Return a list of Date objects from a list of datetime.date objects

classmethod from_datetime(date: datetime.date) Date[source]#

Return a Date object from a datetime.date object

classmethod from_tuple(date_tuple: tuple) Date[source]#

Return a Date object from a tuple of (year, month, day)

__sub__(other: object) int[source]#
__add__(days: int) Date[source]#
__eq__(other: object) bool[source]#

Return self==value.

__lt__(other: object) bool[source]#

Return self<value.

__repr__() str[source]#

Return repr(self).

add_days(days: int) Date[source]#

Return a new Date object by adding days to self

add_months(months: int) Date[source]#

Return a new Date object by adding months to self

add_weeks(weeks: int) Date[source]#

Return a new Date object by adding weeks to self

add_years(years: int) Date[source]#

Return a new Date object by adding years to self

add_tenor(tenor: str) Date[source]#

Return a new Date object by adding tenor to self

add_interval(time_interval: TimeInterval) Date[source]#
strftime(fmt: str) str[source]#

Return a string representing the date, controlled by an explicit format string

__hash__() int[source]#

Return hash(self).

class finpricing.utils.CDSStyle[source]#

Bases: finpricing.utils.tools.ClassUtil

classmethod from_name(name)[source]#

Return a CDSStyle instance from a string of name

classmethod CORP_NA()[source]#
class finpricing.utils.CDSStubType[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

NO_STUB = 'NO_STUB'#
class finpricing.utils.CDSTermStyle[source]#

Bases: enum.Enum

Generic enumeration.

Derive from this class to define new enumerations.

IMM_CORPORATE = 'IMM_CORPORATE'#
finpricing.utils.datetimeToDates(func)[source]#
class finpricing.utils.Calendar(calendarType: finpricing.utils.holiday.CalendarTypes)[source]#
is_holiday(date: finpricing.utils.date.Date) bool[source]#
is_business_day(date: finpricing.utils.date.Date) bool[source]#

Return True if the date is a business day

Weekend or not is determined solely by the date itself. Holiday or not is determined by the calendar.

add_business_days(date: finpricing.utils.date.Date, num_days: int) finpricing.utils.date.Date[source]#

Return a new Date object by adding num_days business days to the date

adjust(date: finpricing.utils.date.Date, busDayAdjType: finpricing.utils.bus_day_adj.BusDayAdjustTypes) finpricing.utils.date.Date[source]#

Return a new Date object by adjusting the date according to the Business Day Convention

For details, https://jollycontrarian.com/index.php?title=Business_Day_Convention_-_ISDA_Definition

class finpricing.utils.DateGenerator[source]#
static generate_extended(start_date: datetime.date | finpricing.utils.date.Date, maturity_date: datetime.date | finpricing.utils.date.Date, time_interval: finpricing.utils.date.TimeInterval | str, stub_at_end: bool)[source]#

generate a list of dates from start_date to maturity_date with given time_interval

Note

if stub_at_end is True, the dates will be generated from start_date and roll forward until and after maturity_date if stub_at_end is False, the dates will be generated from maturity_date and roll backward until and before start_date

Parameters:
  • start_date (Union[datetime.date, Date]) – start date

  • maturity_date (Union[datetime.date, Date]) – maturity date

  • time_interval (Union[TimeInterval, str]) – time interval

  • stub_at_end (bool) – whether the stub is at the end of the period

static generate_cds(start_date, maturity_date, cds_style: finpricing.utils.cds_style.CDSStyle, stub_at_end: bool = False)[source]#

generate a list of dates from start_date to maturity_date with given cds_style

Note

Most likely, the stub is at the end of the period. In a NO_STUB case, the first date will be removed. So the first date will be after start_date.

static generate_cds_adjust(start_date, maturity_date, cds_style: finpricing.utils.cds_style.CDSStyle, stub_at_end: bool = False)[source]#

generate accrual start dates, accrual end dates, and calendar adjusted payment dates for a cds contract

NOTE only the payment dates are adjusted!

static generate_cds_maturity_date(market_date: finpricing.utils.date.Date | datetime.date, maturity_date: finpricing.utils.date.Date | datetime.date, term_style: str = None)[source]#

return the adjusted maturity date for a cds contract by its term style

FIXME This is not effectively implemented yet. It seems IMM CORPORATE returns the same maturity date as the input.

static generate_cds_effective_date(market_date: finpricing.utils.date.Date | datetime.date, maturity_date: finpricing.utils.date.Date | datetime.date, cds_style: finpricing.utils.cds_style.CDSStyle | str)[source]#

generate the effective date for a cds contract when market date is in between coupon dates

class finpricing.utils.Literal[source]#

Bases: enum.Enum

Literal constants

ONE_BASIS_POINT = 0.0001#
ONE_PERCENT = 0.01#
ZERO = 0#
UNIT = 1#
ONE_HUNDRED = 100#
ONE_THOUSAND = 1000#
ONE_MILLION = 1000000#
ONE_BILLION = 1000000000#