private string  GetAuthMsg(ref bool bIsValid)
        {
            try
            {
                long   authNonce   = BitfinexAuthentication.CreateAuthNonce();
                string authPayload = BitfinexAuthentication.CreateAuthPayload(authNonce);
                string authSig     = BitfinexAuthentication.CreateSignature(authPayload, _apiSecret);

                RequestAuth reqAuth = new RequestAuth
                {
                    ApiKey      = _apiKey,
                    AuthNonce   = authNonce,
                    AuthSig     = authSig,
                    AuthPayload = authPayload
                };

                bIsValid = true;

                return(CBitfinexJsonSerializer.SerializeObject(reqAuth));
            }
            catch (Exception e)
            {
                Error("Unable gen AuthMsg");
                return("");
            }
        }
        public string GetSubscribeTrade(string instrument)
        {
            RequestSubsribeTrades reqTr = new RequestSubsribeTrades {
                Symbol = instrument
            };
            string msgTrade = CBitfinexJsonSerializer.SerializeObject(reqTr);

            return(msgTrade);
        }
        public void SendPing(int cid = 0)
        {
            RequestPing rp = new RequestPing {
                Cid = cid
            };
            string msg = CBitfinexJsonSerializer.SerializeObject(rp);

            SendMessage(msg);
        }
        /*
         * public void UpdateStock(long chanId, CBookParam 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)
         *      {
         *          bNeedRemove = true;
         *          keyRemove = kvp.Key;
         *      }
         *  }
         *  if (bNeedRemove)
         *      _dictBookChanidInstr.Remove(keyRemove);
         *
         *
         *  _dictBookChanidInstr[chanId] = newBookParam;
         *
         *
         * }
         */



        public string GetSubscribeBook(string instrument, int precisionLevel)
        {
            // int precisionLevel = 1;
            RequestSubscribeBook reqBook = new RequestSubscribeBook {
                Prec   = String.Format("P{0}", precisionLevel),
                Symbol = instrument,
                Len    = precisionLevel == 0 ? "25": "100"
                         // Len = "100"
            };
            string msgBook = CBitfinexJsonSerializer.SerializeObject(reqBook);

            return(msgBook);
        }
        private void ProcessResponseError(string rawMsg, ResponseError responseError)
        {
            string msg = Err.GetFullErrorMessage(responseError);

            if (responseError.Code == Err.ERR_SUB_FAIL)
            {
                ResponseErrorSubscribe res = CBitfinexJsonSerializer.DeserializeObject <ResponseErrorSubscribe>(rawMsg);
                msg += " Symbol=" + res.Symbol;
            }


            Log(msg);
            Error(msg);
        }
        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");
            }
        }