finds.structured.busday
Implement custom trading-day and weekly business date calendars
Copyright 2022, Terence Lim
MIT License
- class finds.structured.busday.BusDay(sql: SQL, start: int = 19251231, end: int = 20251231, hols: List[int] = [20230102, 20230116, 20230220, 20230407, 20230529, 20230619, 20230704, 20230904, 20231123, 20231225, 20240101, 20240115, 20240219, 20240329, 20240527, 20240619, 20240704, 20240902, 20241128, 20241225, 20250101, 20250129, 20250217, 20250418, 20250526, 20250619, 20250704, 20250901, 20251127, 20251225], endweek: int | str = 0, new: bool = False, verbose: int = 0)[source]
Bases:
object
Implement custom business-day and weekly dates calendar
- Parameters:
sql – SQL connection instance to store dates
hols – List of expected future holidays
start – Start date of calendar
end – End date of calendar
endweek – Ending day of week for weekly calendar: 0-6 or ‘Mon’-‘Sun’
new – Load trading days from French library (True), else local database
- Variables:
weeks – DataFrame of weeks in rows, index is weeknum and columns for beg, end dates and ismonthend indicator
Notes:
Non-trading holidays inferred from Ken French and NYSE websites
Earliest French daily factors is 19260701, except STRev is 19260126
- begmo(date: int | List[int], months: int = 0) int | List[int] [source]
Return (list of) business month begin date, optional months offset
- begwk(date=typing.Union[int, typing.List[int]], weeks: int = 0) int | List[int] [source]
Return beginning business week dates, with optional offset
- begyr(date: int | List[int], years: int = 0) int | List[int] [source]
Return (list of) business year begin date, optional years offset
- date_range(start: int, end: int, freq: str | int = 'daily') List[int] [source]
Return business dates at desired freq between beg and end dates
- Parameters:
start – Inclusive start of date range
end – Inclusive end of date range
freq – If int, then annual ending on month-end business date, else in {(‘d’)aily, (‘b’)egmo, (‘e’)ndmo, (‘w’)eekly, (‘q’)uarterly
- date_tuples(dates: List[int]) List[Tuple[int, int]] [source]
Return (beg, end) holding period between rebalance dates
- datetime(date: Any | Iterable[Any], month: int | None = None, day: int | None = None) datetime | List[datetime] [source]
Convert int date or pandas timestamp to pydatetime type
- Parameters:
date – Input date or year or pandas Timestamp or datetime64
month – If None, then month is inferred from input date
day – If None, then day is inferred from input date
- Returns:
datetime object or list-like with same shape as input date
Notes:
If input day is 00, then returns custom business month begin date
If input day is 99, then returns custom business month end date
- static day(date: int | Iterable[int]) int | List[int] [source]
Helper to extract int day of month from input date or list
- december_fiscal(dates: int | List[int]) int | List[int] [source]
Return (list of) Fama-French December fiscal year-end date/s
- endmo(date: int | List[int], months: int = 0) int | List[int] [source]
Return (list of) business month end date, optional months offset
- endqr(date: int | List[int], quarters: int = 0) int | List[int] [source]
Return (list of) business month end date, optional months offset
- endwk(date=typing.Union[int, typing.List[int]], weeks: int = 0) int | List[int] [source]
Return ending business week dates, with optional offset
- endyr(date: int | List[int], years: int = 0) int | List[int] [source]
Return (list of) business year end date, optional years offset
- ismonthend(date: int | List[int]) int | List[int] [source]
If dates is in last complete week in a month
- june_universe(dates: int | List[int]) int | List[int] [source]
Return (list of) Fama-French June universe selection date/s
- static month(date: int | Iterable[int]) int | List[int] [source]
Helper to extract int months from input date or list
- offset(dates: int | List[int], offsets: int = 0, end: int | None = None, roll: str = 'preceding') int | List[int] [source]
Return valid business date with optional offset or roll treatment
- Parameters:
dates – Input dates in YYYYMMDD int format
offsets – Number of business days to offset
end – End index of offset window, None to return a single date
roll – How to treat dates that are not a valid day, in {‘raise’, ‘forward’, ‘following’, ‘backward’, ‘preceding’}
- static to_date(dates: Any, format: str = '%Y-%m-%d') int | List[int] [source]
Construct int date from strings using input and output formats
- Parameters:
date – Input date strings or Timestamp or pydatetime to convert
format – Optional format of input date string
- Returns:
int dates with year, month, date components according to outformat
Formats specified as in strptime() and strftime():
%b %B %h = input month name
%F = %Y-%m-%d
%T = %H:%M:%S
%n = whitespace
%w %W %U %V = week number
%u = day of week (1-7)
Examples:
>>> to_date('12-31-1999', format='%m-%d-%Y') >>> to_date(['1999-01-01', '1999-12-31']) >>> int(datetime.strptime(str(19991231), '%Y%m%d').strftime('%Y%m%d'))
- static to_datetime(arg: Any, format: str = '%Y%m%d', **kwargs) Timestamp | DatetimeIndex [source]
Wraps pd.to_datetime to convert string to pandas TimeStamp format
- static to_monthend(dates: int | Iterable[int]) int | List[int] [source]
Return calendar monthend date given an int date or list
- Parameters:
dates – input YYYYMMDD (or first 4-, 6-digits) int date, or list
- Returns:
Output dates converted to monthend of calendar