Skip to content

Trading Centres

Trading centres provide business day calendars for financial date calculations. A TradingCentre wraps a single holiday calendar. TradingCentres combines multiple centres for joint business day calculations.

Installation

This feature requires the holidays extra:

pip install ccy[holidays]

Available centres

Code Description
TGT TARGET (European Central Bank)
LON London (United Kingdom)
NY New York (United States)

Functions

ccy.tradingcentres.centres

centres(codes=None)

Return a TradingCentres instance for the given centre codes.

PARAMETER DESCRIPTION
codes

Comma-separated trading centre codes, e.g. 'LON,NY'

TYPE: str | None DEFAULT: None

Source code in ccy/tradingcentres/__init__.py
def centres(
    codes: Annotated[
        str | None, Doc("Comma-separated trading centre codes, e.g. 'LON,NY'")
    ] = None,
) -> TradingCentres:
    """Return a [TradingCentres][ccy.tradingcentres.TradingCentres] instance
    for the given centre codes."""
    tcs = TradingCentres()
    if codes:
        lcs = codes.upper().replace(" ", "").split(",")
        for code in lcs:
            tc = trading_centres.get(code)
            if tc:
                tcs.centres[tc.code] = tc
    return tcs

ccy.tradingcentres.nextbizday

nextbizday(dte, nd=1, tcs=None)

Return the date nd business days after dte.

PARAMETER DESCRIPTION
dte

The reference date

TYPE: date

nd

Number of business days to move forward; 0 adjusts to next biz day

TYPE: int DEFAULT: 1

tcs

Comma-separated trading centre codes

TYPE: str | None DEFAULT: None

Source code in ccy/tradingcentres/__init__.py
def nextbizday(
    dte: Annotated[date, Doc("The reference date")],
    nd: Annotated[
        int,
        Doc("Number of business days to move forward; 0 adjusts to next biz day"),
    ] = 1,
    tcs: Annotated[str | None, Doc("Comma-separated trading centre codes")] = None,
) -> date:
    """Return the date nd business days after dte."""
    return centres(tcs).nextbizday(dte, nd)

ccy.tradingcentres.prevbizday

prevbizday(dte, nd=1, tcs=None)

Return the date nd business days before dte.

PARAMETER DESCRIPTION
dte

The reference date

TYPE: date

nd

Number of business days to move back

TYPE: int DEFAULT: 1

tcs

Comma-separated trading centre codes

TYPE: str | None DEFAULT: None

Source code in ccy/tradingcentres/__init__.py
def prevbizday(
    dte: Annotated[date, Doc("The reference date")],
    nd: Annotated[int, Doc("Number of business days to move back")] = 1,
    tcs: Annotated[str | None, Doc("Comma-separated trading centre codes")] = None,
) -> date:
    """Return the date nd business days before dte."""
    return centres(tcs).prevbizday(dte, nd)

Classes

ccy.tradingcentres.TradingCentre pydantic-model

Bases: BaseModel

Fields:

code pydantic-field

code

The code of the trading centre

calendar pydantic-field

calendar

The holiday calendar of the trading centre

isholiday

isholiday(dte)

Return True if the date is a holiday.

PARAMETER DESCRIPTION
dte

The date to check

TYPE: date

Source code in ccy/tradingcentres/__init__.py
def isholiday(self, dte: Annotated[date, Doc("The date to check")]) -> bool:
    """Return True if the date is a holiday."""
    return dte in self.calendar

ccy.tradingcentres.TradingCentres pydantic-model

Bases: BaseModel

Fields:

centres pydantic-field

centres

code property

code

Comma-separated sorted codes of the trading centres.

isbizday

isbizday(dte)

Return True if the date is a business day across all centres.

PARAMETER DESCRIPTION
dte

The date to check

TYPE: date

Source code in ccy/tradingcentres/__init__.py
def isbizday(self, dte: Annotated[date, Doc("The date to check")]) -> bool:
    """Return True if the date is a business day across all centres."""
    if dte.isoweekday() in isoweekend:
        return False
    for c in self.centres.values():
        if c.isholiday(dte):
            return False
    return True

nextbizday

nextbizday(dte, nd=1)

Return the date nd business days after dte.

PARAMETER DESCRIPTION
dte

The reference date

TYPE: date

nd

Number of business days to move forward; 0 adjusts to next biz day

TYPE: int DEFAULT: 1

Source code in ccy/tradingcentres/__init__.py
def nextbizday(
    self,
    dte: Annotated[date, Doc("The reference date")],
    nd: Annotated[
        int,
        Doc("Number of business days to move forward; 0 adjusts to next biz day"),
    ] = 1,
) -> date:
    """Return the date nd business days after dte."""
    n = 0
    while not self.isbizday(dte):
        dte += oneday
    while n < nd:
        dte += oneday
        if self.isbizday(dte):
            n += 1
    return dte

prevbizday

prevbizday(dte, nd=1)

Return the date nd business days before dte.

PARAMETER DESCRIPTION
dte

The reference date

TYPE: date

nd

Number of business days to move back

TYPE: int DEFAULT: 1

Source code in ccy/tradingcentres/__init__.py
def prevbizday(
    self,
    dte: Annotated[date, Doc("The reference date")],
    nd: Annotated[int, Doc("Number of business days to move back")] = 1,
) -> date:
    """Return the date nd business days before dte."""
    n = 0
    if nd < 0:
        return self.nextbizday(dte, -nd)
    else:
        while not self.isbizday(dte):
            dte -= oneday
        n = 0
        while n < nd:
            dte -= oneday
            if self.isbizday(dte):
                n += 1
    return dte

Usage

Business day checks

from datetime import date
from ccy.tradingcentres import centres

tcs = centres("TGT")
tcs.isbizday(date(2024, 12, 25))  # False — Christmas
tcs.isbizday(date(2024, 12, 24))  # True

Combine multiple centres — a day is a business day only if it is in all of them:

tcs = centres("LON,NY")
tcs.code  # "LON,NY"
tcs.isbizday(date(2024, 7, 4))  # False — US Independence Day

Next and previous business day

from ccy.tradingcentres import nextbizday, prevbizday
from datetime import date

friday = date(2024, 12, 20)

nextbizday(friday)        # date(2024, 12, 23) — skips weekend
nextbizday(friday, nd=2)  # date(2024, 12, 24)

prevbizday(friday)        # date(2024, 12, 19)
prevbizday(friday, nd=3)  # date(2024, 12, 17)

Pass a centre code to apply its holiday calendar:

nextbizday(date(2024, 12, 24), tcs="TGT")  # date(2024, 12, 27) — skips Christmas

nd=0 — adjust to business day

Passing nd=0 to nextbizday adjusts the date forward to the next business day if it falls on a weekend or holiday, and leaves it unchanged otherwise:

saturday = date(2024, 12, 21)
nextbizday(saturday, nd=0)  # date(2024, 12, 23)
nextbizday(friday, nd=0)    # date(2024, 12, 20) — already a business day