InterestRateSwapSecurityConverter extends FinancialSecurityVisitorAdapter<InstrumentDefinition<?>> { Pair<CouponStub, CouponStub> parseStubs(final StubCalculationMethod stubCalcMethod) { CouponStub startStub = null; CouponStub endStub = null; if (stubCalcMethod != null) { stubCalcMethod.validate(); StubType stubType = stubCalcMethod.getType(); double firstStubRate = stubCalcMethod.hasFirstStubRate() ? stubCalcMethod.getFirstStubRate() : Double.NaN; LocalDate firstStubDate = stubCalcMethod.getFirstStubEndDate(); ExternalId firstStubStartReferenceRateId = stubCalcMethod.getFirstStubStartReferenceRateId(); IborIndex firstStubStartIndex = null; if (stubCalcMethod.hasFirstStubStartReferenceRateId()) { firstStubStartIndex = (IborIndex) getIborIndex(firstStubStartReferenceRateId); } IborIndex firstStubEndIndex = null; ExternalId firstStubEndReferenceRateId = stubCalcMethod.getFirstStubEndReferenceRateId(); if (stubCalcMethod.hasFirstStubEndReferenceRateId()) { firstStubEndIndex = (IborIndex) getIborIndex(firstStubEndReferenceRateId); } double finalStubRate = stubCalcMethod.hasLastStubRate() ? stubCalcMethod.getLastStubRate() : Double.NaN; LocalDate finalStubDate = stubCalcMethod.getLastStubEndDate(); ExternalId lastStubStartReferenceRateId = stubCalcMethod.getLastStubStartReferenceRateId(); IborIndex lastStubStartIndex = null; if (stubCalcMethod.hasLastStubStartReferenceRateId()) { lastStubStartIndex = (IborIndex) getIborIndex(lastStubStartReferenceRateId); } IborIndex lastStubEndIndex = null; ExternalId lastStubEndReferenceRateId = stubCalcMethod.getLastStubEndReferenceRateId(); if (stubCalcMethod.hasLastStubEndReferenceRateId()) { lastStubEndIndex = (IborIndex) getIborIndex(lastStubEndReferenceRateId); } if (StubType.BOTH == stubType) { if (!Double.isNaN(firstStubRate)) { startStub = new CouponStub(stubType, firstStubDate, stubCalcMethod.getFirstStubRate()); } else if (firstStubStartIndex != null && firstStubEndIndex != null) { startStub = new CouponStub(stubType, firstStubDate, firstStubStartIndex, firstStubEndIndex); } else { startStub = new CouponStub(stubType, firstStubDate); } if (!Double.isNaN(finalStubRate)) { endStub = new CouponStub(stubType, finalStubDate, stubCalcMethod.getLastStubRate()); } else if (lastStubStartIndex != null && lastStubEndIndex != null) { endStub = new CouponStub(stubType, finalStubDate, lastStubStartIndex, lastStubEndIndex); } else { endStub = new CouponStub(stubType, finalStubDate); } } else if (StubType.LONG_START == stubType || StubType.SHORT_START == stubType) { if (!Double.isNaN(firstStubRate)) { startStub = new CouponStub(stubType, firstStubDate, firstStubRate); } else if (firstStubStartIndex != null && firstStubEndIndex != null) { startStub = new CouponStub(stubType, firstStubStartIndex, firstStubEndIndex); } else { startStub = new CouponStub(stubType); } } else if (StubType.LONG_END == stubType || StubType.SHORT_END == stubType) { if (!Double.isNaN(finalStubRate)) { endStub = new CouponStub(stubType, finalStubDate, finalStubRate); } else if (lastStubStartIndex != null && lastStubEndIndex != null) { endStub = new CouponStub(stubType, lastStubStartIndex, lastStubEndIndex); } else { endStub = new CouponStub(stubType); } } else if (stubType != null) { startStub = new CouponStub(stubType); endStub = new CouponStub(stubType); } } return Pairs.of(startStub, endStub); } @Inject  InterestRateSwapSecurityConverter(final HolidaySource holidaySource); @Deprecated  InterestRateSwapSecurityConverter(
      HolidaySource holidaySource,
      ConventionSource conventionSource,
      SecuritySource securitySource); @Override InstrumentDefinition<?> visitForwardSwapSecurity(final ForwardSwapSecurity security); @Override InstrumentDefinition<?> visitInterestRateSwapSecurity(final InterestRateSwapSecurity security); @Override boolean equals(Object o); @Override int hashCode();  }
