예제 #1
0
파일: FCoin.cs 프로젝트: lionzhou1981/Lion
        protected override object[] HttpCallAuth(HttpClient _http, string _method, ref string _url, object[] _keyValues)
        {
            Dictionary <string, string> _list = new Dictionary <string, string>();

            for (int i = 0; i < _keyValues.Length - 1; i += 2)
            {
                _list.Add(_keyValues[i].ToString(), _keyValues[i + 1].ToString());
            }
            KeyValuePair <string, string>[] _sorted = _list.ToArray().OrderBy(c => c.Key).ToArray();

            string _ts   = (DateTimePlus.DateTime2JSTime(DateTime.UtcNow) * 1000).ToString();
            string _sign = _method + this.HttpUrl + _url + _ts;

            for (int i = 0; i < _sorted.Length; i++)
            {
                _sign += i == 0 ? "" : "&";
                _sign += _sorted[i].Key + "=" + _sorted[i].Value;
            }
            _sign = Base64.Encode(Encoding.UTF8.GetBytes(_sign));
            _sign = SHA.EncodeHMACSHA1ToBase64(_sign, base.Secret);

            _http.Headers.Add("FC-ACCESS-KEY", base.Key);
            _http.Headers.Add("FC-ACCESS-SIGNATURE", _sign);
            _http.Headers.Add("FC-ACCESS-TIMESTAMP", _ts);

            return(_keyValues);
        }
예제 #2
0
        protected override object[] HttpCallAuth(HttpClient _http, string _method, ref string _url, object[] _keyValues)
        {
            IList <object> _keyValueList = _keyValues.ToList();

            _keyValueList.Add("endpoint");
            _keyValueList.Add(_url);

            string _query = "";

            for (int i = 0; i < _keyValueList.Count - 1; i += 2)
            {
                _query += _query == "" ? "" : "&";
                _query += _keyValueList[i] + "=" + Uri.EscapeDataString(_keyValueList[i + 1].ToString());
            }
            string _timestamp = DateTimePlus.DateTime2JSTime(DateTime.UtcNow).ToString() + DateTime.UtcNow.Millisecond.ToString("D03");
            string _sign      = _url + (char)0 + _query + (char)0 + _timestamp;

            byte[] _rgbyKey = Encoding.UTF8.GetBytes(this.Secret);
            using (var _hmacsha512 = new HMACSHA512(_rgbyKey))
            {
                _hmacsha512.ComputeHash(Encoding.UTF8.GetBytes(_sign));
                _sign = ByteToString(_hmacsha512.Hash);
            }

            _sign = Convert.ToBase64String(StringToByte(_sign));
            _http.Headers.Add("Api-Key", base.Key);
            _http.Headers.Add("Api-Sign", _sign);
            _http.Headers.Add("Api-Nonce", _timestamp);
            _http.ContentType = "application/x-www-form-urlencoded";

            return(_keyValueList.ToArray());
        }
예제 #3
0
        public override OrderItem OrderCreate(string _pair, MarketSide _side, OrderType _type, decimal _amount, decimal _price = 0)
        {
            string _urlSide = _side == MarketSide.Ask ? "/selllimit" : "/buylimit";
            string _url     = "/v1.1/market" + _urlSide;

            IList <object> _values = new List <object>();

            _values.Add("apikey");
            _values.Add(this.Key);
            _values.Add("nonce");
            _values.Add(DateTimePlus.DateTime2JSTime(DateTime.UtcNow));
            _values.Add("market");
            _values.Add(_pair.ToUpper());
            _values.Add("quantity");
            _values.Add(_amount.ToString());
            _values.Add("rate");
            _values.Add(_price.ToString());

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

            if (_token == null || _token.ToString(Newtonsoft.Json.Formatting.None).Trim() == "{}")
            {
                return(null);
            }

            OrderItem _item = new OrderItem();

            _item.Id = _token["uuid"].Value <string>();
            return(_item);
        }
