DownloadBitmexData

Defined in dccd.continuous_dl.bitmex

class DownloadBitmexData(time_step=60, until=3600)[source]

Bases: ContinuousDownloader

Basis object to download data from a stream websocket client API.

Parameters:
time_stepint or None, optional

Number of seconds between two snapshots of data, minimum is 1, default is 60 (one minute). Each time_step seconds data will be processed and pushed to the database. Pass None to receive data tick-by-tick without periodic aggregation.

untilint, optional

Number of seconds before stopping, or a future Unix timestamp at which to stop. Default is 3600 (one hour).

Attributes:
hoststr

Address of host to connect.

conn_pardict

Parameters of websocket connection.

wswebsockets.client.WebSocketClientProtocol

Connection with the websocket client.

is_connectbool

True if is connected, False otherwise.

tsint

Number of second between two snapshots of data.

tint

Current timestamp but rounded by ts.

untilint

Timestamp to stop to download data.

Methods

set_process_data(func, **kwargs)

Set processing function.

set_saver(call, **kwargs)

Set saver object to save data or update a database.

__call__(*args)

Open a websocket connection and save/update the database.

__call__(*args)[source]

Open a websocket connection and save/update the database.

Run asynchronously two loops to get data from Bitmex websocket and save/update the database.

Parameters:
*argsstr

Positional arguments joined with ':' and passed as the args subscribe parameter. The first element should be the channel name (e.g. 'orderBookL2_25' or 'trade') followed by any instrument symbol (e.g. 'XBTUSD').

Warning

‘_raw’ option not yet working for Bitmex.

References

get_parser(key)

Get allowed data parser.

Parameters:
keystr

Name code of data to parse.

Returns:
function

The allowed function to parse this kind of data.

Raises:
KeyError

If key is not in the allowed parser keys.

on_close()

On websocket close print and fire event.

async on_error(error, *args)

On websocket error print and fire event.

async on_message(data)[source]

Route an incoming websocket message to the appropriate parser.

on_open(**kwargs)

On websocket open.

Parameters:
**kwargs

Any relevant keyword arguments to set connection.

parser_book(data)[source]

Parse and maintain a local copy of the order book.

Handles partial (snapshot), insert, update, and delete actions from the Bitmex WebSocket feed.

Parameters:
datadict

Order book message from the WebSocket API. Must contain 'action' and 'data' keys.

parser_trades(data)[source]

Parse trade data and accumulate records for the current timestep.

Parameters:
datadict

Trade message from the WebSocket API. Must contain a 'data' key with a list of trade records.

set_book_saver(saver, process_func=<function set_marketdepth>, **kwargs)

Set saver for the order-book channel.

Parameters:
savercallable

Callable to persist the processed DataFrame (e.g. IODataBase).

process_funccallable, optional

Function to convert the book dict to a DataFrame, default is dccd.process_data.set_marketdepth.

**kwargs

Extra keyword arguments forwarded to saver on each call.

set_process_data(func, **kwargs)

Set processing function.

Parameters:
funccallable

Function to process and clean data before to be saved. Must take data in arguments and can take any optional keywords arguments, cf exemples in dccd.process_data.

**kwargs

Any keyword arguments to be passed to func.

set_saver(call, **kwargs)

Set saver object to save data or update a database.

Parameters:
callcallable

Callable object to save data or update a database. Must take data in arguments and can take any optional keywords arguments, cf exemples in dccd.io_tools.

**kwargs

Any keyword arguments to be passed to call.

set_trades_saver(saver, process_func=<function set_trades>, **kwargs)

Set saver for the trades channel.

Parameters:
savercallable

Callable to persist the processed DataFrame (e.g. IODataBase).

process_funccallable, optional

Function to convert raw trade list to a DataFrame, default is dccd.process_data.set_trades.

**kwargs

Extra keyword arguments forwarded to saver on each call.

async wait_that(is_true)

Wait before running.