Bitmex.Client.Websocket
3.2.1
dotnet add package Bitmex.Client.Websocket --version 3.2.1
NuGet\Install-Package Bitmex.Client.Websocket -Version 3.2.1
<PackageReference Include="Bitmex.Client.Websocket" Version="3.2.1" />
paket add Bitmex.Client.Websocket --version 3.2.1
#r "nuget: Bitmex.Client.Websocket, 3.2.1"
// Install Bitmex.Client.Websocket as a Cake Addin #addin nuget:?package=Bitmex.Client.Websocket&version=3.2.1 // Install Bitmex.Client.Websocket as a Cake Tool #tool nuget:?package=Bitmex.Client.Websocket&version=3.2.1
Bitmex websocket API client
This is a C# implementation of the Bitmex websocket API found here:
https://www.bitmex.com/app/wsAPI
License:
Apache License 2.0
Features
- installation via NuGet (Bitmex.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 = BitmexValues.ApiWebsocketUrl;
using (var communicator = new BitmexWebsocketCommunicator(url))
{
using (var client = new BitmexWebsocketClient(communicator))
{
client.Streams.InfoStream.Subscribe(info =>
{
Console.WriteLine($"Info received, reconnection happened.");
client.Send(new PingRequest()).Wait();
});
client.Streams.PongStream.Subscribe(pong =>
{
Console.WriteLine($"Pong received!");
exitEvent.Set();
});
await communicator.Start();
exitEvent.WaitOne(TimeSpan.FromSeconds(30));
}
}
More usage examples:
API coverage
PUBLIC | Covered |
---|---|
Info | ✔ |
Ping-Pong | ✔ |
Errors | ✔ |
Subscribe | ✔ |
Unsubscribe | ✔ |
Announcement | |
Chat | |
Connected | |
Funding | ✔ |
Instrument | ✔ |
Insurance | |
Liquidation | ✔ |
Orderbook L2 | ✔ |
Orderbook L2 (25) | ✔ |
Orderbook snapshot (10) | |
Public notifications | |
Quote | ✔ |
Quote bin 1m | |
Quote bin 5m | |
Quote bin 1h | |
Quote bin 1d | |
Settlement | |
Trade | ✔ |
Trade bin 1m | ✔ |
Trade bin 5m | ✔ |
Trade bin 1h | ✔ |
Trade bin 1d | ✔ |
AUTHENTICATED | Covered |
---|---|
Affilate | |
Execution | ✔ |
Order | ✔ |
Margin | ✔ |
Position | ✔ |
Private notifications | |
Transact | |
Wallet | ✔ |
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/bitfinex-client-websocket"><img src="https://user-images.githubusercontent.com/1294454/27766244-e328a50c-5ed2-11e7-947b-041416579bb3.jpg"></a> <br /> <a href="https://github.com/Marfusios/bitfinex-client-websocket">Bitfinex</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>
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 Bitmex 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 IBitmexCommunicator
. There are two communicator implementations:
BitmexWebsocketCommunicator
- a realtime communication with Bitmex via websocket API.BitmexFileCommunicator
- 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 IBitmexCommunicator
on your own, for example, load raw data from database, cache, etc.
Usage:
var communicator = new BitmexFileCommunicator();
communicator.FileNames = new[]
{
"data/bitmex_raw_xbtusd_2018-11-13.txt"
};
communicator.Delimiter = ";;";
var client = new BitmexWebsocketClient(communicator);
client.Streams.TradesStream.Subscribe(response =>
{
// 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
- Utf8Json (>= 1.3.7)
- Websocket.Client (>= 5.1.1)
-
net6.0
- Utf8Json (>= 1.3.7)
- Websocket.Client (>= 5.1.1)
-
net7.0
- Utf8Json (>= 1.3.7)
- Websocket.Client (>= 5.1.1)
-
net8.0
- Utf8Json (>= 1.3.7)
- Websocket.Client (>= 5.1.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Bitmex.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 |
---|---|---|
3.2.1 | 958 | 2/16/2024 |
3.2.0 | 210 | 2/16/2024 |
3.1.82 | 2,838 | 11/20/2021 |
3.1.78 | 7,237 | 9/9/2020 |
3.1.77 | 5,070 | 7/2/2020 |
3.1.76 | 2,235 | 5/4/2020 |
3.1.75 | 774 | 4/26/2020 |
3.1.74 | 4,179 | 1/24/2020 |
3.1.73 | 1,428 | 1/17/2020 |
3.1.72 | 766 | 1/17/2020 |
3.0.71 | 2,299 | 12/18/2019 |
3.0.70 | 1,466 | 12/10/2019 |
3.0.69 | 1,799 | 12/6/2019 |
2.0.68 | 4,128 | 10/8/2019 |
2.0.66 | 2,980 | 8/22/2019 |
2.0.65 | 774 | 8/22/2019 |
2.0.63 | 3,500 | 8/4/2019 |
2.0.62 | 3,254 | 7/12/2019 |
2.0.59 | 4,559 | 4/23/2019 |
2.0.58 | 843 | 4/18/2019 |
2.0.57 | 2,120 | 4/7/2019 |
2.0.56 | 4,905 | 4/4/2019 |
2.0.54 | 871 | 3/20/2019 |
2.0.53 | 851 | 3/15/2019 |
2.0.52 | 1,425 | 2/12/2019 |
2.0.51 | 920 | 2/5/2019 |
2.0.49 | 909 | 2/4/2019 |
1.0.48 | 936 | 2/3/2019 |
1.0.47 | 900 | 2/3/2019 |
1.0.45 | 1,178 | 1/19/2019 |
1.0.43 | 1,127 | 1/12/2019 |
1.0.42 | 972 | 1/11/2019 |
1.0.41 | 906 | 1/11/2019 |
1.0.39 | 968 | 1/4/2019 |
1.0.38 | 1,106 | 12/10/2018 |
1.0.37 | 967 | 12/10/2018 |
1.0.36 | 935 | 12/9/2018 |
1.0.35 | 961 | 12/7/2018 |
1.0.34 | 997 | 12/7/2018 |
1.0.33 | 1,001 | 12/7/2018 |
1.0.32 | 1,007 | 11/29/2018 |
1.0.31 | 1,034 | 11/15/2018 |
1.0.30 | 951 | 11/13/2018 |
1.0.29 | 968 | 11/13/2018 |
1.0.28 | 1,027 | 10/26/2018 |
1.0.27 | 978 | 10/22/2018 |
1.0.26 | 1,021 | 10/17/2018 |
1.0.25 | 1,323 | 8/20/2018 |
1.0.24 | 1,155 | 8/20/2018 |
1.0.22 | 1,232 | 7/9/2018 |
1.0.19 | 1,247 | 7/2/2018 |
1.0.18 | 1,297 | 6/29/2018 |
1.0.17 | 1,109 | 6/18/2018 |
1.0.15 | 1,165 | 6/18/2018 |
1.0.14 | 1,216 | 6/12/2018 |
1.0.11 | 1,336 | 5/25/2018 |
1.0.9 | 1,223 | 5/25/2018 |
1.0.8 | 1,284 | 5/17/2018 |
1.0.7 | 1,354 | 5/14/2018 |
1.0.6 | 1,274 | 5/7/2018 |
1.0.5 | 1,360 | 5/4/2018 |
1.0.4 | 1,320 | 5/3/2018 |
1.0.3 | 1,382 | 5/3/2018 |
1.0.2 | 1,239 | 5/2/2018 |
1.0.1 | 1,298 | 5/1/2018 |
Enhancements