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);
            }
        }
        protected internal override void OnOrderBookSocketMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            base.OnOrderBookSocketMessageReceived(sender, e);
            LastWebSocketRecvTime = DateTime.Now;
            SocketConnectionInfo info = OrderBookSockets.FirstOrDefault(c => c.Key == sender);

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

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

            const string incrementalUpdateStartString = "{\"table\":\"orderBookL2\",\"action\":\"";

            if (e.Message.StartsWith(incrementalUpdateStartString))
            {
                int index = incrementalUpdateStartString.Length;
                if (e.Message[index] == 'u')  // update
                {
                    index = e.Message.IndexOf("[{");
                    List <string[]> jsItems = JSonHelper.Default.DeserializeArrayOfObjects(Encoding.ASCII.GetBytes(e.Message), ref index, UpdateItems);
                    foreach (string[] item in jsItems)
                    {
                        OrderBookEntryType entryType = item[2][0] == 'S' ? OrderBookEntryType.Ask : OrderBookEntryType.Bid;
                        t.OrderBook.ApplyIncrementalUpdate(entryType, OrderBookUpdateType.Modify, FastValueConverter.ConvertPositiveLong(item[1]), null, item[3]);
                    }
                    return;
                }
                //else if(e.Message[index] == 'i') { // insert
                //    index = e.Message.IndexOf("[{");
                //    List<string[]> jsItems = JSonHelper.Default.DeserializeArrayOfObjects(Encoding.ASCII.GetBytes(e.Message), ref index, InsertItems);
                //    foreach(string[] item in jsItems) {
                //        OrderBookEntryType entryType = item[2][0] == 'S' ? OrderBookEntryType.Ask : OrderBookEntryType.Bid;
                //        t.OrderBook.ApplyIncrementalUpdate(entryType, OrderBookUpdateType.Add, FastValueConverter.ConvertPositiveLong(item[1]), item[4], item[3]);
                //    }
                //    return;
                //}
                else if (e.Message[index] == 'd')  // delete
                {
                    index = e.Message.IndexOf("[{");
                    List <string[]> jsItems = JSonHelper.Default.DeserializeArrayOfObjects(Encoding.ASCII.GetBytes(e.Message), ref index, DeleteItems);
                    foreach (string[] item in jsItems)
                    {
                        OrderBookEntryType entryType = item[2][0] == 'S' ? OrderBookEntryType.Ask : OrderBookEntryType.Bid;
                        t.OrderBook.ApplyIncrementalUpdate(entryType, OrderBookUpdateType.Remove, FastValueConverter.ConvertPositiveLong(item[1]), null, null);
                    }
                    return;
                }
            }
            JObject obj   = JsonConvert.DeserializeObject <JObject>(e.Message);
            JArray  items = obj.Value <JArray>("data");

            if (items == null)
            {
                return;
            }

            OrderBookUpdateType type = String2UpdateType(obj.Value <string>("action"));

            lock (t.OrderBook.Bids) {
                lock (t.OrderBook.Asks) {
                    if (type == OrderBookUpdateType.RefreshAll)
                    {
                        t.OrderBook.Clear();
                        t.OrderBook.BeginUpdate();
                    }
                    for (int i = 0; i < items.Count; i++)
                    {
                        JObject item = (JObject)items[i];

                        OrderBookEntryType entryType = item.Value <string>("side")[0] == 'S' ? OrderBookEntryType.Ask : OrderBookEntryType.Bid;
                        string             rate      = null;
                        if (type == OrderBookUpdateType.Add || type == OrderBookUpdateType.RefreshAll)
                        {
                            rate = item.Value <string>("price");
                        }
                        string size = null;
                        if (type != OrderBookUpdateType.Remove)
                        {
                            size = item.Value <string>("size");
                        }
                        t.OrderBook.ApplyIncrementalUpdate(entryType, type, item.Value <long>("id"), rate, size);
                    }
                }
            }
            if (type == OrderBookUpdateType.RefreshAll)
            {
                t.OrderBook.EndUpdate();
            }
        }
Ejemplo n.º 3
0
        internal void OnUpdateOrderBook(Ticker ticker, byte[] bytes)
        {
            int startIndex = 0;

            string[] updateId = JSonHelper.Default.StartDeserializeObject(bytes, ref startIndex, OrderBookStartItems);

            if (ticker.CaptureData)
            {
                ticker.CaptureDataCore(CaptureStreamType.OrderBook, CaptureMessageType.Snapshot, System.Text.ASCIIEncoding.Default.GetString(bytes));
            }

            const string bidString = "\"bids\":";
            const string askString = "\"asks\":";

            startIndex += bidString.Length + 1;
            List <string[]> jbids = JSonHelper.Default.DeserializeArrayOfArrays(bytes, ref startIndex, 2);

            startIndex += askString.Length + 1;
            List <string[]> jasks = JSonHelper.Default.DeserializeArrayOfArrays(bytes, ref startIndex, 2);

            ticker.OrderBook.BeginUpdate();
            try {
                List <OrderBookEntry> bids  = ticker.OrderBook.Bids;
                List <OrderBookEntry> asks  = ticker.OrderBook.Asks;
                List <OrderBookEntry> iasks = ticker.OrderBook.AsksInverted;

                bids.Clear();
                asks.Clear();
                if (iasks != null)
                {
                    iasks.Clear();
                }
                if (jbids != null)
                {
                    for (int i = 0; i < jbids.Count; i++)
                    {
                        string[] item = jbids[i];
                        bids.Add(new OrderBookEntry()
                        {
                            ValueString = item[0], AmountString = item[1]
                        });
                    }
                }
                if (jasks != null)
                {
                    for (int i = 0; i < jasks.Count; i++)
                    {
                        string[]       item = jasks[i];
                        OrderBookEntry e    = new OrderBookEntry()
                        {
                            ValueString = item[0], AmountString = item[1]
                        };
                        asks.Add(e);
                        if (iasks != null)
                        {
                            iasks.Insert(0, e);
                        }
                    }
                }
                ticker.OrderBook.Updates.Clear(FastValueConverter.ConvertPositiveLong(updateId[0]) + 1);
            }
            finally {
                ticker.OrderBook.IsDirty = false;
                ticker.OrderBook.EndUpdate();
            }
            ticker.RaiseChanged();
        }