private List <Date> getListOfPeriodDatesIncludingQuasiPayments(Schedule schedule) { // Process the schedule into an array of dates. Date issueDate = schedule.date(0); Date firstCoupon = schedule.date(1); Date notionalCoupon = schedule.calendar().advance(firstCoupon, -schedule.tenor(), schedule.businessDayConvention(), schedule.endOfMonth()); List <Date> newDates = schedule.dates(); newDates[0] = notionalCoupon; //long first coupon if (notionalCoupon > issueDate) { Date priorNotionalCoupon = schedule.calendar().advance(notionalCoupon, -schedule.tenor(), schedule.businessDayConvention(), schedule.endOfMonth()); newDates.Insert(0, priorNotionalCoupon); } return(newDates); }
private List <Date> getListOfPeriodDatesIncludingQuasiPayments(Schedule schedule) { // Process the schedule into an array of dates. Date issueDate = schedule.date(0); Date firstCoupon = schedule.date(1); Date notionalFirstCoupon = schedule.calendar().advance(firstCoupon, -schedule.tenor(), schedule.businessDayConvention(), schedule.endOfMonth()); List <Date> newDates = schedule.dates().ToList(); newDates[0] = notionalFirstCoupon; // The handling of the last coupon is is needed for odd final periods var notionalLastCoupon = schedule.calendar().advance( schedule.date(schedule.Count - 2), schedule.tenor(), schedule.businessDayConvention(), schedule.endOfMonth()); newDates[schedule.Count - 1] = notionalLastCoupon; //long first coupon if (notionalFirstCoupon > issueDate) { Date priorNotionalCoupon = schedule.calendar().advance(notionalFirstCoupon, -schedule.tenor(), schedule.businessDayConvention(), schedule.endOfMonth()); newDates.Insert(0, priorNotionalCoupon); } // long last coupon if (notionalLastCoupon < schedule.endDate()) { Date nextNotionalCoupon = schedule.calendar().advance( notionalLastCoupon, schedule.tenor(), schedule.businessDayConvention(), schedule.endOfMonth()); newDates.Add(nextNotionalCoupon); } return(newDates); }
public FloatingRateBond(int settlementDays, double faceAmount, Schedule schedule, IborIndex index, DayCounter paymentDayCounter, BusinessDayConvention paymentConvention, int fixingDays, List<double> gearings, List<double> spreads, List<double> caps, List<double> floors, bool inArrears, double redemption, Date issueDate) : base(settlementDays, schedule.calendar(), issueDate) { maturityDate_ = schedule.endDate(); cashflows_ = new IborLeg(schedule, index) .withPaymentDayCounter(paymentDayCounter) .withFixingDays(fixingDays) .withGearings(gearings) .withSpreads(spreads) .withCaps(caps) .withFloors(floors) .inArrears(inArrears) .withNotionals(faceAmount) .withPaymentAdjustment(paymentConvention); addRedemptionsToCashflows(new List<double>() { redemption }); if (cashflows().Count == 0) throw new ApplicationException("bond with no cashflows!"); if (redemptions_.Count != 1) throw new ApplicationException("multiple redemptions created"); index.registerWith(update); }
protected ConvertibleBond( Exercise exercise, double conversionRatio, DividendSchedule dividends, CallabilitySchedule callability, Handle<Quote> creditSpread, Date issueDate, int settlementDays, Schedule schedule, double redemption) : base(settlementDays, schedule.calendar(), issueDate) { conversionRatio_ = conversionRatio; callability_ = callability; dividends_ = dividends; creditSpread_ = creditSpread; maturityDate_ = schedule.endDate(); if (!callability.empty()) { Utils.QL_REQUIRE( callability.Last().date() <= maturityDate_, () => "last callability date (" + callability.Last().date() + ") later than maturity (" + maturityDate_.ToShortDateString() + ")"); } creditSpread.registerWith(update); }
//! fixed-rate bond /*! \ingroup instruments * * \test calculations are tested by checking results against * cached values. */ //! simple annual compounding coupon rates public FixedRateBond(int settlementDays, double faceAmount, Schedule schedule, List <double> coupons, DayCounter accrualDayCounter, BusinessDayConvention paymentConvention = BusinessDayConvention.Following, double redemption = 100, Date issueDate = null, Calendar paymentCalendar = null, Period exCouponPeriod = null, Calendar exCouponCalendar = null, BusinessDayConvention exCouponConvention = BusinessDayConvention.Unadjusted, bool exCouponEndOfMonth = false) : base(settlementDays, paymentCalendar ?? schedule.calendar(), issueDate) { frequency_ = schedule.tenor().frequency(); dayCounter_ = accrualDayCounter; maturityDate_ = schedule.endDate(); cashflows_ = new FixedRateLeg(schedule) .withCouponRates(coupons, accrualDayCounter) .withExCouponPeriod(exCouponPeriod, exCouponCalendar, exCouponConvention, exCouponEndOfMonth) .withPaymentCalendar(calendar_) .withNotionals(faceAmount) .withPaymentAdjustment(paymentConvention); addRedemptionsToCashflows(new List <double>() { redemption }); Utils.QL_REQUIRE(cashflows().Count != 0, () => "bond with no cashflows!"); Utils.QL_REQUIRE(redemptions_.Count == 1, () => "multiple redemptions created"); }
protected CallableBond(int settlementDays, Schedule schedule, DayCounter paymentDayCounter, Date issueDate = null, CallabilitySchedule putCallSchedule = null) : base(settlementDays, schedule.calendar(), issueDate) { if (putCallSchedule == null) { putCallSchedule = new CallabilitySchedule(); } paymentDayCounter_ = paymentDayCounter; putCallSchedule_ = putCallSchedule; maturityDate_ = schedule.dates().Last(); if (!putCallSchedule_.empty()) { Date finalOptionDate = Date.minDate(); for (int i = 0; i < putCallSchedule_.Count; ++i) { finalOptionDate = Date.Max(finalOptionDate, putCallSchedule_[i].date()); } Utils.QL_REQUIRE(finalOptionDate <= maturityDate_, () => "Bond cannot mature before last call/put date"); } // derived classes must set cashflows_ and frequency_ }
public AmortizingFixedRateBond( int settlementDays, List<double> notionals, Schedule schedule, List<double> coupons, DayCounter accrualDayCounter, BusinessDayConvention paymentConvention = BusinessDayConvention.Following, Date issueDate = null) :base(settlementDays, schedule.calendar(), issueDate) { frequency_ = schedule.tenor().frequency(); dayCounter_ = accrualDayCounter; schedule_ = schedule; maturityDate_ = schedule.endDate(); cashflows_ = new FixedRateLeg(schedule) .withCouponRates(coupons, accrualDayCounter) .withNotionals(notionals) .withPaymentAdjustment(paymentConvention).value(); addRedemptionsToCashflows(); if ( cashflows().empty()) throw new ApplicationException("bond with no cashflows!"); }
protected ConvertibleBond(Exercise exercise, double conversionRatio, DividendSchedule dividends, CallabilitySchedule callability, Handle <Quote> creditSpread, Date issueDate, int settlementDays, Schedule schedule, double redemption) : base(settlementDays, schedule.calendar(), issueDate) { conversionRatio_ = conversionRatio; callability_ = callability; dividends_ = dividends; creditSpread_ = creditSpread; maturityDate_ = schedule.endDate(); if (!callability.empty()) { Utils.QL_REQUIRE(callability.Last().date() <= maturityDate_, () => "last callability date (" + callability.Last().date() + ") later than maturity (" + maturityDate_.ToShortDateString() + ")"); } creditSpread.registerWith(update); }
//! simple annual compounding coupon rates public FixedRateBond(int settlementDays, double faceAmount, Schedule schedule, List <double> coupons, DayCounter accrualDayCounter, BusinessDayConvention paymentConvention, double redemption, Date issueDate, Calendar paymentCalendar) : base(settlementDays, paymentCalendar == new Calendar() ? schedule.calendar() : paymentCalendar, issueDate) { frequency_ = schedule.tenor().frequency(); dayCounter_ = accrualDayCounter; maturityDate_ = schedule.endDate(); cashflows_ = new FixedRateLeg(schedule) .withCouponRates(coupons, accrualDayCounter) .withPaymentCalendar(calendar_) .withNotionals(faceAmount) .withPaymentAdjustment(paymentConvention); addRedemptionsToCashflows(new List <double>() { redemption }); if (cashflows().Count == 0) { throw new ApplicationException("bond with no cashflows!"); } if (redemptions_.Count != 1) { throw new ApplicationException("multiple redemptions created"); } }
public AmortizingFixedRateBond( int settlementDays, List <double> notionals, Schedule schedule, List <double> coupons, DayCounter accrualDayCounter, BusinessDayConvention paymentConvention = BusinessDayConvention.Following, Date issueDate = null) : base(settlementDays, schedule.calendar(), issueDate) { frequency_ = schedule.tenor().frequency(); dayCounter_ = accrualDayCounter; schedule_ = schedule; maturityDate_ = schedule.endDate(); cashflows_ = new FixedRateLeg(schedule) .withCouponRates(coupons, accrualDayCounter) .withNotionals(notionals) .withPaymentAdjustment(paymentConvention).value(); addRedemptionsToCashflows(); if (cashflows().empty()) { throw new ApplicationException("bond with no cashflows!"); } }
//! fixed-rate bond /*! \ingroup instruments \test calculations are tested by checking results against cached values. */ //! simple annual compounding coupon rates public FixedRateBond(int settlementDays, double faceAmount, Schedule schedule,List<double> coupons, DayCounter accrualDayCounter, BusinessDayConvention paymentConvention = BusinessDayConvention.Following, double redemption = 100, Date issueDate = null,Calendar paymentCalendar = null, Period exCouponPeriod = null, Calendar exCouponCalendar = null, BusinessDayConvention exCouponConvention = BusinessDayConvention.Unadjusted, bool exCouponEndOfMonth = false) : base(settlementDays, paymentCalendar == null ? schedule.calendar() : paymentCalendar, issueDate) { frequency_ = schedule.tenor().frequency(); dayCounter_ = accrualDayCounter; maturityDate_ = schedule.endDate(); cashflows_ = new FixedRateLeg(schedule) .withCouponRates(coupons, accrualDayCounter) .withExCouponPeriod(exCouponPeriod, exCouponCalendar, exCouponConvention, exCouponEndOfMonth) .withPaymentCalendar(calendar_) .withNotionals(faceAmount) .withPaymentAdjustment(paymentConvention); addRedemptionsToCashflows(new List<double>() { redemption }); if (cashflows().Count == 0) throw new ApplicationException("bond with no cashflows!"); if (redemptions_.Count != 1) throw new ApplicationException("multiple redemptions created"); }
public FloatingRateBond(int settlementDays, double faceAmount, Schedule schedule, IborIndex index, DayCounter paymentDayCounter, BusinessDayConvention paymentConvention, int fixingDays, List <double> gearings, List <double> spreads, List <double> caps, List <double> floors, bool inArrears, double redemption, Date issueDate) : base(settlementDays, schedule.calendar(), issueDate) { maturityDate_ = schedule.endDate(); cashflows_ = new IborLeg(schedule, index) .withPaymentDayCounter(paymentDayCounter) .withFixingDays(fixingDays) .withGearings(gearings) .withSpreads(spreads) .withCaps(caps) .withFloors(floors) .inArrears(inArrears) .withNotionals(faceAmount) .withPaymentAdjustment(paymentConvention); addRedemptionsToCashflows(new List <double>() { redemption }); if (cashflows().Count == 0) { throw new Exception("bond with no cashflows!"); } if (redemptions_.Count != 1) { throw new Exception("multiple redemptions created"); } index.registerWith(update); }
/// <summary> /// CDS quoted as running-spread only /// </summary> /// <param name="side">Whether the protection is bought or sold.</param> /// <param name="notional">Notional value</param> /// <param name="spread">Running spread in fractional units.</param> /// <param name="schedule">Coupon schedule.</param> /// <param name="convention">Business-day convention for payment-date adjustment.</param> /// <param name="dayCounter">Day-count convention for accrual.</param> /// <param name="settlesAccrual">Whether or not the accrued coupon is due in the event of a default.</param> /// <param name="paysAtDefaultTime">If set to true, any payments triggered by a default event are /// due at default time. If set to false, they are due at the end of the accrual period.</param> /// <param name="protectionStart">The first date where a default event will trigger the contract.</param> /// <param name="claim"></param> /// <param name="lastPeriodDayCounter">Day-count convention for accrual in last period</param> /// <param name="rebatesAccrual">The protection seller pays the accrued scheduled current coupon at the start /// of the contract. The rebate date is not provided but computed to be two days after protection start.</param> public CreditDefaultSwap(Protection.Side side, double notional, double spread, Schedule schedule, BusinessDayConvention convention, DayCounter dayCounter, bool settlesAccrual = true, bool paysAtDefaultTime = true, Date protectionStart = null, Claim claim = null, DayCounter lastPeriodDayCounter = null, bool rebatesAccrual = true) { side_ = side; notional_ = notional; upfront_ = null; runningSpread_ = spread; settlesAccrual_ = settlesAccrual; paysAtDefaultTime_ = paysAtDefaultTime; claim_ = claim; protectionStart_ = protectionStart ?? schedule[0]; Utils.QL_REQUIRE(protectionStart_ <= schedule[0] || schedule.rule() == DateGeneration.Rule.CDS || schedule.rule() == DateGeneration.Rule.CDS2015 , () => "protection can not start after accrual"); leg_ = new FixedRateLeg(schedule) .withLastPeriodDayCounter(lastPeriodDayCounter) .withCouponRates(spread, dayCounter) .withNotionals(notional) .withPaymentAdjustment(convention); Date effectiveUpfrontDate = schedule.calendar().advance(protectionStart_, 2, TimeUnit.Days, convention); // '2' is used above since the protection start is assumed to be on trade_date + 1 if (rebatesAccrual) { FixedRateCoupon firstCoupon = leg_[0] as FixedRateCoupon; Date rebateDate = effectiveUpfrontDate; accrualRebate_ = new SimpleCashFlow(firstCoupon.accruedAmount(protectionStart_), rebateDate); } upfrontPayment_ = new SimpleCashFlow(0.0, effectiveUpfrontDate); if (claim_ == null) { claim_ = new FaceValueClaim(); } claim_.registerWith(update); maturity_ = schedule.dates().Last(); }
public CPIBond(int settlementDays, double faceAmount, bool growthOnly, double baseCPI, Period observationLag, ZeroInflationIndex cpiIndex, InterpolationType observationInterpolation, Schedule schedule, List <double> fixedRate, DayCounter accrualDayCounter, BusinessDayConvention paymentConvention = BusinessDayConvention.ModifiedFollowing, Date issueDate = null, Calendar paymentCalendar = null, Period exCouponPeriod = null, Calendar exCouponCalendar = null, BusinessDayConvention exCouponConvention = BusinessDayConvention.Unadjusted, bool exCouponEndOfMonth = false) : base(settlementDays, paymentCalendar ?? schedule.calendar(), issueDate) { frequency_ = schedule.tenor().frequency(); dayCounter_ = accrualDayCounter; growthOnly_ = growthOnly; baseCPI_ = baseCPI; observationLag_ = observationLag; cpiIndex_ = cpiIndex; observationInterpolation_ = observationInterpolation; maturityDate_ = schedule.endDate(); // a CPIleg know about zero legs and inclusion of base inflation notional cashflows_ = new CPILeg(schedule, cpiIndex_, baseCPI_, observationLag_) .withSubtractInflationNominal(growthOnly_) .withObservationInterpolation(observationInterpolation_) .withPaymentDayCounter(accrualDayCounter) .withFixedRates(fixedRate) .withPaymentCalendar(calendar_) .withExCouponPeriod(exCouponPeriod, exCouponCalendar, exCouponConvention, exCouponEndOfMonth) .withNotionals(faceAmount) .withPaymentAdjustment(paymentConvention); calculateNotionalsFromCashflows(); cpiIndex_.registerWith(update); foreach (CashFlow i in cashflows_) { i.registerWith(update); } }
public static List <CashFlow> OvernightLeg(List <double> nominals, Schedule schedule, BusinessDayConvention paymentAdjustment, OvernightIndex overnightIndex, List <double> gearings, List <double> spreads, DayCounter paymentDayCounter ) { if (nominals.Count == 0) { throw new ArgumentException("no nominal given"); } List <CashFlow> leg = new List <CashFlow>(); // the following is not always correct Calendar calendar = schedule.calendar(); Date refStart, start, refEnd, end; Date paymentDate; int n = schedule.Count; for (int i = 0; i < n - 1; ++i) { refStart = start = schedule.date(i); refEnd = end = schedule.date(i + 1); paymentDate = calendar.adjust(end, paymentAdjustment); if (i == 0 && !schedule.isRegular(i + 1)) { refStart = calendar.adjust(end - schedule.tenor(), paymentAdjustment); } if (i == n - 1 && !schedule.isRegular(i + 1)) { refEnd = calendar.adjust(start + schedule.tenor(), paymentAdjustment); } leg.Add(new OvernightIndexedCoupon(paymentDate, Utils.Get(nominals, i), start, end, overnightIndex, Utils.Get(gearings, i, 1.0), Utils.Get(spreads, i, 0.0), refStart, refEnd, paymentDayCounter)); } return(leg); }
public CPIBond(int settlementDays, double faceAmount, bool growthOnly, double baseCPI, Period observationLag, ZeroInflationIndex cpiIndex, InterpolationType observationInterpolation, Schedule schedule, List<double> fixedRate, DayCounter accrualDayCounter, BusinessDayConvention paymentConvention = BusinessDayConvention.ModifiedFollowing, Date issueDate = null, Calendar paymentCalendar = null, Period exCouponPeriod = null, Calendar exCouponCalendar = null, BusinessDayConvention exCouponConvention = BusinessDayConvention.Unadjusted, bool exCouponEndOfMonth = false) :base(settlementDays, paymentCalendar == null ? schedule.calendar() : paymentCalendar, issueDate) { frequency_ = schedule.tenor().frequency(); dayCounter_ = accrualDayCounter; growthOnly_ = growthOnly; baseCPI_=baseCPI; observationLag_ = observationLag; cpiIndex_= cpiIndex; observationInterpolation_ = observationInterpolation; maturityDate_ = schedule.endDate(); // a CPIleg know about zero legs and inclusion of base inflation notional cashflows_ = new CPILeg(schedule, cpiIndex_, baseCPI_, observationLag_) .withSubtractInflationNominal(growthOnly_) .withObservationInterpolation(observationInterpolation_) .withPaymentDayCounter(accrualDayCounter) .withFixedRates(fixedRate) .withPaymentCalendar(calendar_) .withExCouponPeriod(exCouponPeriod, exCouponCalendar, exCouponConvention, exCouponEndOfMonth) .withNotionals(faceAmount) .withPaymentAdjustment(paymentConvention); calculateNotionalsFromCashflows(); cpiIndex_.registerWith(update); foreach ( CashFlow i in cashflows_) i.registerWith(update); }
public AmortizingFloatingRateBond(int settlementDays, List<double> notionals, Schedule schedule, IborIndex index, DayCounter accrualDayCounter, BusinessDayConvention paymentConvention = BusinessDayConvention.Following, int fixingDays = 0, List<double> gearings = null, List<double> spreads = null, List<double> caps = null, List<double> floors = null, bool inArrears = false, Date issueDate = null) :base(settlementDays, schedule.calendar(), issueDate) { if ( gearings == null ) gearings = new List<double>() {1, 1.0}; if (spreads == null) spreads = new List<double>() { 1, 0.0 }; if (caps == null) caps = new List<double>() ; if (floors == null) floors = new List<double>(); maturityDate_ = schedule.endDate(); cashflows_ = new IborLeg(schedule, index) .withCaps(caps) .withFloors(floors) .inArrears(inArrears) .withSpreads(spreads) .withGearings(gearings) .withFixingDays(fixingDays) .withPaymentDayCounter(accrualDayCounter) .withPaymentAdjustment(paymentConvention) .withNotionals(notionals).value(); addRedemptionsToCashflows(); Utils.QL_REQUIRE( !cashflows().empty(), () => "bond with no cashflows!" ); index.registerWith(update); }
public FixedRateBond(int settlementDays, double faceAmount, Schedule schedule, List <InterestRate> coupons, BusinessDayConvention paymentConvention = BusinessDayConvention.Following, double redemption = 100, Date issueDate = null, Calendar paymentCalendar = null, Period exCouponPeriod = null, Calendar exCouponCalendar = null, BusinessDayConvention exCouponConvention = BusinessDayConvention.Unadjusted, bool exCouponEndOfMonth = false) : base(settlementDays, paymentCalendar == null ? schedule.calendar() : paymentCalendar, issueDate) { frequency_ = schedule.tenor().frequency(); dayCounter_ = coupons[0].dayCounter(); maturityDate_ = schedule.endDate(); cashflows_ = new FixedRateLeg(schedule) .withCouponRates(coupons) .withExCouponPeriod(exCouponPeriod, exCouponCalendar, exCouponConvention, exCouponEndOfMonth) .withPaymentCalendar(calendar_) .withNotionals(faceAmount) .withPaymentAdjustment(paymentConvention); addRedemptionsToCashflows(new List <double>() { redemption }); if (cashflows().Count == 0) { throw new ApplicationException("bond with no cashflows!"); } if (redemptions_.Count != 1) { throw new ApplicationException("multiple redemptions created"); } }
public CmsRateBond(int settlementDays, double faceAmount, Schedule schedule, SwapIndex index, DayCounter paymentDayCounter, BusinessDayConvention paymentConvention = BusinessDayConvention.Following, int fixingDays = 0, List<double> gearings = null, List<double> spreads = null, List<double> caps = null, List<double> floors = null, bool inArrears = false, double redemption = 100.0, Date issueDate = null) : base(settlementDays, schedule.calendar(), issueDate) { // Optional value check if ( gearings == null ) gearings = new List<double>(){1}; if ( spreads == null ) spreads = new List<double>(){0}; if (caps == null) caps = new List<double>(); if (floors == null) floors = new List<double>(); maturityDate_ = schedule.endDate(); cashflows_ = new CmsLeg(schedule, index) .withPaymentDayCounter(paymentDayCounter) .withFixingDays(fixingDays) .withGearings(gearings) .withSpreads(spreads) .withCaps(caps) .withFloors(floors) .inArrears(inArrears) .withNotionals(faceAmount) .withPaymentAdjustment(paymentConvention); addRedemptionsToCashflows(new List<double>() { redemption }); if (cashflows().Count == 0) throw new ApplicationException("bond with no cashflows!"); if (redemptions_.Count != 1) throw new ApplicationException("multiple redemptions created"); index.registerWith(update); }
public CPILeg(Schedule schedule, ZeroInflationIndex index, double baseCPI, Period observationLag) { schedule_ = schedule; index_ = index; baseCPI_ = baseCPI; observationLag_ = observationLag; paymentDayCounter_ = new Thirty360(); paymentAdjustment_ = BusinessDayConvention.ModifiedFollowing; paymentCalendar_ = schedule.calendar(); fixingDays_ = new List <int>() { 0 }; observationInterpolation_ = InterpolationType.AsIndex; subtractInflationNominal_ = true; spreads_ = new List <double>() { 0 }; }
public FloatingCatBond(int settlementDays, double faceAmount, Schedule schedule, IborIndex iborIndex, DayCounter paymentDayCounter, NotionalRisk notionalRisk, BusinessDayConvention paymentConvention = QLNet.BusinessDayConvention.Following, int fixingDays = 0, List <double> gearings = null, List <double> spreads = null, List <double?> caps = null, List <double?> floors = null, bool inArrears = false, double redemption = 100.0, Date issueDate = null) : base(settlementDays, schedule.calendar(), issueDate, notionalRisk) { maturityDate_ = schedule.endDate(); cashflows_ = new IborLeg(schedule, iborIndex) .withFixingDays(fixingDays) .withGearings(gearings) .withSpreads(spreads) .withCaps(caps) .withFloors(floors) .inArrears(inArrears) .withPaymentDayCounter(paymentDayCounter) .withNotionals(faceAmount) .withPaymentAdjustment(paymentConvention); addRedemptionsToCashflows(new InitializedList <double>(1, redemption)); Utils.QL_REQUIRE(!cashflows().empty(), () => "bond with no cashflows!"); Utils.QL_REQUIRE(redemptions_.Count == 1, () => "multiple redemptions created"); iborIndex.registerWith(update); }
//! simple annual compounding coupon rates public FixedRateBond(int settlementDays, double faceAmount, Schedule schedule,List<double> coupons, DayCounter accrualDayCounter, BusinessDayConvention paymentConvention, double redemption, Date issueDate,Calendar paymentCalendar) : base(settlementDays, paymentCalendar == new Calendar() ? schedule.calendar() : paymentCalendar, issueDate) { frequency_ = schedule.tenor().frequency(); dayCounter_ = accrualDayCounter; maturityDate_ = schedule.endDate(); cashflows_ = new FixedRateLeg(schedule) .withCouponRates(coupons, accrualDayCounter) .withPaymentCalendar(calendar_) .withNotionals(faceAmount) .withPaymentAdjustment(paymentConvention); addRedemptionsToCashflows(new List<double>() { redemption }); if (cashflows().Count == 0) throw new ApplicationException("bond with no cashflows!"); if (redemptions_.Count != 1) throw new ApplicationException("multiple redemptions created"); }
public AssetSwap(bool parAssetSwap, Bond bond, double bondCleanPrice, double nonParRepayment, double gearing, IborIndex iborIndex, double spread = 0.0, DayCounter floatingDayCount = null, Date dealMaturity = null, bool payBondCoupon = false) : base(2) { bond_ = bond; bondCleanPrice_ = bondCleanPrice; nonParRepayment_ = nonParRepayment; spread_ = spread; parSwap_ = parAssetSwap; Schedule tempSch = new Schedule(bond_.settlementDate(), bond_.maturityDate(), iborIndex.tenor(), iborIndex.fixingCalendar(), iborIndex.businessDayConvention(), iborIndex.businessDayConvention(), DateGeneration.Rule.Backward, false); // endOfMonth if (dealMaturity == null) dealMaturity = bond_.maturityDate(); Utils.QL_REQUIRE( dealMaturity <= tempSch.dates().Last(), () => "deal maturity " + dealMaturity + " cannot be later than (adjusted) bond maturity " + tempSch.dates().Last()); Utils.QL_REQUIRE( dealMaturity > tempSch.dates()[0], () => "deal maturity " + dealMaturity + " must be later than swap start date " + tempSch.dates()[0]); // the following might become an input parameter BusinessDayConvention paymentAdjustment = BusinessDayConvention.Following; Date finalDate = tempSch.calendar().adjust(dealMaturity, paymentAdjustment); Schedule schedule = tempSch.until(finalDate); // bondCleanPrice must be the (forward) clean price // at the floating schedule start date upfrontDate_ = schedule.startDate(); double dirtyPrice = bondCleanPrice_ + bond_.accruedAmount(upfrontDate_); double notional = bond_.notional(upfrontDate_); /* In the market asset swap, the bond is purchased in return for payment of the full price. The notional of the floating leg is then scaled by the full price. */ if (!parSwap_) notional *= dirtyPrice / 100.0; if (floatingDayCount == null) legs_[1] = new IborLeg(schedule, iborIndex) .withSpreads(spread) .withGearings(gearing) .withNotionals(notional) .withPaymentAdjustment(paymentAdjustment); else legs_[1] = new IborLeg(schedule, iborIndex) .withSpreads(spread) .withGearings(gearing) .withPaymentDayCounter(floatingDayCount) .withNotionals(notional) .withPaymentAdjustment(paymentAdjustment); foreach (CashFlow c in legs_[1]) c.registerWith(update); List<CashFlow> bondLeg = bond_.cashflows(); // skip bond redemption int i; for (i = 0; i < bondLeg.Count && bondLeg[i].date() <= dealMaturity; ++i) { // whatever might be the choice for the discounting engine // bond flows on upfrontDate_ must be discarded bool upfrontDateBondFlows = false; if (!bondLeg[i].hasOccurred(upfrontDate_, upfrontDateBondFlows)) legs_[0].Add(bondLeg[i]); } // if the first skipped cashflow is not the redemption // and it is a coupon then add the accrued coupon if (i < bondLeg.Count - 1) { Coupon c = bondLeg[i] as Coupon; if (c != null) { CashFlow accruedCoupon = new SimpleCashFlow(c.accruedAmount(dealMaturity), finalDate); legs_[0].Add(accruedCoupon); } } // add the nonParRepayment_ CashFlow nonParRepaymentFlow = new SimpleCashFlow(nonParRepayment_, finalDate); legs_[0].Add(nonParRepaymentFlow); Utils.QL_REQUIRE( !legs_[0].empty(), () => "empty bond leg to start with" ); // special flows if (parSwap_) { // upfront on the floating leg double upfront = (dirtyPrice - 100.0) / 100.0 * notional; CashFlow upfrontCashFlow = new SimpleCashFlow(upfront, upfrontDate_); legs_[1].Insert(0, upfrontCashFlow); // backpayment on the floating leg // (accounts for non-par redemption, if any) double backPayment = notional; CashFlow backPaymentCashFlow = new SimpleCashFlow(backPayment, finalDate); legs_[1].Add(backPaymentCashFlow); } else { // final notional exchange CashFlow finalCashFlow = new SimpleCashFlow(notional, finalDate); legs_[1].Add(finalCashFlow); } Utils.QL_REQUIRE( !legs_[0].empty(), () => "empty bond leg" ); foreach (CashFlow c in legs_[0]) c.registerWith(update); if (payBondCoupon) { payer_[0] = -1.0; payer_[1] = +1.0; } else { payer_[0] = +1.0; payer_[1] = -1.0; } }
public AssetSwap(bool parAssetSwap, Bond bond, double bondCleanPrice, double nonParRepayment, double gearing, IborIndex iborIndex, double spread = 0.0, DayCounter floatingDayCount = null, Date dealMaturity = null, bool payBondCoupon = false) : base(2) { bond_ = bond; bondCleanPrice_ = bondCleanPrice; nonParRepayment_ = nonParRepayment; spread_ = spread; parSwap_ = parAssetSwap; Schedule tempSch = new Schedule(bond_.settlementDate(), bond_.maturityDate(), iborIndex.tenor(), iborIndex.fixingCalendar(), iborIndex.businessDayConvention(), iborIndex.businessDayConvention(), DateGeneration.Rule.Backward, false); // endOfMonth if (dealMaturity == null) { dealMaturity = bond_.maturityDate(); } Utils.QL_REQUIRE(dealMaturity <= tempSch.dates().Last(), () => "deal maturity " + dealMaturity + " cannot be later than (adjusted) bond maturity " + tempSch.dates().Last()); Utils.QL_REQUIRE(dealMaturity > tempSch.dates()[0], () => "deal maturity " + dealMaturity + " must be later than swap start date " + tempSch.dates()[0]); // the following might become an input parameter BusinessDayConvention paymentAdjustment = BusinessDayConvention.Following; Date finalDate = tempSch.calendar().adjust(dealMaturity, paymentAdjustment); Schedule schedule = tempSch.until(finalDate); // bondCleanPrice must be the (forward) clean price // at the floating schedule start date upfrontDate_ = schedule.startDate(); double dirtyPrice = bondCleanPrice_ + bond_.accruedAmount(upfrontDate_); double notional = bond_.notional(upfrontDate_); /* In the market asset swap, the bond is purchased in return for * payment of the full price. The notional of the floating leg is * then scaled by the full price. */ if (!parSwap_) { notional *= dirtyPrice / 100.0; } if (floatingDayCount == null) { legs_[1] = new IborLeg(schedule, iborIndex) .withSpreads(spread) .withGearings(gearing) .withNotionals(notional) .withPaymentAdjustment(paymentAdjustment); } else { legs_[1] = new IborLeg(schedule, iborIndex) .withSpreads(spread) .withGearings(gearing) .withPaymentDayCounter(floatingDayCount) .withNotionals(notional) .withPaymentAdjustment(paymentAdjustment); } foreach (CashFlow c in legs_[1]) { c.registerWith(update); } List <CashFlow> bondLeg = bond_.cashflows(); // skip bond redemption int i; for (i = 0; i < bondLeg.Count && bondLeg[i].date() <= dealMaturity; ++i) { // whatever might be the choice for the discounting engine // bond flows on upfrontDate_ must be discarded bool upfrontDateBondFlows = false; if (!bondLeg[i].hasOccurred(upfrontDate_, upfrontDateBondFlows)) { legs_[0].Add(bondLeg[i]); } } // if the first skipped cashflow is not the redemption // and it is a coupon then add the accrued coupon if (i < bondLeg.Count - 1) { Coupon c = bondLeg[i] as Coupon; if (c != null) { CashFlow accruedCoupon = new SimpleCashFlow(c.accruedAmount(dealMaturity), finalDate); legs_[0].Add(accruedCoupon); } } // add the nonParRepayment_ CashFlow nonParRepaymentFlow = new SimpleCashFlow(nonParRepayment_, finalDate); legs_[0].Add(nonParRepaymentFlow); Utils.QL_REQUIRE(!legs_[0].empty(), () => "empty bond leg to start with"); // special flows if (parSwap_) { // upfront on the floating leg double upfront = (dirtyPrice - 100.0) / 100.0 * notional; CashFlow upfrontCashFlow = new SimpleCashFlow(upfront, upfrontDate_); legs_[1].Insert(0, upfrontCashFlow); // backpayment on the floating leg // (accounts for non-par redemption, if any) double backPayment = notional; CashFlow backPaymentCashFlow = new SimpleCashFlow(backPayment, finalDate); legs_[1].Add(backPaymentCashFlow); } else { // final notional exchange CashFlow finalCashFlow = new SimpleCashFlow(notional, finalDate); legs_[1].Add(finalCashFlow); } Utils.QL_REQUIRE(!legs_[0].empty(), () => "empty bond leg"); foreach (CashFlow c in legs_[0]) { c.registerWith(update); } if (payBondCoupon) { payer_[0] = -1.0; payer_[1] = +1.0; } else { payer_[0] = +1.0; payer_[1] = -1.0; } }
/// <summary> /// CDS quoted as upfront and running spread /// </summary> /// <param name="side">Whether the protection is bought or sold.</param> /// <param name="notional"> Notional value</param> /// <param name="upfront">Upfront in fractional units.</param> /// <param name="runningSpread">Running spread in fractional units.</param> /// <param name="schedule">Coupon schedule.</param> /// <param name="convention">Business-day convention for payment-date adjustment.</param> /// <param name="dayCounter">Day-count convention for accrual.</param> /// <param name="settlesAccrual">Whether or not the accrued coupon is due in the event of a default.</param> /// <param name="paysAtDefaultTime">If set to true, any payments triggered by a default event are /// due at default time. If set to false, they are due at the end of the accrual period.</param> /// <param name="protectionStart">The first date where a default event will trigger the contract.</param> /// <param name="upfrontDate">Settlement date for the upfront payment.</param> /// <param name="claim"></param> /// <param name="lastPeriodDayCounter">Day-count convention for accrual in last period</param> /// <param name="rebatesAccrual">The protection seller pays the accrued scheduled current coupon at the start /// of the contract. The rebate date is not provided but computed to be two days after protection start.</param> public CreditDefaultSwap(Protection.Side side, double notional, double upfront, double runningSpread, Schedule schedule, BusinessDayConvention convention, DayCounter dayCounter, bool settlesAccrual = true, bool paysAtDefaultTime = true, Date protectionStart = null, Date upfrontDate = null, Claim claim = null, DayCounter lastPeriodDayCounter = null, bool rebatesAccrual = true) { side_ = side; notional_ = notional; upfront_ = upfront; runningSpread_ = runningSpread; settlesAccrual_ = settlesAccrual; paysAtDefaultTime_ = paysAtDefaultTime; claim_ = claim; protectionStart_ = protectionStart ?? schedule[0]; Utils.QL_REQUIRE(protectionStart_ <= schedule[0] || schedule.rule() == DateGeneration.Rule.CDS , () => "protection can not start after accrual"); leg_ = new FixedRateLeg(schedule) .withLastPeriodDayCounter(lastPeriodDayCounter) .withCouponRates(runningSpread, dayCounter) .withNotionals(notional) .withPaymentAdjustment(convention); // If empty, adjust to T+3 standard settlement, alternatively add // an arbitrary date to the constructor Date effectiveUpfrontDate = upfrontDate == null? schedule.calendar().advance(protectionStart_, 2, TimeUnit.Days, convention) : upfrontDate; // '2' is used above since the protection start is assumed to be // on trade_date + 1 upfrontPayment_ = new SimpleCashFlow(notional * upfront, effectiveUpfrontDate); Utils.QL_REQUIRE(effectiveUpfrontDate >= protectionStart_, () => "upfront can not be due before contract start"); if (rebatesAccrual) { FixedRateCoupon firstCoupon = leg_[0] as FixedRateCoupon; // adjust to T+3 standard settlement, alternatively add // an arbitrary date to the constructor Date rebateDate = effectiveUpfrontDate; accrualRebate_ = new SimpleCashFlow(firstCoupon.accruedAmount(protectionStart_), rebateDate); } if (claim_ == null) { claim_ = new FaceValueClaim(); } claim_.registerWith(update); maturity_ = schedule.dates().Last(); }
public static List <CashFlow> yoyInflationLeg(List <double> notionals_, Schedule schedule_, BusinessDayConvention paymentAdjustment_, YoYInflationIndex index_, List <double> gearings_, List <double> spreads_, DayCounter paymentDayCounter_, List <double> caps_, List <double> floors_, Calendar paymentCalendar_, List <int> fixingDays_, Period observationLag_) { int n = schedule_.Count - 1; Utils.QL_REQUIRE(!notionals_.empty(), () => "no notional given"); Utils.QL_REQUIRE(notionals_.Count <= n, () => "too many nominals (" + notionals_.Count + "), only " + n + " required"); if (gearings_ != null) { Utils.QL_REQUIRE(gearings_.Count <= n, () => "too many gearings (" + gearings_.Count + "), only " + n + " required"); } if (spreads_ != null) { Utils.QL_REQUIRE(spreads_.Count <= n, () => "too many spreads (" + spreads_.Count + "), only " + n + " required"); } if (caps_ != null) { Utils.QL_REQUIRE(caps_.Count <= n, () => "too many caps (" + caps_.Count + "), only " + n + " required"); } if (floors_ != null) { Utils.QL_REQUIRE(floors_.Count <= n, () => "too many floors (" + floors_.Count + "), only " + n + " required"); } List <CashFlow> leg = new List <CashFlow>(n); Calendar calendar = paymentCalendar_; Date refStart, start, refEnd, end; for (int i = 0; i < n; ++i) { refStart = start = schedule_.date(i); refEnd = end = schedule_.date(i + 1); Date paymentDate = calendar.adjust(end, paymentAdjustment_); if (i == 0 && !schedule_.isRegular(i + 1)) { BusinessDayConvention bdc = schedule_.businessDayConvention(); refStart = schedule_.calendar().adjust(end - schedule_.tenor(), bdc); } if (i == n - 1 && !schedule_.isRegular(i + 1)) { BusinessDayConvention bdc = schedule_.businessDayConvention(); refEnd = schedule_.calendar().adjust(start + schedule_.tenor(), bdc); } if (Utils.Get(gearings_, i, 1.0).IsEqual(0.0)) { // fixed coupon leg.Add(new FixedRateCoupon(paymentDate, Utils.Get(notionals_, i, 1.0), Utils.effectiveFixedRate(spreads_, caps_, floors_, i), paymentDayCounter_, start, end, refStart, refEnd)); } else { // yoy inflation coupon if (Utils.noOption(caps_, floors_, i)) { // just swaplet YoYInflationCoupon coup = new YoYInflationCoupon(paymentDate, Utils.Get(notionals_, i, 1.0), start, end, Utils.Get(fixingDays_, i, 0), index_, observationLag_, paymentDayCounter_, Utils.Get(gearings_, i, 1.0), Utils.Get(spreads_, i, 0.0), refStart, refEnd); // in this case you can set a pricer // straight away because it only provides computation - not data YoYInflationCouponPricer pricer = new YoYInflationCouponPricer(); coup.setPricer(pricer); leg.Add(coup); } else { // cap/floorlet leg.Add(new CappedFlooredYoYInflationCoupon( paymentDate, Utils.Get(notionals_, i, 1.0), start, end, Utils.Get(fixingDays_, i, 0), index_, observationLag_, paymentDayCounter_, Utils.Get(gearings_, i, 1.0), Utils.Get(spreads_, i, 0.0), Utils.toNullable(Utils.Get(caps_, i, Double.MinValue)), Utils.toNullable(Utils.Get(floors_, i, Double.MinValue)), refStart, refEnd)); } } } return(leg); }
// constructor public FixedRateLeg(Schedule schedule) { schedule_ = schedule; calendar_ = schedule.calendar(); paymentAdjustment_ = BusinessDayConvention.Following; }
public void testDateConstructor() { // Testing the constructor taking a vector of dates and possibly additional meta information List<Date> dates = new List<Date>(); dates.Add(new Date(16, Month.May, 2015)); dates.Add(new Date(18, Month.May, 2015)); dates.Add(new Date(18, Month.May, 2016)); dates.Add(new Date(31, Month.December, 2017)); // schedule without any additional information Schedule schedule1 = new Schedule(dates); if (schedule1.Count != dates.Count) Assert.Fail("schedule1 has size {0}, expected {1}", schedule1.Count, dates.Count); for (int i = 0; i < dates.Count; ++i) if (schedule1[i] != dates[i]) Assert.Fail("schedule1 has {0} at position {1}, expected {2}", schedule1[i], i, dates[i]); if (schedule1.calendar() != new NullCalendar()) Assert.Fail("schedule1 has calendar {0}, expected null calendar", schedule1.calendar().name()); if (schedule1.businessDayConvention() != BusinessDayConvention.Unadjusted) Assert.Fail("schedule1 has convention {0}, expected unadjusted", schedule1.businessDayConvention()); // schedule with metadata List<bool> regular = new List<bool>(); regular.Add(false); regular.Add(true); regular.Add(false); Schedule schedule2 = new Schedule(dates, new TARGET(), BusinessDayConvention.Following, BusinessDayConvention.ModifiedPreceding, new Period(1, TimeUnit.Years), DateGeneration.Rule.Backward, true, regular); for (int i = 1; i < dates.Count; ++i) if (schedule2.isRegular(i) != regular[i - 1]) Assert.Fail("schedule2 has a {0} period at position {1}, expected {2}", (schedule2.isRegular(i) ? "regular" : "irregular"), i, (regular[i - 1] ? "regular" : "irregular")); if (schedule2.calendar() != new TARGET()) Assert.Fail("schedule1 has calendar {0}, expected TARGET", schedule2.calendar().name()); if (schedule2.businessDayConvention() != BusinessDayConvention.Following) Assert.Fail("schedule2 has convention {0}, expected Following", schedule2.businessDayConvention()); if (schedule2.terminationDateBusinessDayConvention() != BusinessDayConvention.ModifiedPreceding) Assert.Fail("schedule2 has convention {0}, expected Modified Preceding", schedule2.terminationDateBusinessDayConvention()); if (schedule2.tenor() != new Period(1, TimeUnit.Years)) Assert.Fail("schedule2 has tenor {0}, expected 1Y", schedule2.tenor()); if (schedule2.rule() != DateGeneration.Rule.Backward) Assert.Fail("schedule2 has rule {0}, expected Backward", schedule2.rule()); if (schedule2.endOfMonth() != true) Assert.Fail("schedule2 has end of month flag false, expected true"); }
public CPISwap(Type type, double nominal, bool subtractInflationNominal, // float+spread leg double spread, DayCounter floatDayCount, Schedule floatSchedule, BusinessDayConvention floatPaymentRoll, int fixingDays, IborIndex floatIndex, // fixed x inflation leg double fixedRate, double baseCPI, DayCounter fixedDayCount, Schedule fixedSchedule, BusinessDayConvention fixedPaymentRoll, Period observationLag, ZeroInflationIndex fixedIndex, InterpolationType observationInterpolation = InterpolationType.AsIndex, double?inflationNominal = null) : base(2) { type_ = type; nominal_ = nominal; subtractInflationNominal_ = subtractInflationNominal; spread_ = spread; floatDayCount_ = floatDayCount; floatSchedule_ = floatSchedule; floatPaymentRoll_ = floatPaymentRoll; fixingDays_ = fixingDays; floatIndex_ = floatIndex; fixedRate_ = fixedRate; baseCPI_ = baseCPI; fixedDayCount_ = fixedDayCount; fixedSchedule_ = fixedSchedule; fixedPaymentRoll_ = fixedPaymentRoll; fixedIndex_ = fixedIndex; observationLag_ = observationLag; observationInterpolation_ = observationInterpolation; Utils.QL_REQUIRE(floatSchedule_.Count > 0, () => "empty float schedule"); Utils.QL_REQUIRE(fixedSchedule_.Count > 0, () => "empty fixed schedule"); // todo if roll!=unadjusted then need calendars ... inflationNominal_ = inflationNominal ?? nominal_; List <CashFlow> floatingLeg; if (floatSchedule_.Count > 1) { floatingLeg = new IborLeg(floatSchedule_, floatIndex_) .withFixingDays(fixingDays_) .withPaymentDayCounter(floatDayCount_) .withSpreads(spread_) .withNotionals(nominal_) .withPaymentAdjustment(floatPaymentRoll_); } else { floatingLeg = new List <CashFlow>(); } if (floatSchedule_.Count == 1 || !subtractInflationNominal_ || (subtractInflationNominal && Math.Abs(nominal_ - inflationNominal_) > 0.00001) ) { Date payNotional; if (floatSchedule_.Count == 1) { // no coupons payNotional = floatSchedule_[0]; payNotional = floatSchedule_.calendar().adjust(payNotional, floatPaymentRoll_); } else { // use the pay date of the last coupon payNotional = floatingLeg.Last().date(); } double floatAmount = subtractInflationNominal_ ? nominal_ - inflationNominal_ : nominal_; CashFlow nf = new SimpleCashFlow(floatAmount, payNotional); floatingLeg.Add(nf); } // a CPIleg know about zero legs and inclusion of base inflation notional List <CashFlow> cpiLeg = new CPILeg(fixedSchedule_, fixedIndex_, baseCPI_, observationLag_) .withFixedRates(fixedRate_) .withPaymentDayCounter(fixedDayCount_) .withObservationInterpolation(observationInterpolation_) .withSubtractInflationNominal(subtractInflationNominal_) .withNotionals(inflationNominal_) .withPaymentAdjustment(fixedPaymentRoll_); foreach (CashFlow cashFlow in cpiLeg) { cashFlow.registerWith(update); } if (floatingLeg.Count > 0) { foreach (CashFlow cashFlow in floatingLeg) { cashFlow.registerWith(update); } } legs_[0] = cpiLeg; legs_[1] = floatingLeg; if (type_ == Type.Payer) { payer_[0] = 1.0; payer_[1] = -1.0; } else { payer_[0] = -1.0; payer_[1] = 1.0; } }
public static List <CashFlow> FloatingDigitalLeg <InterestRateIndexType, FloatingCouponType, DigitalCouponType>( List <double> nominals, Schedule schedule, InterestRateIndexType index, DayCounter paymentDayCounter, BusinessDayConvention paymentAdj, List <int> fixingDays, List <double> gearings, List <double> spreads, bool isInArrears, List <double> callStrikes, Position.Type callPosition, bool isCallATMIncluded, List <double> callDigitalPayoffs, List <double> putStrikes, Position.Type putPosition, bool isPutATMIncluded, List <double> putDigitalPayoffs, DigitalReplication replication) where InterestRateIndexType : InterestRateIndex, new() where FloatingCouponType : FloatingRateCoupon, new() where DigitalCouponType : DigitalCoupon, new() { int n = schedule.Count; Utils.QL_REQUIRE(!nominals.empty(), () => "no notional given"); Utils.QL_REQUIRE(nominals.Count <= n, () => "too many nominals (" + nominals.Count + "), only " + n + " required"); if (gearings != null) { Utils.QL_REQUIRE(gearings.Count <= n, () => "too many gearings (" + gearings.Count + "), only " + n + " required"); } if (spreads != null) { Utils.QL_REQUIRE(spreads.Count <= n, () => "too many spreads (" + spreads.Count + "), only " + n + " required"); } if (callStrikes != null) { Utils.QL_REQUIRE(callStrikes.Count <= n, () => "too many nominals (" + callStrikes.Count + "), only " + n + " required"); } if (putStrikes != null) { Utils.QL_REQUIRE(putStrikes.Count <= n, () => "too many nominals (" + putStrikes.Count + "), only " + n + " required"); } List <CashFlow> leg = new List <CashFlow>(); // the following is not always correct Calendar calendar = schedule.calendar(); Date refStart, start, refEnd, end; Date paymentDate; for (int i = 0; i < n; ++i) { refStart = start = schedule.date(i); refEnd = end = schedule.date(i + 1); paymentDate = calendar.adjust(end, paymentAdj); if (i == 0 && !schedule.isRegular(i + 1)) { BusinessDayConvention bdc = schedule.businessDayConvention(); refStart = calendar.adjust(end - schedule.tenor(), bdc); } if (i == n - 1 && !schedule.isRegular(i + 1)) { BusinessDayConvention bdc = schedule.businessDayConvention(); refEnd = calendar.adjust(start + schedule.tenor(), bdc); } if (Utils.Get(gearings, i, 1.0).IsEqual(0.0)) { // fixed coupon leg.Add(new FixedRateCoupon(paymentDate, Utils.Get(nominals, i, 1.0), Utils.Get(spreads, i, 1.0), paymentDayCounter, start, end, refStart, refEnd)); } else { // floating digital coupon FloatingCouponType underlying = FastActivator <FloatingCouponType> .Create().factory( Utils.Get(nominals, i, 1.0), paymentDate, start, end, Utils.Get(fixingDays, i, index.fixingDays()), index, Utils.Get(gearings, i, 1.0), Utils.Get(spreads, i, 0.0), refStart, refEnd, paymentDayCounter, isInArrears) as FloatingCouponType; DigitalCouponType digitalCoupon = FastActivator <DigitalCouponType> .Create().factory( underlying, Utils.toNullable(Utils.Get(callStrikes, i, Double.MinValue)), callPosition, isCallATMIncluded, Utils.toNullable(Utils.Get(callDigitalPayoffs, i, Double.MinValue)), Utils.toNullable(Utils.Get(putStrikes, i, Double.MinValue)), putPosition, isPutATMIncluded, Utils.toNullable(Utils.Get(putDigitalPayoffs, i, Double.MinValue)), replication) as DigitalCouponType; leg.Add(digitalCoupon); } } return(leg); }
public void testBondFromScheduleWithDateVector() { // Testing South African R2048 bond price using Schedule constructor with Date vector SavedSettings backup = new SavedSettings(); //When pricing bond from Yield To Maturity, use NullCalendar() Calendar calendar = new NullCalendar(); int settlementDays = 3; Date issueDate = new Date(29, Month.June, 2012); Date today = new Date(7, Month.September, 2015); Date evaluationDate = calendar.adjust(today); Date settlementDate = calendar.advance(evaluationDate, new Period(settlementDays, TimeUnit.Days)); Settings.setEvaluationDate(evaluationDate); // For the schedule to generate correctly for Feb-28's, make maturity date on Feb 29 Date maturityDate = new Date(29, Month.February, 2048); double coupon = 0.0875; Compounding comp = Compounding.Compounded; Frequency freq = Frequency.Semiannual; DayCounter dc = new ActualActual(ActualActual.Convention.Bond); // Yield as quoted in market InterestRate yield = new InterestRate(0.09185, dc, comp, freq); Period tenor = new Period(6, TimeUnit.Months); Period exCouponPeriod = new Period(10, TimeUnit.Days); // Generate coupon dates for 31 Aug and end of Feb each year // For leap years, this will generate 29 Feb, but the bond // actually pays coupons on 28 Feb, regardsless of whether // it is a leap year or not. Schedule schedule = new Schedule(issueDate, maturityDate, tenor, new NullCalendar(), BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted, DateGeneration.Rule.Backward, true); // Adjust the 29 Feb's to 28 Feb List<Date> dates = new List<Date>(); for (int i = 0; i < schedule.Count; ++i) { Date d = schedule.date(i); if (d.Month == 2 && d.Day == 29) dates.Add(new Date(28, Month.February, d.Year)); else dates.Add(d); } schedule = new Schedule(dates, schedule.calendar(), schedule.businessDayConvention(), schedule.terminationDateBusinessDayConvention(), schedule.tenor(), schedule.rule(), schedule.endOfMonth(), schedule.isRegular()); FixedRateBond bond = new FixedRateBond( 0, 100.0, schedule, new List<double>() { coupon }, dc, BusinessDayConvention.Following, 100.0, issueDate, calendar, exCouponPeriod, calendar, BusinessDayConvention.Unadjusted, false); double calculatedPrice = BondFunctions.dirtyPrice(bond, yield, settlementDate); double expectedPrice = 95.75706; double tolerance = 1e-5; if (Math.Abs(calculatedPrice - expectedPrice) > tolerance) { Assert.Fail(string.Format("failed to reproduce R2048 dirty price\nexpected: {0}\ncalculated: {1}", expectedPrice, calculatedPrice)); } }
public static List <CashFlow> FloatingLeg <InterestRateIndexType, FloatingCouponType, CappedFlooredCouponType>( List <double> nominals, Schedule schedule, InterestRateIndexType index, DayCounter paymentDayCounter, BusinessDayConvention paymentAdj, List <int> fixingDays, List <double> gearings, List <double> spreads, List <double> caps, List <double> floors, bool isInArrears, bool isZero) where InterestRateIndexType : InterestRateIndex, new() where FloatingCouponType : FloatingRateCoupon, new() where CappedFlooredCouponType : CappedFlooredCoupon, new() { int n = schedule.Count; Utils.QL_REQUIRE(!nominals.empty(), () => "no notional given"); Utils.QL_REQUIRE(nominals.Count <= n, () => "too many nominals (" + nominals.Count + "), only " + n + " required"); if (gearings != null) { Utils.QL_REQUIRE(gearings.Count <= n, () => "too many gearings (" + gearings.Count + "), only " + n + " required"); } if (spreads != null) { Utils.QL_REQUIRE(spreads.Count <= n, () => "too many spreads (" + spreads.Count + "), only " + n + " required"); } if (caps != null) { Utils.QL_REQUIRE(caps.Count <= n, () => "too many caps (" + caps.Count + "), only " + n + " required"); } if (floors != null) { Utils.QL_REQUIRE(floors.Count <= n, () => "too many floors (" + floors.Count + "), only " + n + " required"); } Utils.QL_REQUIRE(!isZero || !isInArrears, () => "in-arrears and zero features are not compatible"); List <CashFlow> leg = new List <CashFlow>(); // the following is not always correct Calendar calendar = schedule.calendar(); Date lastPaymentDate = calendar.adjust(schedule[n - 1], paymentAdj); for (int i = 0; i < n - 1; ++i) { Date refStart, start, refEnd, end; refStart = start = schedule[i]; refEnd = end = schedule[i + 1]; Date paymentDate = isZero ? lastPaymentDate : calendar.adjust(end, paymentAdj); if (i == 0 && !schedule.isRegular(i + 1)) { refStart = calendar.adjust(end - schedule.tenor(), schedule.businessDayConvention()); } if (i == n - 1 && !schedule.isRegular(i + 1)) { refEnd = calendar.adjust(start + schedule.tenor(), schedule.businessDayConvention()); } if (Utils.Get(gearings, i, 1).IsEqual(0.0)) { // fixed coupon leg.Add(new FixedRateCoupon(paymentDate, Utils.Get(nominals, i), Utils.effectiveFixedRate(spreads, caps, floors, i), paymentDayCounter, start, end, refStart, refEnd)); } else { if (Utils.noOption(caps, floors, i)) { leg.Add(FastActivator <FloatingCouponType> .Create().factory( Utils.Get(nominals, i), paymentDate, start, end, Utils.Get(fixingDays, i, index.fixingDays()), index, Utils.Get(gearings, i, 1), Utils.Get(spreads, i), refStart, refEnd, paymentDayCounter, isInArrears)); } else { leg.Add(FastActivator <CappedFlooredCouponType> .Create().factory( Utils.Get(nominals, i), paymentDate, start, end, Utils.Get(fixingDays, i, index.fixingDays()), index, Utils.Get(gearings, i, 1), Utils.Get(spreads, i), Utils.toNullable(Utils.Get(caps, i, Double.MinValue)), Utils.toNullable(Utils.Get(floors, i, Double.MinValue)), refStart, refEnd, paymentDayCounter, isInArrears)); } } } return(leg); }
public CPILeg(Schedule schedule, ZeroInflationIndex index, double baseCPI, Period observationLag) { schedule_ = schedule; index_ = index; baseCPI_ = baseCPI; observationLag_ = observationLag; paymentDayCounter_ = new Thirty360(); paymentAdjustment_ = BusinessDayConvention.ModifiedFollowing; paymentCalendar_ = schedule.calendar(); fixingDays_ = new List<int>() { 0 }; observationInterpolation_ = InterpolationType.AsIndex; subtractInflationNominal_ = true; spreads_ = new List<double>() { 0 }; }
public CmsRateBond(int settlementDays, double faceAmount, Schedule schedule, SwapIndex index, DayCounter paymentDayCounter, BusinessDayConvention paymentConvention = BusinessDayConvention.Following, int fixingDays = 0, List <double> gearings = null, List <double> spreads = null, List <double?> caps = null, List <double?> floors = null, bool inArrears = false, double redemption = 100.0, Date issueDate = null) : base(settlementDays, schedule.calendar(), issueDate) { // Optional value check if (gearings == null) { gearings = new List <double>() { 1 } } ; if (spreads == null) { spreads = new List <double>() { 0 } } ; if (caps == null) { caps = new List <double?>(); } if (floors == null) { floors = new List <double?>(); } maturityDate_ = schedule.endDate(); cashflows_ = new CmsLeg(schedule, index) .withPaymentDayCounter(paymentDayCounter) .withFixingDays(fixingDays) .withGearings(gearings) .withSpreads(spreads) .withCaps(caps) .withFloors(floors) .inArrears(inArrears) .withNotionals(faceAmount) .withPaymentAdjustment(paymentConvention); addRedemptionsToCashflows(new List <double>() { redemption }); Utils.QL_REQUIRE(cashflows().Count != 0, () => "bond with no cashflows!"); Utils.QL_REQUIRE(redemptions_.Count == 1, () => "multiple redemptions created"); index.registerWith(update); } } }
public AmortizingFloatingRateBond(int settlementDays, List <double> notionals, Schedule schedule, IborIndex index, DayCounter accrualDayCounter, BusinessDayConvention paymentConvention = BusinessDayConvention.Following, int fixingDays = 0, List <double> gearings = null, List <double> spreads = null, List <double> caps = null, List <double> floors = null, bool inArrears = false, Date issueDate = null) : base(settlementDays, schedule.calendar(), issueDate) { if (gearings == null) { gearings = new List <double>() { 1, 1.0 } } ; if (spreads == null) { spreads = new List <double>() { 1, 0.0 } } ; if (caps == null) { caps = new List <double>(); } if (floors == null) { floors = new List <double>(); } maturityDate_ = schedule.endDate(); cashflows_ = new IborLeg(schedule, index) .withCaps(caps) .withFloors(floors) .inArrears(inArrears) .withSpreads(spreads) .withGearings(gearings) .withFixingDays(fixingDays) .withPaymentDayCounter(accrualDayCounter) .withPaymentAdjustment(paymentConvention) .withNotionals(notionals).value(); addRedemptionsToCashflows(); Utils.QL_REQUIRE(!cashflows().empty(), "bond with no cashflows!"); index.registerWith(update); } } }