def __init__(self, val_date: Date, t: Periods, nominal, startDate, matDate, curve_irr, curve_spread, coupon, frequency, base="ACT/365", calendar=Calendar(), **kwargs): super().__init__(val_date=val_date, t=t, nominal=nominal, startDate=startDate, matDate=matDate, curve_irr=curve_irr, curve_spread=curve_spread, base=base, calendar=calendar, **kwargs) if (not isinstance(frequency, int) or frequency < 1): raise ValueError( "frequency must be an integer greater than 0. Consider the BondZeroCoupon pricer" ) self.coupon = coupon self.frequency = int(12 / frequency) self.c_dates = self.couponPayment()[1:] self.first = self.couponPayment()[0]
def __init__(self, val_date: Date, t: Periods, nominal, startDate, matDate, curve_irr, curve_spread, coupon, frequency, base="ACT/365", calendar=Calendar(), **kwargs): super().__init__(val_date=val_date, t=t, nominal=nominal, startDate=startDate, matDate=matDate, curve_irr=curve_irr, curve_spread=curve_spread, base=base, calendar=calendar, coupon=coupon, frequency=frequency, **kwargs) self.coupon = max(self.proxyCoupon(), 0)
def __init__(self, val_date: Date, t: Periods, nominal, startDate, matDate, curve_irr, coupon, frequency, index, curve_index: Curve, curve_spread=NullCurve(), base="BUSS/252", calendar=Calendar(), **kwargs): super().__init__(val_date=val_date, t=t, nominal=nominal, startDate=startDate, matDate=matDate, curve_irr=curve_irr, curve_spread=curve_spread, coupon=coupon, frequency=frequency, base=base, calendar=calendar, index=1, curve_index=curve_index, **kwargs) self.coupon = max(self.proxyCoupon(), 0)
def __init__(self, val_date: Date, t: Periods, nominal, startDate, matDate, curve_irr, curve_spread, base="ACT/365", calendar=Calendar(), **kwargs): if not isinstance(calendar, Calendar): raise ValueError("calendar must be a calendar class") if not isinstance(startDate, Date) or not isinstance(matDate, Date): raise ValueError("startDate and matDate must be Date classes") if not isinstance(curve_irr, Curve) or not isinstance( curve_spread, Curve): raise ValueError( "curve_irr and curve_spread must be Curve classes") super().__init__(val_date=val_date, t=t, **kwargs) self.nominal = nominal self.startDate = startDate self.matDate = matDate self.base = get_base(base) self.curve_irr = curve_irr self.curve_spread = curve_spread self.calendar = calendar
def PBO(self): curve = self.curve_irr + self.curve_spread m = self.val_date.month() inf = self.curve_inf.rate(self.val_date + Days(self.duration))[0] r = curve.rate(self.val_date + Days(self.duration))[0] u_i = round_d(round_up(inf * 4) / 4, 4) l_i = round_d(round_down(inf * 4) / 4, 4) l = len(self.dates) d = 0 td = [0] * l if inf <= self.RLPI.RPI.max(): inf_a = (inf - l_i) / 0.0025 * (float(self.RLPI['LPI-A'][self.RLPI.RPI == u_i]) - float(self.RLPI['LPI-A'][self.RLPI.RPI == l_i])) + \ float(self.RLPI['LPI-A'][self.RLPI.RPI == l_i]) else: inf_a = self.RLPI['LPI-A'].max() if inf <= self.RLPI.RPI.max(): inf_d = (inf - l_i) / 0.0025 * (float(self.RLPI['LPI-D'][self.RLPI.RPI == u_i]) - float(self.RLPI['LPI-D'][self.RLPI.RPI == l_i])) + \ float(self.RLPI['LPI-D'][self.RLPI.RPI == l_i]) else: inf_d = self.RLPI['LPI-D'].max() if inf <= self.RLPI.RPI.max(): inf_p = (inf - l_i) / 0.0025 * (float(self.RLPI['LPI-P'][self.RLPI.RPI == u_i]) - float(self.RLPI['LPI-P'][self.RLPI.RPI == l_i])) + \ float(self.RLPI['LPI-P'][self.RLPI.RPI == l_i]) else: inf_p = self.RLPI['LPI-P'].max() for i in range(0, l): td[i] = Thirty360.yearFraction(self.dates[0], self.dates[i] + Years(1), Calendar()) for i in range(0, l - 1): d += ((((12 - m) / 12) * self.active[i] * ((1 + inf_a) ** td[i])) + ((m / 12) * self.active[i + 1] * ((1 + inf_a) ** td[i + 1]))) / \ ((1 + r) ** (td[i] - 0.5)) d += ((((12 - m) / 12) * self.deferred[i] * ((1 + inf_d) ** td[i])) + ((m / 12) * self.deferred[i + 1] * ((1 + inf_d) ** td[i + 1]))) / \ ((1 + r) ** (td[i] - 0.5)) d += ((((12 - m) / 12) * self.pensioner[i] * ((1 + inf_p) ** td[i])) + ((m / 12) * self.pensioner[i + 1] * ((1 + inf_p) ** td[i + 1]))) / \ ((1 + r) ** (td[i] - 0.5)) d += (((12 - m) / 12) * self.active[l - 1] * ((1 + inf_a) ** td[l - 1])) / \ ((1 + r) ** td[l - 1]) d += (((12 - m) / 12) * self.deferred[l - 1] * ((1 + inf_d) ** td[l - 1])) / \ ((1 + r) ** td[l - 1]) d += (((12 - m) / 12) * self.pensioner[l - 1] * ((1 + inf_p) ** td[l - 1])) / \ ((1 + r) ** td[l - 1]) return d
def __init__(self, name, dates, rates, compounding=compounded, base="ACT/360", interpolator=linear, calendar=Calendar()): if len(dates) != len(rates): raise ValueError("Dates and Rates must have the same length") if not isinstance(dates, list): raise ValueError("Dates must be a list") if not isinstance(rates, list): raise ValueError("Rates must be a list") self.name = name self.dates = dates self.rates = rates self.base = get_base(base) self.compounding = compounding self.interpolator = interpolator self.calendar = calendar
def duration(self): irr = self.curve_irr.rate(self.dates) spread = self.curve_spread.rate(self.dates) l = len(self.dates) d = [0] * l td = [0] * l num = 0 den = 0 for i in range(0, l): td[i] = Actual365.yearFraction(self.val_date, self.dates[i], Calendar()) for i in range(0, l): d[i] = self.flows[i] / ((1 + irr[i] + spread[i])**td[i]) for i in range(0, l): num += (d[i] * td[i]) for i in range(0, l): den += d[i] return num / den
def PBO(self): curve = self.curve_irr + self.curve_spread m = self.val_date.month() inf = self.curve_inf.rate(self.val_date + Days(self.duration))[0] r = curve.rate(self.val_date + Days(self.duration))[0] l = len(self.dates) d = 0 td = [0] * l for i in range(0, l): td[i] = Thirty360.yearFraction(self.dates[0], self.dates[i] + Years(1), Calendar()) for i in range(0, l - 1): d += ((((12 - m) / 12) * self.flows[i] * (( 1 + inf) ** td[i])) + \ ((m / 12) * self.flows[i + 1] * ((1 + inf) ** td[i + 1]))) / \ ((1 + r) ** (td[i] - 0.5)) d += (((12 - m) / 12) * self.flows[l - 1] * (( 1 + inf) ** td[l - 1])) / \ ((1 + r) ** td[l - 1]) return d
class Brazil(Calendar): def __init__(self): super(Brazil, self).__init__() def isHoliday(self, date): if super(Brazil, self).isHoliday(date): return True for i in ANBIMA: if (date == i): return True return False def businessDaysBetween(self, date_from: Date, date_to: Date): d = super().businessDaysBetween(date_from, date_to) i = 0 while ANBIMA[i] < date_from: i += 1 j = i while ANBIMA[j] < date_to: j += 1 return d - (j - i) if __name__ == "__main__": gen = Calendar() cal = Brazil() print(cal.businessDaysBetween(Date(5, 1, 2018), Date(6, 1, 2020))) print(cal.businessDaysBetween2(Date(5, 1, 2018), Date(6, 1, 2020))) print(Date(1, 1, 2001).weekday())