Skip to content
sonvister edited this page Dec 6, 2018 · 18 revisions

Binance Wiki

A full-featured .NET Binance API library.

A complete implementation of the official Binance Exchange API.

Compatible with .NET Standard 2.0 and .NET Framework 4.7.1.

REST API - IBinanceApi

The IBinanceApi interface is an abstraction of the Binance REST API whose methods return immutable domain/value objects and HTTP error responses as exceptions.

The library services are designed to be instantiated using dependency injection via the ServiceCollection and ServiceProvider classes (Microsoft.Extensions.DependencyInjection), but services can also be instantiated directly along with their default dependencies and no logging capabilities (Microsoft.Extensions.Logging).

For example, instantiating the Binance REST API service (this 'api' variable is referred to in other examples):

var api = serviceProvider.GetService<IBinanceApi>();
  // or...
var api = new BinanceApi(); // no configurable DI or logging.

NOTE: The BinanceApi class does not require an API-Key or API-Secret for instantiation unlike most other API implementations. Instead, Authentication information is only required where necessary and passed via method injection with an IBinanceApiUser instance.

JSON REST API - IBinanceHttpClient

An alternative to the high-level REST API interface IBinanceApi, is the lower-level IBinanceHttpClient interface with all the same capabilities but with methods that return string values as either JSON objects or arrays as received from Binance. The implementation is a singleton and can be accessed multiple ways:

var httpClient = serviceProvider.GetService<IBinanceHttpClient>();
  // or...
var httpClient = BinanceHttpClient.Instance;
  // or...
var httpClient = api.HttpClient;

Rate Limiting

The IBinanceHttpClient implementation has an IApiRateLimiter that ensures the API request rate doesn't exceed a configurable threshold. This helps provide fair use of the Binance API and to prevent potentially being IP banned.

Default Request Rate Limits
  • Count: 1200
  • Duration: 1 minute
  • Burst Count: 100
  • Burst Duration: 1 second
Default Order Rate Limits
  • Count: 100000
  • Duration: 1 day
  • Burst Count: 10
  • Burst Duration: 1 minute

NOTE: Currently, only the Place, Get, and Cancel order methods use the 'order' rate limiter associated with the User; all other methods use the 'request' rate limiter associated with the API.

General Information

All REST API methods are asynchronous with an optional CancellationToken 'token' parameter.

For consistency with the official Binance REST API:

  • All IEnumerable<> data is returned in ascending chronological order (oldest first, newest last).

Date/Time Properties and Timestamps

All DateTime properties are UTC.

Timestamps from Binance API are converted from Unix time milliseconds using:

DateTimeOffset.FromUnixTimeMilliseconds(timestamp).UtcDateTime;

NOTE: If you want to convert UTC DateTime to a timestamp (Unix time milliseconds), use the ToTimestamp() extension.

Assets and Symbols

For convenience, there are pre-defined static symbols (e.g. BTC_USDT) in the Symbol class and static assets in the Asset class. These static assets and symbols are provided for reference and are updated with each release using the BinanceCodeGenerator tool. To update the assets and symbols at runtime, use Symbol.UpdateCacheAsync(api).

Update Frequency

At a minimum, it is recommended to update the cache when an application starts (as new releases of this library will become less frequent). An application should also update the cache if a 'LOT_SIZE' or a similar filter failure occurs during order placement after validation passes. However, some changes to this data may not cause an order placement error, but still not give the best result. So, a periodic update should also be done which is application dependent. The exchange info rate limit weight is 1, so conceivably this data could be refreshed frequently.

NOTE: Binance adds new traiding pairs regularly, it is not a goal of this project to create a new release just to update the static assets and symbols.

Configuration Options

The following class is used to provide configurable default options: BinanceApiOptions. The BinanceConsoleApp demonstrates how these options can be configured using an external JSON file.

Clone this wiki locally