Example #1
0
        // This callback is what typically message callback looks like
        // Processes messages that are important to Plaza-2 datastream lifecycle
        private static int MessageHandlerTools(Connection conn, Listener listener, Message msg)
        {
            try
            {
                if (msg.Type == MessageType.MsgStreamData)
                {
                    StreamDataMessage replmsg = (StreamDataMessage)msg;
                    fut_sess_contents tool    = new fut_sess_contents(replmsg.Data);
                    int id = tool.isin_id;

                    var oldData = APIMongo.GetToolsById(id).Result;
                    if (oldData == null)
                    {
                        var data = new DataTools {
                            Id = id, Name = tool.isin
                        };
                        APIMongo.SetTools(data.ToBsonDocument());
                    }
                }

                return(0);
            }
            catch (CGateException e)
            {
                return((int)e.ErrCode);
            }
        }
Example #2
0
        private static int MessageHandlerGlass(Connection conn, Listener listener, Message msg)
        {
            try
            {
                if (msg.Type == MessageType.MsgStreamData)
                {
                    //APIMongo.Dell();

                    StreamDataMessage replmsg = (StreamDataMessage)msg;
                    orders_aggr       glass   = new orders_aggr(replmsg.Data);
                    int id = glass.isin_id;

                    if (glass.volume == 0 || glass.price_scale == 0)
                    {
                        return(0);
                    }

                    var data = APIMongo.GetGlassById(id).Result;

                    Glass gl = new Glass();
                    gl.Price = glass.price_scale;
                    gl.Volum = glass.volume;
                    gl.Dir   = glass.dir;

                    if (data == null)
                    {
                        data    = new DataGlass();
                        data.Id = id;
                        data.ArrayGlass.Add(gl);

                        APIMongo.SetGlass(data.ToBsonDocument());
                    }
                    else
                    {
                        const int maxElem      = 50;
                        int       countElement = data.ArrayGlass.Count;

                        if (countElement == maxElem)
                        {
                            data.ArrayGlass.RemoveAt(0);
                        }

                        data.ArrayGlass.Add(gl);

                        APIMongo.UpdateGlass(data);
                    }
                }

                return(0);
            }
            catch (CGateException e)
            {
                return((int)e.ErrCode);
            }
        }
Example #3
0
        /// <summary>
        /// загрузить новую строку с изменением в станане
        /// </summary>
        /// <param name="replmsg">строка сообщения</param>
        /// <param name="security">инструмент которому принадлежит сообщение</param>
        /// <returns>возвращаем стакан в который было внесено изменение</returns>
        public MarketDepth SetNewMessage(StreamDataMessage replmsg, Security security)
        {
            long replAct = replmsg["replAct"].asLong();

            string[] str = replmsg.ToString().Split(':');

            int volume = replmsg["volume"].asInt();

            if (volume != 0 && replAct == 0 && replmsg.MsgName == "orders_aggr")
            {
                return(Insert(replmsg, security));
            }
            else
            {
                return(Delete(replmsg, security));
            }
        }
Example #4
0
        private static int MessageHandlerCandles(Connection conn, Listener listener, Message msg)
        {
            try
            {
                if (msg.Type == MessageType.MsgStreamData)
                {
                    StreamDataMessage replmsg = (StreamDataMessage)msg;
                    common            com     = new common(replmsg.Data);
                    int id = com.isin_id;

                    var dataTik = APIMongo.GetCandlesTikById(id, ElementMongo.NameTableCandlesOnTik).Result;

                    if (com.price_scale > 0)
                    {
                        if (dataTik == null)
                        {
                            DataCandlesTik data = new DataCandlesTik();
                            data.Id = id;
                            data.ArrayCandles.Add(com.price_scale);

                            APIMongo.SetCandles(data.ToBsonDocument(), ElementMongo.NameTableCandlesOnTik);
                        }
                        else
                        {
                            int countElement = dataTik.ArrayCandles.Count;

                            if (dataTik.ArrayCandles[countElement - 1] != com.price_scale)
                            {
                                dataTik.ArrayCandles.Add(com.price_scale);
                                dataTik.ArrayMax.Add(com.max_price_scale);
                                dataTik.ArrayMin.Add(com.min_price_scale);
                                dataTik.ArrayTime.Add(com.deal_time);

                                APIMongo.UpdateCandlesTik(dataTik, ElementMongo.NameTableCandlesOnTik);
                            }
                        }
                    }


                    var dataDay = APIMongo.GetCandlesDayById(id, ElementMongo.NameTableCandlesOnDays).Result;

                    if (com.close_price_scale == 0)
                    {
                        return(0);
                    }

                    Price price;

                    price.Max   = com.max_price_scale;
                    price.Min   = com.min_price_scale;
                    price.Open  = com.open_price_scale;
                    price.Close = com.close_price_scale;
                    price.Time  = com.deal_time;

                    if (dataDay == null)
                    {
                        dataDay = new DataCandlesDay();

                        dataDay.Id = id;
                        dataDay.ArrayPrices.Add(price);

                        APIMongo.SetCandles(dataDay.ToBsonDocument(), ElementMongo.NameTableCandlesOnDays);
                    }
                    else
                    {
                        dataDay.ArrayPrices.Add(price);

                        APIMongo.UpdateCandlesDay(dataDay, ElementMongo.NameTableCandlesOnDays);
                    }
                }

                return(0);
            }
            catch (CGateException e)
            {
                return((int)e.ErrCode);
            }
        }
