/** * Copyright (C) 2014 - present by OpenGamma Inc. and the OpenGamma group of companies * * Please see distribution for license. */ package com.opengamma.analytics.financial.instrument.index; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertTrue; import org.testng.annotations.Test; import org.threeten.bp.Period; import org.threeten.bp.ZonedDateTime; import com.opengamma.analytics.financial.datasets.CalendarUSD; import com.opengamma.analytics.financial.instrument.annuity.AnnuityDefinition; import com.opengamma.analytics.financial.instrument.payment.CouponONSpreadDefinition; import com.opengamma.analytics.financial.schedule.ScheduleCalculator; import com.opengamma.financial.convention.StubType; import com.opengamma.financial.convention.businessday.BusinessDayConventions; import com.opengamma.financial.convention.calendar.Calendar; import com.opengamma.util.money.Currency; import com.opengamma.util.test.TestGroup; import com.opengamma.util.time.DateUtils; /** * Tests the generator of legs based on compounded Overnight coupons (OIS style). */ @Test(groups = TestGroup.UNIT) public class GeneratorLegONCompoundedTest { private static final Calendar NYC = new CalendarUSD("NYC"); private static final IndexON FEDFUND = IndexONMaster.getInstance().getIndex("FED FUND"); private static final Currency USD = Currency.USD; private static final String NAME = "LEG_USD1YFEDFUND"; private static final int OFFSET_SPOT = 2; private static final int OFFSET_PAYMENT = 3; private static final GeneratorLegONCompounded GENERATOR = new GeneratorLegONCompounded(NAME, USD, FEDFUND, Period.ofMonths(12), OFFSET_SPOT, OFFSET_PAYMENT, BusinessDayConventions.MODIFIED_FOLLOWING, true, StubType.SHORT_START, false, NYC, NYC); @Test(expectedExceptions = IllegalArgumentException.class) public void nullName() { new GeneratorLegONCompounded(null, USD, FEDFUND, Period.ofMonths(12), 2, 2, BusinessDayConventions.MODIFIED_FOLLOWING, true, StubType.SHORT_START, false, NYC, NYC); } @Test(expectedExceptions = IllegalArgumentException.class) public void nullIndex() { new GeneratorLegONCompounded("LEG_USD1YFEDFUND", USD, null, Period.ofMonths(12), 2, 2, BusinessDayConventions.MODIFIED_FOLLOWING, true, StubType.SHORT_START, false, NYC, NYC); } @Test(expectedExceptions = IllegalArgumentException.class) public void nullTenor() { new GeneratorLegONCompounded("LEG_USD1YFEDFUND", USD, FEDFUND, null, 2, 2, BusinessDayConventions.MODIFIED_FOLLOWING, true, StubType.SHORT_START, false, NYC, NYC); } @Test(expectedExceptions = IllegalArgumentException.class) public void nullBusinessDay() { new GeneratorLegONCompounded("LEG_USD1YFEDFUND", USD, FEDFUND, Period.ofMonths(12), 2, 2, null, true, StubType.SHORT_START, false, NYC, NYC); } @Test(expectedExceptions = IllegalArgumentException.class) public void nullStub() { new GeneratorLegONCompounded("LEG_USD1YFEDFUND", USD, FEDFUND, Period.ofMonths(12), 2, 2, BusinessDayConventions.MODIFIED_FOLLOWING, true, null, false, NYC, NYC); } @Test(expectedExceptions = IllegalArgumentException.class) public void nullCalendarIndex() { new GeneratorLegONCompounded("LEG_USD1YFEDFUND", USD, FEDFUND, Period.ofMonths(12), 2, 2, BusinessDayConventions.MODIFIED_FOLLOWING, true, StubType.SHORT_START, false, null, NYC); } @Test(expectedExceptions = IllegalArgumentException.class) public void nullCalendarPayment() { new GeneratorLegONCompounded("LEG_USD1YFEDFUND", USD, FEDFUND, Period.ofMonths(12), 2, 2, BusinessDayConventions.MODIFIED_FOLLOWING, true, StubType.SHORT_START, false, NYC, null); } @Test public void getter() { assertEquals("GeneratorLegONCompounded: getter", NAME, GENERATOR.getName()); assertEquals("GeneratorLegONCompounded: getter", FEDFUND, GENERATOR.getIndexON()); assertEquals("GeneratorLegONCompounded: getter", Period.ofMonths(12), GENERATOR.getPaymentPeriod()); assertEquals("GeneratorLegONCompounded: getter", OFFSET_SPOT, GENERATOR.getSpotOffset()); assertEquals("GeneratorLegONCompounded: getter", OFFSET_PAYMENT, GENERATOR.getPaymentOffset()); assertEquals("GeneratorLegONCompounded: getter", BusinessDayConventions.MODIFIED_FOLLOWING, GENERATOR.getBusinessDayConvention()); assertEquals("GeneratorLegONCompounded: getter", true, GENERATOR.isEndOfMonth()); assertEquals("GeneratorLegONCompounded: getter", StubType.SHORT_START, GENERATOR.getStubType()); assertEquals("GeneratorLegONCompounded: getter", false, GENERATOR.isExchangeNotional()); assertEquals("GeneratorLegONCompounded: getter", NYC, GENERATOR.getIndexCalendar()); assertEquals("GeneratorLegONCompounded: getter", NYC, GENERATOR.getPaymentCalendar()); } @Test /** Tests the instrument generated by the generator. */ public void generateInstrument() { double notional = 1000000; double spread = 0.0025; int legTenorYear = 3; Period legTenor = Period.ofYears(legTenorYear); GeneratorAttributeIR attribute = new GeneratorAttributeIR(legTenor); ZonedDateTime valuationDate = DateUtils.getUTCDate(2014, 1, 22); AnnuityDefinition<?> instrumentDefinition = GENERATOR.generateInstrument(valuationDate, spread, notional, attribute); assertEquals("GeneratorLegONCompounded: generate - number of coupons", instrumentDefinition.getNumberOfPayments(), legTenorYear); ZonedDateTime spotDate = ScheduleCalculator.getAdjustedDate(valuationDate, GENERATOR.getSpotOffset(), NYC); ZonedDateTime startDate = spotDate; ZonedDateTime endDate; for(int loopcpn = 0 ; loopcpn<legTenorYear; loopcpn++) { assertTrue("GeneratorLegONCompounded: generate - coupon type", instrumentDefinition.getNthPayment(loopcpn) instanceof CouponONSpreadDefinition); CouponONSpreadDefinition cpn = (CouponONSpreadDefinition)instrumentDefinition.getNthPayment(loopcpn); endDate = ScheduleCalculator.getAdjustedDate(spotDate, GENERATOR.getPaymentPeriod().multipliedBy(loopcpn+1), GENERATOR.getBusinessDayConvention(), NYC, GENERATOR.isEndOfMonth()); assertEquals("GeneratorLegONCompounded: generate - start accrual date", startDate, cpn.getAccrualStartDate()); assertEquals("GeneratorLegONCompounded: generate - end accrual date", endDate, cpn.getAccrualEndDate()); ZonedDateTime paymentDate = ScheduleCalculator.getAdjustedDate( cpn.getFixingPeriodDate()[cpn.getFixingPeriodDate().length-1], GENERATOR.getPaymentOffset(), NYC); assertEquals("GeneratorLegONCompounded: generate - payment date", paymentDate, cpn.getPaymentDate()); assertEquals("GeneratorLegONCompounded: generate - spread", spread, cpn.getSpread()); startDate = endDate; } } }