Esempio n. 1
0
        //Ордерлог
        private List <List <Trade> > OrdLogReader(string filePath)
        {
            var list = new List <List <Trade> > {
                new List <Trade>()
            };

            using (var qr = QshReader.Open(filePath))
            {
                IOrdLogStream stream = null;

                for (var i = 0; i < qr.StreamCount; i++)
                {
                    var logStream = qr[i] as IOrdLogStream;

                    if (logStream != null)
                    {
                        stream = logStream;
                        break;
                    }
                }

                if (stream == null)
                {
                    return(list);
                }

                var security = SecurityConverter(stream.Security);

                using (var converter = new OrderLogToMarketDepth(security))
                {
                    converter.NewTick += tick =>
                    {
                        var lastDate = DateTime.MaxValue;

                        var lastList = list[list.Count - 1];

                        if (lastList.Count > 0)
                        {
                            lastDate = lastList[lastList.Count - 1].Time.Date;
                        }

                        if (tick.Time.Date > lastDate)
                        {
                            list.Add(new List <Trade>());
                        }

                        list[list.Count - 1].Add(tick);
                    };

                    stream.Handler += ol => converter.Add(OrdLogConverter(ol));

                    while (qr.CurrentDateTime != DateTime.MaxValue)
                    {
                        qr.Read(true);
                    }
                }
            }

            return(list);
        }
Esempio n. 2
0
        //Сделки с направлением
        private List <Trade> DealsReader(string filePath)
        {
            var list = new List <Trade>();

            using (var qr = QshReader.Open(filePath))
            {
                try
                {
                    for (var i = 0; i < qr.StreamCount; i++)
                    {
                        var stream = qr[i] as IDealsStream;

                        if (stream == null)
                        {
                            return(list);
                        }

                        var security = SecurityConverter(stream.Security);

                        var step = (decimal)stream.Security.Step;

                        stream.Handler += dael =>
                        {
                            list.Add(new Trade
                            {
                                ID        = dael.Id,
                                Price     = dael.Price * step,
                                Volume    = dael.Volume,
                                Time      = dael.DateTime,
                                Security  = security,
                                Aggressor = DealTypeConverter(dael.Type)
                            });
                        };
                    }

                    while (qr.CurrentDateTime != DateTime.MaxValue)
                    {
                        qr.Read(true);
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.ToString());
                }
            }

            return(list);
        }
Esempio n. 3
0
        private static void ConvertFile(string fileName, string format)
        {
            var Quotes      = new List <string>();
            var fileNameKey = $"{format}_{fileName}";

            using (var qr = QshReader.Open(fileName))
            {
                var reader      = qr;
                var currentDate = qr.CurrentDateTime;

                for (var i = 0; i < qr.StreamCount; i++)
                {
                    var stream = (ISecurityStream)qr[i];

                    if (stream.Type != StreamType.Quotes)
                    {
                        continue;
                    }

                    ((IQuotesStream)stream).Handler += quotes =>
                    {
                        string s = string.Empty;

                        foreach (var item in quotes)
                        {
                            s = s + "||" + item.Price.ToString() + ";" + item.Type.ToString() + ";" + item.Volume.ToString();
                        }

                        s = s.Remove(0, 2) + "||" + qr.CurrentDateTime.ToString();
                        Quotes.Add(s);
                    };

                    break;
                }

                while (qr.CurrentDateTime != DateTime.MaxValue)
                {
                    qr.Read(true);
                }
            }

            TryFlushData(Quotes);
        }
