WebSocket Client¶
Async WebSocket connection manager for Kraken WS v2.
Classes:
-
TradingError–Raised when a trading method request is rejected by the server.
-
KrakenWSClient–Async WebSocket client for Kraken WS v2.
TradingError
¶
Bases: Exception
Raised when a trading method request is rejected by the server.
KrakenWSClient
¶
Async WebSocket client for Kraken WS v2.
Manages a single WebSocket connection with heartbeat monitoring, client-initiated pings, automatic reconnection, and system status tracking.
Usage::
async with KrakenWSClient() as client:
msg = await client.receive()
Parameters:
-
url(str, default:'wss://ws.kraken.com/v2') –WebSocket endpoint URL.
-
ping_interval(float, default:10.0) –Seconds between client pings.
-
ping_timeout(float, default:10.0) –Max seconds to wait for pong response.
-
heartbeat_timeout(float, default:30.0) –Max silence before triggering reconnect.
-
backoff_base(float, default:0.5) –Base delay for exponential backoff.
-
backoff_max(float, default:30.0) –Maximum backoff delay.
-
max_reconnect_attempts(int, default:10) –Max reconnect retries (0 = disabled).
Methods:
-
connect–Open the WebSocket connection and start background tasks.
-
disconnect–Cleanly shut down the connection and background tasks.
-
send–Serialize and send a request message.
-
receive–Get the next typed message from the queue.
-
subscribe–Subscribe to a channel.
-
unsubscribe–Unsubscribe from a channel.
-
add_order–Place a single order.
-
amend_order–Amend an order in-place (preserves queue priority).
-
edit_order–Edit an order (cancel-and-replace, new order_id).
-
cancel_order–Cancel one or more orders.
-
cancel_all–Cancel all open orders.
-
cancel_all_orders_after–Set/refresh/disable the dead man's switch.
-
batch_add–Place 2-15 orders on a single pair.
-
batch_cancel–Cancel 2-50 orders.
-
__aiter__–Allow
async for msg in client:iteration. -
__anext__–Yield the next message, stopping when disconnected and drained.
Attributes:
-
state(ConnectionState) –Current connection state (read-only).
-
system_status(str | None) –Latest system status from the status channel.
-
connection_id(int | None) –Connection ID from the status channel.
-
book_manager(OrderBookManager) –Access the order book manager for reading book state.
-
subscriptions(dict[tuple[tuple[str, Any], ...], SubscriptionEntry]) –Currently tracked subscriptions (read-only copy).
connect
async
¶
Open the WebSocket connection and start background tasks.
Raises:
-
RuntimeError–If already connected or connecting.
send
async
¶
Serialize and send a request message.
Parameters:
-
msg(WSRequest) –The request envelope to send.
Raises:
-
RuntimeError–If not connected.
receive
async
¶
Get the next typed message from the queue.
Blocks until a message is available.
subscribe
async
¶
Subscribe to a channel.
Parameters:
-
params(SubscriptionParams) –Typed subscription parameters.
Returns:
-
WSResponse–The server's success response.
Raises:
-
SubscriptionError–If the server rejects the subscription.
-
TimeoutError–If no response within request_timeout.
-
RuntimeError–If not connected.
unsubscribe
async
¶
Unsubscribe from a channel.
Parameters:
-
params(SubscriptionParams) –Typed subscription parameters (must match a tracked sub).
Returns:
-
WSResponse–The server's success response.
Raises:
-
KeyError–If no matching subscription is tracked.
-
SubscriptionError–If the server rejects the unsubscribe.
-
TimeoutError–If no response within request_timeout.
-
RuntimeError–If not connected.
add_order
async
¶
amend_order
async
¶
Amend an order in-place (preserves queue priority).
Raises:
-
TradingError–If the server rejects the amendment.
edit_order
async
¶
Edit an order (cancel-and-replace, new order_id).
Raises:
-
TradingError–If the server rejects the edit.
cancel_order
async
¶
cancel_all
async
¶
cancel_all_orders_after
async
¶
Set/refresh/disable the dead man's switch.
Send timeout > 0 to set or refresh the timer. Send timeout = 0 to disable it.
Raises:
-
TradingError–If the server rejects the request.