예제 #1
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
            }
        }
예제 #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);
                    }
                }
            }
        }
예제 #3
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));
                    }
                }
            }
        }