Esempio n. 1
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));
        }
Esempio n. 2
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));
        }