Beispiel #1
0
        Dictionary <DateTime, StockDataItem> fillDataDictionary(string csvData)
        {
            Dictionary <DateTime, StockDataItem> parsedStockData = new Dictionary <DateTime, StockDataItem>();

            using (StringReader reader = new StringReader(csvData))
            {
                string csvLine;

                reader.ReadLine();
                while ((csvLine = reader.ReadLine()) != null)
                {
                    string[] splitLine = csvLine.Split(',');

                    if (splitLine.Length >= 6)
                    {
                        StockDataItem newItem = new StockDataItem();

                        float tempOpen;

                        if (Single.TryParse(splitLine[1], out tempOpen))
                        {
                            newItem.open = tempOpen;
                        }

                        float tempHigh;

                        if (Single.TryParse(splitLine[2], out tempHigh))
                        {
                            newItem.high = tempHigh;
                        }

                        float tempLow;

                        if (Single.TryParse(splitLine[3], out tempLow))
                        {
                            newItem.low = tempLow;
                        }

                        float tempClose;

                        if (Single.TryParse(splitLine[4], out tempClose))
                        {
                            newItem.close = tempClose;
                        }

                        float tempVolume;

                        if (Single.TryParse(splitLine[5], out tempVolume))
                        {
                            newItem.volume = tempVolume;
                        }

                        DateTime tempDate;

                        if (DateTime.TryParse(splitLine[0], out tempDate))
                        {
                            parsedStockData.Add(tempDate, newItem);
                        }
                    }
                }
            }
            return(parsedStockData);
        }
        // Class constructor with file and column heading inputs
        // pulls data from google
        public StockDataDownloader(string fileName, int exchangesHeaderIndex, int tickersHeaderIndex, DateTime startDate, DateTime endDate)
        {
            // Temporary placeholder to populate some stock data to work with (delete once auto scan is fixed)
            tickerSymbols.Add("AMD");
            tickerSymbols.Add("TUP");

            Dictionary <DateTime, StockDataItem> amdDict = new Dictionary <DateTime, StockDataItem>();

            string[] amdLines = Properties.Resources.AMD.Split('\n');
            for (int i = 1; i < amdLines.Length - 1; i++)
            {
                string[]      values        = amdLines[i].Split(',');
                StockDataItem stockDataItem = new StockDataItem();
                stockDataItem.close  = Single.Parse(values[6]);
                stockDataItem.low    = Single.Parse(values[4]);
                stockDataItem.high   = Single.Parse(values[3]);
                stockDataItem.open   = Single.Parse(values[2]);
                stockDataItem.volume = Single.Parse(values[7]);
                amdDict.Add(DateTime.Parse(values[0]), stockDataItem);
            }
            stockDataDictionaries.Add(amdDict);

            Dictionary <DateTime, StockDataItem> tupDict = new Dictionary <DateTime, StockDataItem>();

            string[] tupLines = Properties.Resources.TUP.Split('\n');
            for (int i = 1; i < tupLines.Length - 1; i++)
            {
                string[]      values        = tupLines[i].Split(',');
                StockDataItem stockDataItem = new StockDataItem();
                stockDataItem.close  = Single.Parse(values[6]);
                stockDataItem.low    = Single.Parse(values[4]);
                stockDataItem.high   = Single.Parse(values[3]);
                stockDataItem.open   = Single.Parse(values[2]);
                stockDataItem.volume = Single.Parse(values[7]);
                tupDict.Add(DateTime.Parse(values[0]), stockDataItem);
            }
            stockDataDictionaries.Add(tupDict);

            /*
             * This portion commented out until web-based stock data retrieval service is fixed
             *
             * // Retrieve data
             * WebClientForStockFinanceHistory downloaderClient = new WebClientForStockFinanceHistory();
             * using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
             * {
             *  using (StreamReader reader = new StreamReader(stream))
             *  {
             *      string line = reader.ReadLine();
             *      string[] values;
             *      while (!reader.EndOfStream)
             *      {
             *          line = reader.ReadLine();
             *          values = line.Split(',');
             *          tickerSymbols.Add(values[tickersHeaderIndex]);
             *          stockDataDictionaries.Add(downloaderClient.getStockDataFromGoogle(values[exchangesHeaderIndex], tickerSymbols.Last(), startDate, endDate));
             *      }
             *  }
             * }
             *
             */

            // Populate orderedDates
            DateTime dt = startDate;

            while (dt <= endDate)
            {
                orderedDates.Add(dt);
                dt = dt.AddDays(7);
            }
        }