/// <summary>
 /// To get the order books storage.
 /// </summary>
 /// <param name="security">Security.</param>
 /// <returns>The order books storage.</returns>
 protected override IMarketDataStorage <MarketDepth> GetStorage(Security security)
 {
     return(StorageRegistry.GetMarketDepthStorage(security, Drive));
 }
        private void ProcessMarketDataMessage(MarketDataMessage message)
        {
            var securityId = message.SecurityId;
            var security   = SecurityProvider.LookupById(securityId.ToStringId());

            if (security == null)
            {
                RaiseMarketDataMessage(message, new InvalidOperationException(LocalizedStrings.Str704Params.Put(securityId)));
                return;
            }

            if (StorageRegistry == null)
            {
                RaiseMarketDataMessage(message, new InvalidOperationException(LocalizedStrings.Str1117Params.Put(message.DataType, securityId)));
                return;
            }

            var history  = message as HistorySourceMessage;
            var storages = BasketStorage.InnerStorages;

            Exception error = null;

            switch (message.DataType)
            {
            case MarketDataTypes.Level1:
            {
                if (message.IsSubscribe)
                {
                    if (history == null)
                    {
                        storages.Add(StorageRegistry.GetLevel1MessageStorage(security, Drive, StorageFormat));

                        storages.Add(new InMemoryMarketDataStorage <ClearingMessage>(security, null, date => new[]
                            {
                                new ClearingMessage
                                {
                                    LocalTime        = date.Date + security.Board.ExpiryTime,
                                    SecurityId       = securityId,
                                    ClearMarketDepth = true
                                }
                            }));
                    }
                    else
                    {
                        storages.Add(new InMemoryMarketDataStorage <Level1ChangeMessage>(security, null, history.GetMessages));
                    }
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <Level1ChangeMessage> >(security, MessageTypes.Level1Change, null);
                    RemoveStorage <InMemoryMarketDataStorage <ClearingMessage> >(security, ExtendedMessageTypes.Clearing, null);
                }

                break;
            }

            case MarketDataTypes.MarketDepth:
            {
                if (message.IsSubscribe)
                {
                    if (history == null)
                    {
                        storages.Add((IMarketDataStorage <QuoteChangeMessage>)StorageRegistry.GetMarketDepthStorage(security, Drive, StorageFormat));
                    }
                    else
                    {
                        storages.Add(new InMemoryMarketDataStorage <QuoteChangeMessage>(security, null, history.GetMessages));
                    }
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <QuoteChangeMessage> >(security, MessageTypes.QuoteChange, null);
                }

                break;
            }

            case MarketDataTypes.Trades:
            {
                if (message.IsSubscribe)
                {
                    if (history == null)
                    {
                        storages.Add((IMarketDataStorage <ExecutionMessage>)StorageRegistry.GetTradeStorage(security, Drive, StorageFormat));
                    }
                    else
                    {
                        storages.Add(new InMemoryMarketDataStorage <ExecutionMessage>(security, null, history.GetMessages));
                    }
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <ExecutionMessage> >(security, MessageTypes.Execution, ExecutionTypes.Tick);
                }

                break;
            }

            case MarketDataTypes.OrderLog:
            {
                if (message.IsSubscribe)
                {
                    if (history == null)
                    {
                        storages.Add((IMarketDataStorage <ExecutionMessage>)StorageRegistry.GetOrderLogStorage(security, Drive, StorageFormat));
                    }
                    else
                    {
                        storages.Add(new InMemoryMarketDataStorage <ExecutionMessage>(security, null, history.GetMessages));
                    }
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <ExecutionMessage> >(security, MessageTypes.Execution, ExecutionTypes.OrderLog);
                }

                break;
            }

            case MarketDataTypes.CandleTimeFrame:
            case MarketDataTypes.CandleTick:
            case MarketDataTypes.CandleVolume:
            case MarketDataTypes.CandleRange:
            case MarketDataTypes.CandlePnF:
            case MarketDataTypes.CandleRenko:
            {
                var msgType = message.DataType.ToCandleMessageType();

                if (message.IsSubscribe)
                {
                    var candleType = message.DataType.ToCandleMessage();

                    if (history == null)
                    {
                        storages.Add(StorageRegistry.GetCandleMessageStorage(candleType, security, message.Arg, Drive, StorageFormat));
                    }
                    else
                    {
                        storages.Add(new InMemoryMarketDataStorage <CandleMessage>(security, message.Arg, history.GetMessages, candleType));
                    }
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <CandleMessage> >(security, msgType, message.Arg);
                }

                break;
            }

            default:
                error = new InvalidOperationException(LocalizedStrings.Str1118Params.Put(message.DataType));
                break;
            }

            RaiseMarketDataMessage(message, error);
        }