예제 #4
0
파일: Upbit.cs 프로젝트: lionzhou1981/Lion
        protected override object[] HttpCallAuth(HttpClient _http, string _method, ref string _url, object[] _keyValues)
        {
            string  _query = "";
            JObject _json  = new JObject();

            for (int i = 0; i < _keyValues.Length - 1; i += 2)
            {
                _query += _query == "" ? "" : "&";
                _query += _keyValues[i] + "=" + System.Web.HttpUtility.UrlEncode(_keyValues[i + 1].ToString());

                Type _valueType = _keyValues[i + 1].GetType();
                if (_valueType == typeof(int))
                {
                    _json[_keyValues[i]] = (int)_keyValues[i + 1];
                }
                else if (_valueType == typeof(bool))
                {
                    _json[_keyValues[i]] = (bool)_keyValues[i + 1];
                }
                else if (_valueType == typeof(decimal))
                {
                    _json[_keyValues[i]] = (decimal)_keyValues[i + 1];
                }
                else if (_valueType == typeof(long))
                {
                    _json[_keyValues[i]] = (long)_keyValues[i + 1];
                }
                else if (_valueType == typeof(JArray))
                {
                    _json[_keyValues[i]] = (JArray)_keyValues[i + 1];
                }
                else
                {
                    _json[_keyValues[i]] = _keyValues[i + 1].ToString();
                }
            }

            JwtPayload _payload = new JwtPayload {
                { "access_key", base.Key }, { "nonce", DateTimePlus.DateTime2JSTime(DateTime.UtcNow).ToString() + DateTime.UtcNow.Millisecond.ToString() }
            };

            if (_method.ToUpper() == "POST")
            {
                _payload.Add("query", _query);
            }
            byte[] _keyBytes           = Encoding.Default.GetBytes(base.Secret);
            var    _securityKey        = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(_keyBytes);
            var    _credentials        = new Microsoft.IdentityModel.Tokens.SigningCredentials(_securityKey, "HS256");
            var    _header             = new JwtHeader(_credentials);
            var    _secToken           = new JwtSecurityToken(_header, _payload);
            var    _jwtToken           = new JwtSecurityTokenHandler().WriteToken(_secToken);
            var    _authorizationToken = _jwtToken;

            _http.Headers.Add("Authorization", $"Bearer {_authorizationToken}");

            return(_keyValues);
        }
예제 #5
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()}");
                }
            }
        }
예제 #6
0
        public override OrderItem OrderCreate(string _pair, MarketSide _side, OrderType _type, decimal _amount, decimal _price = 0M)
        {
            string _url = "/api/v3/order";

            IList <object> _values = new List <object>();

            _values.Add("symbol");
            _values.Add(_pair.ToUpper());
            _values.Add("side");
            _values.Add(_side == MarketSide.Bid ? "BUY" : "SELL");
            _values.Add("type");
            switch (_type)
            {
            case OrderType.Limit:
                _values.Add("LIMIT");
                _values.Add("price");
                _values.Add(_price.ToString());
                _values.Add("timeInForce");
                _values.Add("GTC");
                break;

            case OrderType.Market:
                _values.Add("MARKET");
                break;
            }
            _values.Add("quantity");
            _values.Add(_amount);
            _values.Add("timestamp");
            long _timestamp = DateTimePlus.DateTime2JSTime(DateTime.UtcNow.AddSeconds(-1));

            _values.Add(_timestamp.ToString() + DateTime.UtcNow.Millisecond.ToString());

            JToken _token = base.HttpCall(HttpCallMethod.Form, "POST", _url, true, _values.ToArray());

            if (_token == null || _token.ToString().Trim() == "{}")
            {
                return(null);
            }
            this.OnLog(_token.ToString(Newtonsoft.Json.Formatting.None));

            OrderItem _item = new OrderItem();

            _item.Id         = _token["orderId"].Value <string>();
            _item.Pair       = _token["symbol"].Value <string>();
            _item.Side       = _token["side"].Value <string>() == "SELL" ? MarketSide.Ask : MarketSide.Bid;
            _item.Price      = _token["price"].Value <decimal>();
            _item.Amount     = _token["origQty"].Value <decimal>();
            _item.CreateTime = DateTimePlus.JSTime2DateTime(long.Parse(_token["transactTime"].Value <string>().Remove(10)));
            return(_item);
        }
