コード例 #1
0
        private void ProcessMarketDataMessage(MarketDataMessage mdMsg)
        {
            switch (mdMsg.DataType)
            {
            case MarketDataTypes.Level1:
            {
                SendCommand(mdMsg.IsSubscribe
                                                                        ? new SubscribeMessage {
                        Quotations = { mdMsg.SecurityId }
                    }
                                                                        : new UnsubscribeMessage {
                        Quotations = { mdMsg.SecurityId }
                    });

                break;
            }

            case MarketDataTypes.MarketDepth:
            {
                SendCommand(mdMsg.IsSubscribe
                                                                        ? new SubscribeMessage {
                        Quotes = { mdMsg.SecurityId }
                    }
                                                                        : new UnsubscribeMessage {
                        Quotes = { mdMsg.SecurityId }
                    });

                break;
            }

            case MarketDataTypes.Trades:
            {
                if (mdMsg.IsSubscribe)
                {
                    //Подписаться/отписаться на тики можно двумя способами:
                    //SubscribeMessage/UnsubscribeMessage - тики приходят с момента подписки
                    //SubscribeTicksMessage - Тики приходят с момента подиски(TradeNo = 0), или с любого номера. При повторном запросе отписка получения тиков по предыдущему запросу.


                    //var command = new SubscribeMessage();
                    //command.AllTrades.Add(security.GetTransaqId());

                    //ApiClient.Send(new Tuple<BaseCommandMessage, Action<BaseResponse>>(command, ProcessResult));

                    //---

                    _registeredSecurityIds.Add(mdMsg.SecurityId);

                    var command = new SubscribeTicksMessage {
                        Filter = true
                    };                                                                                     //Filter только сделки нормально периода торгов

                    foreach (var id in _registeredSecurityIds)
                    {
                        command.Items.Add(new SubscribeTicksSecurity
                            {
                                SecId   = (int)id.Native,
                                TradeNo = id.Equals(mdMsg.SecurityId) ? 1 : 0,
                            });
                    }

                    SendCommand(command);
                }
                else
                {
                    //var command = new UnsubscribeMessage();
                    //command.AllTrades.Add(security.GetTransaqId());

                    //ApiClient.Send(new Tuple<BaseCommandMessage, Action<BaseResponse>>(command, ProcessResult));

                    //---

                    _registeredSecurityIds.Remove(mdMsg.SecurityId);

                    var command = new SubscribeTicksMessage {
                        Filter = true
                    };                                                                                     //Filter только сделки нормально периода торгов

                    foreach (var id in _registeredSecurityIds)
                    {
                        command.Items.Add(new SubscribeTicksSecurity
                            {
                                SecId   = (int)id.Native,
                                TradeNo = 0,
                            });
                    }

                    SendCommand(command);
                }

                break;
            }

            case MarketDataTypes.News:
            {
                if (mdMsg.IsSubscribe)
                {
                    if (mdMsg.NewsId.IsEmpty())
                    {
                        var count = (int)mdMsg.Count;

                        if (count <= 0)
                        {
                            throw new InvalidOperationException(LocalizedStrings.Str3511Params.Put(count));
                        }

                        if (count > MaxNewsHeaderCount)
                        {
                            throw new InvalidOperationException(LocalizedStrings.Str3512Params.Put(count, MaxNewsHeaderCount));
                        }

                        SendCommand(new RequestOldNewsMessage {
                                Count = count
                            });
                    }
                    else
                    {
                        SendCommand(new RequestNewsBodyMessage {
                                NewsId = mdMsg.NewsId.To <int>()
                            });
                    }
                }

                break;
            }

            case MarketDataTypes.CandleTimeFrame:
            {
                if (mdMsg.IsSubscribe)
                {
                    var periodId = _candlePeriods.GetKeys((TimeSpan)mdMsg.Arg).First();

                    _candleTransactions.Add(Tuple.Create((int)mdMsg.SecurityId.Native, periodId), mdMsg.TransactionId);

                    var command = new RequestHistoryDataMessage
                    {
                        SecId  = (int)mdMsg.SecurityId.Native,
                        Period = periodId,
                        Count  = mdMsg.Count,
                        Reset  = mdMsg.To == DateTimeOffset.MaxValue,
                    };

                    SendCommand(command);
                }

                break;
            }

            default:
                throw new ArgumentOutOfRangeException("mdMsg", mdMsg.DataType, LocalizedStrings.Str1618);
            }

            var reply = (MarketDataMessage)mdMsg.Clone();

            reply.OriginalTransactionId = mdMsg.TransactionId;
            SendOutMessage(reply);
        }