Пример #3
0
        private void ProcessMarketDataMessage(MarketDataMessage message)
        {
            var security = _sessionHolder.SecurityProvider.LookupById(message.SecurityId.SecurityCode + "@" + message.SecurityId.BoardCode);

            if (security == null)
            {
                RaiseMarketDataMessage(message, new InvalidOperationException(LocalizedStrings.Str704Params.Put(message.SecurityId)));
                return;
            }

            if (TryGetGenerator(message) != null)
            {
                RaiseMarketDataMessage(message, null);
                return;
            }

            if (StorageRegistry == null)
            {
                RaiseMarketDataMessage(message, new InvalidOperationException(LocalizedStrings.Str1117Params.Put(message.DataType, message.SecurityId)));
                return;
            }

            Exception error = null;

            switch (message.DataType)
            {
            case MarketDataTypes.Level1:
            {
                if (message.IsSubscribe)
                {
                    _basketStorage.InnerStorages.Add(StorageRegistry.GetLevel1MessageStorage(security, Drive, StorageFormat));

                    _basketStorage.InnerStorages.Add(new InMemoryMarketDataStorage <ClearingMessage>(date => new[]
                        {
                            new ClearingMessage
                            {
                                LocalTime        = date.Date + security.Board.ExpiryTime,
                                SecurityId       = message.SecurityId,
                                ClearMarketDepth = true
                            }
                        }));
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <Level1ChangeMessage> >(security, MessageTypes.Level1Change, message.Arg);
                    RemoveStorage <InMemoryMarketDataStorage <ClearingMessage> >(security, ExtendedMessageTypes.Clearing, message.Arg);
                }

                break;
            }

            case MarketDataTypes.MarketDepth:
            {
                if (message.IsSubscribe)
                {
                    _basketStorage.InnerStorages.Add((IMarketDataStorage <QuoteChangeMessage>)StorageRegistry.GetMarketDepthStorage(security, Drive, StorageFormat));
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <QuoteChangeMessage> >(security, MessageTypes.QuoteChange, message.Arg);
                }

                break;
            }

            case MarketDataTypes.Trades:
            {
                if (message.IsSubscribe)
                {
                    _basketStorage.InnerStorages.Add((IMarketDataStorage <ExecutionMessage>)StorageRegistry.GetTradeStorage(security, Drive, StorageFormat));
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <ExecutionMessage> >(security, MessageTypes.Execution, message.Arg);
                }

                break;
            }

            case MarketDataTypes.OrderLog:
            {
                if (message.IsSubscribe)
                {
                    //var msg = "OrderLog".ValidateLicense();

                    //if (msg == null)
                    _basketStorage.InnerStorages.Add((IMarketDataStorage <ExecutionMessage>)StorageRegistry.GetOrderLogStorage(security, Drive, StorageFormat));
                    //else
                    //	SessionHolder.AddErrorLog(msg);
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <ExecutionMessage> >(security, MessageTypes.Execution, message.Arg);
                }

                break;
            }

            case MarketDataTypes.CandleTimeFrame:
            case MarketDataTypes.CandleTick:
            case MarketDataTypes.CandleVolume:
            case MarketDataTypes.CandleRange:
            case MarketDataTypes.CandlePnF:
            case MarketDataTypes.CandleRenko:
            {
                Type         candleMessageType;
                MessageTypes msgType;

                switch (message.DataType)
                {
                case MarketDataTypes.CandleTimeFrame:
                    msgType           = MessageTypes.CandleTimeFrame;
                    candleMessageType = typeof(TimeFrameCandleMessage);
                    break;

                case MarketDataTypes.CandleTick:
                    msgType           = MessageTypes.CandleTick;
                    candleMessageType = typeof(TickCandleMessage);
                    break;

                case MarketDataTypes.CandleVolume:
                    msgType           = MessageTypes.CandleVolume;
                    candleMessageType = typeof(VolumeCandleMessage);
                    break;

                case MarketDataTypes.CandleRange:
                    msgType           = MessageTypes.CandleRange;
                    candleMessageType = typeof(RangeCandleMessage);
                    break;

                case MarketDataTypes.CandlePnF:
                    msgType           = MessageTypes.CandlePnF;
                    candleMessageType = typeof(PnFCandleMessage);
                    break;

                case MarketDataTypes.CandleRenko:
                    msgType           = MessageTypes.CandleRenko;
                    candleMessageType = typeof(RenkoCandleMessage);
                    break;

                default:
                    throw new InvalidOperationException();
                }

                if (message.IsSubscribe)
                {
                    _basketStorage.InnerStorages.Add(StorageRegistry.GetCandleMessageStorage(candleMessageType, security, message.Arg, Drive, StorageFormat));
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <CandleMessage> >(security, msgType, message.Arg);
                }

                break;
            }

            default:
                error = new InvalidOperationException(LocalizedStrings.Str1118Params.Put(message.DataType));
                break;
            }

            RaiseMarketDataMessage(message, error);
        }
