Skip to content

Commit f7785c9

Browse files
committed
Better day counters
1 parent 0fb8099 commit f7785c9

4 files changed

Lines changed: 75 additions & 1 deletion

File tree

docs/daycounters.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
```

docs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ eur = ccy.currency("aud")
2323
eur.printinfo()
2424
```
2525

26-
a currency object has the following properties:
26+
A currency object has the following properties:
27+
2728
* *code*: the [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) three letters code.
2829
* *twoletterscode*: two letter code.
2930
* *default_country*: the default [ISO 3166-1 alpha_2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) country code for the currency.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ requires-python = ">=3.11"
99
dependencies = [
1010
"python-dateutil>=2.9.0",
1111
"pycountry>=24.6.1",
12+
"typing-extensions>=4.12",
1213
]
1314

1415
[project.optional-dependencies]

uv.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)