A Python library for financial date calculations, business calendars, and day count conventions
Project description
Calfi
A Python library for financial date calculations, business calendars, and day count conventions.
Installation
Install Calfi using pip:
pip install calfi
Or install from source:
git clone <repository-url>
cd calfi
pip install -e .
Features
- Business Calendars: Define custom business calendars with holidays and weekends.
- Predefined Calendars: Ready-to-use calendars like TARGET and US Government calendars.
- Day Count Conventions: Support for ACT/360, ACT/365, ACT/ACT, and 30/360 day count methods.
- Business Day Conventions: Adjust dates according to Following, Modified Following, Preceding, Modified Preceding, and End of Month rules.
- Frequency Calculations: Handle periodic date calculations for financial instruments.
Usage
Basic Business Calendar
from datetime import date
from calfi import BusinessCalendar, Target
# Create a custom calendar or use predefined
calendar = Target()
# Check if a date is a business day
dt = date(2023, 12, 25) # Christmas
is_business = calendar.is_business_day(dt) # False
# Find next business day
next_business = calendar.next_business_day(date(2023, 12, 25))
print(next_business) # 2023-12-26
Creating Custom Holidays
Calfi provides helper functions to easily define custom holidays for your business calendars. These functions return callable objects that can be passed to the BusinessCalendar constructor.
from calfi.fin_cal import BusinessCalendar, fixed_day, nth_weekday_of_month, last_weekday_of_month, us_dayoff_adjust, easter_sunday_delta
# Fixed date holidays
new_years = fixed_day(1, 1) # January 1st every year
christmas = fixed_day(12, 25) # December 25th
# Nth weekday of month (e.g., third Monday in January)
mlk_day = nth_weekday_of_month(1, 0, 3) # 0 = Monday
# Last weekday of month (e.g., last Monday in May)
memorial_day = last_weekday_of_month(5, 0)
# Easter-related holidays
good_friday = easter_sunday_delta(-2) # 2 days before Easter Sunday
easter_monday = easter_sunday_delta(1) # 1 day after Easter Sunday
# US-style holiday adjustment (moves weekend holidays to Monday)
adjusted_christmas = us_dayoff_adjust(christmas)
# Create custom calendar
custom_calendar = BusinessCalendar(days_off=[new_years, adjusted_christmas, mlk_day, memorial_day, good_friday, easter_monday])
Day Count Conventions
from calfi import DayCountConvention
start = date(2023, 1, 1)
end = date(2023, 1, 15)
days = DayCountConvention.ACT_360.days(start, end)
print(days) # 14
denom = DayCountConvention.ACT_360.denominator()
print(denom) # 360
Business Day Adjustments
from calfi import BusinessDayConvention
dt = date(2023, 12, 25) # Sunday
adjusted = BusinessDayConvention.FOLLOWING.adjust_date(dt, calendar)
print(adjusted) # Next business day
API Reference
Classes
BusinessCalendar: Base class for business calendars.Target: TARGET calendar (European banking days).USGovt: US Government calendar.DayCountConvention: Enum for day count methods.BusinessDayConvention: Enum for business day adjustment rules.Frequency: Enum for payment frequencies.
Methods
BusinessCalendar.is_business_day(date): Check if date is a business day.BusinessCalendar.next_business_day(date, included=True): Get next business day.BusinessCalendar.previous_business_day(date, included=True): Get previous business day.DayCountConvention.days(start, end): Calculate days between dates.DayCountConvention.denominator(year=None): Get denominator for fraction.BusinessDayConvention.adjust_date(date, calendar): Adjust date per convention.Frequency.next_date(base_date): Calculate next date per frequency.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file calfi-1.0.0.tar.gz.
File metadata
- Download URL: calfi-1.0.0.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
afa8c847be672b7f9eec9e2b710ed5d3891a311b0727cf8a4d199bb656532a00
|
|
| MD5 |
bbd01a05fe0309eeeda3dc7be44379f3
|
|
| BLAKE2b-256 |
642d900c23189ff2f8f66c674d08cc471cdde7a3f5fb700439381d4b72545b40
|
File details
Details for the file calfi-1.0.0-py3-none-any.whl.
File metadata
- Download URL: calfi-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8df8d17262eb2ef39984f6faaeeac31dc9acbf31ac5b43f63c6e4c3641aff91
|
|
| MD5 |
aaa80aba4c96adcec68977e48dd900f9
|
|
| BLAKE2b-256 |
2e9687c48172270a2dd52189fe4a865d7d3d95248a723bc68245a35035b1687a
|