BitMEX.Net

BitMEX.Net is a client library for accessing the BitMEX REST and Websocket API.

Features

  • Response data is mapped to descriptive models
  • Input parameters and response values are mapped to discriptive enum values where possible
  • Automatic websocket (re)connection management
  • Client side rate limiting
  • Cient side order book implementation
  • Extensive logging
  • Support for different environments
  • Easy integration with other exchange client based on the CryptoExchange.Net base library
This library is based on the CryptoExchange.Net base package and can be used in combination with other exchange packages!

Supported Frameworks

The library is targeting both .NET Standard 2.0 and .NET Standard 2.1 for optimal compatibility

.NET implementation Version Support
.NET Core2.0 and higher
.NET Framework4.6.1 and higher
Mono5.4 and higher
Xamarin.iOS10.14 and higher
Xamarin.Android8.0 and higher
UWP10.0.16299 and higher
Unity2018.1 and higher

Discord

A Discord server is available here. Feel free to join for discussion and/or questions around the CryptoExchange.Net and implementation libraries.

Support the project

Donate

Make a one time donation in a crypto currency of your choice. If you prefer to donate a currency not listed here please contact me.

Btc: bc1q277a5n54s2l2mzlu778ef7lpkwhjhyvghuv8qf
Eth: 0xcb1b63aCF9fef2755eBf4a0506250074496Ad5b7
USDT (TRX): TKigKeJPXZYyMVDgMyXxMf17MWYia92Rjd

Sponsor

Alternatively, sponsor me on Github using Github Sponsors.

Any support is greatly appreciated.

Getting Started

The package is available on Nuget. After installing the package the BitMEX API is available by using the BitMEXRestClient and BitMEXSocketClient.

More information and usage documentation is available at the CryptoExchange.Net documentation page

Installation

Nuget

dotnet add package JKorf.BitMEX.Net
GitHub packages

BitMEX.Net is available on GitHub packages. You'll need to add https://nuget.pkg.github.com/JKorf/index.json as a NuGet package source.

Download release

The NuGet package files are added along side the source with the latest GitHub release which can found here.


API Access

BitMEX.Net can be configured using Dotnet dependency injection, after which the clients can be injected into your services. It also correctly configures logging and HttpClient usage.

// Configure options from config file
// see https://github.com/JKorf/CryptoExchange.Net/tree/master/Examples/example-config.json for an example
builder.Services.AddBitMEX(builder.Configuration.GetSection("BitMEX"));
		  
// OR
		  
 builder.Services.AddBitMEX(options => {
  // Configure options in code
  options.ApiCredentials = new ApiCredentials("APIKEY", "APISECRET");
});

The IBitMEXRestClient and IBitMEXtSocketClient can then be injected.

Alternatively the rest and socket client can be constructed directly

var restClient = new BitMEXRestClient(options => {
  // Options can be configured here, for example:
  options.ApiCredentials = new ApiCredentials("APIKEY", "APISECRET");
});
var socketClient = new BitMEXSocketClient();

API Quantities

BitMEX handles quantities a bit differently than most exchange API's. Asset quantities like account balances are denoted in a base value. For example 9846 XBt instead of 0.00009846 BTC. The same logic is also applied to trading quantities, if you want to place a spot order for 0.1 BTC you'd need to specify a quantity of 10000000 XBt. Note that futures trading works with contracts.

How many decimal places are used for each asset and symbol can be requested using the restClient.ExchangeApi.ExchangeData.GetAssetsAsync and restClient.ExchangeApi.ExchangeData.GetActiveSymbolsAsync endpoints.

The library offers an easy way of converting between these quantities with the ToSharedAssetQuantity, ToSharedSymbolQuantity, ToBitMEXAssetQuantity and ToBitMEXSymbolQuantity extension methods. For example:

Getting balance info
// Make sure to call this at least once at the start of the program to retrieve the conversion data
await BitMEXUtils.UpdateSymbolInfoAsync();

var balances = await bitMEXRestClient.ExchangeApi.Account.GetBalancesAsync();
foreach (var balance in balances.Data)
{
    var bitMEXQuantity = balance.Quantity; // The quantity as used in the BitMEX API, for example 1000000
    var bitMEXAssetName = balance.Currency; // The currency name as used in the BitMEX API, for example `gwei`
    var assetQuantity = balance.Quantity.ToSharedAssetQuantity(balance.Currency); // The quantity in the actual assset, for example 0.01
    var assetName = BitMEXUtils.GetAssetFromCurrency(balance.Currency); // The asset name, for example `ETH`
}
Placing spot order
// Make sure to call this at least once at the start of the program to retrieve the conversion data
await BitMEXUtils.UpdateSymbolInfoAsync();

