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.

Features

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

Installation

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():
        print(message)

trio.run(main)

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.

API

bitmex__trio__websocket.BitMEXWebsocket

constructor

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.

storage

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.

ws

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

See - https://trio-websocket.readthedocs.io/en/stable/api.html#connections

Credits

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.

History

0.2.5 (2019-11-25)

  • Documentation fixes.

0.2.4 (2019-11-24)

  • Added documentation.

0.2.1 (2019-11-24)

  • First release on PyPI.

Project details


Download files

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

Source Distribution

bitmex-trio-websocket-0.2.5.tar.gz (14.2 kB view hashes)

Uploaded Source

Built Distribution

bitmex_trio_websocket-0.2.5-py2.py3-none-any.whl (8.8 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page