Example #1
0
 private Exception JsonError(JObject obj)
 {
     if (obj.Property("error_code") == null || obj.Property("error_code").ToString() == "")
     {
         return(new Exception("\"error_code\" not found in raw json string!"));
     }
     else
     {
         return(new Exception(SpotErrcode2Msg.Prase(obj["error_code"].ToString())));
     }
 }
Example #2
0
        public List <Order> GetHisOrders(string tradingpair)
        {
            CheckSet();
            string       rawresponse;
            Order        order        = null;
            List <Order> orders_array = new List <Order>();
            string       url          = GetPublicApiURL("", "order_history.do");
            RestClient   rc           = new RestClient(url);

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

            rr.AddParameter("api_key", _key);
            rr.AddParameter("symbol", tradingpair);
            rr.AddParameter("status", 1);
            rr.AddParameter("current_page", page);
            rr.AddParameter("page_length", 200);

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

            paras.Add("api_key", _key);
            paras.Add("symbol", tradingpair);
            paras.Add("status", "1");
            paras.Add("current_page", page.ToString());

            paras.Add("page_length", "200");
            String sign = CommonLab.TokenGen.CreateSign_Binance(paras, _secret);

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

            int total;
            int totalpage = 0;

            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());
                total     = Convert.ToInt32(obj["total"].ToString());
                totalpage = total / 200 + 1;
                if (orders.Count > 0)
                {
                    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);
                    }
                }
                for (page = 2; page <= totalpage; page++)
                {
                    rr.Parameters.Clear();
                    paras.Clear();
                    rr.AddParameter("api_key", _key);
                    rr.AddParameter("symbol", tradingpair);
                    rr.AddParameter("status", 1);
                    rr.AddParameter("current_page", page);
                    rr.AddParameter("page_length", 200);


                    paras.Add("api_key", _key);
                    paras.Add("symbol", tradingpair);
                    paras.Add("status", "1");
                    paras.Add("current_page", page.ToString());

                    paras.Add("page_length", "200");
                    sign = CommonLab.TokenGen.CreateSign_Binance(paras, _secret);
                    rr.AddParameter("sign", sign);
                    response = new RestClient(url).Execute(rr);


                    rawresponse = response.Content;
                    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));
                    }
                    orders = JArray.Parse(obj["orders"].ToString());
                    if (orders.Count > 0)
                    {
                        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;
            }
        }