Bitfinex.Client.Websocket
4.2.0
dotnet add package Bitfinex.Client.Websocket --version 4.2.0
NuGet\Install-Package Bitfinex.Client.Websocket -Version 4.2.0
<PackageReference Include="Bitfinex.Client.Websocket" Version="4.2.0" />
paket add Bitfinex.Client.Websocket --version 4.2.0
#r "nuget: Bitfinex.Client.Websocket, 4.2.0"
// Install Bitfinex.Client.Websocket as a Cake Addin #addin nuget:?package=Bitfinex.Client.Websocket&version=4.2.0 // Install Bitfinex.Client.Websocket as a Cake Tool #tool nuget:?package=Bitfinex.Client.Websocket&version=4.2.0
Bitfinex & Ethfinex websocket API client
This is a C# implementation of the Bitfinex & Ethfinex websocket API version 2.0 found here:
https://bitfinex.readme.io/v2/docs (Ethfinex)
You can do almost everything with provided websocket API. Using REST API is unnecessary! As a benefit, you will get real-time data and fast execution of your commands.
License:
Apache License 2.0
Features
- installation via NuGet (Bitfinex.Client.Websocket)
- public and authenticated API
- targeting .NET Standard 2.0 (.NET Core, Linux/MacOS compatible)
- reactive extensions (Rx.NET)
- integrated logging abstraction (LibLog)
Usage
var exitEvent = new ManualResetEvent(false);
var url = BitfinexValues.ApiWebsocketUrl;
using (var communicator = new BitfinexWebsocketCommunicator(url))
{
using (var client = new BitfinexWebsocketClient(communicator))
{
client.Streams.InfoStream.Subscribe(info =>
{
Log.Information($"Info received, reconnection happened, resubscribing to streams");
await client.Send(new PingRequest() {Cid = 123456});
//await client.Send(new TickerSubscribeRequest("BTC/USD"));
});
client.Streams.PongStream.Subscribe(pong =>
{
Console.WriteLine($"Pong received! Id: {pong.Cid}") // Pong received! Id: 123456
exitEvent.Set();
});
await communicator.Start();
exitEvent.WaitOne(TimeSpan.FromSeconds(30));
}
}
More usage examples:
API coverage
PUBLIC | Covered |
---|---|
Info | ✔ |
Ping-Pong | ✔ |
Errors | ✔ |
Configuration | ✔ |
Channel subscribing | ✔ |
Channel unsubscribing | ✔ |
Ticker | ✔ |
Ticker - funding | |
Trades | ✔ |
Trades - funding | ✔ |
Books | ✔ |
Books - funding | ✔ |
Raw books | ✔ |
Raw books - funding | ✔ |
Candles | ✔ |
Funding | ✔ |
Sequencing | ✔ |
Server timestamp | ✔ |
Book checksum | ✔ |
AUTHENTICATED | Covered |
---|---|
Account info | ✔ |
Orders | ✔ |
Positions | ✔ |
Trades | ✔ |
Funding | |
Wallets | ✔ |
Balance | ✔ |
Notifications | ✔ |
AUTHENTICATED - INPUT | Covered |
---|---|
New order | ✔ |
Update order | ✔ |
Cancel order | ✔ |
Cancel order multi | ✔ |
Order multi-op | |
New offer | |
Cancel offer | |
Calc | ✔ |
Pull Requests are welcome!
Other websocket libraries
<table> <tr>
<td> <a href="https://github.com/Marfusios/crypto-websocket-extensions"><img src="https://raw.githubusercontent.com/Marfusios/crypto-websocket-extensions/master/cwe_logo.png" height="80px"></a> <br /> <a href="https://github.com/Marfusios/crypto-websocket-extensions">Extensions</a> <br /> <span>All order books together, etc.</span> </td>
<td> <a href="https://github.com/Marfusios/bitmex-client-websocket"><img src="https://user-images.githubusercontent.com/1294454/27766319-f653c6e6-5ed4-11e7-933d-f0bc3699ae8f.jpg"></a> <br /> <a href="https://github.com/Marfusios/bitmex-client-websocket">Bitmex</a> </td>
<td> <a href="https://github.com/Marfusios/binance-client-websocket"><img src="https://user-images.githubusercontent.com/1294454/29604020-d5483cdc-87ee-11e7-94c7-d1a8d9169293.jpg"></a> <br /> <a href="https://github.com/Marfusios/binance-client-websocket">Binance</a> </td>
<td> <a href="https://github.com/Marfusios/coinbase-client-websocket"><img src="https://user-images.githubusercontent.com/1294454/41764625-63b7ffde-760a-11e8-996d-a6328fa9347a.jpg"></a> <br /> <a href="https://github.com/Marfusios/coinbase-client-websocket">Coinbase</a> </td>
</tr> </table>
Placing orders
Bitfinex supports input authenticated API via websockets. You are able to place, update, cancel orders. Also via multi batch. Usage:
// placing buy
client.Send(new NewOrderRequest(gid: 33, cid: 100, "ETH/USD", OrderType.Limit, 0.2, 163) {Flags = OrderFlag.PostOnly});
// palcing sell
client.Send(new NewOrderRequest(gid: 33, cid: 200, "ETH/USD", OrderType.Limit, -0.2, 188) { Flags = OrderFlag.PostOnly });
// updating by client id
client.Send(new UpdateOrderRequest(new CidPair(100, DateTime.UtcNow)) { Amount = 0.3, Price = 161});
// canceling by client id
client.Send(new CancelOrderRequest(new CidPair(100, DateTime.UtcNow)));
// other canceling options
client.Send(CancelMultiOrderRequest.CancelEverything());
client.Send(CancelMultiOrderRequest.CancelGroup(33));
client.Send(new CancelMultiOrderRequest(new[]
{
new CidPair(100, DateTime.UtcNow),
new CidPair(200, DateTime.UtcNow)
}));
Reconnecting
There is a built-in reconnection which invokes after 1 minute (default) of not receiving any messages from the server. It is possible to configure that timeout via communicator.ReconnectTimeoutMs
. Also, there is a stream ReconnectionHappened
which sends information about a type of reconnection. However, if you are subscribed to low rate channels, it is very likely that you will encounter that timeout - higher the timeout to a few minutes or call PingRequest
by your own every few seconds.
In the case of Bitfinex outage, there is a built-in functionality which slows down reconnection requests (could be configured via communicator.ErrorReconnectTimeoutMs
, the default is 1 minute).
Beware that you need to resubscribe to channels after reconnection happens. You should subscribe to Streams.InfoStream
, Streams.AuthenticationStream
and send subscriptions requests (see #12 for example).
Backtesting
The library is prepared for backtesting. The dependency between Client
and Communicator
is via abstraction IBitfinexCommunicator
. There are two communicator implementations:
BitfinexWebsocketCommunicator
- a realtime communication with Bitfinex via websocket API.BitfinexFileCommunicator
- a simulated communication, raw data are loaded from files and streamed. If you are interested in buying historical raw data (trades, order book events), contact me.
Feel free to implement IBitfinexCommunicator
on your own, for example, load raw data from database, cache, etc.
Usage:
var communicator = new BitfinexFileCommunicator();
communicator.FileNames = new[]
{
"data/bitfinex_raw_2018-11-12.txt"
};
communicator.Delimiter = ";;";
var client = new BitfinexWebsocketClient(communicator);
client.Streams.TradesStream.Subscribe(trade =>
{
// do something with trade
});
await communicator.Start();
Multi-threading
Observables from Reactive Extensions are single threaded by default. It means that your code inside subscriptions is called synchronously and as soon as the message comes from websocket API. It brings a great advantage of not to worry about synchronization, but if your code takes a longer time to execute it will block the receiving method, buffer the messages and may end up losing messages. For that reason consider to handle messages on the other thread and unblock receiving thread as soon as possible. I've prepared a few examples for you:
Default behavior
Every subscription code is called on a main websocket thread. Every subscription is synchronized together. No parallel execution. It will block the receiving thread.
client
.Streams
.TradesStream
.Subscribe(trade => { code1 });
client
.Streams
.BookStream
.Subscribe(book => { code2 });
// 'code1' and 'code2' are called in a correct order, according to websocket flow
// ----- code1 ----- code1 ----- ----- code1
// ----- ----- code2 ----- code2 code2 -----
Parallel subscriptions
Every single subscription code is called on a separate thread. Every single subscription is synchronized, but different subscriptions are called in parallel.
client
.Streams
.TradesStream
.ObserveOn(TaskPoolScheduler.Default)
.Subscribe(trade => { code1 });
client
.Streams
.BookStream
.ObserveOn(TaskPoolScheduler.Default)
.Subscribe(book => { code2 });
// 'code1' and 'code2' are called in parallel, do not follow websocket flow
// ----- code1 ----- code1 ----- code1 -----
// ----- code2 code2 ----- code2 code2 code2
Parallel subscriptions with synchronization
In case you want to run your subscription code on the separate thread but still want to follow websocket flow through every subscription, use synchronization with gates:
private static readonly object GATE1 = new object();
client
.Streams
.TradesStream
.ObserveOn(TaskPoolScheduler.Default)
.Synchronize(GATE1)
.Subscribe(trade => { code1 });
client
.Streams
.BookStream
.ObserveOn(TaskPoolScheduler.Default)
.Synchronize(GATE1)
.Subscribe(book => { code2 });
// 'code1' and 'code2' are called concurrently and follow websocket flow
// ----- code1 ----- code1 ----- ----- code1
// ----- ----- code2 ----- code2 code2 ----
Async/Await integration
Using async/await
in your subscribe methods is a bit tricky. Subscribe from Rx.NET doesn't await
tasks,
so it won't block stream execution and cause sometimes undesired concurrency. For example:
client
.Streams
.TradesStream
.Subscribe(async trade => {
// do smth 1
await Task.Delay(5000); // waits 5 sec, could be HTTP call or something else
// do smth 2
});
That await Task.Delay
won't block stream and subscribe method will be called multiple times concurrently.
If you want to buffer messages and process them one-by-one, then use this:
client
.Streams
.TradesStream
.Select(trade => Observable.FromAsync(async () => {
// do smth 1
await Task.Delay(5000); // waits 5 sec, could be HTTP call or something else
// do smth 2
}))
.Concat() // executes sequentially
.Subscribe();
If you want to process them concurrently (avoid synchronization), then use this
client
.Streams
.TradesStream
.Select(trade => Observable.FromAsync(async () => {
// do smth 1
await Task.Delay(5000); // waits 5 sec, could be HTTP call or something else
// do smth 2
}))
.Merge() // executes concurrently
// .Merge(4) you can limit concurrency with a parameter
// .Merge(1) is same as .Concat()
// .Merge(0) is invalid (throws exception)
.Subscribe();
More info on Github issue.
Don't worry about websocket connection, those sequential execution via .Concat()
or .Merge(1)
has no effect on receiving messages.
It won't affect receiving thread, only buffers messages inside TradesStream
.
But beware of producer-consumer problem when the consumer will be too slow. Here is a StackOverflow issue with an example how to ignore/discard buffered messages and always process only the last one.
Desktop application (WinForms or WPF)
Due to the large amount of questions about integration of this library into a desktop application (old full .NET Framework), I've prepared WinForms example (link).
Available for help
I do consulting, please don't hesitate to contact me if you have a custom solution you would like me to implement (web, m@mkotas.cz)
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Newtonsoft.Json (>= 13.0.3)
- System.Reactive (>= 6.0.0)
- Websocket.Client (>= 5.1.1)
-
net6.0
- Newtonsoft.Json (>= 13.0.3)
- System.Reactive (>= 6.0.0)
- Websocket.Client (>= 5.1.1)
-
net7.0
- Newtonsoft.Json (>= 13.0.3)
- System.Reactive (>= 6.0.0)
- Websocket.Client (>= 5.1.1)
-
net8.0
- Newtonsoft.Json (>= 13.0.3)
- System.Reactive (>= 6.0.0)
- Websocket.Client (>= 5.1.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Bitfinex.Client.Websocket:
Package | Downloads |
---|---|
Crypto.Websocket.Extensions
Extensions to cryptocurrency websocket clients |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
4.2.0 | 759 | 2/16/2024 |
4.1.116 | 2,150 | 11/20/2021 |
4.1.114 | 696 | 2/23/2021 |
4.1.112 | 419 | 2/19/2021 |
4.1.110 | 8,427 | 8/7/2020 |
4.1.109 | 1,203 | 7/2/2020 |
4.1.108 | 1,217 | 5/4/2020 |
4.1.107 | 555 | 4/16/2020 |
4.1.106 | 532 | 3/24/2020 |
4.1.105 | 530 | 3/24/2020 |
4.1.1 | 134 | 2/16/2024 |
4.1.0 | 121 | 2/16/2024 |
4.0.102 | 2,288 | 1/24/2020 |
4.0.101 | 2,467 | 12/18/2019 |
4.0.100 | 1,218 | 12/10/2019 |
4.0.99 | 1,819 | 12/6/2019 |
3.0.98 | 3,242 | 10/8/2019 |
3.0.97 | 3,427 | 8/5/2019 |
3.0.95 | 1,674 | 8/4/2019 |
3.0.94 | 634 | 8/2/2019 |
3.0.92 | 636 | 7/12/2019 |
3.0.91 | 4,497 | 5/29/2019 |
3.0.89 | 781 | 4/23/2019 |
3.0.88 | 702 | 4/23/2019 |
3.0.87 | 747 | 4/23/2019 |
3.0.86 | 816 | 4/23/2019 |
3.0.84 | 690 | 4/23/2019 |
3.0.82 | 682 | 4/19/2019 |
2.0.78 | 1,721 | 4/7/2019 |
2.0.76 | 4,663 | 3/29/2019 |
2.0.73 | 648 | 3/20/2019 |
2.0.72 | 679 | 3/15/2019 |
2.0.70 | 740 | 2/4/2019 |
1.0.69 | 745 | 2/3/2019 |
1.0.68 | 784 | 1/28/2019 |
1.0.67 | 731 | 1/28/2019 |
1.0.66 | 717 | 1/28/2019 |
1.0.65 | 758 | 1/28/2019 |
1.0.64 | 743 | 1/27/2019 |
1.0.63 | 719 | 1/27/2019 |
1.0.62 | 713 | 1/27/2019 |
1.0.61 | 720 | 1/27/2019 |
1.0.60 | 730 | 1/27/2019 |
1.0.59 | 730 | 1/27/2019 |
1.0.58 | 750 | 1/26/2019 |
1.0.57 | 709 | 1/26/2019 |
1.0.56 | 769 | 1/25/2019 |
1.0.55 | 726 | 1/25/2019 |
1.0.54 | 735 | 1/25/2019 |
1.0.52 | 796 | 1/6/2019 |
1.0.48 | 805 | 12/10/2018 |
1.0.47 | 867 | 12/7/2018 |
1.0.46 | 771 | 12/7/2018 |
1.0.45 | 763 | 12/7/2018 |
1.0.44 | 781 | 11/29/2018 |
1.0.43 | 808 | 11/13/2018 |
1.0.42 | 861 | 11/13/2018 |
1.0.41 | 851 | 11/13/2018 |
1.0.40 | 884 | 10/26/2018 |
1.0.39 | 895 | 10/22/2018 |
1.0.37 | 880 | 10/16/2018 |
1.0.36 | 1,089 | 8/13/2018 |
1.0.33 | 1,094 | 7/9/2018 |
1.0.32 | 1,044 | 7/9/2018 |
1.0.31 | 1,063 | 7/2/2018 |
1.0.30 | 1,013 | 6/29/2018 |
1.0.29 | 930 | 6/29/2018 |
1.0.28 | 1,041 | 6/29/2018 |
1.0.27 | 1,310 | 5/17/2018 |
1.0.26 | 1,174 | 5/14/2018 |
1.0.25 | 1,128 | 5/3/2018 |
1.0.24 | 1,132 | 5/1/2018 |
1.0.23 | 1,107 | 5/1/2018 |
0.1.2 | 1,184 | 3/21/2018 |
0.1.1 | 1,309 | 1/22/2018 |
0.1.0 | 1,087 | 1/21/2018 |
0.0.9 | 1,118 | 1/18/2018 |
0.0.8 | 1,098 | 1/18/2018 |
0.0.7 | 1,071 | 1/18/2018 |
0.0.6 | 1,218 | 1/10/2018 |
0.0.5 | 1,152 | 1/9/2018 |
0.0.4 | 1,127 | 1/9/2018 |
0.0.3 | 1,080 | 1/9/2018 |
0.0.2 | 1,101 | 1/9/2018 |
0.0.1 | 1,321 | 1/8/2018 |
Enhancements