Exemple #1
0
        private void BooksRunner(object _object)
        {
            string _pair  = _object.ToString();
            string _pairA = _pair.Contains("_") ? _pair.Split('_')[0] : _pair;
            string _url   = $"/public/orderbook/{_pairA}";

            if (this.BooksLimit != "")
            {
                _url += $"?count={this.BooksLimit}";
            }

            this.Books[_pair, MarketSide.Ask] = new BookItems(MarketSide.Ask);
            this.Books[_pair, MarketSide.Bid] = new BookItems(MarketSide.Bid);

            string _result = "";

            while (this.Running)
            {
                Thread.Sleep(1000);

                try
                {
                    WebClientPlus _client = new WebClientPlus(5000);
                    _result = _client.DownloadString($"{base.HttpUrl}{_url}");
                    _client.Dispose();

                    JObject _json = JObject.Parse(_result);

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

                    this.Books.Timestamp = _json["data"]["timestamp"].Value <long>();

                    foreach (var _item in _json["data"]["asks"])
                    {
                        decimal _price  = _item["price"].Value <decimal>();
                        decimal _amount = _item["quantity"].Value <decimal>();
                        _asks.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }
                    foreach (var _item in _json["data"]["bids"])
                    {
                        decimal _price  = _item["price"].Value <decimal>();
                        decimal _amount = _item["quantity"].Value <decimal>();
                        _bids.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }

                    this.Books[_pair, MarketSide.Ask] = _asks;
                    this.Books[_pair, MarketSide.Bid] = _bids;
                }
                catch (Exception _ex)
                {
                    this.OnLog($"GetBooks Error:{_result}");
                    this.OnLog($"GetBooks Error:{_ex.ToString()}");
                }
            }
        }