Пример #4
0
        static void Main(string[] args)
        {
            var tradeBufferMax = 200;
            var depthBufferMax = 1000;

            var tradeBuffers = new Dictionary <string, List <Trade> >();
            var depthBuffers = new Dictionary <string, List <MarketDepth> >();

            var tradeStorages = new Dictionary <string, IMarketDataStorage <Trade> >();
            var depthStorages = new Dictionary <string, IMarketDataStorage <MarketDepth> >();

            List <string> securityIds = new List <string>();

            StorageFormats storageFormat;
            string         storagePath;

            var settings = XElement.Load("settings.xml");

            storagePath   = @settings.Element("storage-path").Value;
            storageFormat = (StorageFormats)Enum.Parse(typeof(StorageFormats), settings.Element("storage-format").Value.ToUpper());

            foreach (var item in settings.Element("securities").Elements())
            {
                var secId = item.Element("security").Value;
                securityIds.Add(secId);
                depthBuffers.Add(secId, new List <MarketDepth>());
                tradeBuffers.Add(secId, new List <Trade>());
            }

            var storage = new StorageRegistry()
            {
                DefaultDrive = new LocalMarketDataDrive {
                    Path = storagePath
                }
            };

            var connector = new QuikTrader();

            // Ставим false, чтобы квик при старте не загрузил все 30 тыс инструментов
            connector.RequestAllSecurities = false;

            // Контролируем соединение в течение работы срочного рынка
            connector.ReConnectionSettings.WorkingTime = ExchangeBoard.Forts.WorkingTime;

            // Обработчик события успешного соединения
            connector.Connected += () =>
            {
                Console.WriteLine("Соединение установлено!");
                if (securityIds.Any())
                {
                    // Запрашиваем инструменты
                    foreach (var id in securityIds)
                    {
                        connector.LookupSecurities(new Security()
                        {
                            Code = id.Split('@')[0], Board = ExchangeBoard.GetBoard(id.Split('@')[1])
                        });
                    }
                }
                else
                {
                    Console.WriteLine("Нет инструментов для запроса!");
                }
            };

            connector.LookupSecuritiesResult += (ex, securities) =>
            {
                foreach (var security in securities)
                {
                    if (tradeStorages.ContainsKey(security.Id) || depthStorages.ContainsKey(security.Id))
                    {
                        continue;
                    }

                    // Инициализируем специализированные хранилища
                    tradeStorages.Add(security.Id, storage.GetTradeStorage(security, null, storageFormat));
                    depthStorages.Add(security.Id, storage.GetMarketDepthStorage(security, null, storageFormat));

                    // Региструем получение сделок
                    if (!connector.RegisteredTrades.Contains(security))
                    {
                        connector.RegisterTrades(security);
                    }

                    // Региструем получение стаканов
                    if (!connector.RegisteredMarketDepths.Contains(security))
                    {
                        connector.RegisterMarketDepth(security);
                    }
                }
            };

            connector.NewTrades += trades =>
            {
                foreach (var trade in trades)
                {
                    var secId = trade.Security.Id;

                    tradeBuffers[secId].Add(trade);

                    if (tradeBuffers[secId].Count >= tradeBufferMax)
                    {
                        var forsave = tradeBuffers[secId].TakeLast(tradeBufferMax);
                        tradeBuffers[secId] = tradeBuffers[secId].Except(forsave).ToList();
                        var task = Task.Factory.StartNew(() => tradeStorages[secId].Save(forsave));
                    }
                }
            };

            connector.MarketDepthsChanged += depths =>
            {
                foreach (var depth in depths)
                {
                    var secId = depth.Security.Id;
                    depthBuffers[secId].Add(depth);

                    if (depthBuffers[secId].Count >= depthBufferMax)
                    {
                        var forsave = depthBuffers[secId].TakeLast(depthBufferMax);
                        depthBuffers[secId] = depthBuffers[secId].Except(forsave).ToList();
                        var task = Task.Factory.StartNew(() => depthStorages[secId].Save(forsave));
                    }
                }
            };

            // Обработчик события разрыва соединения
            connector.Disconnected += () => Console.WriteLine("Соединение разорвано!");

            // Команда соединения
            connector.Connect();

            Console.Read();

            var ttasks = new List <Task>();
            var dtasks = new List <Task>();

            foreach (var security in connector.Securities)
            {
                // Отменяем регистрацию сделок
                if (connector.RegisteredTrades.Contains(security))
                {
                    connector.UnRegisterTrades(security);
                }

                // Отменяем получение стаканов
                if (connector.RegisteredMarketDepths.Contains(security))
                {
                    connector.UnRegisterMarketDepth(security);
                }

                // Записываем остатки данных
                ttasks.Add(Task.Factory.StartNew(() => tradeStorages[security.Id].Save(tradeBuffers[security.Id])));
                dtasks.Add(Task.Factory.StartNew(() => depthStorages[security.Id].Save(depthBuffers[security.Id])));
            }

            Console.WriteLine("Записываем остатки данных!");

            // Чуток ждем
            Task.WaitAll(ttasks.ToArray());
            Task.WaitAll(dtasks.ToArray());

            // Команда разрыва соединения
            connector.Disconnect();
        }
