def GenerateCashFlows(self, now=None): """ Generate the cash flow vector. TODO: Optimise this so that recalculation only occurs if 'now' changes. :param now: float :return: None """ if now is None: now = self.Now # TODO: Do an optimisation to skip recalculation. self.Now = now if self.Now is None: raise ValueError('Must set ' 'now' ' to calculate cash flows.') # deal with corner case of being beyond maturity date. if self.Now >= self.Maturity: self.CashFlows = [] self.CashFlowDates = [] return # Generate the time axis with create_grid. The trick is that create_grid aligns the # grid to the start date. We need to align the grid to maturity. We accomplish this by # multiplying by -1. (Try doing that with calendar dates!) minus_t = create_grid(-self.Maturity, -self.Now, self.CouponFrequency) # Go back to positive time... self.CashFlowDates = [-x for x in minus_t] self.CashFlowDates.reverse() # if on top of a payment, pop it out. if self.Now == self.CashFlowDates[0]: self.CashFlowDates.pop(0) coupon_payment = self.PriceBase * self.Coupon / self.CouponFrequency # This creates an empty list if we only have a single payment self.CashFlows = [coupon_payment] * (len(self.CashFlowDates) - 1) self.CashFlows.append(self.PriceBase + coupon_payment)
try: from simplepricers.utils import create_grid import simplepricers.yieldcalculations as yc import simplepricers.bonds_curves as bonds except ImportError: print( 'If these imports fail, put the base directory above onto the PYTHONPATH' ) raise from plot_for_examples import Quick2DPlot # ------------------------------------------------------ # price/yield for consols vs. coupon bond yield_grid = create_grid(.001, .05, 1000) consol = bonds.Consol(.05) consol_price = [ None, ] * len(yield_grid) # 5% 10-year annual coupon bond cpn = bonds.CouponBond(10., .05, 1) coupon_price = [ None, ] * len(yield_grid) for i in range(0, len(yield_grid)): consol_price[i] = consol.GetPrice(yield_grid[i]) coupon_price[i] = cpn.GetPrice(yield_grid[i], price_type='dirty') obj = Quick2DPlot(yield_grid, consol_price,
def test_create_grid_5(self): self.assertEqual(create_grid(100., 105., 1), [100., 101., 102., 103., 104., 105.])
def test_create_grid_4(self): self.assertEqual(utils.create_grid(.3, 1.3, 2), [0.3, .8, 1.3])
def test_create_grid_3(self): self.assertEqual(utils.create_grid(0., 1., 2), [0., .5, 1.])
def test_create_grid2(self): self.assertEqual(utils.create_grid(0., .99, 1), [ 0., ])
""" try: from simplepricers.utils import create_grid import simplepricers.yieldcalculations as yc except ImportError: print('If these imports fail, put the base directory above onto the PYTHONPATH') raise from plot_for_examples import Quick2DPlot # ------------------------------------------------------ # Show discount factors t = create_grid(0., 10., 1) # Create flat zero curve r = [0.05, ] * len(t) df = yc.DF(t, r) Quick2DPlot(t, df, 'Discount Factor For Flat 5% Curve') # -------------------------------------------------------- print('Second example: calculate the NPV of a 2-year bond (4% annual coupon)') # Time axis t = create_grid(1., 2., 1) # 4% zero rate r = [.04, .04] df = yc.DF(t, r) CF = [4., 104.] NPV = 0.