Ejemplo n.º 1
0
        public virtual void StopListenTickerStream(Ticker ticker)
        {
            SubscribedTickers.Remove(ticker);

            SocketConnectionInfo info = GetConnectionInfo(ticker, OrderBookSockets);

            if (info != null)
            {
                info.Socket.Error           -= OnOrderBookSocketError;
                info.Socket.Opened          -= OnOrderBookSocketOpened;
                info.Socket.Closed          -= OnOrderBookSocketClosed;
                info.Socket.MessageReceived -= OnOrderBookSocketMessageReceived;
                info.Close();
                info.Dispose();
                OrderBookSockets.Remove(info.Socket);
            }

            info = GetConnectionInfo(ticker, TradeHistorySockets);
            if (info != null)
            {
                info.Socket.Error           -= OnTradeHistorySocketError;
                info.Socket.Opened          -= OnTradeHistorySocketOpened;
                info.Socket.Closed          -= OnTradeHistorySocketClosed;
                info.Socket.MessageReceived -= OnTradeHistorySocketMessageReceived;
                info.Close();
                info.Dispose();
                TradeHistorySockets.Remove(info.Socket);
            }
        }
Ejemplo n.º 2
0
        public override void StartListenTickerStream(Ticker ticker)
        {
            base.StartListenTickerStream(ticker);
            SocketConnectionInfo info = CreateOrderBookWebSocket(ticker);

            OrderBookSockets.Add(info.Socket, info);
            SocketConnectionInfo tradeInfo = CreateTradesWebSocket(ticker);

            TradeHistorySockets.Add(tradeInfo.Socket, tradeInfo);
        }
 protected internal override void ApplyCapturedEvent(Ticker ticker, TickerCaptureDataInfo info)
 {
     if (info.StreamType == CaptureStreamType.OrderBook)
     {
         OnOrderBookSocketMessageReceived(OrderBookSockets.FirstOrDefault(s => s.Ticker == ticker).Socket, new MessageReceivedEventArgs(info.Message));
     }
     else if (info.StreamType == CaptureStreamType.TradeHistory)
     {
         OnTradeHistorySocketMessageReceived(TradeHistorySockets.FirstOrDefault(s => s.Ticker == ticker).Socket, new MessageReceivedEventArgs(info.Message));
     }
 }
        protected internal override void OnTradeHistorySocketError(object sender, ErrorEventArgs e)
        {
            SocketConnectionInfo info = TradeHistorySockets.FirstOrDefault(c => c.Key == sender);

            if (info != null)
            {
                if (CheckProcessError(info))
                {
                    return;
                }
            }
            base.OnTradeHistorySocketError(sender, e);
        }
Ejemplo n.º 5
0
        protected internal override void OnTradeHistorySocketMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            byte[] bytes      = Encoding.Default.GetBytes(e.Message);
            int    startIndex = 0;

            string[]             trades = JSonHelper.Default.DeserializeObject(bytes, ref startIndex, TradeItems);
            SocketConnectionInfo info   = TradeHistorySockets.FirstOrDefault(c => c.Key == sender);

            if (info != null && info.Ticker.CaptureData)
            {
                info.Ticker.CaptureDataCore(CaptureStreamType.TradeHistory, CaptureMessageType.Incremental, e.Message);
            }
            OnTradeHistoryItemRecv(info.Ticker, trades);
        }
        protected internal override void OnTradeHistorySocketMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            LastWebSocketRecvTime = DateTime.Now;
            SocketConnectionInfo info = TradeHistorySockets.FirstOrDefault(c => c.Key == sender);

            if (info == null)
            {
                return;
            }
            Ticker t = info.Ticker;

            if (t.IsUpdatingTrades)
            {
                return;
            }

            if (t.CaptureData)
            {
                t.CaptureDataCore(CaptureStreamType.TradeHistory, CaptureMessageType.Incremental, e.Message);
            }

            JObject obj   = JsonConvert.DeserializeObject <JObject>(e.Message);
            JArray  items = obj.Value <JArray>("data");

            if (items == null)
            {
                return;
            }
            foreach (JObject item in items)
            {
                TradeInfoItem ti = new TradeInfoItem(null, t);
                ti.TimeString   = item.Value <string>("timestamp");
                ti.Type         = String2TradeType(item.Value <string>("side"));
                ti.AmountString = item.Value <string>("size");
                ti.RateString   = item.Value <string>("price");
                t.AddTradeHistoryItem(ti); //.InsertTradeHistoryItem(ti);
            }

            if (t.HasTradeHistorySubscribers)
            {
                TradeHistoryChangedEventArgs ee = new TradeHistoryChangedEventArgs()
                {
                    NewItem = t.TradeHistory.First()
                };
                t.RaiseTradeHistoryChanged(ee);
            }
        }