Beispiel #1
0
 // parse a single file
 private void parse(CharSource charSource, ListMultimap <string, CurveSensitivities> parsed, IList <FailureItem> failures)
 {
     try
     {
         using (CsvIterator csv = CsvIterator.of(charSource, true))
         {
             if (!csv.containsHeader(TENOR_HEADER) && !csv.containsHeader(DATE_HEADER))
             {
                 failures.Add(FailureItem.of(FailureReason.PARSING, "CSV file could not be parsed as sensitivities, invalid format"));
             }
             else if (csv.containsHeader(REFERENCE_HEADER) && csv.containsHeader(TYPE_HEADER) && csv.containsHeader(VALUE_HEADER))
             {
                 parseStandardFormat(csv, parsed, failures);
             }
             else if (csv.containsHeader(REFERENCE_HEADER))
             {
                 parseListFormat(csv, parsed, failures);
             }
             else
             {
                 parseGridFormat(csv, parsed, failures);
             }
         }
     }
     catch (Exception ex)
     {
         failures.Add(FailureItem.of(FailureReason.PARSING, ex, "CSV file could not be parsed: {}", ex.Message));
     }
 }
Beispiel #2
0
        //-------------------------------------------------------------------------
        // parses the file in grid format
        private void parseGridFormat(CsvIterator csv, ListMultimap <string, CurveSensitivities> parsed, IList <FailureItem> failures)
        {
            // find the applicable reference columns
            IDictionary <string, CurveName> references = new LinkedHashMap <string, CurveName>();

            foreach (string header in csv.headers())
            {
                string headerLowerCase = header.ToLower(Locale.ENGLISH);
                if (!REF_HEADERS.contains(headerLowerCase) && !resolver.isInfoColumn(headerLowerCase))
                {
                    references[header] = CurveName.of(header);
                }
            }

            // loop around all rows, peeking to match batches with the same identifier
            // no exception catch at this level to avoid infinite loops
            while (csv.hasNext())
            {
                CsvRow            peekedRow = csv.peek();
                PortfolioItemInfo info      = parseInfo(peekedRow);
//JAVA TO C# CONVERTER TODO TASK: Method reference arbitrary object instance method syntax is not converted by Java to C# Converter:
                string id = info.Id.map(StandardId::toString).orElse("");

                // process in batches, where the ID is the same
                CurveSensitivitiesBuilder builder   = CurveSensitivities.builder(info);
                IList <CsvRow>            batchRows = csv.nextBatch(r => matchId(r, id));
                foreach (CsvRow batchRow in batchRows)
                {
                    try
                    {
                        ParameterMetadata      metadata = parseMetadata(batchRow, true);
                        CurveSensitivitiesType type     = batchRow.findValue(TYPE_HEADER).map(str => CurveSensitivitiesType.of(str)).orElse(CurveSensitivitiesType.ZERO_RATE_DELTA);
                        foreach (KeyValuePair <string, CurveName> entry in references.SetOfKeyValuePairs())
                        {
                            CurveName reference         = entry.Value;
                            CurveName resolvedCurveName = resolver.checkCurveName(reference);
                            string    valueStr          = batchRow.getField(entry.Key);
                            Currency  currency          = parseCurrency(batchRow, reference);
                            if (valueStr.Length > 0)
                            {
                                double value = LoaderUtils.parseDouble(valueStr);
                                builder.add(type, resolvedCurveName, currency, metadata, value);
                            }
                        }
                    }
                    catch (System.ArgumentException ex)
                    {
                        failures.Add(FailureItem.of(PARSING, "CSV file could not be parsed at line {}: {}", batchRow.lineNumber(), ex.Message));
                    }
                }
                CurveSensitivities sens = builder.build();
                if (!sens.TypedSensitivities.Empty)
                {
                    parsed.put(sens.Id.map(object.toString).orElse(""), sens);
                }
            }
        }
