簡體   English   中英

C#發送字符串數據到我的方法時的問題

[英]c# Issue when sending string data to my method

我的項目包含“ program.cs”和“ Bitmex.cs”兩部分,我正在努力尋找如何將數據從“ program.cs”發送到“ Bitmex.cs”。

在“ program.cs”中,我有:

                //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");

當我將數據發送到Bitmex.cs的方法時,一切正常。

當我發送此消息時,問題就開始了(不起作用):

bitmex.selllimit(sumB, "1");

之間有什么區別?

bitmex.selllimit(sumB, "1");

和(工作):

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

都是字符串不是嗎????

這是我在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);
        }

這是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

    }

}

謝謝你的幫助

bitmex.selllimit(sumB, "1");什么區別bitmex.selllimit(sumB, "1"); 和(有效): bitmex.selllimit("10000", "1");

一個是字符串文字,另一個是調用double.String的結果,該字符串可能包含小數點,科學計數法,正負無窮或NotANumber。

我猜測bitmex在小數點上令人窒息。

您可以使用

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

得到像“ 135.65”這樣的字符串,而不是“ 135,65”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM