public void testDefaultSettlementDate() { // Testing default evaluation date in cashflows methods... Date today = Settings.Instance.evaluationDate(); Schedule schedule = new MakeSchedule() .from(today - new Period(2, TimeUnit.Months)).to(today + new Period(4, TimeUnit.Months)) .withFrequency(Frequency.Semiannual) .withCalendar(new TARGET()) .withConvention(BusinessDayConvention.Unadjusted) .backwards().value(); List <CashFlow> leg = new FixedRateLeg(schedule) .withCouponRates(0.03, new Actual360()) .withPaymentCalendar(new TARGET()) .withNotionals(100.0) .withPaymentAdjustment(BusinessDayConvention.Following); double accruedPeriod = CashFlows.accruedPeriod(leg, false); if (accruedPeriod == 0.0) { QAssert.Fail("null accrued period with default settlement date"); } int accruedDays = CashFlows.accruedDays(leg, false); if (accruedDays == 0) { QAssert.Fail("no accrued days with default settlement date"); } double accruedAmount = CashFlows.accruedAmount(leg, false); if (accruedAmount == 0.0) { QAssert.Fail("null accrued amount with default settlement date"); } }
public void testInArrears() { // Testing in-arrears swap calculation CommonVars vars = new CommonVars(); /* See Hull, 4th ed., page 550 * Note: the calculation in the book is wrong (work out the adjustment and you'll get 0.05 + 0.000115 T1) */ Date maturity = vars.today + new Period(5, TimeUnit.Years); Calendar calendar = new NullCalendar(); Schedule schedule = new Schedule(vars.today, maturity, new Period(Frequency.Annual), calendar, BusinessDayConvention.Following, BusinessDayConvention.Following, DateGeneration.Rule.Forward, false); DayCounter dayCounter = new SimpleDayCounter(); List <double> nominals = new List <double>() { 100000000.0 }; IborIndex index = new IborIndex("dummy", new Period(1, TimeUnit.Years), 0, new EURCurrency(), calendar, BusinessDayConvention.Following, false, dayCounter, vars.termStructure); double oneYear = 0.05; double r = Math.Log(1.0 + oneYear); vars.termStructure.linkTo(Utilities.flatRate(vars.today, r, dayCounter)); List <double> coupons = new List <double>() { oneYear }; List <CashFlow> fixedLeg = new FixedRateLeg(schedule) .withCouponRates(coupons, dayCounter) .withNotionals(nominals); List <double> gearings = new List <double>(); List <double> spreads = new List <double>(); int fixingDays = 0; double capletVolatility = 0.22; var vol = new Handle <OptionletVolatilityStructure>( new ConstantOptionletVolatility(vars.today, new NullCalendar(), BusinessDayConvention.Following, capletVolatility, dayCounter)); IborCouponPricer pricer = new BlackIborCouponPricer(vol); List <CashFlow> floatingLeg = new IborLeg(schedule, index) .withPaymentDayCounter(dayCounter) .withFixingDays(fixingDays) .withGearings(gearings) .withSpreads(spreads) .inArrears() .withNotionals(nominals); Utils.setCouponPricer(floatingLeg, pricer); Swap swap = new Swap(floatingLeg, fixedLeg); swap.setPricingEngine(new DiscountingSwapEngine(vars.termStructure)); double storedValue = -144813.0; double tolerance = 1.0; if (Math.Abs(swap.NPV() - storedValue) > tolerance) { QAssert.Fail("Wrong NPV calculation:\n" + " expected: " + storedValue + "\n" + " calculated: " + swap.NPV()); } }