Skip to content

CryptoTradee/Bitfinex.Net

 
 

Repository files navigation

Icon Bitfinex.Net

Build status

Bitfinex.Net is a .Net wrapper for the Bitfinex API as described on Bitfinex. It includes all features the API provides using clear and readable C# objects including

  • Reading market info
  • Placing and managing orders
  • Reading balances and funds
  • Live updates using the websocket

Additionally it adds some convenience features like:

  • Configurable rate limiting
  • Autmatic logging

If you think something is broken, something is missing or have any questions, please open an Issue


Also check out my other exchange API wrappers:


Binance

Bittrex

CoinEx

Huobi

And other API wrappers based on CryptoExchange.Net:


Switcheo

Donations

Donations are greatly appreciated and a motivation to keep improving.

Btc: 12KwZk3r2Y3JZ2uMULcjqqBvXmpDwjhhQS
Eth: 0x069176ca1a4b1d6e0b7901a6bc0dbf3bb0bf5cc2
Nano: xrb_1ocs3hbp561ef76eoctjwg85w5ugr8wgimkj8mfhoyqbx4s1pbc74zggw7gs

Installation

Nuget version Nuget downloads

Available on NuGet:

PM> Install-Package Bitfinex.Net

To get started with Bitfinex.Net first you will need to get the library itself. The easiest way to do this is to install the package into your project using NuGet. Using Visual Studio this can be done in two ways.

Using the package manager

In Visual Studio right click on your solution and select 'Manage NuGet Packages for solution...'. A screen will appear which initially shows the currently installed packages. In the top bit select 'Browse'. This will let you download net package from the NuGet server. In the search box type 'Bitfinex.Net' and hit enter. The Bitfinex.Net package should come up in the results. After selecting the package you can then on the right hand side select in which projects in your solution the package should install. After you've selected all project you wish to install and use Bitfinex.Net in hit 'Install' and the package will be downloaded and added to you projects.

Using the package manager console

In Visual Studio in the top menu select 'Tools' -> 'NuGet Package Manager' -> 'Package Manager Console'. This should open up a command line interface. On top of the interface there is a dropdown menu where you can select the Default Project. This is the project that Bitfinex.Net will be installed in. After selecting the correct project type Install-Package Bitfinex.Net in the command line interface. This should install the latest version of the package in your project.

After doing either of above steps you should now be ready to actually start using Bitfinex.Net.

Getting started

After it's time to actually use it. To get started we have to add the Bitfinex.Net namespace: using Bitfinex.Net;.

Bitfinex.Net provides two clients to interact with the Bitfinex API. The BitfinexClient provides all rest API calls. The BitfinexSocketClient provides functions to interact with the websocket provided by the Bitfinex API.

Most API methods are available in two flavors, sync and async:

public void NonAsyncMethod()
{
    using(var client = new BitfinexClient())
    {
        var result = client.GetPlatformStatus();
    }
}

public async Task AsyncMethod()
{
    using(var client = new BitfinexClient())
    {
        var result2 = await client.GetPlatformStatusAsync();
    }
}

Response handling

All API requests will respond with an CallResult object. This object contains whether the call was successful, the data returned from the call and an error if the call wasn't successful. As such, one should always check the Success flag when processing a response. For example:

using(var client = new BitfinexClient())
{
	var priceResult = client.GetTicker("tBTCETH");
	if (priceResult.Success)
		Console.WriteLine($"BTC-ETH price: {priceResult.Data.Last}");
	else
		Console.WriteLine($"Error: {priceResult.Error}");
}

Options & Authentication

The default behavior of the clients can be changed by providing options to the constructor, or using the SetDefaultOptions before creating a new client to set options for all new clients. Api credentials can be provided in the options.

Websockets

The Bitfinex.Net socket client provides several socket endpoint to which can be subscribed and follow this function structure

var client = new BitfinexSocketClient();

var subscribeResult = client.SubscribeToTickerUpdates("tBTCETH", data =>
{
	// handle data
});

Handling socket events