Beispiel #3
0
 /// <summary>
 /// Parses one or more CSV format trade files with a quiet type filter.
 /// <para>
 /// A type is specified to filter the trades.
 /// Trades that do not match the type are silently dropped.
 /// </para>
 /// <para>
 /// CSV files sometimes contain a Unicode Byte Order Mark.
 /// Callers are responsible for handling this, such as by using <seealso cref="UnicodeBom"/>.
 ///
 /// </para>
 /// </summary>
 /// @param <T>  the trade type </param>
 /// <param name="charSources">  the CSV character sources </param>
 /// <param name="tradeType">  the trade type to return </param>
 /// <returns> the loaded trades, all errors are captured in the result </returns>
 public ValueWithFailures <IList <T> > parse <T>(ICollection <CharSource> charSources, Type <T> tradeType) where T : com.opengamma.strata.product.Trade
 {
     try
     {
         ValueWithFailures <IList <T> > result = ValueWithFailures.of(ImmutableList.of());
         foreach (CharSource charSource in charSources)
         {
             ValueWithFailures <IList <T> > singleResult = parseFile(charSource, tradeType);
             result = result.combinedWith(singleResult, Guavate.concatToList);
         }
         return(result);
     }
     catch (Exception ex)
     {
         return(ValueWithFailures.of(ImmutableList.of(), FailureItem.of(FailureReason.ERROR, ex)));
     }
 }
Beispiel #4
0
 // loads a single CSV file, filtering by trade type
 private ValueWithFailures <IList <T> > parseFile <T>(CharSource charSource, Type <T> tradeType) where T : com.opengamma.strata.product.Trade
 {
     try
     {
         using (CsvIterator csv = CsvIterator.of(charSource, true))
         {
             if (!csv.headers().contains(TYPE_FIELD))
             {
                 return(ValueWithFailures.of(ImmutableList.of(), FailureItem.of(FailureReason.PARSING, "CSV file does not contain '{header}' header: {}", TYPE_FIELD, charSource)));
             }
             return(parseFile(csv, tradeType));
         }
     }
     catch (Exception ex)
     {
         return(ValueWithFailures.of(ImmutableList.of(), FailureItem.of(FailureReason.PARSING, ex, "CSV file could not be parsed: {exceptionMessage}: {}", ex.Message, charSource)));
     }
 }