コード例 #2
0
        private void ProcessMarketDataMessage(MarketDataMessage mdMsg)
        {
            switch (mdMsg.DataType)
            {
            case MarketDataTypes.Level1:
            {
                SendCommand(mdMsg.IsSubscribe
                                                                        ? new SubscribeMessage {
                        Quotations = { mdMsg.SecurityId }
                    }
                                                                        : new UnsubscribeMessage {
                        Quotations = { mdMsg.SecurityId }
                    });

                break;
            }

            case MarketDataTypes.MarketDepth:
            {
                SendCommand(mdMsg.IsSubscribe
                                                                        ? new SubscribeMessage {
                        Quotes = { mdMsg.SecurityId }
                    }
                                                                        : new UnsubscribeMessage {
                        Quotes = { mdMsg.SecurityId }
                    });

                break;
            }

            case MarketDataTypes.Trades:
            {
                if (mdMsg.IsSubscribe)
                {
                    //Подписаться/отписаться на тики можно двумя способами:
                    //SubscribeMessage/UnsubscribeMessage - тики приходят с момента подписки
                    //SubscribeTicksMessage - Тики приходят с момента подиски(TradeNo = 0), или с любого номера. При повторном запросе отписка получения тиков по предыдущему запросу.


                    //var command = new SubscribeMessage();
                    //command.AllTrades.Add(security.GetTransaqId());

                    //ApiClient.Send(new Tuple<BaseCommandMessage, Action<BaseResponse>>(command, ProcessResult));

                    //---

                    _registeredSecurityIds.Add(mdMsg.SecurityId);

                    var command = new SubscribeTicksMessage {
                        Filter = true
                    };                                                                                     //Filter только сделки нормально периода торгов

                    foreach (var id in _registeredSecurityIds)
                    {
                        command.Items.Add(new SubscribeTicksSecurity
                            {
                                SecId = (int)id.Native,
                                // http://stocksharp.com/forum/yaf_postsm35978_Obnovlieniie-Tranzak-do-viersii-2-16-1.aspx#post35978
                                //TradeNo = id.Equals(mdMsg.SecurityId) ? 1 : 0,
                                TradeNo = 1,
                            });
                    }

                    SendCommand(command);
                }
                else
                {
                    //var command = new UnsubscribeMessage();
                    //command.AllTrades.Add(security.GetTransaqId());

                    //ApiClient.Send(new Tuple<BaseCommandMessage, Action<BaseResponse>>(command, ProcessResult));

                    //---

                    _registeredSecurityIds.Remove(mdMsg.SecurityId);

                    var command = new SubscribeTicksMessage {
                        Filter = true
                    };                                                                                     //Filter только сделки нормально периода торгов

                    foreach (var id in _registeredSecurityIds)
                    {
                        command.Items.Add(new SubscribeTicksSecurity
                            {
                                SecId   = (int)id.Native,
                                TradeNo = 0,
                            });
                    }

                    SendCommand(command);
                }

                break;
            }

            case MarketDataTypes.News:
            {
                if (mdMsg.IsSubscribe)
                {
                    if (mdMsg.NewsId.IsEmpty())
                    {
                        var count = mdMsg.Count;

                        if (count == null)
                        {
                            count = MaxNewsHeaderCount;
                        }
                        else
                        {
                            if (count < 0)
                            {
                                throw new InvalidOperationException(LocalizedStrings.Str3511Params.Put(count));
                            }

                            if (count > MaxNewsHeaderCount)
                            {
                                throw new InvalidOperationException(LocalizedStrings.Str3512Params.Put(count, MaxNewsHeaderCount));
                            }
                        }

                        SendCommand(new RequestOldNewsMessage {
                                Count = (int)count.Value
                            });
                    }
                    else
                    {
                        SendCommand(new RequestNewsBodyMessage {
                                NewsId = mdMsg.NewsId.To <int>()
                            });
                    }
                }

                break;
            }

            case MarketDataTypes.CandleTimeFrame:
            {
                if (mdMsg.IsSubscribe)
                {
                    var periodId = _candlePeriods.GetKeys((TimeSpan)mdMsg.Arg).First();
                    var secId    = (int)mdMsg.SecurityId.Native;
                    var key      = Tuple.Create(secId, periodId);

                    _candleTransactions.Add(key, mdMsg.TransactionId);

                    var command = new RequestHistoryDataMessage
                    {
                        SecId  = secId,
                        Period = periodId,
                        Count  = mdMsg.Count ?? 0,
                        Reset  = mdMsg.To == null,
                    };

                    try
                    {
                        SendCommand(command);
                    }
                    catch (Exception)
                    {
                        _candleTransactions.Remove(key);
                        throw;
                    }
                }

                break;
            }

            default:
            {
                SendOutMarketDataNotSupported(mdMsg.TransactionId);
                return;
            }
            }

            var reply = (MarketDataMessage)mdMsg.Clone();

            reply.OriginalTransactionId = mdMsg.TransactionId;
            SendOutMessage(reply);
        }