def test_settings_instance_method(self): Settings.instance().evaluation_date = date(2012, 6, 21) self.assertEquals( date(2012, 6, 21), Settings.instance().evaluation_date )
def test_creation(self): # Market information settings = Settings(calendar = TARGET()) calendar = settings.calendar settlement_date = Date(18, September, 2008) # must be a business day settlement_date = calendar.adjust(settlement_date); # must be a business day settings.evaluation_date = pydate_from_qldate( calendar.advance(settlement_date, -2, Days) ) quotes = [0.0096, 0.0145, 0.0194] tenors = [3, 6, 12] rate_helpers = [] deposit_day_counter = Actual365Fixed() convention = ModifiedFollowing end_of_month = True for quote, month in zip(quotes, tenors): tenor = Period(month, Months) fixing_days = 3 helper = DepositRateHelper( quote, tenor, fixing_days, calendar, convention, end_of_month, deposit_day_counter ) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = term_structure_factory( 'discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance ) self.assertIsNotNone(ts) self.assertEquals( Date(18, September, 2008), ts.reference_date) # this is not a real test ... self.assertAlmostEquals(0.9975, ts.discount(Date(21, 12, 2008)), 4)
def test_create_libor_index(self): settings = Settings(calendar = TARGET()).instance() calendar = settings.calendar eval_date = qldate_from_pydate(settings.today()) settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date); index = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360()) self.assertEquals('USD Libor6M Actual/360', index.name)
def test_creation(self): # Market information settings = Settings(calendar=TARGET()) calendar = settings.calendar settlement_date = Date(18, September, 2008) # must be a business day settlement_date = calendar.adjust(settlement_date) # must be a business day settings.evaluation_date = pydate_from_qldate( calendar.advance(settlement_date, -2, Days)) quotes = [0.0096, 0.0145, 0.0194] tenors = [3, 6, 12] rate_helpers = [] deposit_day_counter = Actual365Fixed() convention = ModifiedFollowing end_of_month = True for quote, month in zip(quotes, tenors): tenor = Period(month, Months) fixing_days = 3 helper = DepositRateHelper(quote, tenor, fixing_days, calendar, convention, end_of_month, deposit_day_counter) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = term_structure_factory('discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) self.assertIsNotNone(ts) self.assertEquals(Date(18, September, 2008), ts.reference_date) # this is not a real test ... self.assertAlmostEquals(0.9975, ts.discount(Date(21, 12, 2008)), 4)
def test_using_settings(self): settings = Settings() ql_date_today = Date(12, January, 2012) evaluation_date = pydate_from_qldate(ql_date_today) # have to set the evaluation date before the test as it is a global # attribute for the whole library ... meaning that previous test_cases # might have set this to another date settings.evaluation_date = evaluation_date self.assertTrue( evaluation_date == settings.evaluation_date ) self.assertTrue(settings.version.startswith('1'))
def test_gamma(self): ''' Hull: ex. 14.7 ''' Settings().evaluation_date = 20120731 optn = O.EquityOption(300., 20121201, .03) ov = optn.calc(.25, 305, .08) self.assertAlmostEqual(optn.greeks['gamma'], .00857, 5)
def test_optionvalue1(self): ''' Hull: ex. 12.2 ''' Settings().evaluation_date = 20120928 optn = O.EquityOption(300., 20121201, .03) ov = optn.calc(.2, 310., .08) self.assertEqual(date(2012, 10, 1), optn.settlementDate) self.assertAlmostEqual(ov, 17.28, 2)
def test_deposit_swap(self): # Market information settings = Settings(calendar=TARGET()) calendar = settings.calendar # must be a business day eval_date = qldate_from_pydate(settings.today()) settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) depositData = [[1, Months, 4.581], [2, Months, 4.573], [3, Months, 4.557], [6, Months, 4.496], [9, Months, 4.490]] swapData = [[1, Years, 4.54], [5, Years, 4.99], [10, Years, 5.47], [20, Years, 5.89], [30, Years, 5.96]] rate_helpers = [] end_of_month = True for m, period, rate in depositData: tenor = Period(m, Months) helper = DepositRateHelper(rate / 100, tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) liborIndex = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360()) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, rate in swapData: rate = SimpleQuote(rate / 100) helper = SwapRateHelper(rate, Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex, spread, fwdStart) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = term_structure_factory('discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) # this is not a real test ... self.assertAlmostEquals( 0.9103, ts.discount(calendar.advance(today(), 2, Years)), 3) self.assertAlmostEquals( 0.7836, ts.discount(calendar.advance(today(), 5, Years)), 3) self.assertAlmostEquals( 0.5827, ts.discount(calendar.advance(today(), 10, Years)), 3) self.assertAlmostEquals( 0.4223, ts.discount(calendar.advance(today(), 15, Years)), 3)
def test_deposit_swap(self): # Market information settings = Settings(calendar = TARGET()) calendar = settings.calendar # must be a business day eval_date = qldate_from_pydate(settings.today()) settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date); depositData = [[ 1, Months, 4.581 ], [ 2, Months, 4.573 ], [ 3, Months, 4.557 ], [ 6, Months, 4.496 ], [ 9, Months, 4.490 ]] swapData = [[ 1, Years, 4.54 ], [ 5, Years, 4.99 ], [ 10, Years, 5.47 ], [ 20, Years, 5.89 ], [ 30, Years, 5.96 ]] rate_helpers = [] end_of_month = True for m, period, rate in depositData: tenor = Period(m, Months) helper = DepositRateHelper(rate/100, tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) liborIndex = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360()) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, rate in swapData: rate = SimpleQuote(rate/100) helper = SwapRateHelper(rate, Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex, spread, fwdStart) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = term_structure_factory( 'discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) # this is not a real test ... self.assertAlmostEquals(0.9103, ts.discount(calendar.advance(today(), 2, Years)),3) self.assertAlmostEquals(0.7836, ts.discount(calendar.advance(today(), 5, Years)),3) self.assertAlmostEquals(0.5827, ts.discount(calendar.advance(today(), 10, Years)),3) self.assertAlmostEquals(0.4223, ts.discount(calendar.advance(today(), 15, Years)),3)