Exemplo n.º 1
0
        private void LoadFile(string filename, bool isMarket)
        {
            //LOG("CSVLoader loading from " << filename);

            Date today = Settings.evaluationDate();

            using (var reader = new StreamReader(filename))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();

                    if (line.Length > 0 && line[0] != '#')
                    {
                        line = line.Trim();
                        string[] tokens = line.Split(new[] { " " }, StringSplitOptions.None).ToList().Where(i => i != "").ToArray();
                        //string[] tokens = Regex.Split(line, ",;\t "); //line.Split(',').ToList();

                        // TODO: should we try, catch and log any invalid lines?

                        if (tokens.ToList().Count != 3)
                        {
                            var debug = "";
                        }

                        Utils.QL_REQUIRE(tokens.ToList().Count == 3, () => "Invalid CSVLoader line, 3 tokens expected " + line);
                        Date   date  = Parsers.ParseDateExact(tokens[0], "yyyyMMdd");
                        string key   = tokens[1];
                        double value = Parsers.ParseDouble(tokens[2]);

                        if (isMarket)
                        {
                            // process market
                            // build market datum and add to map
                            try
                            {
                                if (!_data.ContainsKey(date))
                                {
                                    List <MarketDatum> datum = new List <MarketDatum>();
                                    datum.Add(MarketDatumParser.ParseMarketDatum(date, key, value));
                                    _data.Add(date, datum);
                                }
                                else
                                {
                                    _data[date].Add(MarketDatumParser.ParseMarketDatum(date, key, value));
                                }
                                //TLOG("Added MarketDatum " << data_[date].back()->name());
                            }
                            catch (Exception e)
                            {
                                //WLOG("Failed to parse MarketDatum " << key << ": " << e.ToString());
                            }
                        }
                        else
                        {
                            // process fixings
                            if (date < today || (date == today && !_implyTodaysFixings))
                            {
                                _fixings.Add(new Fixing(date, key, value));
                            }
                        }
                    }
                }
                //    LOG("CSVLoader completed processing " << filename);
            }
        }