def update_curves(self, dates): """ Update ``self.yield_curves`` with the yield curves of each date in `dates`. Parameters ---------- dates: list of QuantLib.Dates """ dates = to_list(dates) for date in dates: date = to_ql_date(date) ql.Settings.instance().evaluationDate = date helpers_dict = self._get_helpers(date) # Instantiate the curve helpers = [ndhelper.helper for ndhelper in helpers_dict.values()] # Just bootstrapping the nodes hazard_curve = ql.PiecewiseFlatHazardRate(date, helpers, self.day_counter) hazard_dates = hazard_curve.dates() hazard_rates = [ hazard_curve.hazardRate(h_date) for h_date in hazard_dates ] hazard_curve = ql.HazardRateCurve(hazard_dates, hazard_rates, self.day_counter, self.calendar) hazard_curve.enableExtrapolation() self.hazard_curves[date] = hazard_curve
def update_curves(self, dates): dates = to_list(dates) for date in dates: date = to_ql_date(date) ql.Settings.instance().evaluationDate = date spread_dict = dict() if self.spreads_ts_collection is not None: for ts in self.spreads_ts_collection: maturity = ts.maturity(date=date) rate = ts.spread_rate(date=date).equivalentRate( self.day_counter, self.compounding, self.frequency, date, maturity).rate() spread_dict[maturity] = ql.SimpleQuote(rate) elif self.spreads is not None: # passing the dict with dates and simple quotes directly. spread_dict = self.spreads date_list = list() spread_list = list() for maturity in sorted(spread_dict.keys()): date_list.append(maturity) spread_list.append(ql.QuoteHandle(spread_dict[maturity])) curve_handle = ql.YieldTermStructureHandle( self.yield_curve_time_series.yield_curve(date=date)) spread_curve = ql.SpreadedLinearZeroInterpolatedTermStructure( curve_handle, spread_list, date_list, self.compounding, self.frequency, self.day_counter) spread_curve.enableExtrapolation() self.spreaded_curves[date] = spread_curve
def update_curves(self, dates): """ Update ``self.yield_curves`` with the yield curves of each date in `dates`. Parameters ---------- dates: QuantLib.Date or list of QuantLib.Date The curve dates to be interpolated """ dates = to_list(dates) for date in dates: date = to_ql_date(date) ql.Settings.instance().evaluationDate = date helpers_dict = self._get_helpers(date) # Instantiate the curve helpers = [ndhelper.helper for ndhelper in helpers_dict.values()] # Bootstrapping the nodes yield_curve = to_ql_piecewise_curve( helpers=helpers, calendar=self.calendar, day_counter=self.day_counter, curve_type=self.curve_type, constraint_at_zero=self.constraint_at_zero) # Here you can choose if you want to use the curve linked to the helpers or a frozen curve. # Freezing the curve is needed when you are changing exclusively the global evaluation date, # QuantLib helpers don't understand that only the evaluation date is changing and end up using # the stored spot rates instead of the implied forward rates. # Not freezing the curves is useful when you are changing the underlying prices of the helpers, this way # the curve will update accordingly to the changes in the helpers. if self.freeze_curves: node_dates = yield_curve.dates() node_rates = [ yield_curve.zeroRate(node_date, self.day_counter, ql.Continuous, ql.NoFrequency).rate() for node_date in node_dates ] yield_curve = to_ql_interpolated_curve( node_dates=node_dates, node_rates=node_rates, day_counter=self.day_counter, calendar=self.calendar, interpolation_type=self.frozen_curve_interpolation_type) if self.enable_extrapolation: yield_curve.enableExtrapolation() self.yield_curves[date] = yield_curve
def __init__(self, process_list, correlation_matrix, start_date, end_date, tenor='1D', calendar='NULL', day_counter='ACTUAL365', low_discrepancy=False, brownian_bridge=True, seed=0): """ :param process_list: list of QuantLib.StochasticProcess1D A list with the QuantLib class of the stochastic process :param correlation_matrix: list A list with the correlation matrix. :param start_date: QuantLib.Date The start date of the path :param end_date: QuantLib.Date The end date of the path :param tenor: str The fixed interval used for path generation. Default '1D', daily intervals. :param calendar: str The calendar code of the calendar to be used. Default 'NULL'. :param day_counter: str The day count of the path. Default 'ACTUAL365' :param low_discrepancy: bool Used to choose the type of Gaussian Sequence Generator. :param seed: int Used to fix a certain seed of the random number. """ self.process_list = to_list(process_list) self.correlation_matrix = correlation_matrix self.start_date = to_ql_date(start_date) self.end_date = to_ql_date(end_date) self.tenor = tenor.upper() self.calendar = calendar.upper() self.day_counter = day_counter.upper() self.time_grid = TimeGrid(self.start_date, self.end_date, self.tenor, self.calendar, self.day_counter) self.low_discrepancy = low_discrepancy self.brownian_bridge = brownian_bridge self.seed = seed if len(self.process_list) == 1: self.process = process_list[0] elif len(self.process_list) > 1: self.process = ql.StochasticProcessArray(self.process_list, self.correlation_matrix) else: raise print("No process")
def create_call_component(call_date, call_price, main_bond_schedule, calendar, business_convention, tenor, date_generation, month_end, settlement_days, face_amount, day_counter, issue_date, index, fixed_coupon_len, fixed_coupon, floating_coupon, caps, floors, in_arrears): """Create a QuantLib fixed-rate or zero-coupon bond instance for the ``components`` dict of callable bond. Parameters ---------- call_date: QuantLib.Date Maturity of the component bond. call_price: scalar Redemption price of the component bond. main_bond_schedule: QuantLib.Schedule Cash-flow schedule of the parent bond. calendar: QuantLib.Calendar Calendar of the parent bond. business_convention: int Business-day convention of the parent bond. tenor: QuantLib.Period Coupon payment frequency of the parent bond. date_generation: QuantLib.DateGeneration Date-generation pattern of the parent bond's schedule. month_end: bool End of month parameter of the parent bond's schedule. settlement_days: int Default settlement days for trades in the parent bond. face_amount: scalar Face amount of the parent bond. day_counter: QuantLib.DayCounter DayCounter of the parent bond. issue_date: QuantLib.Date Issue date of the parent bond. index: QuantLib.IborIndex Index used for fixings fixed_coupon_len: int The number of coupon payments of the fixed leg fixed_coupon: list A list with the fixed coupon value(s) floating_coupon: list A list with the floating coupon value(s) caps: list A list with the coupon max value floors: list A list with the coupon min value in_arrears: bool Returns ------- QuantLib.FloatingRateBond Bond representing a component of the parent bond, with the passed call date and price. """ fixed_coupon = to_list(fixed_coupon) floating_coupon = to_list(floating_coupon) schedule = create_schedule_for_component(call_date, main_bond_schedule, calendar, business_convention, tenor, date_generation, month_end) floating_coupon_len = len(schedule) - fixed_coupon_len - 1 gearings = [0] * fixed_coupon_len + [1] * floating_coupon_len spreads = fixed_coupon * fixed_coupon_len + floating_coupon * floating_coupon_len return ql.FloatingRateBond(settlement_days, face_amount, schedule, index, day_counter, business_convention, index.fixingDays(), gearings, spreads, caps, floors, in_arrears, call_price, issue_date)
def ts_implied_volatility(self, date, last_available=False, fill_value=np.nan): date = to_datetime(to_list(date)) return getattr(self.timeseries, IMPLIED_VOL).get_values(index=date, last_available=last_available, fill_value=fill_value)
def ts_mid_price(self, date, last_available=True, fill_value=np.nan): date = to_datetime(to_list(date)) return getattr(self.timeseries, MID_PRICE).get_values(index=date, last_available=last_available, fill_value=fill_value)