Futures Websockets

class kraken.futures.KrakenFuturesWSClient(key: str = '', secret: str = '', url: str = '', callback: Any | None = None, *, sandbox: bool = False)

Bases: KrakenFuturesBaseAPI

Class to access public and (optional) private/authenticated websocket connection.

So far there are no trade endpoints that can be accessed using the Futures Kraken API. If this has changed and is not implemented yet, please open an issue at https://github.com/btschwertfeger/python-kraken-sdk/issues

Parameters:
  • key (str, optional) – The Kraken Futures API key to access private endpoints

  • secret (str, optional) – The Kraken Futures Secret key to access private endpoints

  • url (str, optional) – Set a custom URL (default: futures.kraken.com/ws/v1)

  • sandbox (bool, optional) – Use the Kraken Futures demo environment (URL will switch to demo-futures.kraken.com/ws/v1, default: False)

Futures Websocket: Create the websocket client
 1import asyncio
 2from kraken.futures import KrakenFuturesWSClient
 3
 4async def main() -> None:
 5
 6    # Create the custom client
 7    class Client(KrakenFuturesWSClient):
 8        async def on_message(self, event: dict) -> None:
 9            print(event)
10
11    client = Client()     # unauthenticated
12    auth_client = Client( # authenticated
13        key="api-key",
14        secret="secret-key"
15    )
16
17    # now you can subscribe to channels using
18    await client.subscribe(
19        feed='ticker',
20        products=["XBTUSD", "DOT/EUR"]
21    )
22    # the messages can be used within the `on_message` callback method
23
24    while True:
25        await asyncio.sleep(6)
26
27if __name__ == "__main__":
28    try:
29        asyncio.run(main())
30    except KeyboardInterrupt:
31        pass
Futures Websocket: Create the websocket client as context manager
 1import asyncio
 2from kraken.futures import KrakenFuturesWSClient
 3
 4async def on_messageessage):
 5    print(message)
 6
 7async def main() -> None:
 8    async with KrakenFuturesWSClient(callback=on_message) as session:
 9        await session.subscribe(feed="ticker", products=["PF_XBTUSD"])
10
11    while True:
12        await asyncio.sleep(6)
13
14if __name__ == "__main__":
15    loop = asyncio.new_event_loop()
16    asyncio.set_event_loop(loop)
17    try:
18        asyncio.run(main())
19    except KeyboardInterrupt:
20        pass
21    finally:
22        loop.close()
get_active_subscriptions() list[dict]

Returns the list of active subscriptions.

Returns:

List of active subscriptions including the feed names, products and additional information.

Return type:

list[dict]

Initialize your client as described in kraken.futures.KrakenFuturesWSClient to run the following example:

Futures Websocket: Get the active subscriptions
 1>>> from kraken.futures import KrakenFuturesWSClient
 2...
 3>>> KrakenFuturesWSClient.get_active_subscriptions()
 4[
 5    {
 6        "event": "subscribe",
 7        "feed": "ticker,
 8        "product_ids": ["PI_XBTUSD"]
 9    }, {
10        "event": "subscribe",
11        "feed": "open_orders,
12    }, ...
13]
static get_available_private_subscription_feeds() list[str]

Return all available private feeds that can be un-/subscribed to/from using the Kraken Futures API

Returns:

List of available private feeds

Return type:

list[str]

Futures Websocket: Get the available private subscription feeds
1>>> from kraken.futures import KrakenFuturesWSClient
2>>> KrakenFuturesWSClient.get_available_private_subscription_feeds()
3[
4    "fills", "open_positions", "open_orders",
5    "open_orders_verbose", "balances",
6    "deposits_withdrawals", "account_balances_and_margins",
7    "account_log", "notifications_auth"
8]
static get_available_public_subscription_feeds() list[str]

Return all available public feeds that can be un-/subscribed using the Kraken Futures API.

Returns:

List of available public feeds

Return type:

list[str]

Futures Websocket: Get the available public subscription feeds
1>>> from kraken.futures import KrakenFuturesWSClient
2>>> KrakenFuturesWSClient.get_available_private_subscription_feeds()
3[
4    "trade", "book", "ticker",
5    "ticker_lite", "heartbeat"
6]
get_sign_challenge(challenge: str) str

Sign the challenge/message using the secret key

Parameters:

challenge (str) – The challenge/message to sign

Returns:

The signed message

Raises:

kraken.exceptions.KrakenAuthenticationError – If the credentials are not valid

Return type:

str

property is_auth: bool

Checks if key and secret are set.

Returns:

True if the credentials are set, else False

Return type:

bool

Futures Websocket: Check if the credentials are set
1>>> from kraken.futures import KrakenFuturesWSClient
2>>> KrakenFuturesWSClient().is_auth()
3False
property key: str

Returns the API key

async on_message(message: dict) None

Method that serves as the default callback function Calls the defined callback function (if defined) or overload this function.

This is the default method which just logs the messages. In production you want to overload this with your custom methods, as shown in the Example of kraken.futures.KrakenFuturesWSClient.

Parameters:

message (dict) – The message that was send by Kraken via the websocket connection.

Return type:

None

async subscribe(feed: str, products: list[str] | None = None) None

Subscribe to a Futures websocket channel/feed. For some feeds authentication is required.

Parameters:
  • feed (str) – The websocket feed/channel to subscribe to

  • products (list[str], optional) – The products/futures contracts to subscribe to

Raises:

TypeError – If the parameters don’t match the requirements set by the Kraken API

Initialize your client as described in kraken.futures.KrakenFuturesWSClient to run the following example:

Futures Websocket: Subscribe to a feed
1>>> await bot.subscribe(feed='ticker', products=["XBTUSD", "DOT/EUR"])

Success or failures are sent over the websocket connection and can be received via the default kraken.futures.KrakenFuturesWSClient.on_message() or a custom callback function.

async unsubscribe(feed: str, products: list[str] | None = None) None

Subscribe to a Futures websocket channel/feed. For some feeds authentication is required.

Parameters:
  • feed (str) – The websocket feed/channel to unsubscribe from

  • products (list[str], optional) – The products/futures contracts to unsubscribe from

Raises:

TypeError – If the parameters don’t match the requirements set by the Kraken API

Initialize your client as described in kraken.futures.KrakenFuturesWSClient to run the following example:

Futures Websocket: Unsubscribe from a feed
1>>> await bot.unsubscribe(feed='ticker', products=["XBTUSD", "DOT/EUR"])

Success or failures are sent over the websocket connection and can be received via the default kraken.futures.KrakenFuturesWSClient.on_message`() or a custom callback function.