예제 #7
0
        public override OrderItem OrderDetail(string _orderId, params string[] _values)
        {
            string _url = "/v1.1/account/getorder";

            IList <object> _list = new List <object>();

            _list.Add("apikey");
            _list.Add(this.Key);
            _list.Add("nonce");
            _list.Add(DateTimePlus.DateTime2JSTime(DateTime.UtcNow));
            _list.Add("uuid");
            _list.Add(_orderId);

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

            if (_token == null || _token.ToString(Newtonsoft.Json.Formatting.None).Trim() == "{}")
            {
                return(null);
            }

            OrderItem _order = new OrderItem();

            _order.Id     = _token["OrderUuid"].Value <string>();
            _order.Pair   = _token["Exchange"].Value <string>();
            _order.Side   = _token["Type"].Value <string>().ToUpper().Split('_')[1] == "BUY" ? MarketSide.Bid : MarketSide.Ask;
            _order.Amount = _token["Quantity"].Value <decimal>();
            _order.Price  = _token["Limit"].Value <decimal>();

            decimal _quantityRemaining = _token["QuantityRemaining"].Value <decimal>();

            if (_quantityRemaining >= _order.Amount)
            {
                _order.Status = OrderStatus.New;
            }
            else if (_quantityRemaining <= 0M)
            {
                _order.Status = OrderStatus.Filled;
            }
            else
            {
                _order.Status = OrderStatus.Filling;
            }
            _order.FilledAmount = _order.Amount - _quantityRemaining;
            _order.FilledPrice  = _token["PricePerUnit"].Value <decimal>();
            _order.FilledVolume = _order.FilledAmount * _order.FilledPrice;

            return(_order);
        }
예제 #8
0
        public override OrderItem OrderDetail(string _id, params string[] _values)
        {
            string _url = "/api/v3/order";

            IList <object> _list = new List <object>();

            _list.Add("symbol");
            string _pair = _values[0].Replace("_", "").ToUpper();

            _list.Add(_pair);
            _list.Add("orderId");
            _list.Add(_id);
            _list.Add("timestamp");
            long _timestamp = DateTimePlus.DateTime2JSTime(DateTime.UtcNow.AddSeconds(-1));

            _list.Add(_timestamp.ToString() + DateTime.UtcNow.Millisecond.ToString());

            JToken _token = this.HttpCall(HttpCallMethod.Get, "GET", _url, true, "symbol", _pair, "orderId", _id, "timestamp", _timestamp.ToString() + DateTime.UtcNow.Millisecond.ToString());

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

            OrderItem _order = new OrderItem();

            _order.Id     = _token["orderId"].Value <string>();
            _order.Pair   = _token["symbol"].Value <string>();
            _order.Side   = _token["side"].Value <string>().ToUpper() == "BUY" ? MarketSide.Bid : MarketSide.Ask;
            _order.Amount = _token["origQty"].Value <decimal>();
            _order.Price  = _token["price"].Value <decimal>();
            string _status = _token["status"].Value <string>();

            switch (_status)
            {
            case "NEW": _order.Status = OrderStatus.New; break;

            case "PARTIALLY_FILLED": _order.Status = OrderStatus.Filling; break;

            case "FILLED": _order.Status = OrderStatus.Filled; break;

            case "CANCELED": _order.Status = OrderStatus.Canceled; break;
            }
            _order.FilledAmount = _token["executedQty"].Value <decimal>();
            _order.FilledVolume = _token["cummulativeQuoteQty"].Value <decimal>();

            return(_order);
        }
예제 #9
0
파일: Dktp.cs 프로젝트: lionzhou1981/Lion
        public void SendFirstPackage(SocketEngine _socket)
        {
            Random _random = new Random(RandomPlus.RandomSeed);

            this.socketRxKey    = _random.Next(10000000, 100000000).ToString();
            this.socketChecksum = _random.Next(10000000, 100000000).ToString();

            JObject _json = new JObject();

            _json["code"] = this.Code;
            _json["time"] = DateTimePlus.DateTime2JSTime(DateTime.UtcNow).ToString();
            _json["num"]  = this.socketChecksum;
            _json["key"]  = this.socketRxKey;

            _socket.SendPackage(_json);
        }
예제 #10
0
        public override Balances GetBalances(params object[] _symbols)
        {
            try
            {
                string _url = "/v1.1/account/getbalances";

                IList <object> _values = new List <object>();
                _values.Add("apikey");
                _values.Add(this.Key);
                _values.Add("nonce");
                _values.Add(DateTimePlus.DateTime2JSTime(DateTime.UtcNow));

                JToken _token = base.HttpCall(HttpCallMethod.Get, "GET", _url, true, _values.ToArray());
                if (_token == null || _token.ToString(Newtonsoft.Json.Formatting.None).Trim() == "{}")
                {
                    return(null);
                }

                Balances _balances = new Balances();
                foreach (JToken _item in _token)
                {
                    _balances[_item["Currency"].ToString()] = new BalanceItem()
                    {
                        Symbol = _item["Currency"].ToString(),
                        Free   = _item["Available"].ToString() == "" ? 0 : _item["Available"].Value <decimal>(),
                        Lock   = _item["Pending"].ToString() == "" ? 0 : _item["Pending"].Value <decimal>()
                    };
                }
                foreach (string _symbol in _symbols)
                {
                    if (!_balances.ContainsKey(_symbol.ToUpper()))
                    {
                        _balances[_symbol.ToUpper()] = new BalanceItem()
                        {
                            Symbol = _symbol.ToUpper(),
                            Free   = 0M,
                            Lock   = 0M
                        };
                    }
                }
                return(_balances);
            }
            catch (Exception)
            {
                return(null);
            }
        }
