public SwapSpreadIndex(String familyName, SwapIndex swapIndex1, SwapIndex swapIndex2, double gearing1 = 1.0, double gearing2 = -1.0) : base(familyName, swapIndex1.tenor(), // does not make sense, but we have to provide one swapIndex1.fixingDays(), swapIndex1.currency(), swapIndex1.fixingCalendar(), swapIndex1.dayCounter()) { swapIndex1_ = swapIndex1; swapIndex2_ = swapIndex2; gearing1_ = gearing1; gearing2_ = gearing2; swapIndex1_.registerWith(update); swapIndex2_.registerWith(update); name_ = swapIndex1_.name() + "(" + gearing1 + ") + " + swapIndex2_.name() + "(" + gearing1 + ")"; Utils.QL_REQUIRE(swapIndex1_.fixingDays() == swapIndex2_.fixingDays(), () => "index1 fixing days (" + swapIndex1_.fixingDays() + ")" + "must be equal to index2 fixing days (" + swapIndex2_.fixingDays() + ")"); Utils.QL_REQUIRE(swapIndex1_.fixingCalendar() == swapIndex2_.fixingCalendar(), () => "index1 fixingCalendar (" + swapIndex1_.fixingCalendar() + ")" + "must be equal to index2 fixingCalendar (" + swapIndex2_.fixingCalendar() + ")"); Utils.QL_REQUIRE(swapIndex1_.currency() == swapIndex2_.currency(), () => "index1 currency (" + swapIndex1_.currency() + ")" + "must be equal to index2 currency (" + swapIndex2_.currency() + ")"); Utils.QL_REQUIRE(swapIndex1_.dayCounter() == swapIndex2_.dayCounter(), () => "index1 dayCounter (" + swapIndex1_.dayCounter() + ")" + "must be equal to index2 dayCounter (" + swapIndex2_.dayCounter() + ")"); Utils.QL_REQUIRE(swapIndex1_.fixedLegTenor() == swapIndex2_.fixedLegTenor(), () => "index1 fixedLegTenor (" + swapIndex1_.fixedLegTenor() + ")" + "must be equal to index2 fixedLegTenor (" + swapIndex2_.fixedLegTenor()); Utils.QL_REQUIRE(swapIndex1_.fixedLegConvention() == swapIndex2_.fixedLegConvention(), () => "index1 fixedLegConvention (" + swapIndex1_.fixedLegConvention() + ")" + "must be equal to index2 fixedLegConvention (" + swapIndex2_.fixedLegConvention()); }
public GFunctionExactYield(CmsCoupon coupon) { SwapIndex swapIndex = coupon.swapIndex(); VanillaSwap swap = swapIndex.underlyingSwap(coupon.fixingDate()); Schedule schedule = swap.fixedSchedule(); Handle <YieldTermStructure> rateCurve = swapIndex.forwardingTermStructure(); DayCounter dc = swapIndex.dayCounter(); double swapStartTime = dc.yearFraction(rateCurve.link.referenceDate(), schedule.startDate()); double swapFirstPaymentTime = dc.yearFraction(rateCurve.link.referenceDate(), schedule.date(1)); double paymentTime = dc.yearFraction(rateCurve.link.referenceDate(), coupon.date()); delta_ = (paymentTime - swapStartTime) / (swapFirstPaymentTime - swapStartTime); List <CashFlow> fixedLeg = new List <CashFlow>(swap.fixedLeg()); int n = fixedLeg.Count; accruals_ = new List <double>(); for (int i = 0; i < n; ++i) { Coupon coupon1 = fixedLeg[i] as Coupon; accruals_.Add(coupon1.accrualPeriod()); } }
public Swaption value() { Date evaluationDate = Settings.evaluationDate(); Calendar fixingCalendar = swapIndex_.fixingCalendar(); fixingDate_ = fixingCalendar.advance(evaluationDate, optionTenor_, optionConvention_); if (exerciseDate_ == null) { exercise_ = new EuropeanExercise(fixingDate_); } else { if (exerciseDate_ <= fixingDate_) { throw new ArgumentException( "exercise date (" + exerciseDate_ + ") must be less " + "than or equal to fixing date (" + fixingDate_ + ")"); } exercise_ = new EuropeanExercise(exerciseDate_); } double usedStrike; if (strike_ == null) { // ATM on the forecasting curve if (!swapIndex_.forwardingTermStructure().empty()) { throw new ArgumentException( "no forecasting term structure set to " + swapIndex_.name()); } VanillaSwap temp = swapIndex_.underlyingSwap(fixingDate_); temp.setPricingEngine(new DiscountingSwapEngine( swapIndex_.forwardingTermStructure())); usedStrike = temp.fairRate(); } else { usedStrike = strike_.Value; } BusinessDayConvention bdc = swapIndex_.fixedLegConvention(); underlyingSwap_ = new MakeVanillaSwap(swapIndex_.tenor(), swapIndex_.iborIndex(), usedStrike) .withEffectiveDate(swapIndex_.valueDate(fixingDate_)) .withFixedLegCalendar(swapIndex_.fixingCalendar()) .withFixedLegDayCount(swapIndex_.dayCounter()) .withFixedLegConvention(bdc) .withFixedLegTerminationDateConvention(bdc); Swaption swaption = new Swaption(underlyingSwap_, exercise_, delivery_); swaption.setPricingEngine(engine_); return(swaption); }
//public SwapRateHelper(Quote rate, SwapIndex swapIndex) : // this(rate, swapIndex, new SimpleQuote(), new Period(0, TimeUnit.Days)) { } //public SwapRateHelper(Quote rate, SwapIndex swapIndex, Quote spread) : // this(rate, swapIndex, spread, new Period(0, TimeUnit.Days)) { } public SwapRateHelper(Handle<Quote> rate, SwapIndex swapIndex, Handle<Quote> spread, Period fwdStart) : base(rate) { tenor_ = swapIndex.tenor(); calendar_ = swapIndex.fixingCalendar(); fixedConvention_ = swapIndex.fixedLegConvention(); fixedFrequency_ = swapIndex.fixedLegTenor().frequency(); fixedDayCount_ = swapIndex.dayCounter(); iborIndex_ = swapIndex.iborIndex(); spread_ = spread; fwdStart_ = fwdStart; // add observers iborIndex_.registerWith(update); spread_.registerWith(update); initializeDates(); }
//public SwapRateHelper(double rate, SwapIndex swapIndex) // : this(rate, swapIndex, new SimpleQuote()) { } //public SwapRateHelper(double rate, SwapIndex swapIndex, Quote spread) // : this(rate, swapIndex, spread, new Period(0, TimeUnit.Days)) { } public SwapRateHelper(double rate, SwapIndex swapIndex, Handle <Quote> spread, Period fwdStart) : base(rate) { tenor_ = swapIndex.tenor(); calendar_ = swapIndex.fixingCalendar(); fixedConvention_ = swapIndex.fixedLegConvention(); fixedFrequency_ = swapIndex.fixedLegTenor().frequency(); fixedDayCount_ = swapIndex.dayCounter(); iborIndex_ = swapIndex.iborIndex(); spread_ = spread; fwdStart_ = fwdStart; // add observers iborIndex_.registerWith(update); spread_.registerWith(update); initializeDates(); }
//===========================================================================// // GFunctionWithShifts // //===========================================================================// public GFunctionWithShifts(CmsCoupon coupon, Handle <Quote> meanReversion) { meanReversion_ = meanReversion; calibratedShift_ = 0.03; tmpRs_ = 10000000.0; accuracy_ = 1.0e-14; SwapIndex swapIndex = coupon.swapIndex(); VanillaSwap swap = swapIndex.underlyingSwap(coupon.fixingDate()); swapRateValue_ = swap.fairRate(); objectiveFunction_ = new ObjectiveFunction(this, swapRateValue_); Schedule schedule = swap.fixedSchedule(); Handle <YieldTermStructure> rateCurve = swapIndex.forwardingTermStructure(); DayCounter dc = swapIndex.dayCounter(); swapStartTime_ = dc.yearFraction(rateCurve.link.referenceDate(), schedule.startDate()); discountAtStart_ = rateCurve.link.discount(schedule.startDate()); double paymentTime = dc.yearFraction(rateCurve.link.referenceDate(), coupon.date()); shapedPaymentTime_ = shapeOfShift(paymentTime); List <CashFlow> fixedLeg = new List <CashFlow>(swap.fixedLeg()); int n = fixedLeg.Count; shapedSwapPaymentTimes_ = new List <double>(); swapPaymentDiscounts_ = new List <double>(); accruals_ = new List <double>(); for (int i = 0; i < n; ++i) { Coupon coupon1 = fixedLeg[i] as Coupon; accruals_.Add(coupon1.accrualPeriod()); Date paymentDate = new Date(coupon1.date().serialNumber()); double swapPaymentTime = dc.yearFraction(rateCurve.link.referenceDate(), paymentDate); shapedSwapPaymentTimes_.Add(shapeOfShift(swapPaymentTime)); swapPaymentDiscounts_.Add(rateCurve.link.discount(paymentDate)); } discountRatio_ = swapPaymentDiscounts_.Last() / discountAtStart_; }
public override void initialize(FloatingRateCoupon coupon) { coupon_ = coupon as CmsCoupon; Utils.QL_REQUIRE(coupon_ != null, () => "CMS coupon needed"); gearing_ = coupon_.gearing(); spread_ = coupon_.spread(); fixingDate_ = coupon_.fixingDate(); paymentDate_ = coupon_.date(); SwapIndex swapIndex = coupon_.swapIndex(); rateCurve_ = swapIndex.forwardingTermStructure().link; Date today = Settings.evaluationDate(); if (paymentDate_ > today) { discount_ = rateCurve_.discount(paymentDate_); } else { discount_ = 1.0; } spreadLegValue_ = spread_ * coupon_.accrualPeriod() * discount_; if (fixingDate_ > today) { swapTenor_ = swapIndex.tenor(); VanillaSwap swap = swapIndex.underlyingSwap(fixingDate_); swapRateValue_ = swap.fairRate(); double bp = 1.0e-4; annuity_ = (swap.floatingLegBPS() / bp); int q = (int)swapIndex.fixedLegTenor().frequency(); Schedule schedule = swap.fixedSchedule(); DayCounter dc = swapIndex.dayCounter(); //DayCounter dc = coupon.dayCounter(); double startTime = dc.yearFraction(rateCurve_.referenceDate(), swap.startDate()); double swapFirstPaymentTime = dc.yearFraction(rateCurve_.referenceDate(), schedule.date(1)); double paymentTime = dc.yearFraction(rateCurve_.referenceDate(), paymentDate_); double delta = (paymentTime - startTime) / (swapFirstPaymentTime - startTime); switch (modelOfYieldCurve_) { case GFunctionFactory.YieldCurveModel.Standard: gFunction_ = GFunctionFactory.newGFunctionStandard(q, delta, swapTenor_.length()); break; case GFunctionFactory.YieldCurveModel.ExactYield: gFunction_ = GFunctionFactory.newGFunctionExactYield(coupon_); break; case GFunctionFactory.YieldCurveModel.ParallelShifts: { Handle <Quote> nullMeanReversionQuote = new Handle <Quote>(new SimpleQuote(0.0)); gFunction_ = GFunctionFactory.newGFunctionWithShifts(coupon_, nullMeanReversionQuote); } break; case GFunctionFactory.YieldCurveModel.NonParallelShifts: gFunction_ = GFunctionFactory.newGFunctionWithShifts(coupon_, meanReversion_); break; default: throw new ApplicationException("unknown/illegal gFunction type"); } vanillaOptionPricer_ = new BlackVanillaOptionPricer(swapRateValue_, fixingDate_, swapTenor_, swaptionVolatility().link); } }