Beispiel #1
0
        public int LoadFromCsv(string fileName, int tickerCode)
        {
            var storeThread = new DBStoreThread(true);
            var candles = new List<CandleData>();
            var totalStored = 0;

            using (var sr = new StreamReader(fileName, Encoding.GetEncoding(1252)))
            {
                while (!sr.EndOfStream)
                {
                    var line = sr.ReadLine();
                    if (string.IsNullOrEmpty(line)) continue;
                    var parts = line.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
                    // date - time - o-h-l-c
                    if (parts.Length < 6) continue;
                    DateTime time;
                    if (!DateTime.TryParseExact(parts[0] + " " + parts[1],
                                                "yyyy.MM.dd HH:mm", CultureProvider.Common, DateTimeStyles.AssumeLocal,
                                                out time))
                        continue;
                    var open = parts[2].ToFloatUniformSafe() ?? 0;
                    var high = parts[3].ToFloatUniformSafe() ?? 0;
                    var low = parts[4].ToFloatUniformSafe() ?? 0;
                    var close = parts[5].ToFloatUniformSafe() ?? 0;

                    if (open == 0 || high == 0 || low == 0 || close == 0) continue;
                    candles.Add(new CandleData(open, high, low, close, time, time.AddMinutes(1)));
                    if (candles.Count > BufferSize)
                    {
                        storeThread.PushQuotes(new Dictionary<int, List<CandleData>>
                            { { tickerCode, candles.ToList() } });
                        totalStored += candles.Count;
                        candles.Clear();
                    }
                }
                totalStored += candles.Count;
                if (candles.Count > 0)
                    storeThread.PushQuotes(new Dictionary<int, List<CandleData>>
                        { {tickerCode, candles.ToList()} });
            }

            while (storeThread.CandlesLeftInQueue > 0)
            {
                Thread.Sleep(200);
            }
            storeThread.Stop();

            return totalStored;
        }
Beispiel #2
0
        public int LoadFromCsv(string fileName, int tickerCode)
        {
            var storeThread = new DBStoreThread(true);
            var candles     = new List <CandleData>();
            var totalStored = 0;

            using (var sr = new StreamReader(fileName, Encoding.GetEncoding(1252)))
            {
                while (!sr.EndOfStream)
                {
                    var line = sr.ReadLine();
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }
                    var parts = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    // date - time - o-h-l-c
                    if (parts.Length < 6)
                    {
                        continue;
                    }
                    DateTime time;
                    if (!DateTime.TryParseExact(parts[0] + " " + parts[1],
                                                "yyyy.MM.dd HH:mm", CultureProvider.Common, DateTimeStyles.AssumeLocal,
                                                out time))
                    {
                        continue;
                    }
                    var open  = parts[2].ToFloatUniformSafe() ?? 0;
                    var high  = parts[3].ToFloatUniformSafe() ?? 0;
                    var low   = parts[4].ToFloatUniformSafe() ?? 0;
                    var close = parts[5].ToFloatUniformSafe() ?? 0;

                    if (open == 0 || high == 0 || low == 0 || close == 0)
                    {
                        continue;
                    }
                    candles.Add(new CandleData(open, high, low, close, time, time.AddMinutes(1)));
                    if (candles.Count > BufferSize)
                    {
                        storeThread.PushQuotes(new Dictionary <int, List <CandleData> >
                        {
                            { tickerCode, candles.ToList() }
                        });
                        totalStored += candles.Count;
                        candles.Clear();
                    }
                }
                totalStored += candles.Count;
                if (candles.Count > 0)
                {
                    storeThread.PushQuotes(new Dictionary <int, List <CandleData> >
                    {
                        { tickerCode, candles.ToList() }
                    });
                }
            }

            while (storeThread.CandlesLeftInQueue > 0)
            {
                Thread.Sleep(200);
            }
            storeThread.Stop();


            return(totalStored);
        }
        private void WorkerForexiteDoWork(object sender, DoWorkEventArgs e)
        {
            var ptrs = (UploadHistoryParams) e.Argument;
            var startDay = ptrs.start.Date;
            var endDay = (ptrs.end.Hour == 0 && ptrs.end.Minute == 0) ? ptrs.end.Date : ptrs.end.Date.AddDays(1);
            var daysTotal = (endDay - startDay).TotalDays;

            AddStatusMessage(string.Format("Получение {0} котировок с Forexite, {1} дней всего",
                ptrs.tickers.Count,
                (int)Math.Round(daysTotal)));

            var tickerNames = ptrs.tickers.Select(t => t.Title).ToList();

            // таки запросить историю
            // по каждому дню
            int lastProgress = 0, dayNum = 0;
            const int deltaProgressMin = 3;
            int quotesCount = 0, daysLoaded = 0;

            var storeThread = new DBStoreThread(ptrs.rewriteQuotes);
            var readTickers = new List<int>();

            for (var day = startDay; day <= endDay; day = day.AddDays(1))
            {
                if (workerForexite.CancellationPending) break;
                var minuteCandles = ForexiteDownloader.ReadDayQuotesFromForexite(day, 0, tickerNames);
                if (workerForexite.CancellationPending) break;

                if (minuteCandles.Count > 0)
                {
                    if (quotesCount < minuteCandles.Count)
                    {
                        quotesCount = minuteCandles.Count;
                        readTickers = readTickers.Union(minuteCandles.Keys).Distinct().ToList();
                    }
                    daysLoaded++;
                    // записать котировки в БД
                    storeThread.PushQuotes(minuteCandles);
                }

                // обновить прогресс
                dayNum++;
                var progress = (int)(100 * dayNum / daysTotal);
                if (progress > 100) progress = 100;
                if ((progress - lastProgress) > deltaProgressMin)
                {
                    workerForexite.ReportProgress(progress);
                    lastProgress = progress;
                }
            }

            AddStatusMessage("Завершение - запись в базу данных");
            workerForexite.ReportProgress(0);

            var recordsTotal = storeThread.CandlesLeftInQueue;
            long oldProgress = 0;

            while (true)
            {
                if (workerForexite.CancellationPending) break;
                Thread.Sleep(250);
                var recsLeft = storeThread.CandlesLeftInQueue;
                if (recsLeft == 0) break;
                if (recsLeft > recordsTotal) recordsTotal = recsLeft;

                var progress = (recordsTotal - recsLeft) * 100 /recordsTotal;
                if (progress > 100) progress = 100;
                else if (progress < 0) progress = 0;
                if (oldProgress != progress)
                {
                    workerForexite.ReportProgress((int)progress);
                    oldProgress = progress;
                }
            }

            storeThread.Stop();
            var msgResult = string.Format("Загружено {0} котировок за {1} дней", quotesCount, daysLoaded);

            if (readTickers.Count < ptrs.tickers.Count)
            {
                var symbolsRead = readTickers.Select(t => DalSpot.Instance.GetSymbolByFXICode(t)).ToList();
                var symbolsLacked = tickerNames.Where(n => !symbolsRead.Contains(n));

                // получить котировки, которые не были прочитаны, и сформировать из них список
                var strLack = string.Join(", ", symbolsLacked) + " не загружены";
                msgResult = msgResult + Environment.NewLine + strLack;
            }
            e.Result = msgResult;
        }