Skip to main content

Websocket implementation for BitMEX cryptocurrency derivatives exchange.

Project description

BitMEX Trio-Websocket

PyPI Build Status Read the Docs

Websocket implementation for BitMEX cryptocurrency derivatives exchange.


  • Connects to BitMEX websockets for a given symbol or lists of symbols.
  • Supports authenticated connections using api keys.
  • Fully async using async generators. No callbacks or event emitters.
  • Based on trio and trio-websocket.


This library requires Python 3.7 or greater. It could probably be made to run with Python 3.6, since this is the minimal version where async generators are supported. To install from PyPI:

pip install bitmex-trio-websocket

Client example

import trio

from bitmex_trio_websocket import BitMEXWebsocket

async def main():
    bws = BitMEXWebsocket('mainnet', 'XBTUSD')
    async for message in bws.start():

This will print a sequence of tuples of the form (item, symbol|None, table, action), where -

item is the full object resulting from inserting or merging the changes to an item.

symbol is the symbol that was changed or None if the table isn't a symbol table.

table is the table name.

action is the action that was taken.

Note, that delete actions are simply applied and consumed, with no output sent.




Creates a new websocket object.

endpoint str

Network to connect to. Options: 'mainnet', 'testnet'.

symbol Optional[Union[str, Iterable[str]]]

Symbols to subscribe to. Each symbol is subscribed to the following channels: ['instrument', 'quote', 'trade', 'tradeBin1m']. If not provided, no instrument channels are subscribed for this connection. This may be useful if you only want to connect to authenticated channels.

api_key Optional[str]

Api key for authenticated connections. If a valid api key and secret is supplied, the following channels are subscribed: ['margin', 'position', 'order', 'execution'].

api_secret Optional[str]

Api secret for authenticated connections.

await start

Returns an async generator object that yields messages from the websocket.


This attribute contains the storage object for the websocket. The storage object has two properties data and keys. data contains the table state for each channel as a dictionary with the table name as key. The tables themselves are flat lists. keys contains a list of keys by which to look up values in each table. There is a helper function findItemByKeys in the storage unit, which assists in finding particular items in each table. Tables are searched sequentially until a match is found, with is somewhat inefficient. However since there is never a lot of records in each table (at most 200), this is reasonably fast in practice and not a bottleneck.


When connected, contains the underlying trio-websocket object. Can be used to manage the connection.

See -


Thanks to the Trio and Trio-websocket libraries for their awesome work.

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for bitmex-trio-websocket, version 0.2.8
Filename, size File type Python version Upload date Hashes
Filename, size bitmex_trio_websocket-0.2.8-py3-none-any.whl (8.2 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size bitmex-trio-websocket-0.2.8.tar.gz (7.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page