pyokx.websocket_handling

This module handles the real-time interaction with the OKX exchange Websocket APIs. It subscribes to various data streams including public, business, and private channels, processes the incoming data, and optionally stores the data in a Redis stream for further analysis or decision-making. The main function okx_websockets_main_run initializes the websocket connections, handles message receipt, and maintains a heartbeat to keep the connections alive.

pyokx.websocket_handling.get_btc_usdt_usd_index_channel_inputs_to_listen_to()[source]
pyokx.websocket_handling.get_instrument_specific_channel_inputs_to_listen_to()[source]
async pyokx.websocket_handling.handle_reports(message_json, async_redis)[source]
async pyokx.websocket_handling.okx_websockets_main_run(input_channel_models: list, apikey: str | None = None, passphrase: str | None = None, secretkey: str | None = None, sandbox_mode: bool = True, redis_store: bool = True)[source]

Initializes and manages websocket connections to the OKX exchange. It subscribes to the specified channels, processes incoming messages, and optionally stores structured data in a Redis stream.

Parameters:
  • input_channel_models (list) – A list of channel input arguments specifying the channels to subscribe.

  • apikey (str, optional) – The API key for accessing private channels.

  • passphrase (str, optional) – The passphrase associated with the API key.

  • secretkey (str, optional) – The secret key for accessing private channels.

  • sandbox_mode (bool, optional) – If True, use the sandbox environment. Defaults to True.

  • redis_store (bool, optional) – If True, store the received messages in a Redis stream. Defaults to True.

Raises:

Exception – If no channels are provided or if an invalid channel is specified.

async pyokx.websocket_handling.test_restart(public_client, business_client, private_client)[source]
async pyokx.websocket_handling.ws_callback(message)[source]

Callback function to handle messages received from the websocket. It processes the raw message, converts it into structured data, and stores it in Redis if enabled.

Parameters:

message (str) – The raw message received from the websocket.