// create a swap from known start/end dates
 private static SwapTrade createSwap(TradeInfo info, string conventionStr, LocalDate startDate, LocalDate endDate, BuySell buySell, double notional, double fixedRate, double?fxRateOpt)
 {
     if (fxRateOpt.HasValue)
     {
         XCcyIborIborSwapConvention convention = XCcyIborIborSwapConvention.of(conventionStr);
         double notionalFlat = notional * fxRateOpt.Value;
         return(convention.toTrade(info, startDate, endDate, buySell, notional, notionalFlat, fixedRate));
     }
     else
     {
         SingleCurrencySwapConvention convention = SingleCurrencySwapConvention.of(conventionStr);
         return(convention.toTrade(info, startDate, endDate, buySell, notional, fixedRate));
     }
 }
        // parse a trade based on a convention
        internal 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));
            double?                      fxRateOpt            = row.findValue(FX_RATE_FIELD).map(str => LoaderUtils.parseDouble(str));

            // explicit dates take precedence over relative ones
            if (startDateOpt.Present && endDateOpt.Present)
            {
                if (periodToStartOpt.Present || tenorOpt.Present)
                {
                    throw new System.ArgumentException("Swap trade had invalid combination of fields. When these fields are found " + ImmutableList.of(CONVENTION_FIELD, START_DATE_FIELD, END_DATE_FIELD) + " then these fields must not be present " + ImmutableList.of(PERIOD_TO_START_FIELD, TENOR_FIELD));
                }
                LocalDate startDate = startDateOpt.get();
                LocalDate endDate   = endDateOpt.get();
                SwapTrade trade     = createSwap(info, conventionStr, startDate, endDate, buySell, notional, fixedRate, fxRateOpt);
                return(adjustTrade(trade, rollCnvOpt, stubCnvOpt, firstRegStartDateOpt, lastRegEndDateOpt, dateCnv, dateCalOpt));
            }

            // start date + tenor
            if (startDateOpt.Present && tenorOpt.Present)
            {
                if (periodToStartOpt.Present || endDateOpt.Present)
                {
                    throw new System.ArgumentException("Swap trade had invalid combination of fields. When these fields are found " + ImmutableList.of(CONVENTION_FIELD, START_DATE_FIELD, TENOR_FIELD) + " then these fields must not be present " + ImmutableList.of(PERIOD_TO_START_FIELD, END_DATE_FIELD));
                }
                LocalDate startDate = startDateOpt.get();
                Tenor     tenor     = tenorOpt.get();
                LocalDate endDate   = startDate.plus(tenor);
                SwapTrade trade     = createSwap(info, conventionStr, startDate, endDate, buySell, notional, fixedRate, fxRateOpt);
                return(adjustTrade(trade, rollCnvOpt, stubCnvOpt, firstRegStartDateOpt, lastRegEndDateOpt, dateCnv, dateCalOpt));
            }

            // relative dates
            if (periodToStartOpt.Present && tenorOpt.Present && info.TradeDate.Present)
            {
                if (startDateOpt.Present || endDateOpt.Present)
                {
                    throw new System.ArgumentException("Swap trade had invalid combination of fields. When these fields are found " + ImmutableList.of(CONVENTION_FIELD, PERIOD_TO_START_FIELD, TENOR_FIELD, TRADE_DATE_FIELD) + " then these fields must not be present " + ImmutableList.of(START_DATE_FIELD, END_DATE_FIELD));
                }
                LocalDate tradeDate     = info.TradeDate.get();
                Period    periodToStart = periodToStartOpt.get();
                Tenor     tenor         = tenorOpt.get();
                if (fxRateOpt.HasValue)
                {
                    XCcyIborIborSwapConvention convention = XCcyIborIborSwapConvention.of(conventionStr);
                    double    notionalFlat = notional * fxRateOpt.Value;
                    SwapTrade trade        = convention.createTrade(tradeDate, periodToStart, tenor, buySell, notional, notionalFlat, fixedRate, resolver.ReferenceData);
                    trade = trade.toBuilder().info(info).build();
                    return(adjustTrade(trade, rollCnvOpt, stubCnvOpt, firstRegStartDateOpt, lastRegEndDateOpt, dateCnv, dateCalOpt));
                }
                else
                {
                    SingleCurrencySwapConvention convention = SingleCurrencySwapConvention.of(conventionStr);
                    SwapTrade trade = convention.createTrade(tradeDate, periodToStart, tenor, buySell, notional, fixedRate, resolver.ReferenceData);
                    trade = trade.toBuilder().info(info).build();
                    return(adjustTrade(trade, rollCnvOpt, stubCnvOpt, firstRegStartDateOpt, lastRegEndDateOpt, dateCnv, dateCalOpt));
                }
            }

            // no match
            throw new System.ArgumentException("Swap trade had invalid combination of fields. These fields are mandatory:" + ImmutableList.of(BUY_SELL_FIELD, NOTIONAL_FIELD, FIXED_RATE_FIELD) + " and one of these combinations is mandatory: " + ImmutableList.of(CONVENTION_FIELD, TRADE_DATE_FIELD, PERIOD_TO_START_FIELD, TENOR_FIELD) + " or " + ImmutableList.of(CONVENTION_FIELD, START_DATE_FIELD, TENOR_FIELD) + " or " + ImmutableList.of(CONVENTION_FIELD, START_DATE_FIELD, END_DATE_FIELD));
        }