var symbols = await bitMEXRestClient.ExchangeApi.ExchangeData.GetActiveSymbolsAsync();
var ethUsdtSpotSymbol = symbols.Data.SingleOrDefault(x => x.BaseAsset == "ETH" && x.QuoteAsset == "USDT" && x.SymbolType == SymbolType.Spot);
var minQuantityInBaseUnit = ethUsdtSpotSymbol.LotSize; // For example 1000
var minQuantityInETH = ethUsdtSpotSymbol.LotSize.ToSharedSymbolQuantity(ethUsdtSpotSymbol.Symbol); // For example 0.001

var quantityToPlace = 0.1m;
var quantityBitMEX = quantityToPlace.ToBitMEXSymbolQuantity(ethUsdtSpotSymbol.Symbol); // For example 1000000
var result = await bitMEXRestClient.ExchangeApi.Trading.PlaceOrderAsync(ethUsdtSpotSymbol.Symbol, OrderSide.Buy, OrderType.Market, quantityBitMEX);

Examples

Get Symbols

Get a list of supported symbols on BitMEX

await BitMEXClient.ExchangeApi.ExchangeData.GetActiveSymbolsAsync();
Get Tickers

Get a ticker data (price statistics)

var tickersResult = await bitMEXRestClient.ExchangeApi.ExchangeData.GetSymbolsAsync();
if (!tickersResult.Success)
{
    Console.WriteLine("Failed to retrieve tickers: " + tickersResult.Error);
    return;
}

foreach (var ticker in tickersResult.Data)
    Console.WriteLine($"{ticker.Symbol}: " + ticker.LastPrice);
Get Spot Balances

Get the balances on the spot account

// Assumes that API credentials are set in the client options / DI configuration
var balanceResult = await bitMEXRestClient.ExchangeApi.Account.GetBalancesAsync();
if (!balanceResult.Success)
{
    Console.WriteLine("Failed to retrieve balances: " + balanceResult.Error);
    return;
}

foreach (var balance in balanceResult.Data)
    Console.WriteLine($"{balance.Currency}: " + balance.Quantity);
Place Spot order

Place limit order for buying 0.01 ETH at a price of 3000 per ETH

// Assumes that API credentials are set in the client options / DI configuration
await BitMEXUtils.UpdateSymbolInfoAsync();

var orderResult = await bitMEXRestClient.ExchangeApi.Trading.PlaceOrderAsync("ETH_USDT", OrderSide.Buy, OrderType.Limit, 0.01m.ToBitMEXSymbolQuantity("ETH_USDT"), 3000);
if (!orderResult.Success)
{
    Console.WriteLine("Failed to place order: " + orderResult.Error);
    return;
}

Console.WriteLine($"Order placed, id: {orderResult.Data.OrderId}");
Place Futures order

Place limit order for buying 0.01 ETH worth of contracts at a price of 3000 per ETH

// Assumes that API credentials are set in the client options / DI configuration
var symbols = await bitMEXRestClient.ExchangeApi.ExchangeData.GetActiveSymbolsAsync();
var symbolInfo = symbols.Data.Single(x => x.BaseAsset == "ETH" && x.QuoteAsset == "USDT" && x.SymbolType == SymbolType.PerpetualContract);
// For Linear contracts: ContractSize = 1 / UnderlyingToPositionMultiplier
// For Inverse contracts: ContractSize = Multiplier / UnderlyingToSettleMultiplier
var contractSize = 1 / symbolInfo.UnderlyingToPositionMultiplier;
var contractsToBuy = 0.01m / contractSize;

var orderResult = await bitMEXRestClient.ExchangeApi.Trading.PlaceOrderAsync("ETHUSDT", OrderSide.Buy, OrderType.Limit, (long)contractsToBuy, 3000);
if (!orderResult.Success)
{
    Console.WriteLine("Failed to place order: " + orderResult.Error);
    return;
}

Console.WriteLine($"Order placed, id: {orderResult.Data.OrderId}");
Subscribe to book ticker updates

Subscribe to ticker updates for the ETH_USDT symbol

var subscribeResult = await bitMEXSocketClient.ExchangeApi.SubscribeToBookTickerUpdatesAsync("ETH_USDT", update =>
{
    Console.WriteLine($"Best ask: {update.Data.BestAskPrice}, best bid: {update.Data.BestBidPrice}");
});

if (!subscribeResult.Success)
{
    Console.WriteLine($"Failed to subscribe: {subscribeResult.Error}");
    return;
}
Console.WriteLine("Successfully subscribed to book ticker updates");