|
| 1 | +# Day Counters |
| 2 | + |
| 3 | +The `DayCounter` enum provides standard day count conventions used in financial calculations. |
| 4 | + |
| 5 | +## Available conventions |
| 6 | + |
| 7 | +| Value | Description | |
| 8 | +|-------|-------------| |
| 9 | +| `ACT/360` | Actual days over 360 | |
| 10 | +| `ACT/365` | Actual days over 365 | |
| 11 | +| `30/360` | 30-day months over 360 (US/NASD convention) | |
| 12 | +| `30E/360` | 30-day months over 360 (European convention — end date always capped at 30) | |
| 13 | +| `ACT/ACT` | Actual days over actual days in the year | |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +```python |
| 18 | +from ccy import DayCounter |
| 19 | +from datetime import date |
| 20 | + |
| 21 | +start = date(2024, 1, 1) |
| 22 | +end = date(2024, 7, 1) |
| 23 | + |
| 24 | +dc = DayCounter.ACT360 |
| 25 | +print(dc.count(start, end)) # 182.0 |
| 26 | +print(dc.dcf(start, end)) # 0.5055... |
| 27 | +``` |
| 28 | + |
| 29 | +Instantiate from its string value: |
| 30 | + |
| 31 | +```python |
| 32 | +dc = DayCounter("ACT/365") |
| 33 | +print(dc.dcf(start, end)) # 0.4986... |
| 34 | +``` |
| 35 | + |
| 36 | +Iterate over all conventions: |
| 37 | + |
| 38 | +```python |
| 39 | +for dc in DayCounter: |
| 40 | + print(dc.value, dc.dcf(start, end)) |
| 41 | +``` |
| 42 | + |
| 43 | +## Methods |
| 44 | + |
| 45 | +### `count(start, end) -> float` |
| 46 | + |
| 47 | +Returns the number of days between two dates. Accepts both `date` and `datetime` objects. |
| 48 | + |
| 49 | +```python |
| 50 | +from datetime import datetime, timezone |
| 51 | + |
| 52 | +start = datetime(2024, 1, 1, 9, 0, tzinfo=timezone.utc) |
| 53 | +end = datetime(2024, 1, 2, 15, 0, tzinfo=timezone.utc) |
| 54 | + |
| 55 | +DayCounter.ACT360.count(start, end) # 1.25 |
| 56 | +``` |
| 57 | + |
| 58 | +### `dcf(start, end) -> float` |
| 59 | + |
| 60 | +Returns the day count fraction — the period length expressed as a fraction of a year, according to the convention. |
| 61 | + |
| 62 | +```python |
| 63 | +start = date(2024, 1, 1) |
| 64 | +end = date(2025, 1, 1) |
| 65 | + |
| 66 | +DayCounter.ACT365.dcf(start, end) # 1.0027... (366 days / 365) |
| 67 | +DayCounter.ACT360.dcf(start, end) # 1.0166... (366 days / 360) |
| 68 | +DayCounter.ACTACT.dcf(start, end) # 1.0 (spans exactly one year) |
| 69 | +DayCounter.THIRTY360.dcf(start, end) # 360.0 (30/360 raw result) |
| 70 | +``` |
0 commit comments