/** * Resolves the sequence to a list of steps. * * @param existingSteps the existing list of steps * @param rollConv the roll convention * @return the steps */ List<ValueStep> resolve(List<ValueStep> existingSteps, RollConvention rollConv) { ImmutableList.Builder<ValueStep> steps = ImmutableList.builder(); steps.addAll(existingSteps); LocalDate prev = firstStepDate; LocalDate date = firstStepDate; while (!date.isAfter(lastStepDate)) { steps.add(ValueStep.of(date, adjustment)); prev = date; date = rollConv.next(date, frequency); } if (!prev.equals(lastStepDate)) { throw new IllegalArgumentException(Messages.format( "ValueStepSequence lastStepDate did not match frequency '{}' using roll convention '{}', {} != {}", frequency, rollConv, lastStepDate, prev)); } return steps.build(); }
public void test_of_intAdjustment() { ValueStep test = ValueStep.of(2, DELTA_MINUS_2000); assertEquals(test.getDate(), Optional.empty()); assertEquals(test.getPeriodIndex(), OptionalInt.of(2)); assertEquals(test.getValue(), DELTA_MINUS_2000); }
@Override public ValueStep build() { return new ValueStep( periodIndex, date, value); }
public void equals() { ValueStep a1 = ValueStep.of(2, DELTA_MINUS_2000); ValueStep a2 = ValueStep.of(2, DELTA_MINUS_2000); ValueStep b = ValueStep.of(1, DELTA_MINUS_2000); ValueStep c = ValueStep.of(2, ABSOLUTE_100); assertEquals(a1.equals(a1), true); assertEquals(a1.equals(a2), true); assertEquals(a1.equals(b), false); assertEquals(a1.equals(c), false); ValueStep d1 = ValueStep.of(date(2014, 6, 30), DELTA_MINUS_2000); ValueStep d2 = ValueStep.of(date(2014, 6, 30), DELTA_MINUS_2000); ValueStep e = ValueStep.of(date(2014, 7, 30), DELTA_MINUS_2000); ValueStep f = ValueStep.of(date(2014, 7, 30), ABSOLUTE_100); assertEquals(d1.equals(d1), true); assertEquals(d1.equals(d2), true); assertEquals(d1.equals(e), false); assertEquals(d1.equals(f), false); }
List<ValueStep> invalidSteps = new ArrayList<>(); for (ValueStep step : resolvedSteps) { int index = step.findIndex(periods); if (index < 0) { invalidSteps.add(step); continue; if (expandedSteps[index] != null && !expandedSteps[index].equals(step.getValue())) { throw new IllegalArgumentException(Messages.format( "Invalid ValueSchedule, two steps resolved to the same schedule period starting on {}, schedule defined as {}", periods.get(index).getUnadjustedStartDate(), this)); expandedSteps[index] = step.getValue(); double baseValue = result[step.findPreviousIndex(periods)]; double adjusted = step.getValue().adjust(baseValue); if (adjusted != baseValue) { throw new IllegalArgumentException("ValueStep date does not match a period boundary: " + step.getDate().get());
/** * Restricted copy constructor. * @param beanToCopy the bean to copy from, not null */ private Builder(ValueStep beanToCopy) { this.periodIndex = beanToCopy.periodIndex; this.date = beanToCopy.date; this.value = beanToCopy.getValue(); }
public void test_builder_invalid() { assertThrowsIllegalArg(() -> ValueStep.builder().value(DELTA_MINUS_2000).build()); assertThrowsIllegalArg(() -> ValueStep.builder().date(date(2014, 6, 30)).periodIndex(1).value(DELTA_MINUS_2000).build()); assertThrowsIllegalArg(() -> ValueStep.builder().periodIndex(0).value(DELTA_MINUS_2000).build()); assertThrowsIllegalArg(() -> ValueStep.builder().periodIndex(-1).value(DELTA_MINUS_2000).build()); }
@Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case -980601967: // periodIndex return ((ValueStep) bean).periodIndex; case 3076014: // date return ((ValueStep) bean).date; case 111972721: // value return ((ValueStep) bean).getValue(); } return super.propertyGet(bean, propertyName, quiet); }
public void coverage() { coverImmutableBean(ValueStep.of(2, DELTA_MINUS_2000)); }
public void test_of_dateAdjustment() { ValueStep test = ValueStep.of(date(2014, 6, 30), DELTA_MINUS_2000); assertEquals(test.getDate(), Optional.of(date(2014, 6, 30))); assertEquals(test.getPeriodIndex(), OptionalInt.empty()); assertEquals(test.getValue(), DELTA_MINUS_2000); }
CmsPeriod period2 = CmsPeriod.builder() .currency(EUR) .floorlet(FLOOR.getSteps().get(0).getValue().getModifyingValue()) .notional(-NOTIONAL.getSteps().get(0).getValue().getModifyingValue()) .index(INDEX) .startDate(end1) CmsPeriod period2End = CmsPeriod.builder() .currency(EUR) .floorlet(FLOOR.getSteps().get(0).getValue().getModifyingValue()) .notional(-NOTIONAL.getSteps().get(0).getValue().getModifyingValue()) .index(INDEX) .startDate(end1) CmsPeriod periodCap2 = CmsPeriod.builder() .currency(EUR) .notional(-NOTIONAL.getSteps().get(0).getValue().getModifyingValue()) .index(INDEX) .caplet(CAP.getInitialValue())
/** * Obtains an instance that applies at the specified date. * <p> * This factory obtains a step that causes the value to change at the specified date. * <p> * The value may be absolute or relative, as per {@link ValueAdjustment}. * * @param date the start date of the value change * @param value the adjustment to make to the value * @return the varying step */ public static ValueStep of(LocalDate date, ValueAdjustment value) { return new ValueStep(null, date, value); }
public void test_serialization() { assertSerialization(ValueStep.of(2, DELTA_MINUS_2000)); }
/** * Obtains an instance that applies at the specified schedule period index. * <p> * This factory is used to define the date that the step occurs in relative terms. * The date is identified by specifying the zero-based index of the schedule period boundary. * The change will occur at the start of the specified period. * Thus an index of zero is the start of the first period or initial stub. * The index must be one or greater, as a change is not permitted at the start of the first period. * <p> * For example, consider a 5 year swap from 2012-02-01 to 2017-02-01 with 6 month frequency. * A zero-based index of '2' would refer to start of the 3rd period, which would be 2013-02-01. * <p> * The value may be absolute or relative, as per {@link ValueAdjustment}. * * @param periodIndex the index of the period of the value change * @param value the adjustment to make to the value * @return the varying step */ public static ValueStep of(int periodIndex, ValueAdjustment value) { return new ValueStep(periodIndex, null, value); }
public void test_resolve() { ValueStepSequence test = ValueStepSequence.of(date(2016, 4, 20), date(2016, 10, 20), Frequency.P3M, ADJ); ValueStep baseStep = ValueStep.of(date(2016, 1, 20), ValueAdjustment.ofReplace(500d)); List<ValueStep> steps = test.resolve(ImmutableList.of(baseStep), RollConventions.NONE); assertEquals(steps.size(), 4); assertEquals(steps.get(0), baseStep); assertEquals(steps.get(1), ValueStep.of(date(2016, 4, 20), ADJ)); assertEquals(steps.get(2), ValueStep.of(date(2016, 7, 20), ADJ)); assertEquals(steps.get(3), ValueStep.of(date(2016, 10, 20), ADJ)); }
private ValueSchedule parseSchedule(XmlElement scheduleEl, double initialValue, ValueStepSequence seq, FpmlDocument document) { List<XmlElement> stepEls = scheduleEl.getChildren("step"); ImmutableList.Builder<ValueStep> stepBuilder = ImmutableList.builder(); for (XmlElement stepEl : stepEls) { LocalDate stepDate = document.parseDate(stepEl.getChild("stepDate")); double stepValue = document.parseDecimal(stepEl.getChild("stepValue")); stepBuilder.add(ValueStep.of(stepDate, ValueAdjustment.ofReplace(stepValue))); } return ValueSchedule.builder().initialValue(initialValue).steps(stepBuilder.build()).stepSequence(seq).build(); }
public void test_resolveValues_indexBased_duplicateDefinitionValid() { ValueStep step1 = ValueStep.of(1, ValueAdjustment.ofReplace(300d)); ValueStep step2 = ValueStep.of(1, ValueAdjustment.ofReplace(300d)); ValueSchedule test = ValueSchedule.of(200d, ImmutableList.of(step1, step2)); assertEquals(test.resolveValues(SCHEDULE), DoubleArray.of(200d, 300d, 300d)); }
public void test_resolveValues_indexBased_duplicateDefinitionInvalid() { ValueStep step1 = ValueStep.of(1, ValueAdjustment.ofReplace(300d)); ValueStep step2 = ValueStep.of(1, ValueAdjustment.ofReplace(400d)); ValueSchedule test = ValueSchedule.of(200d, ImmutableList.of(step1, step2)); assertThrowsIllegalArg(() -> test.resolveValues(SCHEDULE)); }
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()); }
public void test_resolveValues_dateBased_ignoreExcess() { ValueStep step1 = ValueStep.of(date(2014, 2, 1), ValueAdjustment.ofReplace(300d)); ValueStep step2 = ValueStep.of(date(2014, 2, 15), ValueAdjustment.ofReplace(300d)); // no change to value ValueStep step3 = ValueStep.of(date(2014, 3, 1), ValueAdjustment.ofReplace(400d)); ValueStep step4 = ValueStep.of(date(2014, 3, 15), ValueAdjustment.ofDeltaAmount(0d)); // no change to value ValueStep step5 = ValueStep.of(date(2014, 4, 1), ValueAdjustment.ofMultiplier(1d)); ValueSchedule test = ValueSchedule.of(200d, ImmutableList.of(step1, step2, step3, step4, step5)); assertEquals(test.resolveValues(SCHEDULE), DoubleArray.of(200d, 300d, 400d)); }