def __init__(self, maturity_date=None, coupon=None, issue_date=None, tenor=None, cf=None, name=None): self.name = name self.coupon = coupon self.cf = cf self.maturity_date = to_datetime(maturity_date) if tenor is not None: self._issue_date = issue_date self.tenor = tenor else: self._tenor = tenor self.issue_date = self.holiday_cal.next_b_day(issue_date, 0)
def _price_yield_setup(self, settle_date, tplus=0): """ Does set up work :param settle_date: :return: """ maturity_date = self.maturity_date issue_date = self.issue_date coupon = self.coupon if maturity_date is None: return None, None, None, None if tplus == 0: settle_date = to_datetime(settle_date) else: settle_date = self.holiday_cal.next_b_day(settle_date, tplus) if settle_date >= maturity_date: return None, None, None, None if issue_date is not None and settle_date < issue_date: settle_date = issue_date coupon *= 100 cash_flows = ust_get_cash_flow(settle_date, maturity_date) cf0, cf1 = cash_flows[:2] cash_flow_count = len(cash_flows) - 1 accrual_time = float((settle_date - cf0).days) / float((cf1 - cf0).days) accrued_interest = coupon * accrual_time / 2 cf_times = [1 - accrual_time + i for i in xrange(cash_flow_count)] cf_times.append(cf_times[-1]) cf_values = [coupon / 2 for _ in xrange(cash_flow_count)] cf_values.append(100) price_fun = lambda ytm: sum(cf * (1 + ytm / 2) ** -t for cf, t in zip(cf_values, cf_times)) return accrued_interest, cf_times, cf_values, price_fun
def maturity_date(self, today): self._maturity_date = to_datetime(today)