Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added datasets/data/CDI.xlsx
Binary file not shown.
45 changes: 45 additions & 0 deletions finmath/RateUtils/RateUtils.py
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)

Copy link
Copy Markdown
Member

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


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__":

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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)
Empty file added finmath/RateUtils/__init__.py
Empty file.