def setupQL(self, pricingDate, marketId=''): QuantLib.Settings.instance().evaluationDate = pricingDate.ql() # Get market data curve based on Ccy/Index self.pricingDate = pricingDate curve = InterestRateCurve() curve.pricingDate = pricingDate curve.ccy = self.tCBond.ccy #TODO Fix term to make it TimePeriod curve.index = Enum.Index('LIBOR') curve.term = 'M' curve.numTerms = 3 curve.marketId = marketId # print 'marketDataContainer in setupQL' # print self.marketDataContainer if self.marketDataContainer == None: raise ErrorHandling.MarketDataMissing('marketDataContainer is None') newCurve = self.marketDataContainer.find(curve) #print 'newCurve in setupQL' #print newCurve if newCurve == None: raise ErrorHandling.MarketDataMissing('Cannot find market data %s' % curve) #load OAS and adjust discount curve bondOAS = self.marketDataContainer.find(BondOAS(marketId=marketId,pricingDate=pricingDate, tCBond=self.tCBond)) newCurve.shift(shiftAmount=bondOAS.mid) # Map curve to QuantLib deposit rates depositCurve = newCurve.buildZeroCurve() #print 'deposit curve in setupQL' #print depositCurve.nodes() #Pricing Engine discountTermStructure = QuantLib.RelinkableYieldTermStructureHandle() discountTermStructure.linkTo(depositCurve) fixedSchedule = QuantLib.Schedule(pricingDate.ql(), Date.createQLDateFromPythonDate(self.tCBond.endDate), QuantLib.Period(1,QuantLib.Years), self.tCBond.paymentCalendar.ql(), self.tCBond.paymentRollRule.ql(), self.tCBond.paymentRollRule.ql(), QuantLib.DateGeneration.Forward, False) coupons = [] coupons.append(float(self.tCBond.coupon)) #print self.tCBond.coupon #print self.tCBond.coupon.__class__ # coupons.append(0.04) self.qlBond = QuantLib.FixedRateBond(2, 100, fixedSchedule, coupons, self.tCBond.basis.ql(), self.tCBond.paymentRollRule.ql(), 100, pricingDate.ql()) bondEngine = \ QuantLib.DiscountingBondEngine(discountTermStructure) self.qlBond.setPricingEngine(bondEngine) self.upToDate = True
def _setupQL(self, pricingDate, marketId=''): QuantLib.Settings.instance().evaluationDate = pricingDate.ql() # print 'in setupql' self._pricingDate = pricingDate # Get market data curve based on Ccy/Index curve = InterestRateCurve() curve.pricingDate = pricingDate curve.ccy = self._tcSwap.ccy curve.index = Index('LIBOR') curve.term = 'M' curve.numTerms = self._tcSwap.floatingIndexNumTerms curve.marketId = marketId if self.marketDataContainer == None: raise ErrorHandling.MarketDataMissing('marketDataContainer is None') newCurve = self.marketDataContainer.find(curve) if newCurve == None: raise ErrorHandling.MarketDataMissing('Cannot find market data %s' % curve) depositCurve = newCurve.buildZeroCurve() discountTermStructure = QuantLib.RelinkableYieldTermStructureHandle() discountTermStructure.linkTo(depositCurve) swapEngine = QuantLib.DiscountingSwapEngine(discountTermStructure) #TODO fix the discounting curve to make if projected fwd curve index = QuantLib.Euribor6M(discountTermStructure) #fixingDate = Date.Date(month=9,day=10,year=2012) #index.addFixing(fixingDate.ql(),0.02) #tcSwap dates are python dates fixedSchedule = QuantLib.Schedule(Date.createQLDateFromPythonDate(self._tcSwap.startDate), Date.createQLDateFromPythonDate(self._tcSwap.endDate), #fixedLegTenor is QuantLib.Period(n,unit) #Period is an integer (n) and TimePeriod (unit) #TODO fix payment freq QuantLib.Period(6,QuantLib.Months), self._tcSwap.fixedPaymentCalendar.ql(), self._tcSwap.fixedPaymentRollRule.ql(), self._tcSwap.fixedPaymentRollRule.ql(), QuantLib.DateGeneration.Forward, False) floatingSchedule = QuantLib.Schedule(Date.createQLDateFromPythonDate(self._tcSwap.startDate), Date.createQLDateFromPythonDate(self._tcSwap.endDate), #fixedLegTenor is QuantLib.Period(n,unit) #Period is an integer (n) and TimePeriod (unit) #TODO fix payment freq QuantLib.Period(3,QuantLib.Months), self._tcSwap.floatingPaymentCalendar.ql(), self._tcSwap.floatingPaymentRollRule.ql(), self._tcSwap.floatingPaymentRollRule.ql(), QuantLib.DateGeneration.Forward, False) #TODO: FIx to decide if it's payer or receiver fixed self._qlSwap = QuantLib.VanillaSwap(QuantLib.VanillaSwap.Payer, self.amount, fixedSchedule, self._tcSwap.fixedCoupon, self._tcSwap.fixedBasis.ql(), floatingSchedule, #TODO fix index index, self._tcSwap.floatingSpread, self._tcSwap.floatingBasis.ql()) self._qlSwap.setPricingEngine(swapEngine) self.upToDate = True