Esempio n. 4
0
        private void ConvertFile(string fileName, IStorageRegistry registry, StorageFormats format, ExchangeBoard board, string securityLike, Dictionary <SecurityId, IOrderLogMarketDepthBuilder> orderLog2OrderBookBuilders, int orderBookMaxDepth, TimeZoneInfo tz, TimeZoneInfo mz)
        {
            if (!_isStarted)
            {
                return;
            }

            var fileNameKey = format + "_" + fileName;

            if (_convertedFiles.Contains(fileNameKey))
            {
                return;
            }

            _logManager.Application.AddInfoLog("Начата конвертация файла {0}.", fileName);

            var securitiesStrings = securityLike.Split(",");

            var data = new Dictionary <SecurityId, Tuple <List <QuoteChangeMessage>, List <ExecutionMessage>, List <Level1ChangeMessage>, List <ExecutionMessage> > >();

            DateTimeOffset ToLocalDto(DateTime dt) => dt.ApplyTimeZone(tz);
            DateTimeOffset ToServerDto(DateTime dt) => dt.ApplyTimeZone(mz);

            using (var qr = QshReader.Open(fileName))
            {
                var reader = qr;

                for (var i = 0; i < qr.StreamCount; i++)
                {
                    var stream            = (ISecurityStream)qr[i];
                    var securityId        = GetSecurityId(stream.Security, board.Code);
                    var priceStep         = (decimal)stream.Security.Step;
                    var lastTransactionId = 0L;
                    var builder           = orderLog2OrderBookBuilders?.SafeAdd(securityId, key => new PlazaOrderLogMarketDepthBuilder(key));

                    if (securitiesStrings.Length > 0)
                    {
                        var secCode = securityId.SecurityCode;

                        var streamContainsSecurityFromMask = securitiesStrings.Any(mask =>
                        {
                            var isEndMulti   = mask.EndsWith("*");
                            var isStartMulti = mask.StartsWith("*");

                            if (isStartMulti)
                            {
                                mask = mask.Substring(1);
                            }

                            if (isEndMulti)
                            {
                                mask = mask.Substring(0, mask.Length - 1);
                            }

                            if (isEndMulti)
                            {
                                if (isStartMulti)
                                {
                                    return(secCode.ContainsIgnoreCase(mask));
                                }
                                else
                                {
                                    return(secCode.StartsWith(mask, StringComparison.InvariantCultureIgnoreCase));
                                }
                            }
                            else if (isStartMulti)
                            {
                                return(secCode.EndsWith(mask, StringComparison.InvariantCultureIgnoreCase));
                            }
                            else
                            {
                                return(secCode.CompareIgnoreCase(mask));
                            }
                        });

                        if (!streamContainsSecurityFromMask)
                        {
                            continue;
                        }
                    }

                    var secData = data.SafeAdd(securityId, key => Tuple.Create(new List <QuoteChangeMessage>(), new List <ExecutionMessage>(), new List <Level1ChangeMessage>(), new List <ExecutionMessage>()));

                    switch (stream.Type)
                    {
                    case StreamType.Quotes:
                    {
                        ((IQuotesStream)stream).Handler += quotes =>
                        {
                            var bids = new List <QuoteChange>();
                            var asks = new List <QuoteChange>();

                            foreach (var q in quotes)
                            {
                                switch (q.Type)
                                {
                                //case QuoteType.Unknown:
                                //case QuoteType.Free:
                                //case QuoteType.Spread:
                                //	throw new ArgumentException(q.Type.ToString());
                                case QuoteType.Ask:
                                case QuoteType.BestAsk:
                                    asks.Add(new QuoteChange(priceStep * q.Price, q.Volume));
                                    break;

                                case QuoteType.Bid:
                                case QuoteType.BestBid:
                                    bids.Add(new QuoteChange(priceStep * q.Price, q.Volume));
                                    break;

                                default:
                                {
                                    continue;
                                    //throw new ArgumentException(q.Type.ToString());
                                }
                                }
                            }

                            var md = new QuoteChangeMessage
                            {
                                LocalTime  = ToLocalDto(reader.CurrentDateTime),
                                SecurityId = securityId,
                                ServerTime = ToLocalDto(reader.CurrentDateTime),
                                Bids       = bids.ToArray(),
                                Asks       = asks.ToArray(),
                            };

                            //if (md.Verify())
                            //{
                            secData.Item1.Add(md);

                            TryFlushData(registry, securityId, format, null, secData.Item1, reader);

                            //}
                            //else
                            //	_logManager.Application.AddErrorLog("Стакан для {0} в момент {1} не прошел валидацию. Лучший бид {2}, Лучший офер {3}.", security, qr.CurrentDateTime, md.BestBid, md.BestAsk);
                        };
                        break;
                    }

                    case StreamType.Deals:
                    {
                        ((IDealsStream)stream).Handler += deal =>
                        {
                            secData.Item2.Add(new ExecutionMessage
                                {
                                    LocalTime     = ToLocalDto(reader.CurrentDateTime),
                                    HasTradeInfo  = true,
                                    ExecutionType = ExecutionTypes.Tick,
                                    SecurityId    = securityId,
                                    OpenInterest  = deal.OI == 0 ? (long?)null : deal.OI,
                                    ServerTime    = ToServerDto(deal.DateTime),
                                    TradeVolume   = deal.Volume,
                                    TradeId       = deal.Id == 0 ? (long?)null : deal.Id,
                                    TradePrice    = deal.Price * priceStep,
                                    OriginSide    =
                                        deal.Type == DealType.Buy
                                                                                        ? Sides.Buy
                                                                                        : (deal.Type == DealType.Sell ? Sides.Sell : (Sides?)null)
                                });

                            TryFlushData(registry, securityId, format, ExecutionTypes.Tick, secData.Item2, reader);
                        };
                        break;
                    }

                    case StreamType.OrdLog:
                    {
                        ((IOrdLogStream)stream).Handler += ol =>
                        {
                            var currTransactionId = ol.DateTime.Ticks;

                            if (lastTransactionId < currTransactionId)
                            {
                                lastTransactionId = currTransactionId;
                            }
                            else if (lastTransactionId >= currTransactionId)
                            {
                                lastTransactionId++;
                            }

                            var msg = new ExecutionMessage
                            {
                                LocalTime     = ToLocalDto(reader.CurrentDateTime),
                                ExecutionType = ExecutionTypes.OrderLog,
                                SecurityId    = securityId,
                                OpenInterest  = ol.OI == 0 ? (long?)null : ol.OI,
                                OrderId       = ol.OrderId,
                                OrderPrice    = priceStep * ol.Price,
                                ServerTime    = ToServerDto(ol.DateTime),
                                OrderVolume   = ol.Amount,
                                Balance       = ol.AmountRest,
                                TradeId       = ol.DealId == 0 ? (long?)null : ol.DealId,
                                TradePrice    = ol.DealPrice == 0 ? (decimal?)null : priceStep * ol.DealPrice,
                                TransactionId = lastTransactionId
                            };

                            var status = 0;

                            if (ol.Flags.Contains(OrdLogFlags.Add))
                            {
                                msg.OrderState = OrderStates.Active;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.Fill))
                            {
                                msg.OrderState = OrderStates.Done;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.Canceled))
                            {
                                msg.OrderState = OrderStates.Done;
                                status        |= 0x200000;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.CanceledGroup))
                            {
                                msg.OrderState = OrderStates.Done;
                                status        |= 0x400000;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.Moved))
                            {
                                status |= 0x100000;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.Buy))
                            {
                                msg.Side = Sides.Buy;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.Sell))
                            {
                                msg.Side = Sides.Sell;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.FillOrKill))
                            {
                                msg.TimeInForce = TimeInForce.MatchOrCancel;
                                status         |= 0x00080000;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.Quote))
                            {
                                msg.TimeInForce = TimeInForce.PutInQueue;
                                status         |= 0x01;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.Counter))
                            {
                                status |= 0x02;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.CrossTrade))
                            {
                                status |= 0x20000000;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.NonSystem))
                            {
                                msg.IsSystem = false;
                                status      |= 0x04;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.EndOfTransaction))
                            {
                                status |= 0x1000;
                            }

                            msg.OrderStatus = status;

                            if (builder == null)
                            {
                                secData.Item4.Add(msg);

                                TryFlushData(registry, securityId, format, ExecutionTypes.OrderLog, secData.Item4, reader);
                            }
                            else
                            {
                                //if (builder.Depth.Bids.Any() || builder.Depth.Asks.Any() || msg.ServerTime.TimeOfDay >= new TimeSpan(0, 18, 45, 00, 1))
                                {
                                    bool updated;

                                    try
                                    {
                                        updated = builder.Update(msg);
                                    }
                                    catch
                                    {
                                        updated = false;
                                    }

                                    if (updated)
                                    {
                                        secData.Item1.Add(new QuoteChangeMessage
                                            {
                                                SecurityId = securityId,
                                                ServerTime = builder.Depth.ServerTime,
                                                Bids       = builder.Depth.Bids.Take(orderBookMaxDepth).ToArray(),
                                                Asks       = builder.Depth.Asks.Take(orderBookMaxDepth).ToArray(),
                                                IsSorted   = builder.Depth.IsSorted,
                                                LocalTime  = builder.Depth.LocalTime,
                                            });

                                        TryFlushData(registry, securityId, format, null, secData.Item1, reader);
                                    }
                                }
                            }
                        };
                        break;
                    }

                    case StreamType.AuxInfo:
                    {
                        ((IAuxInfoStream)stream).Handler += info =>
                        {
                            var l1Msg = new Level1ChangeMessage
                            {
                                LocalTime  = ToLocalDto(reader.CurrentDateTime),
                                SecurityId = securityId,
                                ServerTime = ToLocalDto(reader.CurrentDateTime),
                            }
                            .TryAdd(Level1Fields.LastTradePrice, priceStep * info.Price)
                            .TryAdd(Level1Fields.BidsVolume, (decimal)info.BidTotal)
                            .TryAdd(Level1Fields.AsksVolume, (decimal)info.AskTotal)
                            .TryAdd(Level1Fields.HighPrice, priceStep * info.HiLimit)
                            .TryAdd(Level1Fields.LowPrice, priceStep * info.LoLimit)
                            .TryAdd(Level1Fields.StepPrice, (decimal)info.Rate)
                            .TryAdd(Level1Fields.OperatingMargin, (decimal)info.Deposit)
                            .TryAdd(Level1Fields.OpenInterest, (decimal)info.OI);

                            if (l1Msg.Changes.Count == 0)
                            {
                                return;
                            }

                            secData.Item3.Add(l1Msg);

                            TryFlushData(registry, securityId, format, null, secData.Item3, reader);
                        };
                        break;
                    }

                    case StreamType.OwnOrders:
                    case StreamType.OwnTrades:
                    case StreamType.Messages:
                    case StreamType.None:
                    {
                        continue;
                    }

                    default:
                        throw new ArgumentOutOfRangeException("Неподдерживаемый тип потока {0}.".Put(stream.Type));
                    }
                }

                if (data.Count > 0)
                {
                    while (qr.CurrentDateTime != DateTime.MaxValue && _isStarted)
                    {
                        qr.Read(true);
                    }
                }
            }

            if (!_isStarted)
            {
                return;
            }

            foreach (var pair in data)
            {
                if (pair.Value.Item1.Any())
                {
                    registry.GetQuoteMessageStorage(pair.Key, registry.DefaultDrive, format).Save(pair.Value.Item1);
                }

                if (pair.Value.Item2.Any())
                {
                    registry.GetTickMessageStorage(pair.Key, registry.DefaultDrive, format).Save(pair.Value.Item2);
                }

                if (pair.Value.Item3.Any())
                {
                    registry.GetLevel1MessageStorage(pair.Key, registry.DefaultDrive, format).Save(pair.Value.Item3);
                }

                if (pair.Value.Item4.Any())
                {
                    registry.GetOrderLogMessageStorage(pair.Key, registry.DefaultDrive, format).Save(pair.Value.Item4);
                }
            }

            if (data.Count > 0)
            {
                //File.AppendAllLines(_convertedFilesFile, new[] { fileNameKey });
                _convertedFiles.Add(fileNameKey);
                _convertedPerTaskPoolFiles.Add(fileNameKey);
            }

            _logManager.Application.AddInfoLog("Завершена конвертация файла {0}.", fileName);
        }
