-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
754 additions
and
831 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
#IDE | ||
.idea | ||
.vscode | ||
|
||
#python | ||
*.pyc | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,96 @@ | ||
from .centres import TradingCentre, centres | ||
from __future__ import annotations | ||
|
||
__all__ = ["centres", "TradingCentre", "prevbizday", "nextbizday"] | ||
from dataclasses import dataclass, field | ||
from datetime import date, timedelta | ||
import holidays | ||
import holidays.countries | ||
import holidays.financial | ||
|
||
isoweekend = frozenset((6, 7)) | ||
oneday = timedelta(days=1) | ||
|
||
def prevbizday(dte=None, nd=1, tcs=None): | ||
tcs = centres(tcs) | ||
return tcs.prevbizday(dte, nd) | ||
_tcs: dict[str, TradingCentre] = {} | ||
|
||
|
||
def nextbizday(dte=None, nd=1, tcs=None): | ||
tcs = centres(tcs) | ||
return tcs.nextbizday(dte, nd) | ||
def prevbizday(dte: date, nd: int = 1, tcs: str | None = None) -> date: | ||
return centres(tcs).prevbizday(dte, nd) | ||
|
||
|
||
def nextbizday(dte: date, nd: int = 1, tcs: str | None = None) -> date: | ||
return centres(tcs).nextbizday(dte, nd) | ||
|
||
|
||
def centres(codes: str | None = None) -> TradingCentres: | ||
tcs = TradingCentres() | ||
if codes: | ||
lcs = codes.upper().replace(" ", "").split(",") | ||
for code in lcs: | ||
tc = _tcs.get(code) | ||
if tc: | ||
tcs.centres[tc.code] = tc | ||
return tcs | ||
|
||
|
||
@dataclass | ||
class TradingCentre: | ||
code: str | ||
calendar: holidays.HolidayBase | ||
|
||
def isholiday(self, dte: date) -> bool: | ||
return dte not in self.calendar | ||
|
||
|
||
@dataclass | ||
class TradingCentres: | ||
centres: dict[str, TradingCentre] = field(default_factory=dict) | ||
|
||
def isbizday(self, dte: date) -> bool: | ||
if dte.isoweekday() in isoweekend: | ||
return False | ||
for c in self.centres.values(): | ||
if c.isholiday(dte): | ||
return False | ||
return True | ||
|
||
def nextbizday(self, dte: date, nd: int = 1) -> date: | ||
n = 0 | ||
while not self.isbizday(dte): | ||
dte += oneday | ||
while n < nd: | ||
dte += oneday | ||
if self.isbizday(dte): | ||
n += 1 | ||
return dte | ||
|
||
def prevbizday(self, dte: date, nd: int = 1) -> date: | ||
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 | ||
|
||
|
||
_tcs.update( | ||
(tc.code, tc) | ||
for tc in ( | ||
TradingCentre( | ||
code="TGT", | ||
calendar=holidays.financial.european_central_bank.EuropeanCentralBank(), # type: ignore [no-untyped-call] | ||
), | ||
TradingCentre( | ||
code="LON", | ||
calendar=holidays.countries.united_kingdom.UnitedKingdom(), # type: ignore [no-untyped-call] | ||
), | ||
TradingCentre( | ||
code="NY", | ||
calendar=holidays.countries.united_states.UnitedStates(), # type: ignore [no-untyped-call] | ||
), | ||
) | ||
) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.