public void test_findIndex() { assertEquals(LoaderUtils.findIndex("GBP-LIBOR-3M"), IborIndices.GBP_LIBOR_3M); assertEquals(LoaderUtils.findIndex("GBP-SONIA"), OvernightIndices.GBP_SONIA); assertEquals(LoaderUtils.findIndex("GB-RPI"), PriceIndices.GB_RPI); assertEquals(LoaderUtils.findIndex("GBP/USD-WM"), FxIndices.GBP_USD_WM); assertThrowsIllegalArg(() -> LoaderUtils.findIndex("Rubbish")); }
public void test_parsePayReceive() { assertEquals(LoaderUtils.parsePayReceive("PAY"), PayReceive.PAY); assertEquals(LoaderUtils.parsePayReceive("Pay"), PayReceive.PAY); assertEquals(LoaderUtils.parsePayReceive("pay"), PayReceive.PAY); assertEquals(LoaderUtils.parsePayReceive("p"), PayReceive.PAY); assertEquals(LoaderUtils.parsePayReceive("RECEIVE"), PayReceive.RECEIVE); assertEquals(LoaderUtils.parsePayReceive("Receive"), PayReceive.RECEIVE); assertEquals(LoaderUtils.parsePayReceive("receive"), PayReceive.RECEIVE); assertEquals(LoaderUtils.parsePayReceive("rec"), PayReceive.RECEIVE); assertEquals(LoaderUtils.parsePayReceive("r"), PayReceive.RECEIVE); assertThrowsIllegalArg(() -> LoaderUtils.parseBoolean("Rubbish")); }
private static double parseTradeQuantity(CsvRow row) { double quantity = LoaderUtils.parseDouble(row.getValue(QUANTITY_FIELD)); Optional<BuySell> buySellOpt = row.findValue(BUY_SELL_FIELD).map(str -> LoaderUtils.parseBuySell(str)); if (buySellOpt.isPresent()) { quantity = buySellOpt.get().normalize(quantity); } return quantity; }
public void test_parseBuySell() { assertEquals(LoaderUtils.parseBuySell("BUY"), BuySell.BUY); assertEquals(LoaderUtils.parseBuySell("Buy"), BuySell.BUY); assertEquals(LoaderUtils.parseBuySell("buy"), BuySell.BUY); assertEquals(LoaderUtils.parseBuySell("b"), BuySell.BUY); assertEquals(LoaderUtils.parseBuySell("SELL"), BuySell.SELL); assertEquals(LoaderUtils.parseBuySell("Sell"), BuySell.SELL); assertEquals(LoaderUtils.parseBuySell("sell"), BuySell.SELL); assertEquals(LoaderUtils.parseBuySell("s"), BuySell.SELL); assertThrowsIllegalArg(() -> LoaderUtils.parseBoolean("Rubbish")); }
static SwapTrade parseWithConvention(CsvRow row, TradeInfo info, TradeCsvInfoResolver resolver, String conventionStr) { BuySell buySell = LoaderUtils.parseBuySell(row.getValue(BUY_SELL_FIELD)); double notional = LoaderUtils.parseDouble(row.getValue(NOTIONAL_FIELD)); double fixedRate = LoaderUtils.parseDoublePercent(row.getValue(FIXED_RATE_FIELD)); Optional<Period> periodToStartOpt = row.findValue(PERIOD_TO_START_FIELD).map(s -> LoaderUtils.parsePeriod(s)); Optional<Tenor> tenorOpt = row.findValue(TENOR_FIELD).map(s -> LoaderUtils.parseTenor(s)); Optional<LocalDate> startDateOpt = row.findValue(START_DATE_FIELD).map(s -> LoaderUtils.parseDate(s)); Optional<LocalDate> endDateOpt = row.findValue(END_DATE_FIELD).map(s -> LoaderUtils.parseDate(s)); Optional<RollConvention> rollCnvOpt = row.findValue(ROLL_CONVENTION_FIELD).map(s -> LoaderUtils.parseRollConvention(s)); Optional<StubConvention> stubCnvOpt = row.findValue(STUB_CONVENTION_FIELD).map(s -> StubConvention.of(s)); Optional<LocalDate> firstRegStartDateOpt = row.findValue(FIRST_REGULAR_START_DATE_FIELD).map(s -> LoaderUtils.parseDate(s)); Optional<LocalDate> lastRegEndDateOpt = row.findValue(LAST_REGULAR_END_DATE_FIELD).map(s -> LoaderUtils.parseDate(s)); BusinessDayConvention dateCnv = row.findValue(DATE_ADJ_CNV_FIELD) .map(s -> LoaderUtils.parseBusinessDayConvention(s)).orElse(BusinessDayConventions.MODIFIED_FOLLOWING); Optional<HolidayCalendarId> dateCalOpt = row.findValue(DATE_ADJ_CAL_FIELD).map(s -> HolidayCalendarId.of(s)); Optional<Double> fxRateOpt = row.findValue(FX_RATE_FIELD).map(str -> LoaderUtils.parseDouble(str));
private static TermDepositTrade parseRow(CsvRow row, TradeInfo info, TradeCsvInfoResolver resolver) { BuySell buySell = LoaderUtils.parseBuySell(row.getValue(BUY_SELL_FIELD)); double notional = LoaderUtils.parseDouble(row.getValue(NOTIONAL_FIELD)); double fixedRate = LoaderUtils.parseDoublePercent(row.getValue(FIXED_RATE_FIELD)); Optional<TermDepositConvention> conventionOpt = row.findValue(CONVENTION_FIELD).map(s -> TermDepositConvention.of(s)); Optional<Period> tenorOpt = row.findValue(TENOR_FIELD).map(s -> LoaderUtils.parseTenor(s).getPeriod()); Optional<LocalDate> startDateOpt = row.findValue(START_DATE_FIELD).map(s -> LoaderUtils.parseDate(s)); Optional<LocalDate> endDateOpt = row.findValue(END_DATE_FIELD).map(s -> LoaderUtils.parseDate(s)); Optional<Currency> currencyOpt = row.findValue(CURRENCY_FIELD).map(s -> Currency.parse(s)); Optional<DayCount> dayCountOpt = row.findValue(DAY_COUNT_FIELD).map(s -> LoaderUtils.parseDayCount(s)); BusinessDayConvention dateCnv = row.findValue(DATE_ADJ_CNV_FIELD) .map(s -> LoaderUtils.parseBusinessDayConvention(s)).orElse(BusinessDayConventions.MODIFIED_FOLLOWING); Optional<HolidayCalendarId> dateCalOpt = row.findValue(DATE_ADJ_CAL_FIELD).map(s -> HolidayCalendarId.of(s));
private static FraTrade parseRow(CsvRow row, TradeInfo info, TradeCsvInfoResolver resolver) { BuySell buySell = LoaderUtils.parseBuySell(row.getValue(BUY_SELL_FIELD)); double notional = LoaderUtils.parseDouble(row.getValue(NOTIONAL_FIELD)); double fixedRate = LoaderUtils.parseDoublePercent(row.getValue(FIXED_RATE_FIELD)); Optional<FraConvention> conventionOpt = row.findValue(CONVENTION_FIELD).map(s -> FraConvention.of(s)); Optional<Period> periodToStartOpt = row.findValue(PERIOD_TO_START_FIELD).map(s -> LoaderUtils.parsePeriod(s)); Optional<LocalDate> startDateOpt = row.findValue(START_DATE_FIELD).map(s -> LoaderUtils.parseDate(s)); Optional<LocalDate> endDateOpt = row.findValue(END_DATE_FIELD).map(s -> LoaderUtils.parseDate(s)); Optional<IborIndex> indexOpt = row.findValue(INDEX_FIELD).map(s -> IborIndex.of(s)); Optional<IborIndex> interpolatedOpt = row.findValue(INTERPOLATED_INDEX_FIELD).map(s -> IborIndex.of(s)); Optional<DayCount> dayCountOpt = row.findValue(DAY_COUNT_FIELD).map(s -> LoaderUtils.parseDayCount(s)); BusinessDayConvention dateCnv = row.findValue(DATE_ADJ_CNV_FIELD) .map(s -> LoaderUtils.parseBusinessDayConvention(s)).orElse(BusinessDayConventions.MODIFIED_FOLLOWING); Optional<HolidayCalendarId> dateCalOpt = row.findValue(DATE_ADJ_CAL_FIELD).map(s -> HolidayCalendarId.of(s));
double fixedRate = LoaderUtils.parseDoublePercent(getValue(row, leg, FIXED_RATE_FIELD)); DayCount dayCount = findValue(row, leg, DAY_COUNT_FIELD) .map(s -> LoaderUtils.parseDayCount(s)).orElse(defaultFixedLegDayCount); if (dayCount == null) { throw new IllegalArgumentException("Swap leg must define day count using '" + leg + DAY_COUNT_FIELD + "'"); .map(s -> LoaderUtils.parseDoublePercent(s)); Optional<Double> initialStubAmountOpt = findValue(row, leg, INITIAL_STUB_AMOUNT_FIELD) .map(s -> LoaderUtils.parseDouble(s)); if (initialStubRateOpt.isPresent() && initialStubAmountOpt.isPresent()) { throw new IllegalArgumentException( .map(s -> LoaderUtils.parseDoublePercent(s)); Optional<Double> finalStubAmountOpt = findValue(row, leg, FINAL_STUB_AMOUNT_FIELD) .map(s -> LoaderUtils.parseDouble(s)); if (finalStubRateOpt.isPresent() && finalStubAmountOpt.isPresent()) { throw new IllegalArgumentException(
private static FxSwapTrade parseConvention(CsvRow row, TradeInfo info) { CurrencyPair pair = CurrencyPair.parse(row.getValue(CONVENTION_FIELD)); BuySell buySell = LoaderUtils.parseBuySell(row.getValue(BUY_SELL_FIELD)); Currency currency = Currency.parse(row.getValue(CURRENCY_FIELD)); double notional = LoaderUtils.parseDouble(row.getValue(NOTIONAL_FIELD)); double nearFxRate = LoaderUtils.parseDouble(row.getValue(FX_RATE_FIELD)); double farFxRate = LoaderUtils.parseDouble(row.getValue(FAR_FX_RATE_DATE_FIELD)); LocalDate nearPaymentDate = LoaderUtils.parseDate(row.getValue(PAYMENT_DATE_FIELD)); LocalDate farPaymentDate = LoaderUtils.parseDate(row.getValue(FAR_PAYMENT_DATE_FIELD)); Optional<BusinessDayAdjustment> paymentAdj = FxSingleTradeCsvLoader.parsePaymentDateAdjustment(row); CurrencyAmount amount = CurrencyAmount.of(currency, buySell.normalize(notional)); FxRate nearRate = FxRate.of(pair, nearFxRate); FxRate farRate = FxRate.of(pair, farFxRate); FxSwap fx = paymentAdj .map(adj -> FxSwap.of(amount, nearRate, nearPaymentDate, farRate, farPaymentDate, adj)) .orElseGet(() -> FxSwap.of(amount, nearRate, nearPaymentDate, farRate, farPaymentDate)); return FxSwapTrade.of(info, fx); }
static FxSingle parseFxSingle(CsvRow row, String prefix) { PayReceive direction1 = LoaderUtils.parsePayReceive(row.getValue(prefix + LEG_1_DIRECTION_FIELD)); Currency currency1 = Currency.of(row.getValue(prefix + LEG_1_CURRENCY_FIELD)); double notional1 = LoaderUtils.parseDouble(row.getValue(prefix + LEG_1_NOTIONAL_FIELD)); LocalDate paymentDate1 = row.findValue(prefix + LEG_1_PAYMENT_DATE_FIELD) .map(str -> LoaderUtils.parseDate(str)) .orElseGet(() -> LoaderUtils.parseDate(row.getValue(prefix + PAYMENT_DATE_FIELD))); PayReceive direction2 = LoaderUtils.parsePayReceive(row.getValue(prefix + LEG_2_DIRECTION_FIELD)); Currency currency2 = Currency.of(row.getValue(prefix + LEG_2_CURRENCY_FIELD)); double notional2 = LoaderUtils.parseDouble(row.getValue(prefix + LEG_2_NOTIONAL_FIELD)); LocalDate paymentDate2 = row.findValue(prefix + LEG_2_PAYMENT_DATE_FIELD) .map(str -> LoaderUtils.parseDate(str)) .orElseGet(() -> LoaderUtils.parseDate(row.getValue(prefix + PAYMENT_DATE_FIELD))); Optional<BusinessDayAdjustment> paymentAdj = parsePaymentDateAdjustment(row); if (direction1.equals(direction2)) { throw new IllegalArgumentException(Messages.format( "FxSingle legs must not have the same direction: {}, {}", direction1.toString(), direction2.toString())); } Payment payment1 = Payment.of(currency1, direction1.normalize(notional1), paymentDate1); Payment payment2 = Payment.of(currency2, direction2.normalize(notional2), paymentDate2); return paymentAdj .map(adj -> FxSingle.of(payment1, payment2, adj)) .orElseGet(() -> FxSingle.of(payment1, payment2)); }
String valueStr = row.getField(VALUE_FIELD); Index index = LoaderUtils.findIndex(referenceStr); ObservableId id = IndexQuoteId.of(index); double value = Double.parseDouble(valueStr); if (index instanceof PriceIndex) { try { YearMonth ym = LoaderUtils.parseYearMonth(dateStr); date = ym.atEndOfMonth(); } catch (RuntimeException ex) { date = LoaderUtils.parseDate(dateStr); if (date.getDayOfMonth() != date.lengthOfMonth()) { throw new IllegalArgumentException( date = LoaderUtils.parseDate(dateStr);
public void test_parseDouble() { assertEquals(LoaderUtils.parseDouble("1.2"), 1.2d, 1e-10); assertThrowsIllegalArg(() -> LoaderUtils.parseDouble("Rubbish"), "Unable to parse double from 'Rubbish'"); }
public void test_parsePutCall() { assertEquals(LoaderUtils.parsePutCall("PUT"), PutCall.PUT); assertEquals(LoaderUtils.parsePutCall("Put"), PutCall.PUT); assertEquals(LoaderUtils.parsePutCall("put"), PutCall.PUT); assertEquals(LoaderUtils.parsePutCall("p"), PutCall.PUT); assertEquals(LoaderUtils.parsePutCall("CALL"), PutCall.CALL); assertEquals(LoaderUtils.parsePutCall("Call"), PutCall.CALL); assertEquals(LoaderUtils.parsePutCall("call"), PutCall.CALL); assertEquals(LoaderUtils.parsePutCall("c"), PutCall.CALL); assertThrowsIllegalArg(() -> LoaderUtils.parseBoolean("Rubbish")); }
private static CurveNodeDate parseDate(String dateStr) { if (dateStr.isEmpty()) { return CurveNodeDate.END; } if (dateStr.length() == 10 && dateStr.charAt(4) == '-' && dateStr.charAt(7) == '-') { return CurveNodeDate.of(LoaderUtils.parseDate(dateStr)); } String dateUpper = dateStr.toUpperCase(Locale.ENGLISH); if (dateUpper.equals("END")) { return CurveNodeDate.END; } if (dateUpper.equals("LASTFIXING")) { return CurveNodeDate.LAST_FIXING; } throw new IllegalArgumentException(Messages.format( "Invalid format for node date, should be date in 'yyyy-MM-dd' format, 'End' or 'LastFixing': {}", dateUpper)); }
String interpolatedField) { Optional<Double> stubRateOpt = findValue(row, leg, rateField).map(s -> LoaderUtils.parseDoublePercent(s)); Optional<Double> stubAmountOpt = findValue(row, leg, amountField).map(s -> LoaderUtils.parseDouble(s)); Optional<IborIndex> stubIndexOpt = findValue(row, leg, indexField).map(s -> IborIndex.of(s)); Optional<IborIndex> stubIndex2Opt = findValue(row, leg, interpolatedField).map(s -> IborIndex.of(s));
YearMonth yearMonth = LoaderUtils.parseYearMonth(row.getValue(EXPIRY_FIELD)); int week = row.findValue(EXPIRY_WEEK_FIELD).map(s -> LoaderUtils.parseInteger(s)).orElse(0); int day = row.findValue(EXPIRY_DAY_FIELD).map(s -> LoaderUtils.parseInteger(s)).orElse(0); Optional<EtdSettlementType> settleType = row.findValue(SETTLEMENT_TYPE_FIELD).map(s -> parseEtdSettlementType(s)); Optional<EtdOptionType> optionType = row.findValue(EXERCISE_STYLE_FIELD).map(s -> parseEtdOptionType(s));
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()); }
builder.amount(ValueSchedule.of(LoaderUtils.parseDouble(getValueWithFallback(row, leg, NOTIONAL_FIELD)))); .map(s -> LoaderUtils.parseBoolean(s)) .ifPresent(v -> builder.initialExchange(v)); findValue(row, leg, NOTIONAL_INTERMEDIATE_EXCHANGE_FIELD) .map(s -> LoaderUtils.parseBoolean(s)) .ifPresent(v -> builder.intermediateExchange(v)); findValue(row, leg, NOTIONAL_FINAL_EXCHANGE_FIELD) .map(s -> LoaderUtils.parseBoolean(s)) .ifPresent(v -> builder.finalExchange(v)); return builder.build();
private static Optional<AdjustableDate> parseAdjustableDate( CsvRow row, String leg, String dateField, String cnvField, String calField) { Optional<LocalDate> dateOpt = findValue(row, leg, dateField).map(s -> LoaderUtils.parseDate(s)); if (!dateOpt.isPresent()) { return Optional.empty(); } BusinessDayConvention dateCnv = findValue(row, leg, cnvField) .map(s -> LoaderUtils.parseBusinessDayConvention(s)) .orElse(BusinessDayConventions.MODIFIED_FOLLOWING); HolidayCalendarId cal = findValue(row, leg, calField) .map(s -> HolidayCalendarId.of(s)) .orElse(HolidayCalendarIds.NO_HOLIDAYS); return Optional.of(AdjustableDate.of(dateOpt.get(), BusinessDayAdjustment.of(dateCnv, cal))); }