Esempio n. 5
0
        private void TryFlushData <TMessage>(IStorageRegistry registry, SecurityId securityId, StorageFormats format, object arg, List <TMessage> messages, QshReader reader)
            where TMessage : Messages.Message
        {
            const int maxBufCount = 1000;

            if (messages.Count <= maxBufCount)
            {
                return;
            }

            _logManager.Application.AddDebugLog("Файл прочитан на {1}%.", messages.Count, (reader.FilePosition * 100) / reader.FileSize);

            registry.GetStorage(securityId, typeof(TMessage), arg, null, format).Save(messages);
            messages.Clear();
        }
Esempio n. 6
0
        private void ConvertFile(string fileName, IStorageRegistry registry, StorageFormats format, ExchangeBoard board, string securityLike)
        {
            if (!_isStarted)
            {
                return;
            }

            var fileNameKey = format + "_" + fileName;

            if (_convertedFiles.Contains(fileNameKey))
            {
                return;
            }

            _logManager.Application.AddInfoLog("Конвертация файла {0}.", fileName);

            var isExact = !securityLike.EndsWith("*");

            const int maxBufCount = 1000;

            var data = new Dictionary <Security, Tuple <List <QuoteChangeMessage>, List <ExecutionMessage>, List <Level1ChangeMessage>, List <ExecutionMessage> > >();

            using (var qr = QshReader.Open(fileName))
            {
                var currentDate = qr.CurrentDateTime;

                for (var i = 0; i < qr.StreamCount; i++)
                {
                    var stream            = (ISecurityStream)qr[i];
                    var security          = GetSecurity(stream.Security, board);
                    var priceStep         = security.PriceStep ?? 1;
                    var securityId        = security.ToSecurityId();
                    var lastTransactionId = 0L;

                    if (!securityLike.IsEmpty() &&
                        (isExact
                                                        ? !securityId.SecurityCode.CompareIgnoreCase(securityLike)
                                                        : !securityId.SecurityCode.ContainsIgnoreCase(securityLike)))
                    {
                        continue;
                    }

                    var secData = data.SafeAdd(security, key => Tuple.Create(new List <QuoteChangeMessage>(), new List <ExecutionMessage>(), new List <Level1ChangeMessage>(), new List <ExecutionMessage>()));

                    switch (stream.Type)
                    {
                    case StreamType.Stock:
                    {
                        ((IStockStream)stream).Handler += (key, quotes, spread) =>
                        {
                            var quotes2 = quotes.Select(q =>
                                {
                                    Sides side;

                                    switch (q.Type)
                                    {
                                    case QuoteType.Unknown:
                                    case QuoteType.Free:
                                    case QuoteType.Spread:
                                        throw new ArgumentException(q.Type.ToString());

                                    case QuoteType.Ask:
                                    case QuoteType.BestAsk:
                                        side = Sides.Sell;
                                        break;

                                    case QuoteType.Bid:
                                    case QuoteType.BestBid:
                                        side = Sides.Buy;
                                        break;

                                    default:
                                        throw new ArgumentOutOfRangeException();
                                    }

                                    return(new QuoteChange(side, priceStep * q.Price, q.Volume));
                                }).ToArray();

                            var md = new QuoteChangeMessage
                            {
                                SecurityId = securityId,
                                ServerTime = currentDate.ApplyTimeZone(TimeHelper.Moscow),
                                Bids       = quotes2.Where(q => q.Side == Sides.Buy),
                                Asks       = quotes2.Where(q => q.Side == Sides.Sell),
                            };

                            //if (md.Verify())
                            //{
                            secData.Item1.Add(md);

                            if (secData.Item1.Count > maxBufCount)
                            {
                                registry.GetQuoteMessageStorage(security, format: format).Save(secData.Item1);
                                secData.Item1.Clear();
                            }
                            //}
                            //else
                            //	_logManager.Application.AddErrorLog("Стакан для {0} в момент {1} не прошел валидацию. Лучший бид {2}, Лучший офер {3}.", security, qr.CurrentDateTime, md.BestBid, md.BestAsk);
                        };
                        break;
                    }

                    case StreamType.Deals:
                    {
                        ((IDealsStream)stream).Handler += deal =>
                        {
                            secData.Item2.Add(new ExecutionMessage
                                {
                                    ExecutionType = ExecutionTypes.Tick,
                                    SecurityId    = securityId,
                                    OpenInterest  = deal.OI == 0 ? (long?)null : deal.OI,
                                    ServerTime    = deal.DateTime.ApplyTimeZone(TimeHelper.Moscow),
                                    Volume        = deal.Volume,
                                    TradeId       = deal.Id == 0 ? (long?)null : deal.Id,
                                    TradePrice    = (decimal)deal.Price,
                                    OriginSide    =
                                        deal.Type == DealType.Buy
                                                                                        ? Sides.Buy
                                                                                        : (deal.Type == DealType.Sell ? Sides.Sell : (Sides?)null)
                                });

                            if (secData.Item2.Count > maxBufCount)
                            {
                                registry.GetTickMessageStorage(security, format: format).Save(secData.Item2);
                                secData.Item2.Clear();
                            }
                        };
                        break;
                    }

                    case StreamType.OrdLog:
                    {
                        ((IOrdLogStream)stream).Handler += (key, ol) =>
                        {
                            var currTransactionId = ol.DateTime.Ticks;

                            if (lastTransactionId < currTransactionId)
                            {
                                lastTransactionId = currTransactionId;
                            }
                            else if (lastTransactionId >= currTransactionId)
                            {
                                lastTransactionId++;
                            }

                            var msg = new ExecutionMessage
                            {
                                ExecutionType = ExecutionTypes.OrderLog,
                                SecurityId    = securityId,
                                OpenInterest  = ol.OI == 0 ? (long?)null : ol.OI,
                                OrderId       = ol.OrderId,
                                OrderPrice    = priceStep * ol.Price,
                                ServerTime    = ol.DateTime.ApplyTimeZone(TimeHelper.Moscow),
                                Volume        = ol.Amount,
                                Balance       = ol.AmountRest,
                                TradeId       = ol.DealId == 0 ? (long?)null : ol.DealId,
                                TradePrice    = ol.DealPrice == 0 ? (decimal?)null : priceStep * ol.DealPrice,
                                TransactionId = lastTransactionId
                            };

                            var status = 0;

                            if (ol.Flags.Contains(OrdLogFlags.Add))
                            {
                                msg.OrderState = OrderStates.Active;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.Fill))
                            {
                                msg.OrderState = OrderStates.Done;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.Canceled))
                            {
                                msg.OrderState = OrderStates.Done;
                                status        |= 0x200000;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.CanceledGroup))
                            {
                                msg.OrderState = OrderStates.Done;
                                status        |= 0x400000;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.Moved))
                            {
                                status |= 0x100000;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.Buy))
                            {
                                msg.Side = Sides.Buy;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.Sell))
                            {
                                msg.Side = Sides.Sell;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.FillOrKill))
                            {
                                msg.TimeInForce = TimeInForce.MatchOrCancel;
                                status         |= 0x00080000;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.Quote))
                            {
                                msg.TimeInForce = TimeInForce.PutInQueue;
                                status         |= 0x01;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.Counter))
                            {
                                status |= 0x02;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.CrossTrade))
                            {
                                status |= 0x20000000;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.NonSystem))
                            {
                                msg.IsSystem = false;
                                status      |= 0x04;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.EndOfTransaction))
                            {
                                status |= 0x1000;
                            }

                            msg.OrderStatus = (OrderStatus)status;

                            secData.Item4.Add(msg);

                            if (secData.Item4.Count > maxBufCount)
                            {
                                registry.GetOrderLogMessageStorage(security, format: format).Save(secData.Item4);
                                secData.Item4.Clear();
                            }
                        };
                        break;
                    }

                    case StreamType.AuxInfo:
                    {
                        ((IAuxInfoStream)stream).Handler += (key, info) =>
                        {
                            secData.Item3.Add(new Level1ChangeMessage
                                {
                                    SecurityId = securityId,
                                    ServerTime = info.DateTime.ApplyTimeZone(TimeHelper.Moscow),
                                }
                                              .TryAdd(Level1Fields.LastTradePrice, priceStep * info.Price)
                                              .TryAdd(Level1Fields.BidsVolume, (decimal)info.BidTotal)
                                              .TryAdd(Level1Fields.AsksVolume, (decimal)info.AskTotal)
                                              .TryAdd(Level1Fields.HighPrice, priceStep * info.HiLimit)
                                              .TryAdd(Level1Fields.LowPrice, priceStep * info.LoLimit)
                                              .TryAdd(Level1Fields.OpenInterest, (decimal)info.OI));

                            if (secData.Item3.Count > maxBufCount)
                            {
                                registry.GetLevel1MessageStorage(security, format: format).Save(secData.Item3);
                                secData.Item3.Clear();
                            }
                        };
                        break;
                    }

                    case StreamType.Orders:
                    case StreamType.Trades:
                    case StreamType.Messages:
                    case StreamType.None:
                    {
                        continue;
                    }

                    default:
                        throw new ArgumentOutOfRangeException("Неподдерживаемый тип потока {0}.".Put(stream.Type));
                    }
                }

                if (data.Count > 0)
                {
                    while (qr.CurrentDateTime != DateTime.MaxValue && _isStarted)
                    {
                        qr.Read(true);
                    }
                }
            }

            if (!_isStarted)
            {
                return;
            }

            foreach (var pair in data)
            {
                if (pair.Value.Item1.Any())
                {
                    registry.GetQuoteMessageStorage(pair.Key, format: format).Save(pair.Value.Item1);
                }

                if (pair.Value.Item2.Any())
                {
                    registry.GetTickMessageStorage(pair.Key, format: format).Save(pair.Value.Item2);
                }

                if (pair.Value.Item3.Any())
                {
                    registry.GetLevel1MessageStorage(pair.Key, format: format).Save(pair.Value.Item3);
                }

                if (pair.Value.Item4.Any())
                {
                    registry.GetOrderLogMessageStorage(pair.Key, format: format).Save(pair.Value.Item4);
                }
            }

            if (data.Count > 0)
            {
                File.AppendAllLines(_convertedFilesFile, new[] { fileNameKey });
                _convertedFiles.Add(fileNameKey);
            }
        }