Beispiel #5
0
        //-------------------------------------------------------------------------
        // parses the file in standard format
        private void parseStandardFormat(CsvIterator csv, ListMultimap <string, CurveSensitivities> parsed, IList <FailureItem> failures)
        {
            // loop around all rows, peeking to match batches with the same identifier
            // no exception catch at this level to avoid infinite loops
            while (csv.hasNext())
            {
                CsvRow            peekedRow = csv.peek();
                PortfolioItemInfo info      = parseInfo(peekedRow);
//JAVA TO C# CONVERTER TODO TASK: Method reference arbitrary object instance method syntax is not converted by Java to C# Converter:
                string id = info.Id.map(StandardId::toString).orElse("");

                // process in batches, where the ID is the same
                CurveSensitivitiesBuilder builder   = CurveSensitivities.builder(info);
                IList <CsvRow>            batchRows = csv.nextBatch(r => matchId(r, id));
                foreach (CsvRow batchRow in 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);
                        string   valueStr = batchRow.getField(VALUE_HEADER);
                        if (valueStr.Length > 0)
                        {
                            double value = LoaderUtils.parseDouble(valueStr);
                            builder.add(type, resolvedCurveName, currency, metadata, value);
                        }
                    }
                    catch (System.ArgumentException ex)
                    {
                        failures.Add(FailureItem.of(PARSING, "CSV file could not be parsed at line {}: {}", batchRow.lineNumber(), ex.Message));
                    }
                }
                CurveSensitivities sens = builder.build();
                if (!sens.TypedSensitivities.Empty)
                {
                    parsed.put(sens.Id.map(object.toString).orElse(""), sens);
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// Parses one or more CSV format trade files with an error-creating type filter.
        /// <para>
        /// A list of types is specified to filter the trades.
        /// Trades that do not match the type will be included in the failure list.
        /// </para>
        /// <para>
        /// CSV files sometimes contain a Unicode Byte Order Mark.
        /// Callers are responsible for handling this, such as by using <seealso cref="UnicodeBom"/>.
        ///
        /// </para>
        /// </summary>
        /// <param name="charSources">  the CSV character sources </param>
        /// <param name="tradeTypes">  the trade types to return </param>
        /// <returns> the loaded trades, all errors are captured in the result </returns>
        public ValueWithFailures <IList <Trade> > parse(ICollection <CharSource> charSources, IList <Type> tradeTypes)
        {
            ValueWithFailures <IList <Trade> > parsed = parse(charSources, typeof(Trade));
            IList <Trade>       valid    = new List <Trade>();
            IList <FailureItem> failures = new List <FailureItem>(parsed.Failures);

            foreach (Trade trade in parsed.Value)
            {
                if (tradeTypes.Contains(trade.GetType()))
                {
                    valid.Add(trade);
                }
                else
                {
//JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
//JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter:
                    failures.Add(FailureItem.of(FailureReason.PARSING, "Trade type not allowed {tradeType}, only these types are supported: {}", trade.GetType().FullName, tradeTypes.Select(t => t.SimpleName).collect(joining(", "))));
                }
            }
            return(ValueWithFailures.of(valid, failures));
        }
Beispiel #7
0
        // loads a single CSV file
        private ValueWithFailures <IList <T> > parseFile <T>(CsvIterator csv, Type <T> tradeType) where T : com.opengamma.strata.product.Trade
        {
            IList <T>           trades   = new List <T>();
            IList <FailureItem> failures = new List <FailureItem>();

            while (csv.hasNext())
            {
                CsvRow row = csv.next();
                try
                {
                    string    typeRaw = row.getField(TYPE_FIELD);
                    TradeInfo info    = parseTradeInfo(row);
                    switch (typeRaw.ToUpper(Locale.ENGLISH))
                    {
                    case "FRA":
                        if (tradeType == typeof(FraTrade) || tradeType == typeof(Trade))
                        {
                            trades.Add(tradeType.cast(FraTradeCsvLoader.parse(row, info, resolver)));
                        }
                        break;

                    case "SECURITY":
                        if (tradeType == typeof(SecurityTrade) || tradeType == typeof(GenericSecurityTrade) || tradeType == typeof(ResolvableSecurityTrade) || tradeType == typeof(Trade))
                        {
                            SecurityQuantityTrade parsed = SecurityCsvLoader.parseTrade(row, info, resolver);
                            if (tradeType.IsInstanceOfType(parsed))
                            {
                                trades.Add(tradeType.cast(parsed));
                            }
                        }
                        break;

                    case "SWAP":
                        if (tradeType == typeof(SwapTrade) || tradeType == typeof(Trade))
                        {
                            IList <CsvRow> variableRows = new List <CsvRow>();
                            while (csv.hasNext() && csv.peek().getField(TYPE_FIELD).ToUpper(Locale.ENGLISH).Equals("VARIABLE"))
                            {
                                variableRows.Add(csv.next());
                            }
                            trades.Add(tradeType.cast(SwapTradeCsvLoader.parse(row, variableRows, info, resolver)));
                        }
                        break;

                    case "TERMDEPOSIT":
                    case "TERM DEPOSIT":
                        if (tradeType == typeof(TermDepositTrade) || tradeType == typeof(Trade))
                        {
                            trades.Add(tradeType.cast(TermDepositTradeCsvLoader.parse(row, info, resolver)));
                        }
                        break;

                    case "VARIABLE":
                        failures.Add(FailureItem.of(FailureReason.PARSING, "CSV file contained a 'Variable' type at line {lineNumber} that was not preceeded by a 'Swap'", row.lineNumber()));
                        break;

                    case "FX":
                    case "FXSINGLE":
                    case "FX SINGLE":
                        if (tradeType == typeof(FxSingleTrade) || tradeType == typeof(FxTrade) || tradeType == typeof(Trade))
                        {
                            trades.Add(tradeType.cast(FxSingleTradeCsvLoader.parse(row, info, resolver)));
                        }
                        break;

                    case "FXSWAP":
                    case "FX SWAP":
                        if (tradeType == typeof(FxSwapTrade) || tradeType == typeof(FxTrade) || tradeType == typeof(Trade))
                        {
                            trades.Add(tradeType.cast(FxSwapTradeCsvLoader.parse(row, info, resolver)));
                        }
                        break;

                    default:
                        failures.Add(FailureItem.of(FailureReason.PARSING, "CSV file trade type '{tradeType}' is not known at line {lineNumber}", typeRaw, row.lineNumber()));
                        break;
                    }
                }
                catch (Exception ex)
                {
                    failures.Add(FailureItem.of(FailureReason.PARSING, ex, "CSV file trade could not be parsed at line {lineNumber}: {exceptionMessage}", row.lineNumber(), ex.Message));
                }
            }
            return(ValueWithFailures.of(trades, failures));
        }
Beispiel #8
0
        // loads a single CSV file
        private ValueWithFailures <IList <T> > parseFile <T>(CsvIterator csv, Type <T> posType) where T : com.opengamma.strata.product.Position
        {
            IList <T>           positions = new List <T>();
            IList <FailureItem> failures  = new List <FailureItem>();
            int line = 2;

            foreach (CsvRow row in (IEnumerable <CsvRow>)() => csv)
            {
                try
                {
                    PositionInfo      info       = parsePositionInfo(row);
                    Optional <string> typeRawOpt = row.findValue(TYPE_FIELD);
                    if (typeRawOpt.Present)
                    {
                        // type specified
                        string type = typeRawOpt.get().ToUpper(Locale.ENGLISH);
                        switch (type.ToUpper(Locale.ENGLISH))
                        {
                        case "SEC":
                        case "SECURITY":
                            if (posType == typeof(SecurityPosition) || posType == typeof(ResolvableSecurityPosition))
                            {
                                positions.Add(posType.cast(SecurityCsvLoader.parseSecurityPosition(row, info, resolver)));
                            }
                            else if (posType == typeof(GenericSecurityPosition) || posType == typeof(Position))
                            {
                                Position parsed = SecurityCsvLoader.parseNonEtdPosition(row, info, resolver);
                                if (posType.IsInstanceOfType(parsed))
                                {
                                    positions.Add(posType.cast(parsed));
                                }
                            }
                            break;

                        case "FUT":
                        case "FUTURE":
                            if (posType == typeof(EtdPosition) || posType == typeof(EtdFuturePosition) || posType == typeof(ResolvableSecurityPosition) || posType == typeof(Position))
                            {
                                positions.Add(posType.cast((Position)resolver.parseEtdFuturePosition(row, info)));
                            }
                            else if (posType == typeof(SecurityPosition))
                            {
                                positions.Add(posType.cast(resolver.parseEtdFutureSecurityPosition(row, info)));
                            }
                            break;

                        case "OPT":
                        case "OPTION":
                            if (posType == typeof(EtdPosition) || posType == typeof(EtdOptionPosition) || posType == typeof(ResolvableSecurityPosition) || posType == typeof(Position))
                            {
                                positions.Add(posType.cast(resolver.parseEtdOptionPosition(row, info)));
                            }
                            else if (posType == typeof(SecurityPosition))
                            {
                                positions.Add(posType.cast(resolver.parseEtdOptionSecurityPosition(row, info)));
                            }
                            break;

                        default:
                            failures.Add(FailureItem.of(FailureReason.PARSING, "CSV file position type '{positionType}' is not known at line {lineNumber}", typeRawOpt.get(), line));
                            break;
                        }
                    }
                    else
                    {
                        // infer type
                        if (posType == typeof(SecurityPosition))
                        {
                            positions.Add(posType.cast(SecurityCsvLoader.parsePositionLightweight(row, info, resolver)));
                        }
                        else
                        {
                            Position position = SecurityCsvLoader.parsePosition(row, info, resolver);
                            if (posType.IsInstanceOfType(position))
                            {
                                positions.Add(posType.cast(position));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    failures.Add(FailureItem.of(FailureReason.PARSING, ex, "CSV file position could not be parsed at line {lineNumber}: {exceptionMessage}", line, ex.Message));
                }
                line++;
            }
            return(ValueWithFailures.of(positions, failures));
        }