private static SwapTrade adjustTrade( SwapTrade trade, Optional<RollConvention> rollConventionOpt, Optional<StubConvention> stubConventionOpt, Optional<LocalDate> firstRegularStartDateOpt, Optional<LocalDate> lastRegEndDateOpt, BusinessDayConvention dateCnv, Optional<HolidayCalendarId> dateCalOpt) { if (!rollConventionOpt.isPresent() && !stubConventionOpt.isPresent() && !firstRegularStartDateOpt.isPresent() && !lastRegEndDateOpt.isPresent() && !dateCalOpt.isPresent()) { return trade; } ImmutableList.Builder<SwapLeg> legBuilder = ImmutableList.builder(); for (SwapLeg leg : trade.getProduct().getLegs()) { RateCalculationSwapLeg swapLeg = (RateCalculationSwapLeg) leg; PeriodicSchedule.Builder scheduleBuilder = swapLeg.getAccrualSchedule().toBuilder(); rollConventionOpt.ifPresent(rc -> scheduleBuilder.rollConvention(rc)); stubConventionOpt.ifPresent(sc -> scheduleBuilder.stubConvention(sc)); firstRegularStartDateOpt.ifPresent(date -> scheduleBuilder.firstRegularStartDate(date)); lastRegEndDateOpt.ifPresent(date -> scheduleBuilder.lastRegularEndDate(date)); dateCalOpt.ifPresent(cal -> scheduleBuilder.businessDayAdjustment(BusinessDayAdjustment.of(dateCnv, cal))); legBuilder.add(swapLeg.toBuilder() .accrualSchedule(scheduleBuilder.build()) .build()); } return replaceLegs(trade, legBuilder.build()); }
private static SwapTrade parseVariableNotional(SwapTrade trade, List<CsvRow> variableRows) { // parse notionals ImmutableList.Builder<ValueStep> stepBuilder = ImmutableList.builder(); for (CsvRow row : variableRows) { LocalDate date = LoaderUtils.parseDate(row.getValue(START_DATE_FIELD)); row.findValue(NOTIONAL_FIELD) .map(str -> LoaderUtils.parseDouble(str)) .ifPresent(notional -> stepBuilder.add(ValueStep.of(date, ValueAdjustment.ofReplace(notional)))); } ImmutableList<ValueStep> varNotionals = stepBuilder.build(); if (varNotionals.isEmpty()) { return trade; } // adjust the trade, inserting the variable notionals ImmutableList.Builder<SwapLeg> legBuilder = ImmutableList.builder(); for (SwapLeg swapLeg : trade.getProduct().getLegs()) { RateCalculationSwapLeg leg = (RateCalculationSwapLeg) swapLeg; NotionalSchedule notionalSchedule = leg.getNotionalSchedule().toBuilder() .amount(ValueSchedule.of(leg.getNotionalSchedule().getAmount().getInitialValue(), varNotionals)) .build(); legBuilder.add(leg.toBuilder().notionalSchedule(notionalSchedule).build()); } return replaceLegs(trade, legBuilder.build()); }
private static SwapTrade parseVariableRates(SwapTrade trade, List<CsvRow> variableRows) { ImmutableList.Builder<ValueStep> stepBuilder = ImmutableList.builder(); for (CsvRow row : variableRows) { LocalDate date = LoaderUtils.parseDate(row.getValue(START_DATE_FIELD)); row.findValue(FIXED_RATE_FIELD) .map(str -> LoaderUtils.parseDoublePercent(str)) .ifPresent(fixedRate -> stepBuilder.add(ValueStep.of(date, ValueAdjustment.ofReplace(fixedRate)))); } ImmutableList<ValueStep> varRates = stepBuilder.build(); if (varRates.isEmpty()) { return trade; } // adjust the trade, inserting the variable rates ImmutableList.Builder<SwapLeg> legBuilder = ImmutableList.builder(); for (SwapLeg swapLeg : trade.getProduct().getLegs()) { RateCalculationSwapLeg leg = (RateCalculationSwapLeg) swapLeg; if (leg.getCalculation() instanceof FixedRateCalculation) { FixedRateCalculation baseCalc = (FixedRateCalculation) leg.getCalculation(); FixedRateCalculation calc = baseCalc.toBuilder() .rate(ValueSchedule.of(baseCalc.getRate().getInitialValue(), varRates)) .build(); legBuilder.add(leg.toBuilder().calculation(calc).build()); } else { legBuilder.add(leg); } } return replaceLegs(trade, legBuilder.build()); }