Subscribing to a socket stream returns a UpdateSubscription object. This object can be used to be notified when a socket is disconnected or reconnected:

var subscriptionResult = client.SubscribeToTickerUpdates("tBTCETH", data =>
{
	Console.WriteLine("Received ticker update");
});

if(subscriptionResult.Success){
	sub.Data.Disconnected += () =>
	{
		Console.WriteLine("Socket disconnected");
	};

	sub.Data.Reconnected += (e) =>
	{
		Console.WriteLine("Socket reconnected after " + e);
	};
}

Unsubscribing from socket endpoints:

Sockets streams can be unsubscribed by using the client.Unsubscribe method in combination with the stream subscription received from subscribing:

var client = new BitfinexSocketClient();

var successTicker = client.SubscribeToTickerUpdates("tBTCETH", (data) =>
{
	// handle data
});

client.Unsubscribe(successTicker.Data);

Additionaly, all sockets can be closed with the UnsubscribeAll method. Beware that when a client is disposed the sockets are automatically disposed. This means that if the code is no longer in the using statement the eventhandler won't fire anymore. To prevent this from happening make sure the code doesn't leave the using statement or don't use the socket client in a using statement:

// Doesn't leave the using block
using(var client = new BitfinexSocketClient())
{
	var successTicker = client.SubscribeToTickerUpdates("tBTCETH", (data) =>
	{
		// handle data
	});

	Console.ReadLine();
}

// Without using block
var client = new BitfinexSocketClient();
client.SubscribeToTickerUpdates("tBTCETH", (data) =>
{
	// handle data
});

