Skip to content
DreamingLambos edited this page Jan 14, 2019 · 2 revisions

Welcome to the BitMEXWebsocket Wiki!

In this repository you can see how to connect to the BitMEX Websocket and receive live information about your account and not only.

BitMEX provides a Websocket connection that allows you to have live updates of several aspects of the platform, including:

  • Instruments, like the existing instruments, traded volume, leverage and margin requirements and so forth
  • Quotes, which is the current BID and ASK price for an instrument
  • Order Books, which is the existing public limit orders
  • Trades, the individual executed trades
  • Positions, your current position updates
  • Orders, your current orders updates
  • Many More

To Keep the code simple we are going to separate each stream in a separate project so that you can understand the logic and code without interference between one stream and another.

Websocket VS Rest API

What are the differences between Websocket and Rest API?

  • With Rest APIs you need to submit a request every time you want an updated information, this has a rate limit which you can trigger in case you need quick and frequent updates, with Websocket you subscribe to a stream once and then you automatically received all the updates regarding that stream, real time
  • Rest API allows you to submit operations to BitMEX (submit and order for example) while Websocket is read-only

Shared Code

A portion of the code is repeated in all the projects, this is because the basic functions of connection to the Websocket is the same, what is going to change is the interpretation of the messages received and the update of the relevant objects.

Logic

The basic logic is the same for all the stream, what changes from one stream to another is the interpretation of the messages and update of the related objects and graphical objects

The basic process is:

  1. Connect to the websocket endpoint (url)
  2. Authenticate in case you are going to subscribe to streams that require it (your orders for example)
  3. Subscribe to one a stream (or more than one)
  4. The Websocket client will start receiving messages from the server, the message is passed to an interpreter
  5. Every received message (string) is interpreted by an interpreter and prepared to update a specific object
  6. The elaborated message (object) is sent to the instantiated object so it can update itself with the new information
  7. Once the object is up to date you can display the updated information

While step 7 is mandatory only if you are developing an application with GUI, all the other steps are necessary. Steps 4, 5, 6 and 7 happens in loops while the subscription to the stream is active.

File Logging

Log files are automatically created for each of the projects/executables, the logs can be found in the exe working directory and subfolder Logs.

A log string is added for each message that is received through websocket. While this can be useful in testing and debug it can affect performance when used in production and the stream you subscribe is a heavy one, like an Order Book, Trades and Quotes. You might want to change the code to better calibrate the logging.

Follow Us

Web

Reddit

TradingView

Twitter

YouTube