@Test(expectedExceptions = OpenGammaRuntimeException.class) public void testStubBadlyDefinedShortStart() { StubCalculationMethod.Builder shortStubBuilder = StubCalculationMethod.builder() .type(StubType.SHORT_START) .firstStubStartReferenceRateId(ExternalId.of(TICKER, USDLIBOR1M_NAME)); InterestRateSwapNotional notional = new InterestRateSwapNotional(Currency.USD, 1000000); PeriodFrequency freq3m = PeriodFrequency.of(Period.ofMonths(3)); Set<ExternalId> calendarUSNY = Sets.newHashSet(ExternalId.of(ExternalSchemes.ISDA_HOLIDAY, "USNY")); FloatingInterestRateSwapLeg receiveLeg = new FloatingInterestRateSwapLeg(); receiveLeg.setNotional(notional); receiveLeg.setDayCountConvention(DayCounts.ACT_360); receiveLeg.setPaymentDateFrequency(freq3m); receiveLeg.setPaymentDateBusinessDayConvention(BusinessDayConventions.MODIFIED_FOLLOWING); receiveLeg.setPaymentDateCalendars(calendarUSNY); receiveLeg.setAccrualPeriodFrequency(freq3m); receiveLeg.setAccrualPeriodBusinessDayConvention(BusinessDayConventions.MODIFIED_FOLLOWING); receiveLeg.setAccrualPeriodCalendars(calendarUSNY); receiveLeg.setResetPeriodFrequency(freq3m); receiveLeg.setResetPeriodBusinessDayConvention(BusinessDayConventions.MODIFIED_FOLLOWING); receiveLeg.setResetPeriodCalendars(calendarUSNY); receiveLeg.setFixingDateBusinessDayConvention(BusinessDayConventions.PRECEDING); receiveLeg.setFixingDateCalendars(calendarUSNY); receiveLeg.setFixingDateOffset(-2); receiveLeg.setFloatingRateType(FloatingRateType.IBOR); receiveLeg.setFloatingReferenceRateId(ExternalId.of(TICKER, USDLIBOR3M_NAME)); receiveLeg.setPayReceiveType(PayReceiveType.RECEIVE); receiveLeg.setStubCalculationMethod(shortStubBuilder.build()); InterestRateSwapSecurityConverter conv = new InterestRateSwapSecurityConverter(HOLIDAY_SOURCE); conv.parseStubs(receiveLeg.getStubCalculationMethod()); }
@Test(expectedExceptions = OpenGammaRuntimeException.class) public void testStubBadlyDefinedShortLast() { StubCalculationMethod.Builder shortStubBuilder = StubCalculationMethod.builder() .type(StubType.SHORT_END) .lastStubStartReferenceRateId(ExternalId.of(TICKER, USDLIBOR1M_NAME)); InterestRateSwapNotional notional = new InterestRateSwapNotional(Currency.USD, 1000000); PeriodFrequency freq3m = PeriodFrequency.of(Period.ofMonths(3)); Set<ExternalId> calendarUSNY = Sets.newHashSet(ExternalId.of(ExternalSchemes.ISDA_HOLIDAY, "USNY")); FloatingInterestRateSwapLeg receiveLeg = new FloatingInterestRateSwapLeg(); receiveLeg.setNotional(notional); receiveLeg.setDayCountConvention(DayCounts.ACT_360); receiveLeg.setPaymentDateFrequency(freq3m); receiveLeg.setPaymentDateBusinessDayConvention(BusinessDayConventions.MODIFIED_FOLLOWING); receiveLeg.setPaymentDateCalendars(calendarUSNY); receiveLeg.setAccrualPeriodFrequency(freq3m); receiveLeg.setAccrualPeriodBusinessDayConvention(BusinessDayConventions.MODIFIED_FOLLOWING); receiveLeg.setAccrualPeriodCalendars(calendarUSNY); receiveLeg.setResetPeriodFrequency(freq3m); receiveLeg.setResetPeriodBusinessDayConvention(BusinessDayConventions.MODIFIED_FOLLOWING); receiveLeg.setResetPeriodCalendars(calendarUSNY); receiveLeg.setFixingDateBusinessDayConvention(BusinessDayConventions.PRECEDING); receiveLeg.setFixingDateCalendars(calendarUSNY); receiveLeg.setFixingDateOffset(-2); receiveLeg.setFloatingRateType(FloatingRateType.IBOR); receiveLeg.setFloatingReferenceRateId(ExternalId.of(TICKER, USDLIBOR3M_NAME)); receiveLeg.setPayReceiveType(PayReceiveType.RECEIVE); receiveLeg.setStubCalculationMethod(shortStubBuilder.build()); InterestRateSwapSecurityConverter conv = new InterestRateSwapSecurityConverter(HOLIDAY_SOURCE); conv.parseStubs(receiveLeg.getStubCalculationMethod()); }
@Test(expectedExceptions = OpenGammaRuntimeException.class) public void testStubBadlyDefinedLongStart() { StubCalculationMethod.Builder shortStubBuilder = StubCalculationMethod.builder() .type(StubType.LONG_START) .firstStubStartReferenceRateId(ExternalId.of(TICKER, USDLIBOR1M_NAME)); InterestRateSwapNotional notional = new InterestRateSwapNotional(Currency.USD, 1000000); PeriodFrequency freq3m = PeriodFrequency.of(Period.ofMonths(3)); Set<ExternalId> calendarUSNY = Sets.newHashSet(ExternalId.of(ExternalSchemes.ISDA_HOLIDAY, "USNY")); FloatingInterestRateSwapLeg receiveLeg = new FloatingInterestRateSwapLeg(); receiveLeg.setNotional(notional); receiveLeg.setDayCountConvention(DayCounts.ACT_360); receiveLeg.setPaymentDateFrequency(freq3m); receiveLeg.setPaymentDateBusinessDayConvention(BusinessDayConventions.MODIFIED_FOLLOWING); receiveLeg.setPaymentDateCalendars(calendarUSNY); receiveLeg.setAccrualPeriodFrequency(freq3m); receiveLeg.setAccrualPeriodBusinessDayConvention(BusinessDayConventions.MODIFIED_FOLLOWING); receiveLeg.setAccrualPeriodCalendars(calendarUSNY); receiveLeg.setResetPeriodFrequency(freq3m); receiveLeg.setResetPeriodBusinessDayConvention(BusinessDayConventions.MODIFIED_FOLLOWING); receiveLeg.setResetPeriodCalendars(calendarUSNY); receiveLeg.setFixingDateBusinessDayConvention(BusinessDayConventions.PRECEDING); receiveLeg.setFixingDateCalendars(calendarUSNY); receiveLeg.setFixingDateOffset(-2); receiveLeg.setFloatingRateType(FloatingRateType.IBOR); receiveLeg.setFloatingReferenceRateId(ExternalId.of(TICKER, USDLIBOR3M_NAME)); receiveLeg.setPayReceiveType(PayReceiveType.RECEIVE); receiveLeg.setStubCalculationMethod(shortStubBuilder.build()); InterestRateSwapSecurityConverter conv = new InterestRateSwapSecurityConverter(HOLIDAY_SOURCE); conv.parseStubs(receiveLeg.getStubCalculationMethod()); }
@Test(expectedExceptions = OpenGammaRuntimeException.class) public void testStubUndefinedIndexId() { StubCalculationMethod.Builder shortStubBuilder = StubCalculationMethod.builder() .type(StubType.SHORT_END) .lastStubStartReferenceRateId(ExternalId.of(TICKER, "ZARJIBAR3M")) .lastStubEndReferenceRateId(ExternalId.of(TICKER, "ZARJIBAR12M")); InterestRateSwapNotional notional = new InterestRateSwapNotional(Currency.USD, 1000000); PeriodFrequency freq3m = PeriodFrequency.of(Period.ofMonths(3)); Set<ExternalId> calendarUSNY = Sets.newHashSet(ExternalId.of(ExternalSchemes.ISDA_HOLIDAY, "USNY")); FloatingInterestRateSwapLeg receiveLeg = new FloatingInterestRateSwapLeg(); receiveLeg.setNotional(notional); receiveLeg.setDayCountConvention(DayCounts.ACT_360); receiveLeg.setPaymentDateFrequency(freq3m); receiveLeg.setPaymentDateBusinessDayConvention(BusinessDayConventions.MODIFIED_FOLLOWING); receiveLeg.setPaymentDateCalendars(calendarUSNY); receiveLeg.setAccrualPeriodFrequency(freq3m); receiveLeg.setAccrualPeriodBusinessDayConvention(BusinessDayConventions.MODIFIED_FOLLOWING); receiveLeg.setAccrualPeriodCalendars(calendarUSNY); receiveLeg.setResetPeriodFrequency(freq3m); receiveLeg.setResetPeriodBusinessDayConvention(BusinessDayConventions.MODIFIED_FOLLOWING); receiveLeg.setResetPeriodCalendars(calendarUSNY); receiveLeg.setFixingDateBusinessDayConvention(BusinessDayConventions.PRECEDING); receiveLeg.setFixingDateCalendars(calendarUSNY); receiveLeg.setFixingDateOffset(-2); receiveLeg.setFloatingRateType(FloatingRateType.IBOR); receiveLeg.setFloatingReferenceRateId(ExternalId.of(TICKER, USDLIBOR3M_NAME)); receiveLeg.setPayReceiveType(PayReceiveType.RECEIVE); receiveLeg.setStubCalculationMethod(shortStubBuilder.build()); InterestRateSwapSecurityConverter conv = new InterestRateSwapSecurityConverter(HOLIDAY_SOURCE); conv.parseStubs(receiveLeg.getStubCalculationMethod()); }