예제 #11
0
 public static string GetCurrentHeight()
 {
     try
     {
         //string _url = "https://xmr.tokenview.com/api/blocks/xmr/1/1";
         string        _url       = "https://monerohash.com/api/stats?_=" + DateTimePlus.DateTime2JSTime(DateTime.UtcNow).ToString() + DateTime.UtcNow.Millisecond.ToString();
         WebClientPlus _webClient = new WebClientPlus(10000);
         string        _result    = _webClient.DownloadString(_url);
         _webClient.Dispose();
         JObject _json = JObject.Parse(_result);
         //return _json["data"][0]["block_no"].Value<string>();
         return(_json["network"]["height"].Value <string>());
     }
     catch (Exception)
     {
         return("");
     }
 }
예제 #12
0
파일: FCoin.cs 프로젝트: lionzhou1981/Lion
        /// <summary>
        ///
        /// </summary>
        /// <param name="_pair"></param>
        /// <param name="_values">0:type(L20,L100,full)</param>
        public override void SubscribeDepth(string _pair, params object[] _values)
        {
            JObject _json = new JObject();

            _json.Add("type", "hello");
            _json.Add("ts", DateTimePlus.DateTime2JSTime(DateTime.UtcNow));
            _json.Add("cmd", "sub");
            _json.Add("args", new JArray($"depth.{_values[0]}.{_pair}"));

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

            this.Send(_json);
        }
예제 #13
0
파일: Art.cs 프로젝트: lionzhou1981/Lion
        protected override object[] HttpCallAuth(HttpClient _http, string _method, ref string _url, object[] _keyValues)
        {
            Dictionary <string, string> _list = new Dictionary <string, string>();

            if (_method.ToUpper() == "POST")
            {
                for (int i = 0; i < _keyValues.Length; i += 2)
                {
                    _list.Add(_keyValues[i].ToString(), _keyValues[i + 1].ToString());
                }
            }
            string _time = DateTimePlus.DateTime2JSTime(DateTime.UtcNow.AddSeconds(-1)).ToString();

            _list.Add("api_key", base.Key);
            _list.Add("auth_nonce", _time);
            KeyValuePair <string, string>[] _sorted = _list.OrderBy(c => c.Key).ToArray();

            string _sign = "";

            foreach (KeyValuePair <string, string> _item in _sorted)
            {
                _sign += _item.Value;
            }
            _list.Add("auth_sign", MD5.Encode(_sign + base.Secret).ToLower());

            IList <string> _keyValueList = new List <string>();

            if (_method.ToUpper() == "GET")
            {
                foreach (var _item in _keyValues)
                {
                    _keyValueList.Add(_item.ToString());
                }
            }
            foreach (KeyValuePair <string, string> _item in _list)
            {
                _keyValueList.Add(_item.Key);
                _keyValueList.Add(_item.Value);
            }
            return(_keyValueList.ToArray());
        }
예제 #14
0
        protected override object[] HttpCallAuth(HttpClient _http, string _method, ref string _url, object[] _keyValues)
        {
            string _query = "";

            for (int i = 0; i < _keyValues.Length; i += 2)
            {
                _query += _query == "" ? "?" : "&";
                _query += $"{_keyValues[i].ToString()}={_keyValues[i + 1].ToString()}";
            }

            _http.Headers.Add("ACCESS-KEY", base.Key);
            long   _nonce = DateTimePlus.DateTime2JSTime(DateTime.UtcNow);
            string _time  = _nonce.ToString() + DateTime.UtcNow.Millisecond.ToString();

            _http.Headers.Add("ACCESS-NONCE", _time);
            string _signData = _time + _url + _query;
            string _sign     = SHA.EncodeHMACSHA256ToHex(_signData, base.Secret);

            _http.Headers.Add("ACCESS-SIGNATURE", _sign);

            return(_keyValues);
        }
