コード例 #1
0
        protected override void ReceivedDepth(string _symbol, string _type, JToken _token)
        {
            JArray _array = (JArray)_token;
            JArray _list  = (_array.Count == 2 && _array[1].Type == JTokenType.Array) ? _array[1].Value <JArray>() : _array;

            //_symbol = _symbol.Split('.')[1];

            if (_type == "START")
            {
                BookItems _asks = new BookItems(MarketSide.Ask);
                BookItems _bids = new BookItems(MarketSide.Bid);

                foreach (JArray _item in _list)
                {
                    decimal    _price  = _item[0].Value <decimal>();
                    int        _count  = _item[1].Value <int>();
                    decimal    _amount = _item[2].Value <decimal>();
                    MarketSide _side   = _amount > 0 ? MarketSide.Bid : MarketSide.Ask;
                    string     _id     = _price.ToString();

                    BookItem _bookItem = new BookItem(_symbol, _side, _price, Math.Abs(_amount), _id);
                    if (_side == MarketSide.Bid)
                    {
                        _bids.TryAdd(_id, _bookItem);
                    }
                    if (_side == MarketSide.Ask)
                    {
                        _asks.TryAdd(_id, _bookItem);
                    }
                }

                this.Books[_symbol, MarketSide.Ask] = _asks;
                this.Books[_symbol, MarketSide.Bid] = _bids;
                this.OnBookStarted(_symbol);
            }
            else
            {
                decimal    _price  = _list[1].Value <decimal>();
                int        _count  = _list[2].Value <int>();
                decimal    _amount = _list[3].Value <decimal>();
                MarketSide _side   = _amount > 0 ? MarketSide.Bid : MarketSide.Ask;

                BookItems _items = this.Books[_symbol, _side];
                if (_count == 0)
                {
                    BookItem _item = _items.Delete(_price.ToString());
                    if (_item != null)
                    {
                        this.OnBookDelete(_item);
                    }
                }
                else
                {
                    BookItem _item = _items.Update(_price.ToString(), Math.Abs(_amount));
                    if (_item == null)
                    {
                        _item = _items.Insert(_price.ToString(), _price, _amount);
                        this.OnBookInsert(_item);
                    }
                    else
                    {
                        this.OnBookUpdate(_item);
                    }
                }
            }
        }
コード例 #2
0
        protected override void ReceivedDepth(string _symbol, string _type, JToken _token)
        {
            JObject _json = (JObject)_token;

            if (_type == "FULL")
            {
                #region Bid
                IList <KeyValuePair <string, BookItem> > _bidItems = this.Books[_symbol, MarketSide.Bid].ToList();
                BookItems _bidList = new BookItems(MarketSide.Bid);
                JArray    _bids    = _json["bids"].Value <JArray>();
                for (int i = 0; i < _bids.Count; i++)
                {
                    decimal _price  = _bids[i][0].Value <decimal>();
                    decimal _amount = _bids[i][1].Value <decimal>();

                    KeyValuePair <string, BookItem>[] _temps = _bidItems.Where(c => c.Key == _price.ToString()).ToArray();
                    if (_temps.Length == 0)
                    {
                        this.OnBookInsert(_bidList.Insert(_price.ToString(), _price, _amount));
                    }
                    else
                    {
                        BookItem _item = _temps[0].Value;
                        if (_item.Amount != _amount)
                        {
                            _item.Amount = _amount;
                            this.OnBookUpdate(_item);
                        }
                        _bidList.Insert(_item.Id, _item.Price, _amount);
                        _bidItems.Remove(_temps[0]);
                    }
                }
                foreach (KeyValuePair <string, BookItem> _item in _bidItems)
                {
                    this.OnBookDelete(_item.Value);
                }
                #endregion

                #region Ask
                BookItems _askList = new BookItems(MarketSide.Ask);
                IList <KeyValuePair <string, BookItem> > _askItems = this.Books[_symbol, MarketSide.Ask].ToList();
                JArray _asks = _json["asks"].Value <JArray>();
                for (int i = 0; i < _asks.Count; i++)
                {
                    decimal _price  = _asks[i][0].Value <decimal>();
                    decimal _amount = _asks[i][1].Value <decimal>();

                    KeyValuePair <string, BookItem>[] _temps = _askItems.Where(c => c.Key == _price.ToString()).ToArray();
                    if (_temps.Length == 0)
                    {
                        this.OnBookInsert(_askList.Insert(_price.ToString(), _price, _amount));
                    }
                    else
                    {
                        BookItem _item = _temps[0].Value;
                        if (_item.Amount != _amount)
                        {
                            _item.Amount = _amount;
                            this.OnBookUpdate(_item);
                        }
                        _askList.Insert(_item.Id, _item.Price, _amount);
                        _askItems.Remove(_temps[0]);
                    }
                }
                foreach (KeyValuePair <string, BookItem> _item in _askItems)
                {
                    this.OnBookDelete(_item.Value);
                }
                #endregion

                this.Books[_symbol, MarketSide.Ask] = _askList;
                this.Books[_symbol, MarketSide.Bid] = _bidList;
            }
            else if (_type == "UPDATE")
            {
                #region Bid
                if (_json.Property("bids") != null)
                {
                    JArray    _bids    = _json["bids"].Value <JArray>();
                    BookItems _bidList = this.Books[_symbol, MarketSide.Bid];
                    for (int i = 0; i < _bids.Count; i++)
                    {
                        decimal _price  = _bids[i][0].Value <decimal>();
                        decimal _amount = _bids[i][1].Value <decimal>();

                        if (_amount == 0)
                        {
                            BookItem _item = _bidList.Delete(_price.ToString());
                            if (_item != null)
                            {
                                this.OnBookDelete(_item);
                            }
                        }
                        else
                        {
                            BookItem _item = _bidList.Update(_price.ToString(), _amount);
                            if (_item != null)
                            {
                                this.OnBookUpdate(_item);
                            }
                            else
                            {
                                this.OnBookInsert(_bidList.Insert(_price.ToString(), _price, _amount));
                            }
                        }
                    }
                }
                #endregion

                #region Ask
                if (_json.Property("asks") != null)
                {
                    BookItems _askList = this.Books[_symbol, MarketSide.Ask];
                    JArray    _asks    = _json["asks"].Value <JArray>();
                    for (int i = 0; i < _asks.Count; i++)
                    {
                        decimal _price  = _asks[i][0].Value <decimal>();
                        decimal _amount = _asks[i][1].Value <decimal>();

                        if (_amount == 0)
                        {
                            BookItem _item = _askList.Delete(_price.ToString());
                            if (_item != null)
                            {
                                this.OnBookDelete(_item);
                            }
                        }
                        else
                        {
                            BookItem _item = _askList.Update(_price.ToString(), _amount);
                            if (_item != null)
                            {
                                this.OnBookUpdate(_item);
                            }
                            else
                            {
                                this.OnBookInsert(_askList.Insert(_price.ToString(), _price, _amount));
                            }
                        }
                    }
                }
                #endregion
            }
        }