Ejemplo n.º 1
0
        protected override void ReceivedDepth(string _pair, string _type, JToken _token)
        {
            JObject _json = (JObject)_token;

            #region Bid
            IList <KeyValuePair <string, BookItem> > _bidItems = this.Books[_pair, MarketSide.Bid].ToList();
            BookItems _bidList = new BookItems(MarketSide.Bid);
            JArray    _bids    = _json["bids"].Value <JArray>();
            for (int i = 0; i < _bids.Count; i += 2)
            {
                decimal _price  = _bids[i].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[_pair, MarketSide.Ask].ToList();
            JArray _asks = _json["asks"].Value <JArray>();
            for (int i = 0; i < _asks.Count; i += 2)
            {
                decimal _price  = _asks[i].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[_pair, MarketSide.Ask] = _askList;
            this.Books[_pair, MarketSide.Bid] = _bidList;
        }
Ejemplo n.º 2
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);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        protected override void ReceivedDepth(string _symbol, string _type, JToken _token)
        {
            try
            {
                //this.OnLog($"ReceivedDepth:book.count={this.Books[_symbol, MarketSide.Bid].Count}:{this.Books[_symbol, MarketSide.Ask].Count}");
                //this.OnLog(_token.ToString());
                JObject _json = (JObject)_token;
                this.QueueDepth.Enqueue(_json);
                long _U = _json["U"].Value <long>();
                long _u = _json["u"].Value <long>();
                //this.OnLog($"ReceivedDepth:U={_U},u={_u},e={_json["e"]}");
                if (this.Books[_symbol, MarketSide.Bid].Count < 1 && this.Books[_symbol, MarketSide.Ask].Count < 1)
                {
                    string  _snapshot     = this.WebClient.DownloadString(this.SnapshotUrl);
                    JObject _jsonSnapshot = JObject.Parse(_snapshot);
                    this.LastUpdateId = _jsonSnapshot["lastUpdateId"].Value <long>();
                    //this.OnLog($"ReceivedDepth:lastUpdateId={this.LastUpdateId}");
                    if (this.LastUpdateId + 1 > _u)
                    {
                        #region Bid
                        JArray _bids = _jsonSnapshot["bids"].Value <JArray>();
                        foreach (JArray _item in _bids)
                        {
                            decimal _price  = _item[0].Value <decimal>();
                            decimal _amount = _item[1].Value <decimal>();
                            string  _id     = Math.Abs(_price).ToString();

                            if (_amount > 0M)
                            {
                                BookItem _bookItem = this.Books[_symbol, MarketSide.Bid].Update(_id, _amount);
                                if (_bookItem == null)
                                {
                                    _bookItem = this.Books[_symbol, MarketSide.Bid].Insert(_id, Math.Abs(_price), _amount);
                                    this.OnBookInsert(_bookItem);
                                }
                                else
                                {
                                    this.OnBookUpdate(_bookItem);
                                }
                            }
                            else
                            {
                                BookItem _bookItem = this.Books[_symbol, MarketSide.Bid].Delete(_id);
                                if (_bookItem != null)
                                {
                                    this.OnBookDelete(_bookItem);
                                }
                            }
                        }
                        #endregion
                        #region Ask
                        JArray _asks = _jsonSnapshot["asks"].Value <JArray>();
                        foreach (JArray _item in _asks)
                        {
                            decimal _price  = _item[0].Value <decimal>();
                            decimal _amount = _item[1].Value <decimal>();
                            string  _id     = Math.Abs(_price).ToString();

                            if (_amount > 0M)
                            {
                                BookItem _bookItem = this.Books[_symbol, MarketSide.Ask].Update(_id, _amount);
                                if (_bookItem == null)
                                {
                                    _bookItem = this.Books[_symbol, MarketSide.Ask].Insert(_id, Math.Abs(_price), _amount);
                                    this.OnBookInsert(_bookItem);
                                }
                                else
                                {
                                    this.OnBookUpdate(_bookItem);
                                }
                            }
                            else
                            {
                                BookItem _bookItem = this.Books[_symbol, MarketSide.Ask].Delete(_id);
                                if (_bookItem != null)
                                {
                                    this.OnBookDelete(_bookItem);
                                }
                            }
                        }
                        #endregion
                        return;
                    }
                }

                if (_U <= this.LastUpdateId + 1 && _u >= this.LastUpdateId + 1)
                {
                    this.Books.Timestamp = _token["E"].Value <long>();
                    //this.OnLog($"ReceivedDepth:true");
                    #region Bid
                    JArray _b = _token["b"].Value <JArray>();
                    foreach (JArray _item in _b)
                    {
                        decimal _price  = _item[0].Value <decimal>();
                        decimal _amount = _item[1].Value <decimal>();
                        string  _id     = Math.Abs(_price).ToString();

                        if (_amount > 0M)
                        {
                            BookItem _bookItem = this.Books[_symbol, MarketSide.Bid].Update(_id, _amount);
                            if (_bookItem == null)
                            {
                                _bookItem = this.Books[_symbol, MarketSide.Bid].Insert(_id, Math.Abs(_price), _amount);
                                this.OnBookInsert(_bookItem);
                            }
                            else
                            {
                                this.OnBookUpdate(_bookItem);
                            }
                        }
                        else
                        {
                            BookItem _bookItem = this.Books[_symbol, MarketSide.Bid].Delete(_id);
                            if (_bookItem != null)
                            {
                                this.OnBookDelete(_bookItem);
                            }
                        }
                    }
                    #endregion

                    #region Ask
                    JArray _a = _token["a"].Value <JArray>();
                    foreach (JArray _item in _a)
                    {
                        decimal _price  = _item[0].Value <decimal>();
                        decimal _amount = _item[1].Value <decimal>();
                        string  _id     = Math.Abs(_price).ToString();

                        if (_amount > 0M)
                        {
                            BookItem _bookItem = this.Books[_symbol, MarketSide.Ask].Update(_id, _amount);
                            if (_bookItem == null)
                            {
                                _bookItem = this.Books[_symbol, MarketSide.Ask].Insert(_id, Math.Abs(_price), _amount);
                                this.OnBookInsert(_bookItem);
                            }
                            else
                            {
                                this.OnBookUpdate(_bookItem);
                            }
                        }
                        else
                        {
                            BookItem _bookItem = this.Books[_symbol, MarketSide.Ask].Delete(_id);
                            if (_bookItem != null)
                            {
                                this.OnBookDelete(_bookItem);
                            }
                        }
                    }
                    #endregion
                    this.LastUpdateId = _u;
                }
                else
                {
                    //this.OnLog($"ReceivedDepth:false");
                }

                this.Books[_symbol, MarketSide.Ask].OrderBy(b => b.Value.Price);
                for (int i = this.Books[_symbol, MarketSide.Ask].Count - 1; i >= 0; i--)
                {
                    if (this.Books[_symbol, MarketSide.Ask].Count <= 10)
                    {
                        break;
                    }
                    this.Books[_symbol, MarketSide.Ask].TryRemove(this.Books[_symbol, MarketSide.Ask].ElementAt(i).Key, out BookItem _remove);
                }

                this.Books[_symbol, MarketSide.Bid].OrderByDescending(b => b.Value.Price);
                for (int i = this.Books[_symbol, MarketSide.Bid].Count - 1; i >= 0; i--)
                {
                    if (this.Books[_symbol, MarketSide.Bid].Count <= 10)
                    {
                        break;
                    }
                    this.Books[_symbol, MarketSide.Bid].TryRemove(this.Books[_symbol, MarketSide.Bid].ElementAt(i).Key, out BookItem _remove);
                }
            }
            catch (Exception _ex)
            {
                this.OnLog($"ReceivedDepth---{_ex.ToString()}");
            }
        }
Ejemplo n.º 4
0
        protected override void ReceivedDepth(string _pair, string _type, JToken _token)
        {
            JObject _json = (JObject)_token;

            if (_type == "START")
            {
                #region START
                JArray _askArray = _json["asks"].Value <JArray>();
                JArray _bidArray = _json["bids"].Value <JArray>();

                BookItems _asks = new BookItems(MarketSide.Ask);
                BookItems _bids = new BookItems(MarketSide.Bid);

                foreach (JArray _item in _askArray)
                {
                    decimal _price = _item[0].Value <decimal>();
                    decimal _size  = _item[1].Value <decimal>();
                    string  _id    = _price.ToString();

                    BookItem _bookItem = new BookItem(_pair, MarketSide.Ask, _price, _size, _id);
                    _asks.TryAdd(_id, _bookItem);
                }

                foreach (JArray _item in _bidArray)
                {
                    decimal _price = _item[0].Value <decimal>();
                    decimal _size  = _item[1].Value <decimal>();
                    string  _id    = _price.ToString();

                    BookItem _bookItem = new BookItem(_pair, MarketSide.Bid, _price, _size, _id);
                    _bids.TryAdd(_id, _bookItem);
                }

                this.Books[_pair, MarketSide.Ask] = _asks;
                this.Books[_pair, MarketSide.Bid] = _bids;

                this.OnBookStarted(_pair);
                #endregion
            }
            else if (_type == "UPDATE")
            {
                #region UPDATE
                JArray _array = _json["change"].Value <JArray>();
                foreach (JArray _item in _array)
                {
                    decimal _price = _item[0].Value <decimal>();
                    decimal _size  = _item[1].Value <decimal>();
                    string  _id    = Math.Abs(_price).ToString();

                    if (_price > 0)
                    {
                        if (_size > 0)
                        {
                            BookItem _bookItem = this.Books[_pair, MarketSide.Bid].Update(_id, _size);
                            if (_bookItem == null)
                            {
                                _bookItem = this.Books[_pair, MarketSide.Bid].Insert(_id, Math.Abs(_price), _size);
                                this.OnBookInsert(_bookItem);
                            }
                            else
                            {
                                this.OnBookUpdate(_bookItem);
                            }
                        }
                        else
                        {
                            BookItem _bookItem = this.Books[_pair, MarketSide.Bid].Delete(_id);
                            if (_bookItem != null)
                            {
                                this.OnBookDelete(_bookItem);
                            }
                        }
                    }
                    else if (_price < 0)
                    {
                        if (_size > 0)
                        {
                            BookItem _bookItem = this.Books[_pair, MarketSide.Ask].Update(_id, _size);
                            if (_bookItem == null)
                            {
                                _bookItem = this.Books[_pair, MarketSide.Ask].Insert(_id, Math.Abs(_price), _size);
                                this.OnBookInsert(_bookItem);
                            }
                            else
                            {
                                this.OnBookUpdate(_bookItem);
                            }
                        }
                        else
                        {
                            BookItem _bookItem = this.Books[_pair, MarketSide.Ask].Delete(_id);
                            if (_bookItem != null)
                            {
                                this.OnBookDelete(_bookItem);
                            }
                        }
                    }
                }
                //if (this.Books[_pair, MarketSide.Ask].Count <= 0 || this.Books[_pair, MarketSide.Bid].Count <= 0) { return; }
                //if (this.Books[_pair, MarketSide.Ask].Min(c => c.Value.Price) <= this.Books[_pair, MarketSide.Bid].Max(c => c.Value.Price)) { base.Clear(); return; }
                #endregion
            }
        }
Ejemplo n.º 5
0
        protected override void ReceivedDepth(string _symbol, string _type, JToken _token)
        {
            //this.Log("ReceivedDepth - " + _token.ToString());
            JArray _list = (JArray)_token;

            if (_type == "partial")
            {
                _symbol = _list[0]["symbol"].Value <string>();
                BookItems _asks = new BookItems(MarketSide.Ask);
                BookItems _bids = new BookItems(MarketSide.Bid);

                foreach (JObject _item in _list)
                {
                    MarketSide _side   = _item["side"].Value <string>().ToUpper() == "BUY" ? MarketSide.Bid : MarketSide.Ask;
                    string     _id     = _item["id"].Value <string>();
                    decimal    _price  = _item["price"].Value <decimal>();
                    decimal    _amount = _item["size"].Value <decimal>() / _price;

                    BookItem _bookItem = new BookItem(_symbol, _side, _price, _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;

                if (this.BookSize > 0)
                {
                    this.Books[_symbol, MarketSide.Ask].Resize(this.BookSize);
                    this.Books[_symbol, MarketSide.Bid].Resize(this.BookSize);
                }

                this.depths.Add(_symbol);
                this.OnBookStarted(_symbol);
            }
            else if (_type == "insert")
            {
                foreach (JObject _item in _list)
                {
                    _symbol = _item["symbol"].Value <string>().ToUpper();
                    if (!this.depths.Contains(_symbol))
                    {
                        continue;
                    }

                    MarketSide _side   = _item["side"].Value <string>().ToUpper() == "BUY" ? MarketSide.Bid : MarketSide.Ask;
                    string     _id     = _item["id"].Value <string>();
                    decimal    _price  = _item["price"].Value <decimal>();
                    decimal    _amount = _item["size"].Value <decimal>() / _price;

                    BookItem _bookItem = this.Books[_symbol, _side].Insert(_id, _price, _amount);
                    this.OnBookInsert(_bookItem);

                    if (this.BookSize > 0 && this.Books[_symbol, _side].Count > this.BookSize * 2)
                    {
                        this.Books[_symbol, _side].Resize(this.BookSize);
                    }
                }
            }
            else if (_type == "update")
            {
                foreach (JObject _item in _list)
                {
                    _symbol = _item["symbol"].Value <string>().ToUpper();
                    if (!this.depths.Contains(_symbol))
                    {
                        continue;
                    }

                    MarketSide _side   = _item["side"].Value <string>().ToUpper() == "BUY" ? MarketSide.Bid : MarketSide.Ask;
                    string     _id     = _item["id"].Value <string>();
                    decimal    _amount = _item["size"].Value <decimal>();

                    BookItem _bookItem = this.Books[_symbol, _side][_id];
                    if (_bookItem == null && this.BookSize > 0)
                    {
                        return;
                    }
                    else if (_bookItem == null)
                    {
                        this.Log("Book update failed 1 - " + _item.ToString(Newtonsoft.Json.Formatting.None));
                        return;
                    }

                    _amount   = _amount / _bookItem.Price;
                    _bookItem = this.Books[_symbol, _side].Update(_id, _amount);
                    if (_bookItem != null)
                    {
                        this.OnBookUpdate(_bookItem);
                    }
                    else if (this.BookSize == 0)
                    {
                        this.Log("Book update failed 2 - " + _item.ToString(Newtonsoft.Json.Formatting.None));
                    }
                }
            }
            else if (_type == "delete")
            {
                foreach (JObject _item in _list)
                {
                    _symbol = _item["symbol"].Value <string>().ToUpper();
                    if (!this.depths.Contains(_symbol))
                    {
                        continue;
                    }

                    MarketSide _side = _item["side"].Value <string>().ToUpper() == "BUY" ? MarketSide.Bid : MarketSide.Ask;
                    string     _id   = _item["id"].Value <string>();

                    BookItem _bookItem = this.Books[_symbol, _side].Delete(_id);
                    if (_bookItem != null)
                    {
                        this.OnBookDelete(_bookItem);
                    }
                    else if (this.BookSize == 0)
                    {
                        this.Log("Book delete failed - " + _item.ToString(Newtonsoft.Json.Formatting.None));
                    }
                }
            }
        }