예제 #15
0
        protected override object[] HttpCallAuth(HttpClient _http, string _method, ref string _url, object[] _keyValues)
        {
            IList <object> _result = new List <object>();

            SortedDictionary <string, string> _list = new SortedDictionary <string, string>();

            for (int i = 0; i < _keyValues.Length; i += 2)
            {
                _list.Add(_keyValues[i].ToString(), _keyValues[i + 1].ToString());

                _result.Add(_keyValues[i]);
                _result.Add(_keyValues[i + 1]);
            }

            string _sign = "";
            long   _time = DateTimePlus.DateTime2JSTime(DateTime.UtcNow.AddSeconds(-1)) * 1000;

            _list.Add("access_id", base.Key);
            _list.Add("tonce", _time.ToString());

            _result.Add("access_id");
            _result.Add(base.Key);
            _result.Add("tonce");
            _result.Add(_time.ToString());

            foreach (KeyValuePair <string, string> _item in _list)
            {
                _sign += _sign == "" ? "" : "&";
                _sign += $"{_item.Key}={_item.Value}";
            }
            _sign += $"&secret_key={base.Secret}";
            _sign  = MD5.Encode(_sign).ToUpper();

            _http.Headers.Add("authorization", _sign);

            return(_result.ToArray());
        }
예제 #16
0
        public override Balances GetBalances(params object[] _symbols)
        {
            string _url       = "/api/v3/account";
            string _timestamp = DateTimePlus.DateTime2JSTime(DateTime.UtcNow.AddSeconds(-1)).ToString() + DateTime.UtcNow.Millisecond.ToString();
            JToken _token     = base.HttpCall(HttpCallMethod.Get, "GET", _url, true, "timestamp", _timestamp);

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

            Balances _balances = new Balances();

            foreach (JToken _item in _token["balances"])
            {
                _balances[_item["asset"].ToString()] = new BalanceItem()
                {
                    Symbol = _item["asset"].ToString(),
                    Free   = _item["free"].Value <decimal>(),
                    Lock   = _item["locked"].Value <decimal>()
                };
            }
            foreach (string _symbol in _symbols)
            {
                if (!_balances.ContainsKey(_symbol.ToUpper()))
                {
                    _balances[_symbol.ToUpper()] = new BalanceItem()
                    {
                        Symbol = _symbol.ToUpper(),
                        Free   = 0M,
                        Lock   = 0M
                    };
                }
            }
            return(_balances);
        }
예제 #17
0
        protected override object[] HttpCallAuth(HttpClient _http, string _method, ref string _url, object[] _keyValues)
        {
            string  _query = "";
            JObject _json  = new JObject();

            for (int i = 0; i < _keyValues.Length - 1; i += 2)
            {
                _query += _query == "" ? "" : "&";
                _query += _keyValues[i] + "=" + System.Web.HttpUtility.UrlEncode(_keyValues[i + 1].ToString());

                Type _valueType = _keyValues[i + 1].GetType();
                if (_valueType == typeof(int))
                {
                    _json[_keyValues[i]] = (int)_keyValues[i + 1];
                }
                else if (_valueType == typeof(bool))
                {
                    _json[_keyValues[i]] = (bool)_keyValues[i + 1];
                }
                else if (_valueType == typeof(decimal))
                {
                    _json[_keyValues[i]] = (decimal)_keyValues[i + 1];
                }
                else if (_valueType == typeof(long))
                {
                    _json[_keyValues[i]] = (long)_keyValues[i + 1];
                }
                else if (_valueType == typeof(JArray))
                {
                    _json[_keyValues[i]] = (JArray)_keyValues[i + 1];
                }
                else
                {
                    _json[_keyValues[i]] = _keyValues[i + 1].ToString();
                }
            }

            string _nonce = DateTimePlus.DateTime2JSTime(DateTime.UtcNow.AddHours(1)).ToString();
            string _sign  = _method;

            if (_method == "GET")
            {
                _url  += _query == "" ? "" : "?";
                _url  += _query;
                _sign += _url + _nonce;
            }
            else
            {
                _sign += _url + _nonce + _json.ToString(Newtonsoft.Json.Formatting.None);
            }
            _sign = SHA.EncodeHMACSHA256ToHex(_sign, base.Secret).ToLower();

            _http.Headers.Add("accept", "application/json");
            _http.Headers.Add("api-key", base.Key);
            _http.Headers.Add("api-signature", _sign);
            _http.Headers.Add("api-expires", _nonce);
            if (_method == "POST")
            {
                _http.ContentType = "application/json";
            }

            return(_keyValues);
        }
예제 #18
0
파일: OKEx.cs 프로젝트: lionzhou1981/Lion
        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()}");
                }
            }
        }
예제 #19
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()}");
                }
            }
        }