All Projects β†’ Marfusios β†’ bitmex-client-websocket

Marfusios / bitmex-client-websocket

Licence: Apache-2.0 license
πŸ› οΈ C# client for Bitmex websocket API

Programming Languages

C#
18002 projects
HCL
1544 projects

Projects that are alternatives of or similar to bitmex-client-websocket

Coinbase Pro Node
Coinbase Pro API written in TypeScript and covered by tests.
Stars: ✭ 116 (+93.33%)
Mutual labels:  api-client, exchange
Coinapi Sdk
SDKs for CoinAPI
Stars: ✭ 238 (+296.67%)
Mutual labels:  api-client, exchange
Krypto Trading Bot
Self-hosted crypto trading bot (automated high frequency market making) written in C++
Stars: ✭ 2,589 (+4215%)
Mutual labels:  exchange, bitmex
bitmex-orderbook
The fastest order book implementation for the BitMEX WebSocket API.
Stars: ✭ 73 (+21.67%)
Mutual labels:  exchange, bitmex
Cryptofeed
Cryptocurrency Exchange Websocket Data Feed Handler
Stars: ✭ 643 (+971.67%)
Mutual labels:  websockets, exchange
intrinio-realtime-python-sdk
Intrinio Python SDK for Real-Time Stock Prices
Stars: ✭ 79 (+31.67%)
Mutual labels:  websockets, exchange
Pykrakenapi
A python implementation of the Kraken API.
Stars: ✭ 124 (+106.67%)
Mutual labels:  api-client, exchange
binance-client-websocket
πŸ› οΈ C# client for Binance websocket API
Stars: ✭ 41 (-31.67%)
Mutual labels:  api-client, exchange
gemini-python
A python client for the Gemini API and Websocket
Stars: ✭ 71 (+18.33%)
Mutual labels:  websockets, exchange
crypto-websocket-extensions
🧰 Unified and optimized data structures across cryptocurrency exchanges
Stars: ✭ 31 (-48.33%)
Mutual labels:  websockets, exchange
Socktrader
πŸš€ Websocket based trading bot for πŸ’°cryptocurrencies πŸ“ˆ
Stars: ✭ 152 (+153.33%)
Mutual labels:  websockets, exchange
bot-trader
Simple bot trader for Bitmex
Stars: ✭ 14 (-76.67%)
Mutual labels:  exchange, bitmex
goco
Connecting to Google API has never been easier!
Stars: ✭ 14 (-76.67%)
Mutual labels:  api-client
docs
The official soketi documentation. πŸ“‘
Stars: ✭ 55 (-8.33%)
Mutual labels:  websockets
digital-ocean
Idiomatic Clojure client for Digital Ocean that makes it easy to boot virtual servers from your REPL
Stars: ✭ 34 (-43.33%)
Mutual labels:  api-client
spring-websocket-angular6
Example for using Spring Websocket and Angular with Stomp Messaging
Stars: ✭ 18 (-70%)
Mutual labels:  websockets
MoneroMixer
The easiest way to use Monero to anonymously exchange and properly mix XMR, BTC, LTC, ETH, BCH, & 100+ other coins on Tails OS or Whonix.
Stars: ✭ 57 (-5%)
Mutual labels:  exchange
channeled-dashboard
Repository for the talk `Building real time applications with Django and Channels`
Stars: ✭ 20 (-66.67%)
Mutual labels:  websockets
AmigoChat-Realtime-Chat-Application
AmigoChat is a responsive real-time chat application built on MERN Stack and Socket io.
Stars: ✭ 22 (-63.33%)
Mutual labels:  websockets
nomisr
Access UK official statistics from the Nomis database through R.
Stars: ✭ 30 (-50%)
Mutual labels:  api-client

Logo

Bitmex websocket API client Build Status NuGet version Nuget download

This is a C# implementation of the Bitmex websocket API found here:

https://www.bitmex.com/app/wsAPI

Releases and breaking changes

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:

  • integration tests (link)
  • console sample (link)
  • desktop sample (link)

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


Extensions
All order books together, etc.

Bitfinex

Binance

Coinbase

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).

WinForms example screen

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, [email protected])

Donations gratefully accepted.

  • Donate with Bitcoin
  • Donate with Litecoin
  • Donate with Ethereum
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].