def adjust(cls, pydate, calendar=None, convention=None): if not calendar: calendar = cls.TARGET() elif not hasattr(calendar, "adjust"): return None if not convention: convention = BusinessDayConventions.Following qldate = qldate_from_pydate(parse_date(pydate)) try: return pydate_from_qldate(calendar.adjust(qldate, convention)) except: try: return pydate_from_qldate(calendar().adjust(qldate, convention)) except: return None
def advance(cls, pydate, n, timeunit=None, calendar=None, convention=None): """ Advance pydate according the specified calendar and convention :pydate: e.g. 19600809, date(1964, 9, 29), '5-23-1993' :n: integer :timeunit: e.g., enums.TimeUnits.Days usage ----- Note 9/6/1980 is a weekend >>> Calendars.advance(19800906, 1) datetime.date(1980, 9, 8) """ if not calendar: calendar = cls.TARGET() elif not hasattr(calendar, "advance"): return None if not convention: convention = BusinessDayConventions.Following if not timeunit: timeunit = TimeUnits.Days qldate = qldate_from_pydate(parse_date(pydate)) try: return pydate_from_qldate(calendar.advance(qldate, n, timeunit)) except: try: return pydate_from_qldate( calendar().advance(qldate, n, timeunit) ) except: print("failure {}".format(qldate)) return None
def adjust(cls, pydate, calendar=None, convention=None): if not calendar: calendar = cls.TARGET() elif not hasattr(calendar, "adjust"): return None if not convention: convention = BusinessDayConventions.Following qldate = qldate_from_pydate(parse_date(pydate)) try: return pydate_from_qldate(calendar.adjust(qldate, convention)) except: try: return pydate_from_qldate(calendar().adjust( qldate, convention)) except: return None
def advance(cls, pydate, n, timeunit=None, calendar=None, convention=None): """ Advance pydate according the specified calendar and convention :pydate: e.g. 19600809, date(1964, 9, 29), '5-23-1993' :n: integer :timeunit: e.g., enums.TimeUnits.Days usage ----- Note 9/6/1980 is a weekend >>> Calendars.advance(19800906, 1) datetime.date(1980, 9, 8) """ if not calendar: calendar = cls.TARGET() elif not hasattr(calendar, "advance"): return None if not convention: convention = BusinessDayConventions.Following if not timeunit: timeunit = TimeUnits.Days qldate = qldate_from_pydate(parse_date(pydate)) try: return pydate_from_qldate(calendar.advance(qldate, n, timeunit)) except: try: return pydate_from_qldate(calendar().advance( qldate, n, timeunit)) except: print("failure {}".format(qldate)) return None
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 advance(self, date_, convention=Unadjusted, calendar=TARGET(), reverse=False, aspy=True): date_ = qldate_from_pydate(date_) length_ = self.length if not reverse else -self.length date_ = calendar.advance(date_, length_, self.timeunit, convention=convention) return date_ if not aspy else pydate_from_qldate(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_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 schedule(self, settle_, maturity_, convention=Unadjusted, calendar=TARGET(), aspy=True): ''' tenor('3m').schedule(settleDate, maturityDate) or tenor('3m').schedule(settleDate, '10Y') gives a schedule of dates from settleDate to maturity with a short front stub. ''' settle_ = qldate_from_pydate(settle_) mty_ = qldate_from_pydate(maturity_) sched = [] if type(maturity_) == str and not mty_: maturity_ = Tenor(maturity_).advance(settle_, convention=convention, calendar=calendar) else: maturity_ = mty_ dt = maturity_ while dt.serial > settle_.serial: sched.append(calendar.adjust(dt, convention)) dt = self.advance(dt, reverse=True) else: sched.append(settle_) sched.sort(key=lambda dt: dt.serial) if aspy: sched = [pydate_from_qldate(dt) for dt in sched] return sched
def schedule(self, settle_, maturity_, convention=Unadjusted, calendar=TARGET(), aspy=True): ''' tenor('3m').schedule(settleDate, maturityDate) or tenor('3m').schedule(settleDate, '10Y') gives a schedule of dates from settleDate to maturity with a short front stub. ''' settle_ = qldate_from_pydate(settle_) mty_ = qldate_from_pydate(maturity_) sched = [] if type(maturity_) == str and not mty_: maturity_ = Tenor(maturity_).advance(settle_, convention=convention, calendar=calendar ) else: maturity_ = mty_ dt = maturity_ while dt.serial > settle_.serial: sched.append(calendar.adjust(dt, convention)) dt = self.advance(dt, reverse=True) else: sched.append(settle_) sched.sort(key=lambda dt: dt.serial) if aspy: sched = [pydate_from_qldate(dt) for dt in sched] return sched