Release notes

  • Version 2.0.4 - 17 dec 2018

    • Fixed status fields when serializing
    • Fixed reconnecting sometimes throwing an error
    • Fixed error after event timeout in socket client
  • Version 2.0.3 - 10 dec 2018

    • Fix for status field serialization in BitfinexOrder object
  • Version 2.0.2 - 06 dec 2018

    • Fix CryptoExchange reference
  • Version 2.0.1 - 06 dec 2018

    • Fixed freezes if called from the UI thread
  • Version 2.0.0 - 05 dec 2018

    • Updated to CryptoExchange.Net version 2
      • Libraries now use the same standard functionalities
      • Objects returned by socket subscriptions standardized across libraries
  • Version 1.2.6 - 30 okt 2018

    • Fixed error message handling during subscription
    • Fixed reconnect trying to resubscribe failed subscriptions
  • Version 1.2.5 - 18 okt 2018

    • Fixed handling of confirmation preventing missed messages
  • Version 1.2.4 - 21 sep 2018

    • Updated CryptoExchange.Net
  • Version 1.2.3 - 19 sep 2018

    • Fix for exception during reconnecting if socket hangs in connecting state
  • Version 1.2.2 - 07 sep 2018

    • Fix for reconnecting
    • Fix for resubscribing always reporting successful when socket disconnects in the mean time
    • Added error message for duplicate subscriptions
  • Version 1.2.1 - 21 aug 2018

    • Changed SubscribeToTradeUpdates update data to BitfinexSocketEvent<BitfinexTradeSimple[]> to be able to distinguish execution and update messages
  • Version 1.2.0 - 21 aug 2018

    • Refactored connecting/reconnecting
    • Fixed bug disposing default credentials
  • Version 1.1.8 - 20 aug 2018

    • Updated CryptoExchange.Net to fix disposed socket issue
  • Version 1.1.7 - 16 aug 2018

    • Added client interfaces
    • Fixed some resharper warnings
    • Updated CryptoExchange.Net
  • Version 1.1.6 - 15 aug 2018

    • Fixed subscribe calls always reporting success
    • Made resubscribing after lost connection more robust
    • Added reconnect interval setting
  • Version 1.1.5 - 13 aug 2018

    • Added CancelOrders methods to socket client
    • Updated CryptoExchange.Net
  • Version 1.1.4 - 30 jul 2018

    • Changed order confirmation matching to use ClientOrderId instead of trying to match based on amount/price/symbol and type
  • Version 1.1.3 - 24 jul 2018

    • Fix for mixup in TradeUpdate for user trades and for symbol trades
    • Fix for small decimal parsing
  • Version 1.1.2 - 20 jul 2018

    • Fix for resubscribing after lost connection
  • Version 1.1.1 - 18 jul 2018

    • Fix for subscription data handling
  • Version 1.1.0 - 16 jul 2018

    • Refactored multiple subscriptions to expose the EventType that caused the update
  • Version 1.0.19 - 12 jul 2018

    • Fixed limit parameters in various rest calls
    • Fixed response handling of canceled FillOrKill orders
  • Version 1.0.18 - 03 jul 2018

    • Added StopLimit margin ordertype
    • Fixed bug restarting socketclient after stopping
  • Version 1.0.17 - 28 jun 2018

    • Fixed margin order confirmation
  • Version 1.0.16 - 26 jun 2018

    • Fix for OrderStatus mapping
  • Version 1.0.15 - 26 jun 2018

    • Improvided OrderStatus mapping
    • Added OrderStatusString for original order status string returned by server
  • Version 1.0.14 - 25 jun 2018

    • Fix for error parsing nullable ordertype
    • Fix for subscriptions not getting executed if called before Start in socket client
  • Version 1.0.13 - 22 jun 2018

    • Added symbol mapping for GetTrades call
    • Refactored socketclient to be more robust
    • Added additional logging
  • Version 1.0.12 - 11 jun 2018

    • Fix for subscribing to trades updates
    • Fix for PlaceOrder not returning the placed order in socket client
    • Added retry to (re)subscribing
  • Version 1.0.11 - 06 jun 2018

    • Added timeout options
    • Added locking for threadsafety
    • Improved subscribe symbol interpretation
  • Version 1.0.10 - 05 jun 2018

    • Fix for TradeDetails mapping
  • Version 1.0.9 - 04 jun 2018

    • Added GetSymbols and GetSymbolDetails calls
    • Added GetAvailableBalance call
    • Added UpdateOrder call
    • Added ConfigureAwaits to prevent freezes from UI thread
    • Added waiting for authentication in socket client
  • Version 1.0.8 - 12 may 2018

    • Fix for new order parameter in socket client
  • Version 1.0.7 - 12 may 2018

    • Added StopLimitOrder ordertype
  • Version 1.0.6 - 08 may 2018

    • Fixed bug with orderstatus parsing
  • Version 1.0.5 - 07 may 2018

    • Fix for logging issue
  • Version 1.0.4 - 07 may 2018

    • Added optional parameters to place order
  • Version 1.0.3 - 04 may 2018

    • Fix for freezing from UI-thread with authenticated requests
    • Fix for position deserialization
  • Version 1.0.2 - 20 apr 2018

    • Fix for ticker stream data deserialization
  • Version 1.0.1 - 20 apr 2018

    • Fix for OrderStatus parsing
    • Fix for int's being used when should be long
    • Fix swapped parameters in socket order
  • Version 1.0.0 - 19 apr 2018

    • Added raw orderbook subscription
    • Fix for GetCandles limit parameter
    • Fix for V1 Api error parsing
    • Fix for nonce for authenticated requests
  • Version 0.0.8 - 23 mar 2018

    • Updated socket dispose
    • Updated base
  • Version 0.0.7 - 23 mar 2018

    • Added notification message handling resulting in faster error message when failing to place/cancel orders via websocket
    • Fix for unsubsribing exception
  • Version 0.0.6 - 22 mar 2018

    • Fix for order status parsing
  • Version 0.0.5 - 21 mar 2018

    • Can add multiple log writers
    • Added some code docs
  • Version 0.0.4 - 20 mar 2018

    • Fix for precision serialization
  • Version 0.0.3 - 12 mar 2018

    • Fixes for freezes when calling from UI thread
  • Version 0.0.2 - 09 mar 2018

    • Auto reconnecting socket client
    • Info messages send from server processing
    • Socket client status events added
  • Version 0.0.1 - 08 mar 2018

    • Initial release

About

Bitfinex Api wrapper for .Net

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%