简体   繁体   中英

c# Issue when sending string data to my method

My project contains two part "program.cs" and "Bitmex.cs" i'm strugling to find how send data from "program.cs" to "Bitmex.cs".

in "program.cs" i have:

                //data coming from websocket
                dynamic socket = JsonConvert.DeserializeObject(e.Data);

                object bid = socket["data"][0]["bidPrice"];

                object plus = 1000.01;



                double bida = Convert.ToDouble(bid);

                double bidb = Convert.ToDouble(plus);

                double sum = bida + bidb;


                string sumB = sum.ToString();

                Console.WriteLine(sumB);
                bitmex.selllimit("10000", "1");

when i send data to my method to Bitmex.cs everything works.

The issue begin when i send this (does not work):

bitmex.selllimit(sumB, "1");

what is the difference between:

bitmex.selllimit(sumB, "1");

and(works):

bitmex.selllimit("10000", "1");

both are string isn't it??????

Here is my method in Bitmex.cs:

public string selllimit(string price,string volume)
        {
            var param = new Dictionary<string, string>();
            param["symbol"] = "XBTUSD";
            param["side"] = "Sell";
            param["orderQty"] = volume;
            param["price"] = price;
            param["ordType"] = "Limit";
            return Query("POST", "/order", param, true);
        }

here the bitmex Api:

using System;

using System.Collections.Generic;

using System.IO;

using System.Net;

using System.Security.Cryptography;

using System.Text;

using System.Threading;



namespace BitMEX

{





    public class BitMEXApi

    {

        private const string domain = "https://www.bitmex.com";

        private string apiKey;

        private string apiSecret;

        private int rateLimit;



        public BitMEXApi(string bitmexKey = "", string bitmexSecret = "", int rateLimit = 5000)

        {

            this.apiKey = bitmexKey;

            this.apiSecret = bitmexSecret;

            this.rateLimit = rateLimit;

        }



        private string BuildQueryData(Dictionary<string, string> param)

        {

            if (param == null)

                return "";



            StringBuilder b = new StringBuilder();

            foreach (var item in param)

                b.Append(string.Format("&{0}={1}", item.Key, item.Value));



            try { return b.ToString().Substring(1); }

            catch (Exception) { return ""; }

        }



        public static string ByteArrayToString(byte[] ba)

        {

            StringBuilder hex = new StringBuilder(ba.Length * 2);

            foreach (byte b in ba)

                hex.AppendFormat("{0:x2}", b);

            return hex.ToString();

        }



        private long GetNonce()

        {

            DateTime yearBegin = new DateTime(1990, 1, 1);

            return DateTime.UtcNow.Ticks - yearBegin.Ticks;

        }



        private string Query(string method, string function, Dictionary<string, string> param = null, bool auth = false)

        {

            string paramData = BuildQueryData(param);

            string url = "/api/v1" + function + ((method == "GET" && paramData != "") ? "?" + paramData : "");

            string postData = (method != "GET") ? paramData : "";



            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(domain + url);

            webRequest.Method = method;



            if (auth)

            {

                string nonce = GetNonce().ToString();

                string message = method + url + nonce + postData;

                byte[] signatureBytes = hmacsha256(Encoding.UTF8.GetBytes(apiSecret), Encoding.UTF8.GetBytes(message));

                string signatureString = ByteArrayToString(signatureBytes);



                webRequest.Headers.Add("api-nonce", nonce);

                webRequest.Headers.Add("api-key", apiKey);

                webRequest.Headers.Add("api-signature", signatureString);

            }



            try

            {

                if (postData != "")

                {

                    webRequest.ContentType = "application/x-www-form-urlencoded";

                    var data = Encoding.UTF8.GetBytes(postData);

                    using (var stream = webRequest.GetRequestStream())

                    {

                        stream.Write(data, 0, data.Length);

                    }

                }



                using (WebResponse webResponse = webRequest.GetResponse())

                using (Stream str = webResponse.GetResponseStream())

                using (StreamReader sr = new StreamReader(str))

                {

                    return sr.ReadToEnd();

                }

            }

            catch (WebException wex)

            {

                using (HttpWebResponse response = (HttpWebResponse)wex.Response)

                {

                    if (response == null)

                        throw;



                    using (Stream str = response.GetResponseStream())

                    {

                        using (StreamReader sr = new StreamReader(str))

                        {

                            return sr.ReadToEnd();

                        }

                    }

                }

            }

        }



        public string buymarket()

        {

            var param = new Dictionary<string, string>();

            param["symbol"] = "XBTUSD";

            param["side"] = "Buy";

            param["orderQty"] = "1";

            param["ordType"] = "Market";

            return Query("POST", "/order", param, true);

        }


        public string sellmarket()

        {

            var param = new Dictionary<string, string>();

            param["symbol"] = "XBTUSD";

            param["side"] = "Sell";

            param["orderQty"] = "5";

            param["ordType"] = "Market";

            return Query("POST", "/order", param, true);

        }

        public string buylimit()

        {

            var param = new Dictionary<string, string>();

            param["symbol"] = "XBTUSD";

            param["side"] = "Buy";

            param["orderQty"] = "1";

            param["price"] = "2500";

            param["ordType"] = "Limit";

            return Query("POST", "/order", param, true);

        }
        /*
        public string selllimit()

        {

            var param = new Dictionary<string, string>();

            param["symbol"] = "XBTUSD";

            param["side"] = "Sell";

            param["orderQty"] = "1";

            param["price"] = "5000";

            param["ordType"] = "Limit";

            return Query("POST", "/order", param, true);

        }
        */
        public string selllimit(string price,string volume)
        {
            var param = new Dictionary<string, string>();
            param["symbol"] = "XBTUSD";
            param["side"] = "Sell";
            param["orderQty"] = volume;
            param["price"] = price;
            param["ordType"] = "Limit";
            return Query("POST", "/order", param, true);
        }




        public string closeall()

        {

            var param = new Dictionary<string, string>();

            param["symbol"] = "XBTUSD";



            return Query("POST", "/order/closePosition", param, true);


        }










        private byte[] hmacsha256(byte[] keyByte, byte[] messageBytes)

        {

            using (var hash = new HMACSHA256(keyByte))

            {

                return hash.ComputeHash(messageBytes);

            }

        }



        #region RateLimiter



        private long lastTicks = 0;

        private object thisLock = new object();



        private void RateLimit()

        {

            lock (thisLock)

            {

                long elapsedTicks = DateTime.Now.Ticks - lastTicks;

                var timespan = new TimeSpan(elapsedTicks);

                if (timespan.TotalMilliseconds < rateLimit)

                    Thread.Sleep(rateLimit - (int)timespan.TotalMilliseconds);

                lastTicks = DateTime.Now.Ticks;

            }

        }



        #endregion RateLimiter

    }

}

thanks for any help

What is the difference between bitmex.selllimit(sumB, "1"); and(works): bitmex.selllimit("10000", "1"); ?

One is a string literal, the other is the result of a call to double.String which could contain a decimal point, scientific notation, positive or negative infinity, or NotANumber.

I'm guessing bitmex is choking on a decimal point.

You can use

string sumB = sum.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture);

to get a string like "135.65" instead of "135,65"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM