Binance.Net

repository·master·Indexed 22 days ago

https://github.com/jkorf/binance.net

A strongly typed .NET client library for accessing Binance REST and WebSocket APIs. It provides high-performance models for exchange data and supports Binance.com, Binance.us, and Testnet. The library includes support for Spot, USD-M futures, account management, and real-time data streaming via BinanceRestClient and BinanceSocketClient.

Tokens
11.2K
Snippets
25
Records
35
Agent score
78%

What's inside Binance.Net

  1. Performance comparison: Binance.Net vs CCXT and Binance.Api

    master

    This document provides a performance comparison between Binance.Net (both direct and shared-interface clients), CCXT, and Binance.Api for C#/.NET users interacting with Binance REST and WebSocket APIs.

    Key findings:

    • REST Performance: Binance.Net consistently leads in both speed (lowest mean time) and memory efficiency (lowest allocation profile) across .NET 10 and .NET Framework 4.8.
    • WebSocket Performance: Binance.Net shows a significant advantage in WebSocket trade-stream ingestion. On .NET 10, Binance.Net is approximately 3x faster than Binance.Api and 9.6x faster than CCXT.
    • Memory Allocation: Binance.Net has the lowest allocation profile in all measured scenarios. This is critical for market-data consumers to minimize Garbage Collection (GC) activity and maximize headroom for strategy logic and event processing.
    • Shared Interfaces: Binance.Net shared interfaces (exchange-agnostic abstractions) maintain performance very close to direct Binance.Net clients, significantly outperforming CCXT's unified API style in both speed and memory usage.
  2. Access shared client capabilities

    master

    Binance.Net provides shared clients for different API segments (Spot, USD-M Futures, COIN-M Futures) via both REST and WebSocket clients. You can access these shared clients to reuse underlying connections or discover capabilities.

    REST Shared Clients:

    • Spot: client.SpotApi.SharedClient
    • USD-M Futures: client.UsdFuturesApi.SharedClient
    • COIN-M Futures: client.CoinFuturesApi.SharedClient

    WebSocket Shared Clients:

    • Spot: socketClient.SpotApi.SharedClient
    • USD-M Futures: socketClient.UsdFuturesApi.SharedClient
    • COIN-M Futures: socketClient.CoinFuturesApi.SharedClient

    You can use the .Discover() method on these shared clients to see available capabilities.

    // Discover capabilities on a shared REST client
    client.SpotApi.SharedClient.Discover();
  3. Use SharedApis for exchange-agnostic code

    master

    If you are writing code that needs to work across multiple exchanges (e.g., Binance, Bybit, OKX, Kraken) using the CryptoExchange.Net ecosystem, use the SharedClient properties. This allows you to use common interfaces like ISpotTickerRestClient or IFuturesOrderRestClient instead of exchange-specific ones.

    Shared Client Access

    • Spot REST: new BinanceRestClient().SpotApi.SharedClient
    • USD-M Futures REST: new BinanceRestClient().UsdFuturesApi.SharedClient
    • COIN-M Futures REST: new BinanceRestClient().CoinFuturesApi.SharedClient
    • Spot WebSocket: new BinanceSocketClient().SpotApi.SharedClient
    • USD-M Futures WebSocket: new BinanceSocketClient().UsdFuturesApi.SharedClient
    • COIN-M Futures WebSocket: new BinanceSocketClient().CoinFuturesApi.SharedClient

    Shared Interface Examples

    • Spot Ticker (REST): ISpotTickerRestClient.GetSpotTickerAsync(new GetTickerRequest(symbol))
    • Spot Order (REST): ISpotOrderRestClient.PlaceSpotOrderAsync(...)
    • Futures Order (REST): IFuturesOrderRestClient.PlaceFuturesOrderAsync(...)
    • Ticker (Socket): ITickerSocketClient.SubscribeToTickerUpdatesAsync(...)
    • Order Book (Socket): IOrderBookSocketClient.SubscribeToOrderBookUpdatesAsync(...)

    Shared Data and Results

    • Symbol Catalogs: Use ISpotSymbolRestClient.GetSpotSymbolsAsync(...) and access .SpotSymbolCatalog (or .FuturesSymbolCatalog for futures) to load typed symbols.
    • Return Types:
      • REST calls return HttpResult<T> or HttpResult.
      • Socket subscriptions return WebSocketResult<UpdateSubscription>.
      • Non-I/O helpers (like symbol support checks) return ExchangeCallResult<T>.
    • Symbol Metadata: Shared symbols include DisplayName, BaseAssetType/BaseAssetSubType, and QuoteAssetType/QuoteAssetSubType (covering crypto, stablecoins, fiat, and TradFi).
    // Example: Using a shared spot ticker client
    ISpotTickerRestClient sharedClient = new BinanceRestClient().SpotApi.SharedClient;
    var result = await sharedClient.GetSpotTickerAsync(new GetTickerRequest("BTCUSDT"));
  4. Quickstart with AI-Friendly Examples

    master

    The Examples/ai-friendly/ directory contains self-contained, single-file C# examples optimized for AI coding assistants and rapid onboarding. These examples are designed to be dropped directly into a console application. They follow idiomatic Binance.Net 12.x patterns and include heavy commenting to explain the logic behind each operation.

    To use an example, follow these steps:

    1. Create a new console project.
    2. Add the Binance.Net NuGet package.
    3. Copy the content of a specific .cs file from the examples directory into your Program.cs.
    4. Replace the API_KEY and API_SECRET placeholders with your actual Binance credentials.
    5. Run the application.
    dotnet new console -n MyBinanceApp
    cd MyBinanceApp
    dotnet add package Binance.Net
    # Copy the example .cs file content into Program.cs
    # Replace API_KEY / API_SECRET placeholders with your own
    dotnet run
  5. Use Open Iconic Icon Font standalone

    master

    To use Open Iconic as an icon font without Bootstrap or Foundation, include the default stylesheet and use the oi class combined with the data-glyph attribute.

    <!-- Include stylesheet -->
    <link href="/open-iconic/font/css/open-iconic.css" rel="stylesheet">
    
    <!-- Use icon -->
    <span class="oi" data-glyph="icon-name" title="icon name" aria-hidden="true"></span>
  6. Available AI-Friendly Example Scenarios

    master

    The following specialized examples are available in the Examples/ai-friendly/ directory to demonstrate specific Binance.Net capabilities:

    FileCapabilities Demonstrated
    01-spot-quickstart.csClient setup, public ticker, authenticated account balance, placing limit orders, and querying order status.
    02-futures.csUSD-M futures operations: setting leverage, placing market orders, getting positions, and closing positions.
    03-websocket.csWebSocket subscriptions for ticker updates, klines, and user data streams, including proper teardown procedures.
    04-multi-exchange.csUsing the CryptoExchange.Net.SharedApis pattern for exchange-agnostic code, capability discovery, and shared subscriptions.
    05-error-handling.csHandling HttpResult, WebSocketResult, and ExchangeCallResult patterns, implementing retries, and managing common error scenarios.
  7. Use Open Iconic's SVG Sprite

    master

    The SVG sprite allows you to load all icons in a single HTTP request. To use an icon, reference the sprite file and the specific icon ID using the <use> tag.

    Styling Tips:

    • Add a general class to the <svg> tag for shared sizing.
    • Add a unique class to the <use> tag for specific icon coloring.
    • Sizing: Set equal width and height on the <svg> tag.
    • Coloring: Use the fill CSS property on the <use> tag.
    <!-- HTML Structure -->
    <svg class="icon">
      <use xlink:href="open-iconic.svg#account-login" class="icon-account-login"></use>
    </svg>
    
    <!-- CSS Styling -->
    <style>
    .icon {
      width: 16px;
      height: 16px;
    }
    
    .icon-account-login {
      fill: #f00;
    }
    </style>