public void StartServer()
        {
            TaskFactory factory = new TaskFactory(cts.Token);
            factory.StartNew(() =>
            {

                //запустить обход файла
                using (StreamReader sr = new StreamReader(FilePath))
                {
                    Dictionary<string, Ticker> tickersCache = new Dictionary<string, Ticker>();

                    DateTime lastMinute = new DateTime();
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (cts.Token.IsCancellationRequested) //выход из потока по кнопке "Стоп"
                            return;

                        string[] cells = line.Split(Separator);

                        bool isRealTime = true;

                        ConnectorHelper helper = new ConnectorHelper();

                        if (headTable == null) //Должна быть строка с заголовками
                        {
                            headTable = helper.FillHeadTable(cells);
                            continue;
                        }

                        //try
                        //{
                            TickEventArgs tick = new TickEventArgs();
                            tick.Date = helper.ParseDate(cells, headTable, Col_Date, Col_Time, FormatDate, FormatTime);
                            tick.TickerCode = helper.ParseTickerCode(cells, headTable, Col_TickerCode);
                            tick.Price = helper.ParsePrice(cells, headTable, Col_Price, CultureInfo.InvariantCulture);
                            tick.Quantity = helper.ParseQuantity(cells, headTable, Col_Qty);
                            if (tick.Quantity == 0)
                            {
                                if(!tickersCache.ContainsKey(tick.TickerCode))
                                    tickersCache[tick.TickerCode] = tickerRepository.Get(T=>T.Where(t=>t.Code == tick.TickerCode)).FirstOrDefault();

                                Ticker ticker = tickersCache[tick.TickerCode];
                                if (ticker != null && ticker.QtyInLot.HasValue)
                                    tick.Quantity = helper.ParseQuantity(cells, headTable, Col_Volume, ticker.QtyInLot.Value);
                            }
                            tick.IsRealTime = isRealTime;

                            tick.TradePeriod = helper.ParseTradePeriod(cells, headTable, Col_TradePeriod, Val_PeriodOpening, Val_PeriodTrading, Val_PeriodClosing);
                            if (tick.TradePeriod == TradePeriod.Undefended)
                                tick.TradePeriod = helper.ParseTradePeriod(cells, headTable, Col_Time, FormatTime, Val_SessionStart, Val_SessionFinish);

                            tick.Extended = helper.CreateExtended(cells, headTable);

                        //Включаем задержку
                        if(Delay != 0 && lastMinute < tick.Date)
                        {
                            Thread.Sleep(60000 / Delay);
                            lastMinute = tick.Date.AddMinutes(1);
                        }

                            //вызвать событие IConnector
                            var tickPoked = TickPoked;
                            tickPoked?.Invoke(this, tick);

                        //}
                        //catch (Exception)
                        //{
                        //    throw;
                        //}

                    }

                }
            });

        }
Exemple #2
0
        /// <summary>
        /// Метод выполняется по подписке при передачи данных из терминала Quik
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDataPoked(object sender, DataPokeddEventArgs e)
        {
            bool isRealTime = e.Cells.Length == 1 ? true : false; //Если пришла одна строка, то скорее всего это реал тайм )). Или проверить время сервера

            ConnectorHelper helper = new ConnectorHelper();

            foreach (object[] cells in e.Cells)
            {
                if (headTable == null) //Должна быть строка с заголовками
                {
                    headTable = helper.FillHeadTable(cells); 
                    continue;
                }

                try
                {
                    TickEventArgs tick = new TickEventArgs();
                    tick.Date = helper.ParseDate(cells, headTable, Col_Date, Col_Time, FormatDate, FormatTime);
                    tick.TickerCode = helper.ParseTickerCode(cells, headTable, Col_TickerCode);
                    tick.Price = helper.ParsePrice(cells, headTable, Col_Price, CultureInfo.InvariantCulture);
                    tick.Quantity = helper.ParseQuantity(cells, headTable, Col_Qty);
                    tick.IsRealTime = isRealTime;

                    tick.TradePeriod = helper.ParseTradePeriod(cells, headTable, Col_TradePeriod, Val_PeriodOpening, Val_PeriodTrading, Val_PeriodClosing);
                    if (tick.TradePeriod == TradePeriod.Undefended)
                        tick.TradePeriod = helper.ParseTradePeriod(cells, headTable, Col_TradePeriod, FormatTime, Val_SessionStart, Val_SessionFinish);

                    tick.Extended = helper.CreateExtended(cells, headTable);

                    //вызвать событие IConnector
                    var tickPoked = TickPoked;
                    tickPoked?.Invoke(this, tick);

                }
                catch(Exception)
                {
                    throw;
                }
            }

        }