-
Notifications
You must be signed in to change notification settings - Fork 142
Set Up Accrual Methods #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import numpy as np | ||
| import pandas as pd | ||
|
|
||
| from calendars import DayCounts | ||
|
|
||
|
|
||
| class RateUtils: | ||
|
|
||
| accrual_methods = ['BUS', 'ACT'] | ||
|
|
||
| def __init__(self, dc='BUS/252', calendar='anbima'): | ||
| self.dc = DayCounts(dc, calendar=calendar) | ||
| self.accrual_methods_map = { | ||
| 'BUS': self.__accrual_bus, | ||
| 'ACT': self.__accrual_act | ||
| } | ||
|
|
||
| @staticmethod | ||
| def __accrual(rates): | ||
| return (1 + rates).cumprod() | ||
|
|
||
| def __accrual_act(self, rates, dates): | ||
| n_days = [self.dc.days(t0, t1) for t0, t1 in zip(dates[1:], dates[:-1])] | ||
| accrual = n_days * (rates/100)/365 | ||
| return self.__accrual(accrual) | ||
|
|
||
| def __accrual_bus(self, rates, dates): | ||
| return self.__accrual((rates/100)/252) | ||
|
|
||
| def accrual(self, rates: pd.Series, dates: pd.Series, method: str): | ||
| try: | ||
| return self.accrual_methods_map[method](rates, dates) | ||
| except KeyError: | ||
| print(f'Accrual method: {method} is not supported! Supported values: {RateUtils.accrual_methods}') | ||
| raise | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normally, we leave the classes in a separate file and create an example file (see for example the bloomberg api folder). Also, if you are interested, you can use the FiananceHub API to grab the CDI series directly from the BACEN website. This would be way cooler as an a example file. |
||
| cdi = pd.read_excel('../../datasets/data/CDI.xlsx') | ||
| # reverse CDI | ||
| cdi = cdi[::-1].reset_index() | ||
| utils = RateUtils() | ||
|
|
||
| acc = utils.accrual(cdi['CDI (taxa)'], cdi['Date'], 'BUS') | ||
| print(acc) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not think this is correct. It seems like you are dividing the rates by 252 but the BUS accrual by anbima is exponencial, a day's return is given by (1 + rate) ** (1/252) -1 not rate/252 like you have in ACT. See file attached:
Interest-Rate-Instruments-and-Market-Conventions.pdf