private void ProcessOrderBookUpdate(CBookParams bookParam, JArray jArrOrderBookUpdate)
        {
            _perf.StartOrdeBookUpd();

            decimal price  = (decimal)jArrOrderBookUpdate[0];
            long    count  = (long)jArrOrderBookUpdate[1];
            decimal amount = (decimal)jArrOrderBookUpdate[2];

            /*
             * _dictStockStor[bookParam.Instrument].Update(CBfxUtils.GetPrecInt(bookParam.Precision),
             *                                                 price,count, amount);
             *
             */

            _dictStockStor[bookParam.Instrument].Update(
                new CBfxStockStorUpdStock
            {
                amount = amount,
                count  = count,
                prec   = CBfxUtils.GetPrecInt(bookParam.Precision),
                price  = price
            }
                );

            _perf.EndOrderBookUpd();
        }
        private void UpdateBookChanInstr(long chanId, CBookParams newBookParam)
        {
            //UpdateChanInstrDict(_dictBookChanidInstr, chanId, instrument);
            //UpdateBookChanInstr(chanId, newBookParam);
            bool bNeedRemove = false;
            long keyRemove   = 0;

            //if channel changed (for example after disconnect)
            //need to remove old channel first
            foreach (var kvp in _dictBookChanidInstr)
            {
                if (kvp.Value.Instrument == newBookParam.Instrument &&
                    kvp.Value.Precision == newBookParam.Precision)
                {
                    bNeedRemove = true;
                    keyRemove   = kvp.Key;
                }
            }
            if (bNeedRemove)
            {
                _dictBookChanidInstr.Remove(keyRemove);
            }


            _dictBookChanidInstr[chanId] = newBookParam;
        }
        private bool GetBookParam(long chanId, ref CBookParams bookParam)
        {
            if (_dictBookChanidInstr.ContainsKey(chanId))
            {
                bookParam = _dictBookChanidInstr[chanId];
                return(true);
            }


            return(false);
        }
        private void ProcessOrderBookSnapshot(CBookParams bookParam, JArray jArrOrderBook)
        {
            _perf.StartOrdeBookSnapshot();

            /*
             * _dictStockStor[bookParam.Instrument].UpdateBySnapshot(CBfxUtils.GetPrecInt(bookParam.Precision),
             *                                                          jArrOrderBook);
             */


            _dictStockStor[bookParam.Instrument].UpdateBySnapshot
                (new CBfxStockStorMsgUpdSnap
            {
                prec          = CBfxUtils.GetPrecInt(bookParam.Precision),
                jArrOrderBook = jArrOrderBook
            });

            _perf.EndOrderBookSnapshot();
        }
        public void ProcessData(string data)
        {
            LogRaw(data);


            if (data[0] == '{') //event message
            {
                MessageBase mb = CBitfinexJsonSerializer.DeserializeObject <MessageBase>(data);


                if (mb.Event == EnmMessageType.Info)
                {
                    ProcessResponseInfo(CBitfinexJsonSerializer.DeserializeObject <ResponseInfo>(data));
                }
                else if (mb.Event == EnmMessageType.Auth)
                {
                    ProcessResponseAuth(CBitfinexJsonSerializer.DeserializeObject <ResponseAuth>(data));
                }
                else if (mb.Event == EnmMessageType.Error)
                {
                    ProcessResponseError(data, CBitfinexJsonSerializer.DeserializeObject <ResponseError>(data));
                }
                else if (mb.Event == EnmMessageType.Subscribed)
                {
                    ResponseSubscribed rs = CBitfinexJsonSerializer.DeserializeObject <ResponseSubscribed>(data);
                    if (rs.Channel == "book")
                    {
                        ResponseSubscribedBook rsb = CBitfinexJsonSerializer.DeserializeObject <ResponseSubscribedBook>(data);

                        //string instrument = rs.Symbol[0] == 't' ? rs.Symbol.Remove(0, 1) : rs.Symbol;
                        CBookParams bp = new CBookParams
                        {
                            Instrument = rsb.Symbol[0] == 't' ? rsb.Symbol.Remove(0, 1) : rsb.Symbol,
                            Precision  = rsb.Prec
                        };


                        //string instrument = rs.Symbol;
                        UpdateBookChanInstr(rs.ChanId, bp);
                    }
                    else if (rs.Channel == "trades")
                    {
                        UpdateTradesChanInstr(rs.ChanId, rs.Symbol);
                    }
                }
            }
            else if (data[0] == '[') //channel data
            {
                JArray jArr = CBitfinexJsonSerializer.DeserializeObject <JArray>(data);

                if (jArr.Count() < 2)
                {
                    Error("Invalid message");
                    return;
                }

                long channelId = (int)jArr[0];

                string      instrument = "";
                CBookParams bookParam  = null;

                if (channelId == _chanIdPersonalData)
                {
                    string evnt = (string)jArr[1];

                    if (evnt == "ws")
                    {
                        ProcessWalletSnapshot(jArr);
                    }
                    else if (evnt == "wu")
                    {
                        ProcessWalletUpdate(jArr);
                    }
                    else if (evnt == "ps")
                    {
                        ProcessPositionsSnapshot((JArray)jArr[2]);
                    }
                    else if (evnt == "pn")
                    {
                        ProcessPositionNew((JArray)jArr[2]);
                    }
                    else if (evnt == "pu")
                    {
                        ProcessPositionUpdate((JArray)jArr[2]);
                    }
                    else if (evnt == "pc")
                    {
                        ProcessPositionClose((JArray)jArr[2]);
                    }
                    else if (evnt == "os")
                    {
                        ProcessOrdersSnapshot((JArray)jArr[2]);
                    }
                    else if (evnt == "on")
                    {
                        ProcessOrderNew((JArray)jArr[2]);
                    }
                    else if (evnt == "ou")
                    {
                        ProcessOrderUpdate((JArray)jArr[2]);
                    }
                    else if (evnt == "oc")
                    {
                        ProcessOrderCancell((JArray)jArr[2]);
                    }
                    else if (evnt == "fos")
                    {
                        ProcesFundingOrderSnapshot(jArr);
                    }
                    else if (evnt == "n")
                    {
                        ProcessNotification((JArray)jArr[2]);
                    }
                    else if (evnt == "te")
                    {
                        ProcessUserTradeExecute((JArray)jArr[2]);
                    }
                    else if (evnt == "tu")
                    {
                        ProcessUserTradeUpdate((JArray)jArr[2]);
                    }
                    else if (evnt == "hb")
                    {
                        if (_name.Contains("auth"))
                        {
                            ProcessHeartBeatAuth();
                        }
                    }
                }
                else if (GetBookParam(channelId, ref bookParam))
                {
                    if (jArr.Count() == 2)
                    {
                        if (IsHeartBeat(jArr[1]))
                        {
                            return;
                        }
                        JArray arr = (JArray)jArr[1];
                        if (arr[0].Type == JTokenType.Array)
                        {
                            ProcessOrderBookSnapshot(bookParam, arr);
                        }
                        else
                        {
                            ProcessOrderBookUpdate(bookParam, arr);
                        }
                    }
                }
                else if (GetTradesInst(channelId, ref instrument))
                {
                    if (jArr.Count() == 2)
                    {
                        if (IsHeartBeat(jArr[1]))
                        {
                            return;
                        }


                        ProcessTradesSnapshot(instrument, (JArray)jArr[1]);
                    }
                    else if (jArr.Count() == 3)
                    {
                        string evnt = (string)jArr[1];
                        //As we don't need trade id - use te, which must be recieved first,
                        //not tu using this docs:
                        // http://blog.bitfinex.com/api/websocket-api-update/
                        if (evnt == "te")
                        {
                            ProcessTradeExecute(instrument, (JArray)jArr[2]);
                        }
                    }
                }
            }
            else
            {
                Error("Websocket. Unknown message");
            }
        }