Example #5
0
 private Task ProcessStreamItem(HubConnectionContext connection, StreamDataMessage message)
 {
     Log.ReceivedStreamItem(_logger, message);
     return(connection.StreamTracker.ProcessItem(message));
 }
Example #6
0
        public MarketDepth Delete(StreamDataMessage replmsg, Security security)
        {
            // обрабатываем ревизию

            if (_marketDepthsRevisions == null)
            {
                return(null);
            }

            RevisionInfo revision = new RevisionInfo();

            revision.ReplId   = replmsg["replID"].asLong();
            revision.Security = security.Name;

            // удаляем по этой же цене, если такая есть
            RevisionInfo revisionInArray =
                _marketDepthsRevisions.Find(info => info.Security == revision.Security && info.ReplId == revision.ReplId);

            if (revisionInArray != null)
            {
                _marketDepthsRevisions.Remove(revisionInArray);
            }

            MarketDepth myDepth = _marketDepths.Find(depth => depth.SecurityNameCode == security.Name);

            if (myDepth == null)
            {
                myDepth = new MarketDepth();
                myDepth.SecurityNameCode = security.Name;
                _marketDepths.Add(myDepth);
                return(myDepth);
            }


            if (revisionInArray == null)
            {
                return(myDepth);
            }

            int direction = replmsg["dir"].asInt(); // 1 покупка 2 продажа

            // удаляем нашу строку из стакана

            List <MarketDepthLevel> asks = null;

            if (myDepth.Asks != null)
            {
                asks = myDepth.Asks.ToList();
            }

            if (direction == 1)
            {
                // уровни покупок
                if (asks == null || asks.Count == 0)
                {
                    return(myDepth);
                }
                else
                {
                    for (int i = 0; i < asks.Count; i++)
                    {
                        // обрабатываем ситуацию когда такой уровень уже есть
                        if (asks[i].Price == revisionInArray.Price)
                        {
                            asks.Remove(asks[i]);
                            break;
                        }
                    }
                }

                myDepth.Asks = asks;
            }

            if (direction == 2)
            {
                // уровни продажи
                List <MarketDepthLevel> bids = null;

                if (myDepth.Bids != null)
                {
                    bids = myDepth.Bids.ToList();
                }

                if (bids == null || bids.Count == 0)
                {
                    return(myDepth);
                }
                else
                {
                    for (int i = 0; i < bids.Count; i++)
                    {
                        // обрабатываем ситуацию когда такой уровень уже есть
                        if (bids[i].Price == revisionInArray.Price)
                        {
                            bids.Remove(bids[i]);
                            break;
                        }
                    }
                }

                myDepth.Bids = bids;
            }


            return(myDepth);
        }