Esempio n. 7
0
        private void ConvertFile(string file, string name)
        {
            //Все файлы всех бумаг для одного дня должны попасть в один общий commonDict

            var qr = QshReader.Open(file);

            var Quotes = new List <string>();

            try
            {
                using (qr)
                {
                    //waitHandler.WaitOne();

                    var reader = qr;

                    for (int i = 0; i < qr.StreamCount; i++)
                    {
                        var     stream    = (ISecurityStream)qr[i];
                        decimal priceStep = (decimal)stream.Security.Step; //Step – минимальный шаг цены инструмента.


                        switch (stream.Type)
                        {
                        case StreamType.OrdLog:
                        {
                            //IOrdLogStream s = (IOrdLogStream)qr[i];
                            //s.Handler += OrdLogHandler => {};
                            break;
                        }

                        case StreamType.Deals:
                        {
                            IDealsStream s = (IDealsStream)qr[i];
                            s.Handler += DealsHandler =>
                            {
                                Trade trade = new Trade()
                                {
                                    Price  = DealsHandler.Price * priceStep,
                                    Id     = DealsHandler.Id.ToString(),
                                    Volume = DealsHandler.Volume,
                                    Side   = DealsHandler.Type.HasFlag(DealType.Buy) ? Side.Buy :
                                             DealsHandler.Type.HasFlag(DealType.Sell) ? Side.Sell : Side.None,
                                    Time             = qr.CurrentDateTime.AddHours(3), //DealsHandler.DateTime, -- время записи стакана, а не биржевое время сделки
                                    SecurityNameCode = name
                                };

                                if (!_commonDict.ContainsKey(qr.CurrentDateTime.Ticks))
                                {
                                    _commonDict.Add(qr.CurrentDateTime.Ticks, new rowQshData(trade));
                                }
                                else
                                {
                                    _commonDict[qr.CurrentDateTime.Ticks].trade.Add(trade);
                                }
                            };

                            break;
                        }

                        case StreamType.Quotes:
                        {
                            IQuotesStream quo = (IQuotesStream)qr[i];
                            quo.Handler += quotes =>
                            {
                                MarketDepth dept = new MarketDepth();

                                var asks = new List <MarketDepthLevel>();
                                asks = quotes.Where(x => x.Type == QuoteType.Ask | x.Type == QuoteType.BestAsk)
                                       .Select(y => new MarketDepthLevel()
                                    {
                                        Price = y.Price * priceStep, Ask = y.Volume
                                    })
                                       .OrderBy(k => k.Price).ToList();

                                var bids = new List <MarketDepthLevel>();
                                bids = quotes.Where(x => x.Type == QuoteType.Bid | x.Type == QuoteType.BestBid)
                                       .Select(y => new MarketDepthLevel()
                                    {
                                        Price = y.Price * priceStep, Bid = y.Volume
                                    })
                                       .OrderByDescending(k => k.Price).ToList();

                                dept.Asks             = asks;
                                dept.Bids             = bids;
                                dept.SecurityNameCode = name;
                                dept.Time             = qr.CurrentDateTime.AddHours(3);

                                if (!_commonDict.ContainsKey(qr.CurrentDateTime.Ticks))
                                {
                                    _commonDict.Add(qr.CurrentDateTime.Ticks, new rowQshData(dept));
                                }
                                else
                                {
                                    _commonDict[qr.CurrentDateTime.Ticks].depth.Add(dept);
                                }
                            };

                            break;
                        }

                        case StreamType.AuxInfo:
                        {
                            IAuxInfoStream s = (IAuxInfoStream)qr[i];
                            s.Handler += S_Handler =>
                            {
                                var foo = S_Handler;
                            };


                            break;
                        }

                        case StreamType.OwnOrders:
                        case StreamType.OwnTrades:
                        case StreamType.Messages:
                        case StreamType.None:
                        {
                            continue;
                        }

                        default:
                            throw new ArgumentOutOfRangeException("Неподдерживаемый тип потока");
                        } // end switch
                    }     // end for qr.StreamCount

                    Console.WriteLine();
                    Console.WriteLine("Processing...");

                    int lastPercent = -1;

                    while (qr.CurrentDateTime != DateTime.MaxValue)  //тут срабатывает exception
                    {
                        // Выведем, какой процент исходного файла уже обработали. Если
                        // исходные файл сжат, то это значение может быть немного
                        // некорректно (особенно для небольших файлов), т.к. сжатый файл
                        // читается большими блоками.

                        int percent = (int)(100 * qr.FilePosition / qr.FileSize);
                        if (lastPercent != percent)
                        {
                            lastPercent = percent;
                        }

                        // Прочтем кадр исходного файла. Параметр push задает вызывать ли
                        // при этом обработчик или нет. После возврата из метода свойства
                        // qr.CurrentDateTime и qr.CurrentStreamIndex будут содержать
                        // значения для следующего кадра.
                        qr.Read(true);
                    }

                    Console.WriteLine("\rDone.");
                    //File.WriteAllLines(pathFolder + "OrdLog" + outputName, outputLines);
                    //outputLines.Clear();
                }
            }
            catch (Exception u)
            {
                string exception = u.ToString();
            }
            finally
            {
                //// На этом все.
                if (qr != null)
                {
                    qr.Dispose();
                }

                //waitHandler.Set();
            }
        }