-
Notifications
You must be signed in to change notification settings - Fork 77
Home
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.
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.
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;
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.
- Count: 1200
- Duration: 1 minute
- Burst Count: 100
- Burst Duration: 1 second
- 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.
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).
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.
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)
.
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.
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.
- Verify connection to the Binance server (minimal examples).
- Get the market depth (order book) for a symbol.
- Maintain a real-time order book cache for a symbol.
- Get the aggregate trades for a symbol.
- Maintain a real-time trade history cache for a symbol.
- Get the candlesticks for a symbol.
- Maintain a real-time price chart cache for a symbol.
- Get the 24-hour statistics for a symbol.
- Get current prices for all symbols for a price ticker.
- Get best price and quantity on the order book for all symbols.
- Get a list of all current symbols.
- Place a LIMIT order.
- Place a MARKET order.
- Place a TEST order to verify client order properties.
- Look-up an existing order to check status.
- Cancel an open order.
- Get all open orders for a symbol.
- Get all orders for a symbol.
- Get current account information.
- Get account trades for a symbol.
- Submit a withdraw request.
- Get deposit history.
- Get withdraw history.
- Donate BTC to the creator of this library.