Exemple #1
0
        private QuoteData ReadNextQuoteFromStream()
        {
            if (reader == null)
            {
                return(null);
            }
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }

                if (line.Length == 8)
                {
                    var year  = line.Substring(4).ToIntSafe();
                    var month = line.Substring(2, 2).ToIntSafe();
                    var day   = line.Substring(0, 2).ToIntSafe();
                    if (!year.HasValue || !month.HasValue || !day.HasValue)
                    {
                        continue;
                    }
                    isNewFormat = true;
                    curDate     = new DateTime(year.Value, month.Value, day.Value);
                    continue;
                }

                // распарсить строку
                var quote = isNewFormat
                                ? QuoteData.ParseQuoteStringNewFormat(line, curDate)
                                : QuoteData.ParseQuoteStringOldFormat(line);
                return(quote);
            }
            return(null);
        }
        /// <summary>
        /// загрузить котировки из файла и сохранить их в БД, пачками
        /// </summary>
        private void BtnSaveInDbClick(object sender, EventArgs e)
        {
            var           commandsBlock      = new List <string>();
            const int     maxCommandsInBlock = 50;
            var           ticker             = tbTickerId.Text.ToInt();
            SqlConnection connection         = null;
            var           connectionString   = ConfigurationManager.ConnectionStrings["QuoteBase"].ConnectionString;
            var           startTime          = dpStart.Value;
            var           endTime            = dpEnd.Value;

            if (openFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            try
            {
                var newFormat = false;
                connection = new SqlConnection(connectionString);
                connection.Open();
                using (var sr = new StreamReader(openFileDialog.FileName))
                {
                    DateTime?date = null;
                    while (!sr.EndOfStream)
                    {
                        var line = sr.ReadLine();
                        if (string.IsNullOrEmpty(line))
                        {
                            continue;
                        }

                        DateTime?fileDate;
                        var      isNew = QuoteData.IsNewFormatDateRecord(line, out fileDate);
                        if (fileDate.HasValue)
                        {
                            date = fileDate;
                        }
                        if (isNew)
                        {
                            newFormat = true;
                            continue;
                        }
                        var quote = newFormat ? QuoteData.ParseQuoteStringNewFormat(line, date.Value) :
                                    QuoteData.ParseQuoteStringOldFormat(line);
                        if (quote == null)
                        {
                            continue;
                        }
                        if (quote.time < startTime)
                        {
                            continue;
                        }
                        if (quote.time > endTime)
                        {
                            break;
                        }

                        var cmd = MakeInsertQuoteCmd(ticker, quote);
                        commandsBlock.Add(cmd);
                        if (commandsBlock.Count > maxCommandsInBlock)
                        {
                            ExecuteSaveCommands(commandsBlock, connection);
                            commandsBlock.Clear();
                        }
                    }
                }
                if (commandsBlock.Count > 0)
                {
                    ExecuteSaveCommands(commandsBlock, connection);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Ошибка сохранения в БД", ex);
                MessageBox.Show(string.Format("Ошибка сохранения в БД: {0}", ex.Message));
                return;
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
            MessageBox.Show("Котировки сохранены в БД");
        }
Exemple #3
0
        private void ProcessFile(string path, string ticker)
        {
            var dateStart    = dpStart.Value;
            var dateEnd      = dpEnd.Value;
            var destFileName = tbDestFolder.Text.TrimEnd('\\') + "\\" + ticker + ".quote";

            using (var sr = new StreamReader(path))
                using (var sw = new StreamWriter(destFileName))
                {
                    DateTime?curTime   = null;
                    DateTime?storeDate = null;
                    while (!sr.EndOfStream)
                    {
                        var      line = sr.ReadLine();
                        DateTime?quoteDate;
                        QuoteData.IsNewFormatDateRecord(line, out quoteDate);
                        if (quoteDate.HasValue)
                        {
                            curTime = quoteDate;
                        }
                        if (!curTime.HasValue)
                        {
                            continue;
                        }
                        var quote = QuoteData.ParseQuoteStringNewFormat(line, curTime.Value);
                        if (quote == null)
                        {
                            continue;
                        }

                        if (quote.time < dateStart)
                        {
                            continue;
                        }
                        if (quote.time > dateEnd)
                        {
                            break;
                        }

                        // записать котировку
                        var writeDate = false;
                        if (!storeDate.HasValue)
                        {
                            writeDate = true;
                        }
                        else
                        {
                            if (storeDate.Value != quote.time.Date)
                            {
                                writeDate = true;
                            }
                        }
                        storeDate = quote.time.Date;
                        if (writeDate)
                        {
                            sw.WriteLine(string.Format("{0:ddMMyyyy}", quote.time));
                        }
                        sw.WriteLine(
                            string.Format(CultureInfo.InvariantCulture, "{0:HHmm} {1} {2}",
                                          quote.time, quote.bid, quote.ask));
                    }
                }
        }