def __init__(self, timeseries, *args, **kwargs): super().__init__(timeseries) self.base_currency = to_ql_currency(self.ts_attributes[BASE_CURRENCY]) self.counter_currency = to_ql_currency( self.ts_attributes[COUNTER_CURRENCY]) self.maturity_date = to_ql_date(self.ts_attributes[MATURITY_DATE]) self.quotes = self.timeseries.price.ts_values self.calendar = to_ql_calendar(self.ts_attributes[CALENDAR]) self.base_rate_day_counter = to_ql_day_counter( self.ts_attributes[BASE_RATE_DAY_COUNTER]) self.counter_rate_day_counter = to_ql_day_counter( self.ts_attributes[COUNTER_RATE_DAY_COUNTER]) self.base_rate_compounding = to_ql_compounding( self.ts_attributes[BASE_RATE_COMPOUNDING]) self.counter_rate_compounding = to_ql_compounding( self.ts_attributes[COUNTER_RATE_COMPOUNDING]) self.base_rate_frequency = to_ql_frequency( self.ts_attributes[BASE_RATE_FREQUENCY]) self.counter_rate_frequency = to_ql_frequency( self.ts_attributes[COUNTER_RATE_FREQUENCY]) self.settlement_days = int(self.ts_attributes[SETTLEMENT_DAYS]) self.base_currency_future = BaseCurrencyFuture( self.base_currency, self.counter_currency, self.maturity_date, self.calendar, self.base_rate_day_counter, self.base_rate_compounding, self.base_rate_frequency, self.counter_rate_day_counter, self.counter_rate_compounding, self.counter_rate_frequency, self.settlement_days)
def nth_weekday_of_month(start_year, n_years, frequency, weekday, nth_day, min_date=None, max_date=None): """ Function to get a list of dates following a specific weekday at a specific recurrence inside a month example: the 3rd friday of the month, every 3 months. :param start_year: int The base year for the date calculation :param n_years: The amount of years ahead to forecast :param frequency: str The frequency of the monthly occurrence, ex: ANNUAL, SEMIANNUAL, EVERY_FOUR_MONTH, QUARTERLY, BIMONTHLY, MONTHLY" :param weekday: str The weekday of the recurrence, Monday, Tuesday... :param nth_day: int The nth occurrence inside of the month. :param min_date: Date-like, optional The minimum date of the list :param max_date: Date-like, optional The minimum date of the list :return: list of QuantLib.Dates """ frequency = str(frequency).upper() ql_frequency = to_ql_frequency(frequency) if not 0 < ql_frequency <= 12: raise ValueError( "Only supported frequencies are: ANNUAL, SEMIANNUAL, EVERY_FOUR_MONTH," " QUARTERLY, BIMONTHLY, MONTHLY") weekday = str(weekday).upper() ql_weekday = to_ql_weekday(weekday) nth_day = int(nth_day) dates = list() for j in range(n_years + 1): year = start_year + j for i in range(ql_frequency): month = int((i + 1) * (12 / ql_frequency)) dates.append(ql.Date.nthWeekday(nth_day, ql_weekday, month, year)) if min_date is not None and max_date is None: min_date = to_ql_date(min_date) dates = [date for date in dates if date >= min_date] elif min_date is None and max_date is not None: max_date = to_ql_date(max_date) dates = [date for date in dates if date <= max_date] elif min_date is not None and max_date is not None: min_date = to_ql_date(min_date) max_date = to_ql_date(max_date) dates = [date for date in dates if min_date <= date <= max_date] return dates
def __init__(self, timeseries, first_cc): super().__init__(timeseries) self.first_cc = first_cc self.calendar = to_ql_calendar(self.attributes[CALENDAR]) try: self._tenor = ql.PeriodParser.parse(self.attributes[TENOR_PERIOD]) except KeyError: # If the deposit rate has no tenor, it must have a maturity. self._maturity = to_ql_date(to_datetime(self.attributes[MATURITY_DATE])) self.day_counter = to_ql_day_counter(self.attributes[DAY_COUNTER]) self.compounding = to_ql_compounding(self.attributes[COMPOUNDING]) self.frequency = to_ql_frequency(self.attributes[FREQUENCY]) self.business_convention = to_ql_business_convention(self.attributes[BUSINESS_CONVENTION]) self.fixing_days = int(self.attributes[FIXING_DAYS])
def __init__(self, timeseries, is_deposit_rate=False, calculate_convexity=False, telescopic_value_dates=False): super().__init__(timeseries=timeseries) # Class Flags self.is_deposit_rate = is_deposit_rate self.calculate_convexity = calculate_convexity self.telescopic_value_dates = telescopic_value_dates # Database Attributes try: self._tenor = ql.PeriodParser.parse( self.ts_attributes[TENOR_PERIOD]) except KeyError: self._tenor = ql.PeriodParser.parse( self.ts_attributes[INDEX_TENOR]) try: self.issue_date = to_ql_date( to_datetime(self.ts_attributes[ISSUE_DATE])) except KeyError: self.issue_date = DEFAULT_ISSUE_DATE self.compounding = to_ql_compounding(self.ts_attributes[COMPOUNDING]) self.frequency = to_ql_frequency(self.ts_attributes[FREQUENCY]) self.currency = to_ql_currency(self.ts_attributes[CURRENCY]) # Other Interest Rate Class start from here self._index_tenor = None self._maturity = None # QuantLib Objects self.term_structure = ql.RelinkableYieldTermStructureHandle() self.index = None # QuantLib Attributes self.calendar = None self.day_counter = None self.business_convention = None self.fixing_days = None self.month_end = None self.interest_maturity_date = None # Rate Helper self.helper_rate = None self.helper_convexity = None # Defined self._rate_helper = None
def update_spread_from_timeseries(self, date, last_available=True): date = to_ql_date(date) self.spreads[date] = dict() for ts in self.ts_collection: spread = ts.get_values(date, last_available=last_available) calendar = to_ql_calendar(ts.ts_attributes[CALENDAR]) try: spread_date_or_tenor = calendar.advance( date, ql.PeriodParser.parse(ts.ts_attributes[TENOR_PERIOD])) except AttributeError: spread_date_or_tenor = calendar.adjust( to_ql_date(ts.ts_attributes[MATURITY_DATE])) day_counter = to_ql_day_counter(ts.ts_attributes[DAY_COUNTER]) compounding = to_ql_compounding(ts.ts_attributes[COMPOUNDING]) frequency = to_ql_frequency(ts.ts_attributes[FREQUENCY]) self.spreads[date][spread_date_or_tenor] = ql.InterestRate( spread, day_counter, compounding, frequency)
def __init__(self, timeseries): super().__init__(timeseries) self._tenor = ql.PeriodParser.parse(self.ts_attributes[TENOR_PERIOD]) self.calendar = to_ql_calendar(self.ts_attributes[CALENDAR]) self.day_counter = to_ql_day_counter(self.ts_attributes[DAY_COUNTER]) self.business_convention = to_ql_business_convention( self.ts_attributes[BUSINESS_CONVENTION]) self.fixing_days = int(self.ts_attributes[FIXING_DAYS]) self.date_generation = to_ql_date_generation( self.ts_attributes[DATE_GENERATION]) self.coupon_frequency = ql.Period( to_ql_frequency(self.ts_attributes[FREQUENCY])) self.recovery_rate = float(self.ts_attributes[RECOVERY_RATE]) self.coupon = float(self.ts_attributes[COUPONS]) self.base_yield = self.ts_attributes[BASE_SPREAD_TAG] try: self.first_accrual_date = to_ql_date( self.ts_attributes[FIRST_ACCRUAL_DATE]) except KeyError: self.first_accrual_date = None self.month_end = False # Rate Helper self.helper_rate = ql.SimpleQuote(0)