Ejemplo n.º 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));
     }
 }
Ejemplo n.º 2
0
 //-------------------------------------------------------------------------
 /// <summary>
 /// Checks whether the source is a CSV format sensitivities file.
 /// <para>
 /// This parses the headers as CSV and checks that mandatory headers are present.
 ///
 /// </para>
 /// </summary>
 /// <param name="charSource">  the CSV character source to check </param>
 /// <returns> true if the source is a CSV file with known headers, false otherwise </returns>
 public bool isKnownFormat(CharSource charSource)
 {
     try
     {
         using (CsvIterator csv = CsvIterator.of(charSource, true))
         {
             if (!csv.containsHeader(TENOR_HEADER) && !csv.containsHeader(DATE_HEADER))
             {
                 return(false);
             }
             if (csv.containsHeader(REFERENCE_HEADER) && csv.containsHeader(TYPE_HEADER) && csv.containsHeader(VALUE_HEADER))
             {
                 return(true);        // standard format
             }
             else if (csv.containsHeader(REFERENCE_HEADER) || csv.containsHeader(TYPE_HEADER))
             {
                 return(true);        // list or grid format
             }
             else
             {
                 return(csv.headers().Any(SensitivityCsvLoader.knownReference));        // implied grid format
             }
         }
     }
     catch (Exception)
     {
         return(false);
     }
 }
 public IteratorBasedDataProvider(CsvIterator iterator, char separator)
     : base(
         iterator: iterator,
         new QuotesSensitiveRowSplitter(),
         separator: separator
         )
 {
 }
 public IteratorBasedDataProvider(CsvIterator iterator, ISplitRowStrategy rowSplitter, char separator)
     : base(
         iterator: iterator,
         rowSplitter,
         separator: separator
         )
 {
 }
Ejemplo n.º 5
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);
                }
            }
        }
Ejemplo n.º 6
0
 //-------------------------------------------------------------------------
 /// <summary>
 /// Checks whether the source is a CSV format trade file.
 /// <para>
 /// This parses the headers as CSV and checks that mandatory headers are present.
 /// This is determined entirely from the 'Strata Trade Type' column.
 ///
 /// </para>
 /// </summary>
 /// <param name="charSource">  the CSV character source to check </param>
 /// <returns> true if the source is a CSV file with known headers, false otherwise </returns>
 public bool isKnownFormat(CharSource charSource)
 {
     try
     {
         using (CsvIterator csv = CsvIterator.of(charSource, true))
         {
             return(csv.containsHeader(TYPE_FIELD));
         }
     }
     catch (Exception)
     {
         return(false);
     }
 }
Ejemplo n.º 7
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)));
     }
 }
Ejemplo n.º 8
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);
                }
            }
        }
Ejemplo n.º 9
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));
        }
Ejemplo n.º 10
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));
        }