private static Optional<String> findValue(CsvRow row, String leg, String field) { return row.findValue(leg + field); }
static Position parsePosition(CsvRow row, PositionInfo info, PositionCsvInfoResolver resolver) { if (row.findValue(EXPIRY_FIELD).isPresent()) { // etd if (row.findValue(PUT_CALL_FIELD).isPresent() || row.findValue(EXERCISE_PRICE_FIELD).isPresent()) { return resolver.parseEtdOptionPosition(row, info); } else { return resolver.parseEtdFuturePosition(row, info); } } else { return parseNonEtdPosition(row, info, resolver); } }
static SecurityPosition parsePositionLightweight(CsvRow row, PositionInfo info, PositionCsvInfoResolver resolver) { if (row.findValue(EXPIRY_FIELD).isPresent()) { // etd if (row.findValue(PUT_CALL_FIELD).isPresent() || row.findValue(EXERCISE_PRICE_FIELD).isPresent()) { return resolver.parseEtdOptionSecurityPosition(row, info); } else { return resolver.parseEtdFutureSecurityPosition(row, info); } } else { // simple return parseSecurityPosition(row, info, resolver); } }
private static SwapTrade parseRow(CsvRow row, TradeInfo info, TradeCsvInfoResolver resolver) { Optional<String> conventionOpt = row.findValue(CONVENTION_FIELD); if (conventionOpt.isPresent()) { return parseWithConvention(row, info, resolver, conventionOpt.get()); } else { Optional<String> payReceive = row.findValue("Leg 1 " + DIRECTION_FIELD); if (payReceive.isPresent()) { return FullSwapTradeCsvLoader.parse(row, info); } throw new IllegalArgumentException( "Swap trade had invalid combination of fields. Must include either '" + CONVENTION_FIELD + "' or '" + "Leg 1 " + DIRECTION_FIELD + "'"); } }
private PortfolioItemInfo parseInfo(CsvRow row) { PortfolioItemInfo info = PortfolioItemInfo.empty(); String scheme = row.findValue(ID_SCHEME_HEADER).orElse(DEFAULT_SCHEME); StandardId id = row.findValue(ID_HEADER).map(str -> StandardId.of(scheme, str)).orElse(null); if (id != null) { info = info.withId(id); } return resolver.parseSensitivityInfo(row, info); }
static Optional<BusinessDayAdjustment> parsePaymentDateAdjustment(CsvRow row) { Optional<BusinessDayAdjustment> paymentAdj = Optional.empty(); Optional<String> paymentDateCnv = row.findValue(PAYMENT_DATE_CNV_FIELD); // Optional field with Business day adjustment if (paymentDateCnv.isPresent()) { BusinessDayConvention bdCnv = LoaderUtils.parseBusinessDayConvention(paymentDateCnv.get()); if (!bdCnv.equals(BusinessDayConventions.NO_ADJUST)) { Optional<String> paymentDateCalOpt = row.findValue(PAYMENT_DATE_CAL_FIELD); if (paymentDateCalOpt.isPresent()) { paymentAdj = Optional.of(BusinessDayAdjustment.of( LoaderUtils.parseBusinessDayConvention(paymentDateCnv.get()), HolidayCalendarId.of(paymentDateCalOpt.get()))); } } } return paymentAdj; }
private TradeInfo parseTradeInfo(CsvRow row) { TradeInfoBuilder infoBuilder = TradeInfo.builder(); String scheme = row.findField(ID_SCHEME_FIELD).orElse(DEFAULT_TRADE_SCHEME); row.findValue(ID_FIELD).ifPresent(id -> infoBuilder.id(StandardId.of(scheme, id))); String schemeCpty = row.findValue(CPTY_SCHEME_FIELD).orElse(DEFAULT_CPTY_SCHEME); row.findValue(CPTY_FIELD).ifPresent(cpty -> infoBuilder.counterparty(StandardId.of(schemeCpty, cpty))); row.findValue(TRADE_DATE_FIELD).ifPresent(dateStr -> infoBuilder.tradeDate(LoaderUtils.parseDate(dateStr))); row.findValue(TRADE_TIME_FIELD).ifPresent(timeStr -> infoBuilder.tradeTime(LoaderUtils.parseTime(timeStr))); row.findValue(TRADE_ZONE_FIELD).ifPresent(zoneStr -> infoBuilder.zone(ZoneId.of(zoneStr))); row.findValue(SETTLEMENT_DATE_FIELD).ifPresent(dateStr -> infoBuilder.settlementDate(LoaderUtils.parseDate(dateStr))); resolver.parseTradeInfo(row, infoBuilder); return infoBuilder.build(); }
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; }
static Position parseNonEtdPosition(CsvRow row, PositionInfo info, PositionCsvInfoResolver resolver) { SecurityPosition base = parseSecurityPosition(row, info, resolver); Optional<Double> tickSizeOpt = row.findValue(TICK_SIZE).map(str -> LoaderUtils.parseDouble(str)); Optional<Currency> currencyOpt = row.findValue(CURRENCY).map(str -> Currency.of(str)); Optional<Double> tickValueOpt = row.findValue(TICK_VALUE).map(str -> LoaderUtils.parseDouble(str)); double contractSize = row.findValue(CONTRACT_SIZE).map(str -> LoaderUtils.parseDouble(str)).orElse(1d); if (tickSizeOpt.isPresent() && currencyOpt.isPresent() && tickValueOpt.isPresent()) { SecurityPriceInfo priceInfo = SecurityPriceInfo.of(tickSizeOpt.get(), CurrencyAmount.of(currencyOpt.get(), tickValueOpt.get()), contractSize); GenericSecurity sec = GenericSecurity.of(SecurityInfo.of(base.getSecurityId(), priceInfo)); return GenericSecurityPosition.ofLongShort(base.getInfo(), sec, base.getLongQuantity(), base.getShortQuantity()); } return base; }
/** * Parses the quantity. * * @param row the CSV row to parse * @return the quantity, long first, short second * @throws IllegalArgumentException if the row cannot be parsed */ public static DoublesPair parseQuantity(CsvRow row) { Optional<Double> quantityOpt = row.findValue(QUANTITY_FIELD).map(s -> LoaderUtils.parseDouble(s)); if (quantityOpt.isPresent()) { double quantity = quantityOpt.get(); return DoublesPair.of(quantity >= 0 ? quantity : 0, quantity >= 0 ? 0 : -quantity); } Optional<Double> longQuantityOpt = row.findValue(LONG_QUANTITY_FIELD).map(s -> LoaderUtils.parseDouble(s)); Optional<Double> shortQuantityOpt = row.findValue(SHORT_QUANTITY_FIELD).map(s -> LoaderUtils.parseDouble(s)); if (!longQuantityOpt.isPresent() && !shortQuantityOpt.isPresent()) { throw new IllegalArgumentException( Messages.format("Security must contain a quantity column, either '{}' or '{}' and '{}'", QUANTITY_FIELD, LONG_QUANTITY_FIELD, SHORT_QUANTITY_FIELD)); } double longQuantity = ArgChecker.notNegative(longQuantityOpt.orElse(0d), LONG_QUANTITY_FIELD); double shortQuantity = ArgChecker.notNegative(shortQuantityOpt.orElse(0d), SHORT_QUANTITY_FIELD); return DoublesPair.of(longQuantity, shortQuantity); }
static SecurityQuantityTrade parseTrade(CsvRow row, TradeInfo info, TradeCsvInfoResolver resolver) { SecurityTrade trade = parseSecurityTrade(row, info, resolver); SecurityTrade base = resolver.completeTrade(row, trade); Optional<Double> tickSizeOpt = row.findValue(TICK_SIZE).map(str -> LoaderUtils.parseDouble(str)); Optional<Currency> currencyOpt = row.findValue(CURRENCY).map(str -> Currency.of(str)); Optional<Double> tickValueOpt = row.findValue(TICK_VALUE).map(str -> LoaderUtils.parseDouble(str)); double contractSize = row.findValue(CONTRACT_SIZE).map(str -> LoaderUtils.parseDouble(str)).orElse(1d); if (tickSizeOpt.isPresent() && currencyOpt.isPresent() && tickValueOpt.isPresent()) { SecurityPriceInfo priceInfo = SecurityPriceInfo.of(tickSizeOpt.get(), CurrencyAmount.of(currencyOpt.get(), tickValueOpt.get()), contractSize); GenericSecurity sec = GenericSecurity.of(SecurityInfo.of(base.getSecurityId(), priceInfo)); return GenericSecurityTrade.of(base.getInfo(), sec, base.getQuantity(), base.getPrice()); } return base; }
private PositionInfo parsePositionInfo(CsvRow row) { PositionInfoBuilder infoBuilder = PositionInfo.builder(); String scheme = row.findField(ID_SCHEME_FIELD).orElse(DEFAULT_POSITION_SCHEME); row.findValue(ID_FIELD).ifPresent(id -> infoBuilder.id(StandardId.of(scheme, id))); resolver.parsePositionInfo(row, infoBuilder); return infoBuilder.build(); }
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); }
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); }
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()); }
/** * 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); }