Example #7
0
        public MarketDepth Insert(StreamDataMessage replmsg, Security security)
        {
            // обрабатываем ревизию

            // создаём новую
            RevisionInfo revision = new RevisionInfo();

            revision.Price         = Convert.ToDecimal(replmsg["price"].asDecimal());
            revision.TableRevision = replmsg["replRev"].asLong();
            revision.ReplId        = replmsg["replID"].asLong();
            revision.Security      = security.Name;

            if (_marketDepthsRevisions == null)
            {
                _marketDepthsRevisions = new List <RevisionInfo>();
            }

            // удаляем по этой же цене, если такая есть
            RevisionInfo revisionInArray =
                _marketDepthsRevisions.Find(info => info.Security == revision.Security && info.ReplId == revision.ReplId);

            if (revisionInArray != null)
            {
                _marketDepthsRevisions.Remove(revisionInArray);
            }

            // добавляем новую
            _marketDepthsRevisions.Add(revision);


            // создаём строку для стакана

            MarketDepthLevel depthLevel = new MarketDepthLevel();

            int direction = replmsg["dir"].asInt(); // 1 покупка 2 продажа

            if (direction == 1)
            {
                depthLevel.Ask = replmsg["volume"].asInt();
            }
            else
            {
                depthLevel.Bid = replmsg["volume"].asInt();
            }

            depthLevel.Price = Convert.ToDecimal(replmsg["price"].asDecimal());
            // берём наш стакан
            if (_marketDepths == null)
            {
                _marketDepths = new List <MarketDepth>();
            }

            MarketDepth myDepth = _marketDepths.Find(depth => depth.SecurityNameCode == security.Name);

            if (myDepth == null)
            {
                myDepth = new MarketDepth();
                myDepth.SecurityNameCode = security.Name;
                _marketDepths.Add(myDepth);
            }

            // добавляем строку в наш стакан

            List <MarketDepthLevel> asks = null;

            if (myDepth.Asks != null)
            {
                asks = myDepth.Asks.ToList();
            }

            List <MarketDepthLevel> bids = null;

            if (myDepth.Bids != null)
            {
                bids = myDepth.Bids.ToList();
            }

            if (direction == 1)
            {
                // уровни покупок
                if (asks == null || asks.Count == 0)
                {
                    asks = new List <MarketDepthLevel>();
                    asks.Add(depthLevel);
                }
                else
                {
                    bool isInArray = false;
                    for (int i = 0; i < asks.Count; i++)
                    {
                        // обрабатываем ситуацию когда такой уровень уже есть
                        if (asks[i].Price == depthLevel.Price)
                        {
                            asks[i]   = depthLevel;
                            isInArray = true;
                            break;
                        }
                    }

                    if (isInArray == false)
                    {
                        // обрабатываем ситуацию когда такого уровня нет
                        List <MarketDepthLevel> asksNew = new List <MarketDepthLevel>();
                        bool isIn = false;

                        for (int i = 0, i2 = 0; i2 < asks.Count + 1; i2++)
                        {
                            if (i == asks.Count && isIn == false ||
                                (isIn == false &&
                                 depthLevel.Price > asks[i].Price))
                            {
                                isIn = true;
                                asksNew.Add(depthLevel);
                            }
                            else
                            {
                                asksNew.Add(asks[i]);
                                i++;
                            }
                        }


                        while (asksNew.Count > 20)
                        {
                            asksNew.Remove(asksNew[asksNew.Count - 1]);
                        }

                        asks = asksNew;
                    }

                    if (bids != null && bids.Count != 0 &&
                        bids[0].Price <= asks[0].Price)
                    {
                        while (bids.Count != 0 &&
                               bids[0].Price <= asks[0].Price)
                        {
                            bids.Remove(bids[0]);
                        }

                        myDepth.Bids = bids;
                    }
                }

                myDepth.Asks = asks;
            }

            if (direction == 2)
            {
                // уровни продажи
                if (bids == null || bids.Count == 0)
                {
                    bids = new List <MarketDepthLevel>();
                    bids.Add(depthLevel);
                }
                else
                {
                    bool isInArray = false;
                    for (int i = 0; i < bids.Count; i++)
                    {
                        // обрабатываем ситуацию когда такой уровень уже есть
                        if (bids[i].Price == depthLevel.Price)
                        {
                            bids[i]   = depthLevel;
                            isInArray = true;
                            break;
                        }
                    }
                    if (isInArray == false)
                    {
                        // обрабатываем ситуацию когда такого уровня нет
                        List <MarketDepthLevel> bidsNew = new List <MarketDepthLevel>();
                        bool isIn = false;
                        for (int i = 0, i2 = 0; i2 < bids.Count + 1; i2++)
                        {
                            if (i == bids.Count && isIn == false ||
                                (isIn == false &&
                                 depthLevel.Price < bids[i].Price))
                            {
                                isIn = true;
                                bidsNew.Add(depthLevel);
                            }
                            else
                            {
                                bidsNew.Add(bids[i]);
                                i++;
                            }
                        }

                        while (bidsNew.Count > 20)
                        {
                            bidsNew.Remove(bidsNew[bidsNew.Count - 1]);
                        }

                        bids = bidsNew;
                    }

                    if (asks != null && asks.Count != 0 &&
                        bids[0].Price <= asks[0].Price)
                    {
                        while (asks.Count != 0 && bids[0].Price <= asks[0].Price)
                        {
                            asks.Remove(asks[0]);
                        }

                        myDepth.Asks = asks;
                    }
                }

                myDepth.Bids = bids;
            }


            return(myDepth);
        }
 public static void ReceivedStreamItem(ILogger logger, StreamDataMessage message)
 {
     _receivedStreamItem(logger, message.StreamId, null);
 }
Example #9
0
 public Task ProcessItem(StreamDataMessage message)
 {
     return(TryGetConverter(message.StreamId).WriteToStream(message.Item));
 }
 private bool StreamDataMessagesEqual(StreamDataMessage x, StreamDataMessage y)
 {
     return(x.StreamId == y.StreamId &&
            (Equals(x.Item, y.Item) || SequenceEqual(x.Item, y.Item)));
 }