Пример #5
0
        private void ProcessMarketDataMessage(MarketDataMessage message)
        {
            var generatorMessage = message as GeneratorMarketDataMessage;

            if (generatorMessage != null)
            {
                if (generatorMessage.Generator == null)
                {
                    throw new ArgumentException("message");
                }

                var tradeGen = generatorMessage.Generator as TradeGenerator;

                if (tradeGen != null)
                {
                    if (generatorMessage.IsSubscribe)
                    {
                        _tradeGenerators.Add(generatorMessage.SecurityId, tradeGen);
                    }
                    else
                    {
                        _tradeGenerators.Remove(generatorMessage.SecurityId);
                    }
                }
                else
                {
                    var depthGen = generatorMessage.Generator as MarketDepthGenerator;

                    if (depthGen != null)
                    {
                        if (generatorMessage.IsSubscribe)
                        {
                            _depthGenerators.Add(generatorMessage.SecurityId, depthGen);
                        }
                        else
                        {
                            _depthGenerators.Remove(generatorMessage.SecurityId);
                        }
                    }
                    else
                    {
                        var olGen = generatorMessage.Generator as OrderLogGenerator;

                        if (olGen != null)
                        {
                            if (generatorMessage.IsSubscribe)
                            {
                                _orderLogGenerators.Add(generatorMessage.SecurityId, olGen);
                            }
                            else
                            {
                                _orderLogGenerators.Remove(generatorMessage.SecurityId);
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }
                    }
                }

                return;
            }

            var security = SecurityProvider.LookupById(message.SecurityId.SecurityCode + "@" + message.SecurityId.BoardCode);

            if (security == null)
            {
                RaiseMarketDataMessage(message, new InvalidOperationException(LocalizedStrings.Str704Params.Put(message.SecurityId)));
                return;
            }

            if (TryGetGenerator(message) != null)
            {
                RaiseMarketDataMessage(message, null);
                return;
            }

            if (StorageRegistry == null)
            {
                RaiseMarketDataMessage(message, new InvalidOperationException(LocalizedStrings.Str1117Params.Put(message.DataType, message.SecurityId)));
                return;
            }

            Exception error = null;

            switch (message.DataType)
            {
            case MarketDataTypes.Level1:
            {
                if (message.IsSubscribe)
                {
                    BasketStorage.InnerStorages.Add(StorageRegistry.GetLevel1MessageStorage(security, Drive, StorageFormat));

                    BasketStorage.InnerStorages.Add(new InMemoryMarketDataStorage <ClearingMessage>(date => new[]
                        {
                            new ClearingMessage
                            {
                                LocalTime        = date.Date + security.Board.ExpiryTime,
                                SecurityId       = message.SecurityId,
                                ClearMarketDepth = true
                            }
                        }));
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <Level1ChangeMessage> >(security, MessageTypes.Level1Change, message.Arg);
                    RemoveStorage <InMemoryMarketDataStorage <ClearingMessage> >(security, ExtendedMessageTypes.Clearing, message.Arg);
                }

                break;
            }

            case MarketDataTypes.MarketDepth:
            {
                if (message.IsSubscribe)
                {
                    BasketStorage.InnerStorages.Add((IMarketDataStorage <QuoteChangeMessage>)StorageRegistry.GetMarketDepthStorage(security, Drive, StorageFormat));
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <QuoteChangeMessage> >(security, MessageTypes.QuoteChange, message.Arg);
                }

                break;
            }

            case MarketDataTypes.Trades:
            {
                if (message.IsSubscribe)
                {
                    BasketStorage.InnerStorages.Add((IMarketDataStorage <ExecutionMessage>)StorageRegistry.GetTradeStorage(security, Drive, StorageFormat));
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <ExecutionMessage> >(security, MessageTypes.Execution, message.Arg);
                }

                break;
            }

            case MarketDataTypes.OrderLog:
            {
                if (message.IsSubscribe)
                {
                    BasketStorage.InnerStorages.Add((IMarketDataStorage <ExecutionMessage>)StorageRegistry.GetOrderLogStorage(security, Drive, StorageFormat));
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <ExecutionMessage> >(security, MessageTypes.Execution, message.Arg);
                }

                break;
            }

            case MarketDataTypes.CandleTimeFrame:
            case MarketDataTypes.CandleTick:
            case MarketDataTypes.CandleVolume:
            case MarketDataTypes.CandleRange:
            case MarketDataTypes.CandlePnF:
            case MarketDataTypes.CandleRenko:
            {
                Type         candleMessageType;
                MessageTypes msgType;

                switch (message.DataType)
                {
                case MarketDataTypes.CandleTimeFrame:
                    msgType           = MessageTypes.CandleTimeFrame;
                    candleMessageType = typeof(TimeFrameCandleMessage);
                    break;

                case MarketDataTypes.CandleTick:
                    msgType           = MessageTypes.CandleTick;
                    candleMessageType = typeof(TickCandleMessage);
                    break;

                case MarketDataTypes.CandleVolume:
                    msgType           = MessageTypes.CandleVolume;
                    candleMessageType = typeof(VolumeCandleMessage);
                    break;

                case MarketDataTypes.CandleRange:
                    msgType           = MessageTypes.CandleRange;
                    candleMessageType = typeof(RangeCandleMessage);
                    break;

                case MarketDataTypes.CandlePnF:
                    msgType           = MessageTypes.CandlePnF;
                    candleMessageType = typeof(PnFCandleMessage);
                    break;

                case MarketDataTypes.CandleRenko:
                    msgType           = MessageTypes.CandleRenko;
                    candleMessageType = typeof(RenkoCandleMessage);
                    break;

                default:
                    throw new InvalidOperationException();
                }

                if (message.IsSubscribe)
                {
                    BasketStorage.InnerStorages.Add(StorageRegistry.GetCandleMessageStorage(candleMessageType, security, message.Arg, Drive, StorageFormat));
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <CandleMessage> >(security, msgType, message.Arg);
                }

                break;
            }

            default:
                error = new InvalidOperationException(LocalizedStrings.Str1118Params.Put(message.DataType));
                break;
            }

            RaiseMarketDataMessage(message, error);
        }