Exemple #1
0
        public bool CancelOrder(string OrderID, string tradingpair, out string rawresponse)
        {
            CheckSet();

            string     url = GetPublicApiURL("", "cancel_order.do");
            RestClient rc  = new RestClient(url);

            if (_proxy != null)
            {
                rc.Proxy = new WebProxy(_proxy.IP, Convert.ToInt32(_proxy.Port));
            }
            RestRequest rr = new RestRequest(Method.POST);

            rr.AddParameter("api_key", _key);
            rr.AddParameter("symbol", tradingpair);
            rr.AddParameter("order_id", OrderID);

            Dictionary <String, String> paras = new Dictionary <String, String>();

            paras.Add("api_key", _key);
            paras.Add("symbol", tradingpair);
            paras.Add("order_id", OrderID);
            String sign = MD5Util.buildMysignV1(paras, _secret);

            rr.AddParameter("sign", sign);
            var response = new RestClient(url).Execute(rr);


            rawresponse = response.Content;
            try
            {
                JObject obj = JObject.Parse(rawresponse);
                if (Convert.ToBoolean(obj["result"]))
                {
                    return(true);
                }
            }
            catch (Exception e)
            {
                //这里应该抛出错误
            }
            return(false);
        }
Exemple #2
0
        public Account GetAccount(out string rawresponse)
        {
            CheckSet();
            if (_account == null)
            {
                _account = new Account();
            }
            string     url = GetPublicApiURL("", "userinfo.do");
            RestClient rc  = new RestClient(url);

            if (_proxy != null)
            {
                rc.Proxy = new WebProxy(_proxy.IP, Convert.ToInt32(_proxy.Port));
            }
            RestRequest rr = new RestRequest(Method.POST);
            Dictionary <String, String> paras = new Dictionary <String, String>();

            paras.Add("api_key", _key);
            String sign = MD5Util.buildMysignV1(paras, _secret);

            rr.AddParameter("api_key", _key);
            rr.AddParameter("sign", sign);
            var response = new RestClient(url).Execute(rr);


            rawresponse = response.Content;
            //解析
            try
            {
                JObject obj = JObject.Parse(rawresponse);
                if (!Convert.ToBoolean(obj["result"]))
                {
                    throw (new Exception("error:" + rawresponse));
                }
                JToken borrow  = obj["info"]["funds"]["borrow"];
                JToken free    = obj["info"]["funds"]["free"];
                JToken freezed = obj["info"]["funds"]["freezed"];
                foreach (JProperty jp in borrow)
                {
                    if (_account.Balances.ContainsKey(jp.Name))
                    {
                        _account.Balances[jp.Name].borrow    = Convert.ToDouble(jp.Value.ToString());
                        _account.Balances[jp.Name].available = Convert.ToDouble(free[jp.Name].ToString());
                        _account.Balances[jp.Name].reserved  = Convert.ToDouble(freezed[jp.Name].ToString());
                        _account.Balances[jp.Name].balance   = _account.Balances[jp.Name].available + _account.Balances[jp.Name].reserved;
                    }
                    else
                    {
                        Balance b = new Balance();
                        b.borrow    = Convert.ToDouble(jp.Value.ToString());
                        b.available = Convert.ToDouble(free[jp.Name].ToString());
                        b.reserved  = Convert.ToDouble(freezed[jp.Name].ToString());
                        b.balance   = b.available + b.reserved;

                        _account.Balances.Add(jp.Name, b);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return(_account);
        }
Exemple #3
0
        public List <Order> GetOrdersStatus(string tradingpair, out string rawresponse)
        {
            CheckSet();
            Order      order = null;
            string     url   = GetPublicApiURL("", "order_info.do");
            RestClient rc    = new RestClient(url);

            if (_proxy != null)
            {
                rc.Proxy = new WebProxy(_proxy.IP, Convert.ToInt32(_proxy.Port));
            }
            RestRequest rr = new RestRequest(Method.POST);

            rr.AddParameter("api_key", _key);
            rr.AddParameter("symbol", tradingpair);
            rr.AddParameter("order_id", -1);

            Dictionary <String, String> paras = new Dictionary <String, String>();

            paras.Add("api_key", _key);
            paras.Add("symbol", tradingpair);
            paras.Add("order_id", "-1");
            String sign = MD5Util.buildMysignV1(paras, _secret);

            rr.AddParameter("sign", sign);
            var response = new RestClient(url).Execute(rr);


            rawresponse = response.Content;
            try
            {
                JObject obj = JObject.Parse(rawresponse);
                if (obj.Property("error_code") != null)
                {
                    throw (new Exception(SpotErrcode2Msg.Prase(obj["error_code"].ToString())));
                }
                if (!Convert.ToBoolean(obj["result"]))
                {
                    throw (new Exception("error:" + rawresponse));
                }
                JArray orders = JArray.Parse(obj["orders"].ToString());
                if (orders.Count > 0)
                {
                    List <Order> orders_array = new List <Order>();
                    for (int i = 0; i < orders.Count; i++)
                    {
                        order             = new Order();
                        order.Id          = orders[i]["order_id"].ToString();
                        order.Amount      = Convert.ToDouble(orders[i]["amount"].ToString());
                        order.DealAmount  = Convert.ToDouble(orders[i]["deal_amount"].ToString());
                        order.Price       = Convert.ToDouble(orders[i]["price"].ToString());
                        order.Type        = GetOrderTypeFromString(orders[i]["type"].ToString());
                        order.Status      = GetOrderStatus(orders[i]["status"].ToString());
                        order.TradingPair = orders[i]["symbol"].ToString();
                        order.CreatDate   = CommonLab.TimerHelper.ConvertStringToDateTime(Convert.ToDouble(orders[i]["create_date"].ToString()) / 1000);
                        orders_array.Add(order);
                    }
                    return(orders_array);
                }
            }
            catch (Exception e)
            {
                Exception err = new Exception("订单获取解析json失败" + e.Message);
                throw err;
            }
            return(null);
        }
Exemple #4
0
        protected string Trade(OrderType type, string tradingpair, double price, double amount)
        {
            CheckSet();
            string     url = GetPublicApiURL("", "trade.do");
            RestClient rc  = new RestClient(url);

            if (_proxy != null)
            {
                rc.Proxy = new WebProxy(_proxy.IP, Convert.ToInt32(_proxy.Port));
            }
            RestRequest rr = new RestRequest(Method.POST);

            rr.AddParameter("api_key", _key);
            rr.AddParameter("symbol", tradingpair);
            Dictionary <String, String> paras = new Dictionary <String, String>();

            paras.Add("api_key", _key);
            paras.Add("symbol", tradingpair);
            switch (type)
            {
            case OrderType.ORDER_TYPE_BUY:
                paras.Add("type", "buy");
                paras.Add("price", price.ToString());
                rr.AddParameter("type", "buy");
                rr.AddParameter("price", price.ToString());
                break;

            case OrderType.ORDER_TYPE_SELL:
                paras.Add("type", "sell");
                paras.Add("price", price.ToString());
                rr.AddParameter("type", "sell");
                rr.AddParameter("price", price.ToString());
                break;

            case OrderType.ORDER_TYPE_MARKETBUY:
                paras.Add("type", "buy_market");
                rr.AddParameter("type", "buy_market");
                break;

            case OrderType.ORDER_TYPE_MARKETSELL:
                paras.Add("type", "sell_market");
                rr.AddParameter("type", "sell_market");
                break;
            }
            paras.Add("amount", amount.ToString());
            rr.AddParameter("amount", amount.ToString());
            String sign = MD5Util.buildMysignV1(paras, _secret);

            rr.AddParameter("sign", sign);

            var response = new RestClient(url).Execute(rr);


            string rawresponse = response.Content;

            try
            {
                JObject obj = JObject.Parse(rawresponse);
                if (!Convert.ToBoolean(obj["result"]))
                {
                    throw (new Exception("error:" + SpotErrcode2Msg.Prase(obj["error_code"].ToString())));
                }
                return(obj["order_id"].ToString());
            }
            catch (Exception e)
            {
                throw e;
            }
        }