private static SecurityTrade parseSecurityTrade(CsvRow row, TradeInfo info, TradeCsvInfoResolver resolver) { String securityIdScheme = row.findValue(SECURITY_ID_SCHEME_FIELD).orElse(DEFAULT_SECURITY_SCHEME); String securityIdValue = row.getValue(SECURITY_ID_FIELD); SecurityId securityId = SecurityId.of(securityIdScheme, securityIdValue); double price = LoaderUtils.parseDouble(row.getValue(PRICE_FIELD)); double quantity = parseTradeQuantity(row); return SecurityTrade.of(info, securityId, quantity, price); }
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; }
private static OvernightIndex parseOvernightIndex(CsvRow row) { String name = row.getValue(NAME_FIELD); Currency currency = Currency.parse(row.getValue(CURRENCY_FIELD)); boolean active = Boolean.parseBoolean(row.getValue(ACTIVE_FIELD)); DayCount dayCount = DayCount.of(row.getValue(DAY_COUNT_FIELD)); HolidayCalendarId fixingCal = HolidayCalendarId.of(row.getValue(FIXING_CALENDAR_FIELD)); int publicationDays = Integer.parseInt(row.getValue(PUBLICATION_DAYS_FIELD)); int effectiveDays = Integer.parseInt(row.getValue(EFFECTIVE_DAYS_FIELD)); DayCount fixedLegDayCount = DayCount.of(row.getValue(FIXED_LEG_DAY_COUNT)); // build result return ImmutableOvernightIndex.builder() .name(name) .currency(currency) .active(active) .dayCount(dayCount) .fixingCalendar(fixingCal) .publicationDateOffset(publicationDays) .effectiveDateOffset(effectiveDays) .defaultFixedLegDayCount(fixedLegDayCount) .build(); }
/** * Parses the contract specification from the row. * * @param row the CSV row to parse * @param type the ETD type * @return the contract specification * @throws IllegalArgumentException if the specification is not found */ public default EtdContractSpec parseEtdContractSpec(CsvRow row, EtdType type) { ExchangeId exchangeId = ExchangeId.of(row.getValue(EXCHANGE_FIELD)); EtdContractCode contractCode = EtdContractCode.of(row.getValue(CONTRACT_CODE_FIELD)); EtdContractSpecId specId = EtdIdUtils.contractSpecId(type, exchangeId, contractCode); return getReferenceData().findValue(specId).orElseThrow( () -> new IllegalArgumentException("ETD contract specification not found in reference data: " + specId)); }
private static FxSingleTrade 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 fxRate = LoaderUtils.parseDouble(row.getValue(FX_RATE_FIELD)); LocalDate paymentDate = LoaderUtils.parseDate(row.getValue(PAYMENT_DATE_FIELD)); Optional<BusinessDayAdjustment> paymentAdj = parsePaymentDateAdjustment(row); CurrencyAmount amount = CurrencyAmount.of(currency, buySell.normalize(notional)); FxSingle fx = paymentAdj .map(adj -> FxSingle.of(amount, FxRate.of(pair, fxRate), paymentDate, adj)) .orElseGet(() -> FxSingle.of(amount, FxRate.of(pair, fxRate), paymentDate)); return FxSingleTrade.of(info, fx); }
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); }
@Override public PortfolioItemInfo parseSensitivityInfo(CsvRow row, PortfolioItemInfo info) { return info.withAttribute(CCP_ATTR, row.getValue("CCP")); }
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)); }
/** * Parses an ETD option position from the CSV row without using reference data. * <p> * This returns a {@link SecurityPosition} based on a standard ETD identifier from {@link EtdIdUtils}. * * @param row the CSV row to parse * @param info the position information * @return the loaded positions, position-level errors are captured in the result * @throws IllegalArgumentException if the row cannot be parsed */ public default SecurityPosition parseEtdOptionSecurityPosition(CsvRow row, PositionInfo info) { ExchangeId exchangeId = ExchangeId.of(row.getValue(EXCHANGE_FIELD)); EtdContractCode contractCode = EtdContractCode.of(row.getValue(CONTRACT_CODE_FIELD)); Pair<YearMonth, EtdVariant> variant = CsvLoaderUtils.parseEtdVariant(row, EtdType.OPTION); int version = row.findValue(VERSION_FIELD).map(Integer::parseInt).orElse(DEFAULT_OPTION_VERSION_NUMBER); PutCall putCall = LoaderUtils.parsePutCall(row.getValue(PUT_CALL_FIELD)); double strikePrice = Double.parseDouble(row.getValue(EXERCISE_PRICE_FIELD)); YearMonth underlyingExpiry = row.findValue(UNDERLYING_EXPIRY_FIELD) .map(str -> LoaderUtils.parseYearMonth(str)) .orElse(null); SecurityId securityId = EtdIdUtils.optionId( exchangeId, contractCode, variant.getFirst(), variant.getSecond(), version, putCall, strikePrice, underlyingExpiry); DoublesPair quantity = CsvLoaderUtils.parseQuantity(row); SecurityPosition position = SecurityPosition.ofLongShort(info, securityId, quantity.getFirst(), quantity.getSecond()); return completePosition(row, position); }
public void nextBatch_predicate() { try (CsvIterator csvFile = CsvIterator.of(CharSource.wrap(CSV5GROUPED), true)) { ImmutableList<String> headers = csvFile.headers(); assertEquals(headers.size(), 2); assertEquals(headers.get(0), "id"); assertEquals(headers.get(1), "value"); int batches = 0; int total = 0; while (csvFile.hasNext()) { CsvRow first = csvFile.peek(); String id = first.getValue("id"); List<CsvRow> batch = csvFile.nextBatch(row -> row.getValue("id").equals(id)); assertEquals(batch.stream().map(row -> row.getValue("id")).distinct().count(), 1); batches++; total += batch.size(); } assertEquals(batches, 3); assertEquals(total, 6); } }
private static IborIndex parseIborIndex(CsvRow row) { String name = row.getValue(NAME_FIELD); Currency currency = Currency.parse(row.getValue(CURRENCY_FIELD)); boolean active = Boolean.parseBoolean(row.getValue(ACTIVE_FIELD)); DayCount dayCount = DayCount.of(row.getValue(DAY_COUNT_FIELD)); HolidayCalendarId fixingCal = HolidayCalendarId.of(row.getValue(FIXING_CALENDAR_FIELD)); int offsetDays = Integer.parseInt(row.getValue(OFFSET_DAYS_FIELD)); HolidayCalendarId offsetCal = HolidayCalendarId.of(row.getValue(OFFSET_CALENDAR_FIELD)); HolidayCalendarId effectiveCal = HolidayCalendarId.of(row.getValue(EFFECTIVE_DATE_CALENDAR_FIELD)); Tenor tenor = Tenor.parse(row.getValue(TENOR_FIELD)); LocalTime time = LocalTime.parse(row.getValue(FIXING_TIME_FIELD), TIME_FORMAT); ZoneId zoneId = ZoneId.of(row.getValue(FIXING_ZONE_FIELD)); DayCount fixedLegDayCount = DayCount.of(row.getValue(FIXED_LEG_DAY_COUNT));
static SecurityPosition parseSecurityPosition(CsvRow row, PositionInfo info, PositionCsvInfoResolver resolver) { String securityIdScheme = row.findValue(SECURITY_ID_SCHEME_FIELD).orElse(DEFAULT_SECURITY_SCHEME); String securityIdValue = row.getValue(SECURITY_ID_FIELD); SecurityId securityId = SecurityId.of(securityIdScheme, securityIdValue); DoublesPair quantity = CsvLoaderUtils.parseQuantity(row); SecurityPosition position = SecurityPosition.ofLongShort(info, securityId, quantity.getFirst(), quantity.getSecond()); return resolver.completePosition(row, position); }
/** * Parses an ETD future position from the CSV row. * <p> * This is intended to use reference data to find the ETD future security, * returning it as an instance of {@link EtdOptionPosition}. * The reference data lookup uses {@link #parseEtdContractSpec(CsvRow, EtdType)} by default, * however it could be overridden to lookup the security directly in reference data. * * @param row the CSV row * @param info the position info * @return the parsed position * @throws IllegalArgumentException if the row cannot be parsed */ public default Position parseEtdOptionPosition(CsvRow row, PositionInfo info) { EtdContractSpec contract = parseEtdContractSpec(row, EtdType.OPTION); Pair<YearMonth, EtdVariant> variant = CsvLoaderUtils.parseEtdVariant(row, EtdType.OPTION); int version = row.findValue(VERSION_FIELD).map(Integer::parseInt).orElse(DEFAULT_OPTION_VERSION_NUMBER); PutCall putCall = LoaderUtils.parsePutCall(row.getValue(PUT_CALL_FIELD)); double strikePrice = Double.parseDouble(row.getValue(EXERCISE_PRICE_FIELD)); YearMonth underlyingExpiry = row.findValue(UNDERLYING_EXPIRY_FIELD) .map(str -> LoaderUtils.parseYearMonth(str)) .orElse(null); EtdOptionSecurity security = contract.createOption( variant.getFirst(), variant.getSecond(), version, putCall, strikePrice, underlyingExpiry); DoublesPair quantity = CsvLoaderUtils.parseQuantity(row); EtdOptionPosition position = EtdOptionPosition.ofLongShort(info, security, quantity.getFirst(), quantity.getSecond()); return completePosition(row, position, contract); }
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);
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()); }
try { ParameterMetadata metadata = parseMetadata(batchRow, true); CurveName reference = CurveName.of(batchRow.getValue(REFERENCE_HEADER)); CurveName resolvedCurveName = resolver.checkCurveName(reference); for (Entry<String, CurveSensitivitiesType> entry : types.entrySet()) {
for (CsvRow batchRow : batchRows) { try { CurveName reference = CurveName.of(batchRow.getValue(REFERENCE_HEADER)); CurveName resolvedCurveName = resolver.checkCurveName(reference); CurveSensitivitiesType type = CurveSensitivitiesType.of(batchRow.getValue(TYPE_HEADER)); ParameterMetadata metadata = parseMetadata(batchRow, false); Currency currency = parseCurrency(batchRow, reference);
/** * Parses an ETD future position from the CSV row without using reference data. * <p> * This returns a {@link SecurityPosition} based on a standard ETD identifier from {@link EtdIdUtils}. * * @param row the CSV row to parse * @param info the position information * @return the loaded positions, position-level errors are captured in the result * @throws IllegalArgumentException if the row cannot be parsed */ public default SecurityPosition parseEtdFutureSecurityPosition(CsvRow row, PositionInfo info) { ExchangeId exchangeId = ExchangeId.of(row.getValue(EXCHANGE_FIELD)); EtdContractCode contractCode = EtdContractCode.of(row.getValue(CONTRACT_CODE_FIELD)); Pair<YearMonth, EtdVariant> variant = CsvLoaderUtils.parseEtdVariant(row, EtdType.FUTURE); SecurityId securityId = EtdIdUtils.futureId(exchangeId, contractCode, variant.getFirst(), variant.getSecond()); DoublesPair quantity = CsvLoaderUtils.parseQuantity(row); SecurityPosition position = SecurityPosition.ofLongShort(info, securityId, quantity.getFirst(), quantity.getSecond()); return completePosition(row, position); }
assertThrowsIllegalArg(() -> csvFile.row(0).getField("zzz")); assertEquals(csvFile.row(0).getValue("h1"), "r11"); assertEquals(csvFile.row(0).getValue("h2"), "r12"); assertEquals(csvFile.row(1).getValue("h1"), "r21"); assertEquals(csvFile.row(1).getValue("h2"), "r22"); assertThrowsIllegalArg(() -> csvFile.row(0).getValue("zzz")); assertThrowsIllegalArg(() -> csvFile.row(2).getValue("h2")); assertThrowsIllegalArg(() -> csvFile.row(0).getField(Pattern.compile("zzz"))); assertEquals(csvFile.row(0).getValue(Pattern.compile("h[13]")), "r11"); assertEquals(csvFile.row(0).getValue(Pattern.compile("h[24]")), "r12"); assertThrowsIllegalArg(() -> csvFile.row(0).getValue(Pattern.compile("zzz"))); assertThrowsIllegalArg(() -> csvFile.row(2).getValue(Pattern.compile("h2")));