5

I am new to StackOverflow but I heard that there are awesome and helpful people who can help me out.

My mission:

  • To find a way to make a trade calling the Binance REST API using c#
  • Without dlls, using my own code (for speed update)

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:

  • Success: I can call the public API without problem with "WebRequest" and which there is no need authentication.
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());
  • Success: I can call the REST API without problem with "WebRequest" and which there is need authentication. BUT only the account information.
string dataQueryString = "recvWindow=15000&timestamp=" + 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();
  • !!! THE PROBLEM !!! I can't call the ORDER REST API with "WebRequest" and which there is need authentication. I tried the code below. (It is called the same way as the account information but with the type of POST and of course with the plus parameters needed)
string dataQueryString = "symbol=BTCUSDT&side=SELL&type=LIMIT&quantity=0.00039&price=38878&newOrderRespType=RESULT&recvWindow=15000&timestamp=" + 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:

  • I have enough funds on my spot account
  • I trying to sell more than the minimum trade amount is

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!

  • Which exact endpoint are you trying to call, from all the ones in that documentation? You can link to the specific item from the menu – ADyson Feb 05 '21 at 19:16
  • @ADyson He already mentioned exact endpoint i,e. order – Jitendra Pancholi Dec 27 '21 at 06:48
  • @JitendraPancholi but it's not our job to dig around in the documentation for that, or assume there's only one way to call it (e.g. there could be get, post, patch and delete for the same URL). As I said myself, there are multiple entries in the documentation for "order", so it wasn't clear which one was meant. – ADyson Dec 27 '21 at 08:25

1 Answers1

4

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Andrew Stone
  • 1,000
  • 1
  • 7
  • 17
  • Hey. Thanks to your answer your idea is greater as it is more transparent and it can look more organized. However, my problem was a bit other., Now I am able to solve, so I will update my post, and I am working on documentation which I will post. Although I will use c# maybe it will be helpful for you too. – Levente Bencze Feb 06 '21 at 11:45
  • 1
    Hey, how did you solve your problem? i have the same error for Binance order endoint. – Venus H Aug 14 '21 at 07:40
  • Can you update your question and explain how did you solve your issue. Thanks! – Mansour Fahad Jun 21 '22 at 10:17