我的項目包含兩個部分「program.cs」和「Bitmex.cs」 我正在努力尋找如何將數據從「program.cs」發送到「Bitmex.cs」 。c#將字符串數據發送到我的方法時的問題
在 「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
}
}
感謝您的幫助
什麼錯誤,你得到一個字符串? –
「Console.WriteLine(sumB);'顯示的行是什麼? – Fabulous
@Fabulous 3691,11 – itriad