Exemplo n.º 1
0
        /// <summary>
        /// Reads the ticker symbol/exchange symbol info from a csv file
        /// </summary>
        /// <param name="symbolFileInfo">FileInfo - Points to the symbol file</param>
        /// <returns>bool - true if the exchanges were in the file, false if any blank second column was found</returns>
        public bool ReadSymbolFileIntoDictionary(FileInfo symbolFileInfo)
        {
            SymbolList = new Dictionary <string, string>();
            bool foundNoBlankExchangeColumn = true;

            using (StreamReader sr = new StreamReader(symbolFileInfo.FullName))
            {
                try
                {
                    //string buffer = sr.ReadLine();
                    //if (buffer != null && buffer.Split(',')[0].ToLower().Contains("symbol"))
                    //    buffer = sr.ReadLine();

                    while (!sr.EndOfStream)
                    {
                        string buffer = sr.ReadLine();

                        if (buffer != null && !buffer.Contains(","))
                        {
                            buffer += ",";
                        }

                        string[] columns = buffer.Split(',');
                        string   symbol  = columns[0];
                        {
                            if (columns[1].Length == 0)
                            {
                                foundNoBlankExchangeColumn = false;
                                ExchangeLookup lookup = new ExchangeLookup(symbol);
                                columns[1] = lookup.GetExchangeForSymbol(symbol);
                            }
                            symbol = symbol.Replace("\"", "");
                            if (symbol.Contains(@"^"))
                            {
                                continue;
                            }

                            // Skip duplicate symbols
                            if (!SymbolList.ContainsKey(symbol))
                            {
                                SymbolList.Add(symbol, columns[1]);
                                linelist.Add(ColumnJoiner.JoinColumns(columns));
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            return(foundNoBlankExchangeColumn);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Saves the data to a csv or zip file
        /// </summary>
        /// <param name="directory">string - the OutputDirectory to write the data to</param>
        /// <param name="historicalData">string - the formatted data from the DataProcessor</param>
        /// <param name="ticker">string - the symbol to write</param>
        /// <returns>Task</returns>
        private async Task <int> SaveDataAsync(string directory, string historicalData, string ticker)
        {
            DateTime dt = DateTime.Now;

            string[]      lines = historicalData.Split('\n');
            StringBuilder sb    = new StringBuilder();
            string        data  = string.Empty;
            string        filepath;
            var           internalFilename = CreateFilename(directory, ticker, out filepath);

            filepath = filepath.Replace(".csv", ".zip");
            if (File.Exists(filepath))
            {
                data = Compression.Unzip(filepath, internalFilename);
                sb.AppendLine(data.Trim());
            }


            foreach (string line in lines)
            {
                try
                {
                    // Sometimes there is an extra CRLF at the end of the last record.  The last line is blank.
                    //  Treat it as though it is an EOF marker.
                    if (line.Length == 0)
                    {
                        await WriteStreamAsync(dt, sb.ToString(), directory, ticker);

                        return(1);
                    }
                    try
                    {
                        string[] elements = line.Split(',');
                        // skip the first row if it is header
                        if (elements[0] == "Date")
                        {
                            continue;
                        }

                        // skip if the open == 0.  The open on some symbols before 1/3/2000 is 0.  WTF?
                        try
                        {
                            if (Convert.ToDecimal(elements[1]) == 0)
                            {
                                continue;
                            }
                        }
                        catch (Exception e)
                        {
                            throw new Exception(e.Message);
                        }
                        elements[elements.Length - 1] = elements[elements.Length - 1].Replace("\r", string.Empty);
                        string record = ColumnJoiner.JoinColumns(elements);
                        //20151030 00:00,
                        string datestring = line.Substring(0, 15);
                        if (!data.Contains(datestring))
                        {
                            sb.AppendLine(line.Trim());
                        }
                    }
                    catch (Exception e)
                    {
                        throw new Exception(e.Message);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
            await WriteStreamAsync(dt, sb.ToString().Trim(), directory, ticker);

            return(1);
        }