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 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 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"); }