I am new to StackOverflow but I heard that there are awesome and helpful people who can help me out. 😉
My mission:
Now I using the Binance.API package but my bot needs to be a bit faster as its speed is not enough. Also, it would be a great thing to be able to do that without any external sources like dlls. Isn't it? 😎
What I tried:
WebRequest webrequest = WebRequest.Create("https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT");
WebResponse Response = webrequest.GetResponse();
StreamReader reader = new StreamReader(Response.GetResponseStream());
MessageBox.Show(reader.ReadToEnd());
string dataQueryString = "recvWindow=15000×tamp=" + Math.Round(Convert.ToDecimal(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds), 0).ToString();
WebRequest webrequest = WebRequest.Create("https://api.binance.com/api/v3/account?" + dataQueryString + "&signature=" + BitConverter.ToString(new HMACSHA256(Encoding.ASCII.GetBytes(tempAPI_Secret)).ComputeHash(Encoding.ASCII.GetBytes(dataQueryString))).Replace("-", string.Empty).ToLower());
webrequest.Method = "GET";
webrequest.Headers.Add("X-MBX-APIKEY", tempAPI_Key);
WebResponse Response = webrequest.GetResponse();
StreamReader reader = new StreamReader(Response.GetResponseStream());
string response = reader.ReadToEnd();
reader.Close();
Response.Close();
string dataQueryString = "symbol=BTCUSDT&side=SELL&type=LIMIT&quantity=0.00039&price=38878&newOrderRespType=RESULT&recvWindow=15000×tamp=" + Math.Round(Convert.ToDecimal(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds), 0).ToString();
WebRequest webrequest = WebRequest.Create("https://api.binance.com/api/v3/account?" + dataQueryString + "&signature=" + BitConverter.ToString(new HMACSHA256(Encoding.ASCII.GetBytes(tempAPI_Secret)).ComputeHash(Encoding.ASCII.GetBytes(dataQueryString))).Replace("-", string.Empty).ToLower());
webrequest.Method = "POST";
webrequest.Headers.Add("X-MBX-APIKEY", tempAPI_Key);
WebResponse Response = webrequest.GetResponse();
StreamReader reader = new StreamReader(Response.GetResponseStream());
string response = reader.ReadToEnd();
reader.Close();
Response.Close();
The returned ERROR code:
'The remote server returned an error: (400) Bad Request.'
I can't understand why this is not working. (I tried to do the order with exactly these parameters from the web client manually and it was successful)
I checked these possible problems:
There is the official Binance REST API documentation: HERE
I tried to google it but I couldn't find the solution even here.
Thanks to read it and if you could help me I would really appreciate it. 🙏
If something is not clear please ask it, I will answer!
I was literarly doing the same thing you are a few days ago, except I was using python. I'm also glad to see I'm not the only one who likes coding from scratch.
My solution was to leave the url as is https://api.binance.com/api/v3/account
and instead of attaching my order parameters symbol=BTCUSD&side=BUY&etc...
onto the url I had to instead encode and send that data through the data
parameter of python's built in function urllib.request.Request(url, data, headers)
I don't know C# that well so I wouldn't know how to translate my python code to C#, but I did find this doc link that provides an example on how to send data using a POST request. You could also take a look at my question and answer as another example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With