Exemple #2
0
        private void BooksRunner(object _object)
        {
            string _pair = _object.ToString();
            string _url  = $"/api/v1/depth?symbol={_pair}&limit=10";

            this.Books[_pair, MarketSide.Ask] = new BookItems(MarketSide.Ask);
            this.Books[_pair, MarketSide.Bid] = new BookItems(MarketSide.Bid);

            string _result = "";

            while (this.Running)
            {
                Thread.Sleep(1000);

                try
                {
                    WebClientPlus _client = new WebClientPlus(5000);
                    _result = _client.DownloadString($"{base.HttpUrl}{_url}");
                    _client.Dispose();

                    JObject _json = JObject.Parse(_result);

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

                    this.Books.Timestamp = DateTimePlus.DateTime2JSTime(DateTime.UtcNow);

                    foreach (var _item in _json["asks"])
                    {
                        decimal _price  = _item[0].Value <decimal>();
                        decimal _amount = _item[1].Value <decimal>();
                        _asks.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }
                    foreach (var _item in _json["bids"])
                    {
                        decimal _price  = _item[0].Value <decimal>();
                        decimal _amount = _item[1].Value <decimal>();
                        _bids.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }

                    this.Books[_pair, MarketSide.Ask] = _asks;
                    this.Books[_pair, MarketSide.Bid] = _bids;
                }
                catch (Exception _ex)
                {
                    this.OnLog($"GetBooks Error:{_result}");
                    this.OnLog($"GetBooks Error:{_ex.ToString()}");
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// GetDepths
        /// </summary>
        /// <param name="_pair"></param>
        /// <param name="_values">0:limit 1:group</param>
        /// <returns></returns>
        public override Books GetDepths(string _pair, params string[] _values)
        {
            string _url = $"/v1/book/{_pair}";

            if (_values.Length > 0)
            {
                _url += $"?limit_bids={_values[0]}&limit_asks={_values[1]}";
            }
            if (_values.Length > 1)
            {
                _url += $"&group={_values[1]}";
            }

            JToken _token = base.HttpCall(HttpCallMethod.Get, "GET", _url);

            if (_token == null)
            {
                return(null);
            }

            BookItems _bidList = new BookItems(MarketSide.Bid);
            JArray    _bids    = _token["bids"].Value <JArray>();

            for (int i = 0; i < _bids.Count; i++)
            {
                decimal _price  = _bids[i]["price"].Value <decimal>();
                decimal _amount = _bids[i]["amount"].Value <decimal>();
                _bidList.Insert(_price.ToString(), _price, _amount);
            }
            BookItems _askList = new BookItems(MarketSide.Ask);
            JArray    _asks    = _token["asks"].Value <JArray>();

            for (int i = 0; i < _asks.Count; i++)
            {
                decimal _price  = _asks[i]["price"].Value <decimal>();
                decimal _amount = _asks[i]["amount"].Value <decimal>();
                _askList.Insert(_price.ToString(), _price, _amount);
            }

            Books _books = new Books();

            _books[_pair, MarketSide.Bid] = _bidList;
            _books[_pair, MarketSide.Ask] = _askList;

            return(_books);
        }
Exemple #4
0
        public override Books GetDepths(string _pair, params string[] _values)
        {
            string _url = $"/GET/v1/api/depth?pair={_pair}&depth={_values[0]}&accuracy={_values[1]}";

            JToken _token = base.HttpCall(HttpCallMethod.Get, "GET", _url);

            if (_token == null)
            {
                return(null);
            }

            BookItems _bidList = new BookItems(MarketSide.Bid);
            JArray    _bids    = _token["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>();
                _bidList.Insert(_price.ToString(), _price, _amount);
            }

            BookItems _askList = new BookItems(MarketSide.Ask);
            JArray    _asks    = _token["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>();
                _askList.Insert(_price.ToString(), _price, _amount);
            }

            Books _books = new Books();

            _books[_pair, MarketSide.Bid] = _bidList;
            _books[_pair, MarketSide.Ask] = _askList;

            return(_books);
        }
Exemple #5
0
        /// <summary>
        /// GetDepths
        /// </summary>
        /// <param name="_pair"></param>
        /// <param name="_values">0:limit 1:group</param>
        /// <returns></returns>
        public override Books GetDepths(string _pair, params string[] _values)
        {
            string _url = $"/api/v1/orderBook/L2?symbol={_pair}&depth={_values[0]}";

            JToken _token = base.HttpCall(HttpCallMethod.Get, "GET", _url);

            if (_token == null)
            {
                return(null);
            }

            BookItems _bidList = new BookItems(MarketSide.Bid);
            BookItems _askList = new BookItems(MarketSide.Ask);

            foreach (JToken _item in _token)
            {
                string  _id     = _item["id"].Value <string>();
                decimal _price  = _item["price"].Value <decimal>();
                decimal _amount = _item["amount"].Value <decimal>();
                string  _side   = _item["side"].Value <string>().ToLower();
                if (_side == "Sell")
                {
                    _askList.Insert(_price.ToString(), _price, _amount);
                }
                else
                {
                    _bidList.Insert(_price.ToString(), _price, _amount);
                }
            }
            Books _books = new Books();

            _books[_pair, MarketSide.Bid] = _bidList;
            _books[_pair, MarketSide.Ask] = _askList;

            return(_books);
        }
Exemple #6
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;
        }
Exemple #7
0
        private void BooksRunner(object _object)
        {
            string _pair = _object.ToString();
            string _url  = $"/{_pair}/depth";

            this.Books[_pair, MarketSide.Ask] = new BookItems(MarketSide.Ask);
            this.Books[_pair, MarketSide.Bid] = new BookItems(MarketSide.Bid);

            string _result = "";

            while (this.Running)
            {
                Thread.Sleep(1000);

                try
                {
                    WebClientPlus _client = new WebClientPlus(5000);
                    _result = _client.DownloadString($"{this.UrlPublic}{_url}");
                    _client.Dispose();

                    JObject _json = JObject.Parse(_result);

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

                    this.Books.Timestamp = _json["data"]["timestamp"].Value <long>();

                    foreach (var _item in _json["data"]["asks"])
                    {
                        decimal _price  = _item[0].Value <decimal>();
                        decimal _amount = _item[1].Value <decimal>();
                        _asks.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }
                    foreach (var _item in _json["data"]["bids"])
                    {
                        decimal _price  = _item[0].Value <decimal>();
                        decimal _amount = _item[1].Value <decimal>();
                        _bids.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }

                    BookItems _asksLimit = new BookItems(MarketSide.Ask);
                    BookItems _bidsLimit = new BookItems(MarketSide.Bid);
                    foreach (var _item in _asks.OrderBy(b => b.Value.Price).Take(10))
                    {
                        decimal _price  = _item.Value.Price;
                        decimal _amount = _item.Value.Amount;
                        _asksLimit.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }
                    foreach (var _item in _bids.OrderByDescending(b => b.Value.Price).Take(10))
                    {
                        decimal _price  = _item.Value.Price;
                        decimal _amount = _item.Value.Amount;
                        _bidsLimit.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }

                    this.Books[_pair, MarketSide.Ask] = _asksLimit;
                    this.Books[_pair, MarketSide.Bid] = _bidsLimit;
                }
                catch (Exception _ex)
                {
                    this.OnLog($"GetBooks Error:{_result}");
                    this.OnLog($"GetBooks Error:{_ex.ToString()}");
                }
            }
        }
Exemple #8
0
        private void BooksRunner(object _object)
        {
            string _pair = _object.ToString();
            string _url  = $"/api/spot/v3/instruments/{_pair}/book?size=10";

            this.Books[_pair, MarketSide.Ask] = new BookItems(MarketSide.Ask);
            this.Books[_pair, MarketSide.Bid] = new BookItems(MarketSide.Bid);

            string _result = "";

            while (this.Running)
            {
                Thread.Sleep(1000);

                try
                {
                    WebClientPlus _client = new WebClientPlus(10000);
                    _result = _client.DownloadString($"{this.HttpUrl}{_url}");
                    _client.Dispose();

                    JObject _json = JObject.Parse(_result);

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

                    this.Books.Timestamp = DateTimePlus.DateTime2JSTime(DateTime.UtcNow);

                    foreach (var _item in _json["asks"])
                    {
                        decimal _price  = decimal.Parse(_item[0].Value <string>(), System.Globalization.NumberStyles.Float);
                        decimal _amount = decimal.Parse(_item[1].Value <string>(), System.Globalization.NumberStyles.Float);
                        _asks.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }
                    foreach (var _item in _json["bids"])
                    {
                        decimal _price  = decimal.Parse(_item[0].Value <string>(), System.Globalization.NumberStyles.Float);
                        decimal _amount = decimal.Parse(_item[1].Value <string>(), System.Globalization.NumberStyles.Float);
                        _bids.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }

                    BookItems _asksLimit = new BookItems(MarketSide.Ask);
                    BookItems _bidsLimit = new BookItems(MarketSide.Bid);
                    foreach (var _item in _asks.OrderBy(b => b.Value.Price).Take(10))
                    {
                        decimal _price  = _item.Value.Price;
                        decimal _amount = _item.Value.Amount;
                        _asksLimit.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }
                    foreach (var _item in _bids.OrderByDescending(b => b.Value.Price).Take(10))
                    {
                        decimal _price  = _item.Value.Price;
                        decimal _amount = _item.Value.Amount;
                        _bidsLimit.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }

                    this.Books[_pair, MarketSide.Ask] = _asksLimit;
                    this.Books[_pair, MarketSide.Bid] = _bidsLimit;
                }
                catch (Exception _ex)
                {
                    this.OnLog($"GetBooks Error:{_result}");
                    this.OnLog($"GetBooks Error:{_ex.ToString()}");
                }
            }
        }
Exemple #9
0
        private void BooksRunner(object _object)
        {
            string _pair = _object.ToString();
            string _url  = $"/v1.1/public/getorderbook?market={_pair}&type=both";

            this.Books[_pair, MarketSide.Ask] = new BookItems(MarketSide.Ask);
            this.Books[_pair, MarketSide.Bid] = new BookItems(MarketSide.Bid);

            string _result = "";

            while (this.Running)
            {
                Thread.Sleep(1000);

                try
                {
                    WebClientPlus _client = new WebClientPlus(5000);
                    _result = _client.DownloadString($"{this.HttpUrl}{_url}");
                    _client.Dispose();

                    JObject _json = JObject.Parse(_result);

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

                    this.Books.Timestamp = DateTimePlus.DateTime2JSTime(DateTime.UtcNow);

                    foreach (var _item in _json["result"]["sell"])
                    {
                        decimal _price  = _item["Rate"].Value <decimal>();
                        decimal _amount = _item["Quantity"].Value <decimal>();
                        _asks.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }
                    foreach (var _item in _json["result"]["buy"])
                    {
                        decimal _price  = _item["Rate"].Value <decimal>();
                        decimal _amount = _item["Quantity"].Value <decimal>();
                        _bids.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }

                    BookItems _asksLimit = new BookItems(MarketSide.Ask);
                    BookItems _bidsLimit = new BookItems(MarketSide.Bid);
                    foreach (var _item in _asks.OrderBy(b => b.Value.Price).Take(10))
                    {
                        decimal _price  = _item.Value.Price;
                        decimal _amount = _item.Value.Amount;
                        _asksLimit.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }
                    foreach (var _item in _bids.OrderByDescending(b => b.Value.Price).Take(10))
                    {
                        decimal _price  = _item.Value.Price;
                        decimal _amount = _item.Value.Amount;
                        _bidsLimit.Insert(_price.ToString(), Math.Abs(_price), _amount);
                    }

                    this.Books[_pair, MarketSide.Ask] = _asksLimit;
                    this.Books[_pair, MarketSide.Bid] = _bidsLimit;
                }
                catch (Exception _ex)
                {
                    this.OnLog($"GetBooks Error:{_result}");
                    this.OnLog($"GetBooks Error:{_ex.ToString()}");
                }
            }
        }
Exemple #10
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);
                    }
                }
            }
        }