|
1 | | -"""Day Counter for Counting time between 2 dates. |
2 | | -Implemented:: |
3 | | -
|
4 | | - * Actual 360 |
5 | | - * Actual 365 |
6 | | - * 30 / 360 |
7 | | - * Actual Actual |
8 | | -""" |
9 | | - |
10 | 1 | from __future__ import annotations |
11 | 2 |
|
12 | | -from copy import copy |
13 | 3 | from datetime import date |
14 | | -from typing import Any |
15 | | -from ..dates.utils import date_diff |
16 | | - |
17 | | -__all__ = ["getdc", "DayCounter", "alldc"] |
18 | | - |
19 | | - |
20 | | -def getdc(name: str) -> DayCounter: |
21 | | - return _day_counters[name]() |
22 | | - |
23 | | - |
24 | | -def alldc() -> dict[str, DayCounterMeta]: |
25 | | - global _day_counters |
26 | | - return copy(_day_counters) |
27 | | - |
28 | | - |
29 | | -class DayCounterMeta(type): |
30 | | - def __new__(cls, name: str, bases: Any, attrs: Any) -> DayCounterMeta: |
31 | | - new_class = super(DayCounterMeta, cls).__new__(cls, name, bases, attrs) |
32 | | - if name := getattr(new_class, "name", ""): |
33 | | - _day_counters[name] = new_class |
34 | | - return new_class |
35 | | - |
36 | | - |
37 | | -_day_counters: dict[str, DayCounterMeta] = {} |
38 | | - |
39 | | - |
40 | | -class DayCounter(metaclass=DayCounterMeta): |
41 | | - name: str = "" |
42 | | - |
43 | | - def count(self, start: date, end: date) -> float: |
44 | | - """Count the number of days between 2 dates""" |
45 | | - return date_diff(end, start).total_seconds() / 86400 |
46 | | - |
47 | | - def dcf(self, start: date, end: date) -> float: |
48 | | - return self.count(start, end) / 360.0 |
49 | | - |
50 | | - |
51 | | -class Act360(DayCounter): |
52 | | - name = "ACT/360" |
53 | | - |
54 | | - |
55 | | -class Act365(DayCounter): |
56 | | - name = "ACT/365" |
57 | | - |
58 | | - def dcf(self, start: date, end: date) -> float: |
59 | | - return self.count(start, end) / 365.0 |
| 4 | +from enum import StrEnum |
60 | 5 |
|
| 6 | +from ..dates.utils import date_diff |
61 | 7 |
|
62 | | -class Thirty360(DayCounter): |
63 | | - name = "30/360" |
| 8 | +__all__ = ["DayCounter"] |
64 | 9 |
|
65 | | - def dcf(self, start: date, end: date) -> float: |
66 | | - d1 = min(start.day, 30) |
67 | | - d2 = min(end.day, 30) |
68 | | - return 360 * (end.year - start.year) + 30 * (end.month - start.month) + d2 - d1 |
69 | 10 |
|
| 11 | +class DayCounter(StrEnum): |
| 12 | + """Day count convention types""" |
70 | 13 |
|
71 | | -class ActAct(DayCounter): |
72 | | - name = "ACT/ACT" |
| 14 | + ACT360 = "ACT/360" |
| 15 | + ACT365 = "ACT/365" |
| 16 | + THIRTY360 = "30/360" |
| 17 | + THIRTYE360 = "30E/360" |
| 18 | + ACTACT = "ACT/ACT" |
73 | 19 |
|
74 | 20 | def dcf(self, start: date, end: date) -> float: |
75 | | - return self.act_act_years(end) - self.act_act_years(start) |
76 | | - |
77 | | - def act_act_years(self, dt: date) -> float: |
78 | | - y = dt.year |
79 | | - days_in_year = 365 if y % 4 else 366 |
80 | | - dd = date_diff(dt, date(y, 1, 1)).total_seconds() / 86400 |
81 | | - return y + dd / days_in_year |
| 21 | + """Day count fraction between 2 dates""" |
| 22 | + match self: |
| 23 | + case DayCounter.ACT360: |
| 24 | + return count_days(start, end) / 360.0 |
| 25 | + case DayCounter.ACT365: |
| 26 | + return count_days(start, end) / 365.0 |
| 27 | + case DayCounter.THIRTY360: |
| 28 | + return _thirty_360(start, end) |
| 29 | + case DayCounter.THIRTYE360: |
| 30 | + return _thirty_e360(start, end) |
| 31 | + case DayCounter.ACTACT: |
| 32 | + return _act_act_years(end) - _act_act_years(start) |
| 33 | + case _: |
| 34 | + raise ValueError(f"Unknown day counter: {self}") |
| 35 | + |
| 36 | + |
| 37 | +def count_days(start: date, end: date) -> float: |
| 38 | + """Count the number of days between 2 dates""" |
| 39 | + return date_diff(end, start).total_seconds() / 86400 |
| 40 | + |
| 41 | + |
| 42 | +def _thirty_e360(start: date, end: date) -> float: |
| 43 | + d1 = min(start.day, 30) |
| 44 | + d2 = min(end.day, 30) |
| 45 | + return 360 * (end.year - start.year) + 30 * (end.month - start.month) + d2 - d1 |
| 46 | + |
| 47 | + |
| 48 | +def _thirty_360(start: date, end: date) -> float: |
| 49 | + d1 = min(start.day, 30) |
| 50 | + d2 = min(end.day, 30) if d1 == 30 else end.day |
| 51 | + return 360 * (end.year - start.year) + 30 * (end.month - start.month) + d2 - d1 |
| 52 | + |
| 53 | + |
| 54 | +def _act_act_years(dt: date) -> float: |
| 55 | + y = dt.year |
| 56 | + days_in_year = 365 if y % 4 else 366 |
| 57 | + dd = date_diff(dt, date(y, 1, 1)).total_seconds() / 86400 |
| 58 | + return y + dd / days_in_year |
0 commit comments