Futures Websockets
- class kraken.futures.FuturesWSClient(key: str = '', secret: str = '', url: str = '', callback: Callable | None = None, *, sandbox: bool = False)
Bases:
FuturesAsyncClientClass 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 client1import asyncio 2from kraken.futures import FuturesWSClient 3 4# Create the custom client 5class Client(FuturesWSClient): 6 async def on_message(self, event: dict) -> None: 7 print(event) 8 9async def main() -> None: 10 client = Client() # unauthenticated 11 auth_client = Client( # authenticated 12 key="api-key", 13 secret="secret-key" 14 ) 15 16 # open the websocket connections 17 await client.start() 18 await auth_client.start() 19 20 # now you can subscribe to channels using 21 await client.subscribe( 22 feed='ticker', 23 products=["XBTUSD", "DOT/EUR"] 24 ) 25 # the messages can be used within the `on_message` callback method 26 27 while True: 28 await asyncio.sleep(6) 29 30if __name__ == "__main__": 31 try: 32 asyncio.run(main()) 33 except KeyboardInterrupt: 34 pass
Futures Websocket: Create the websocket client as context manager1import asyncio 2from kraken.futures import FuturesWSClient 3 4async def on_message(message): 5 print(message) 6 7async def main() -> None: 8 async with FuturesWSClient(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 try: 16 asyncio.run(main()) 17 except KeyboardInterrupt: 18 pass
- async_close() None
Closes the aiohttp session
- async close() None
Method to stop the websocket connection.
- property exception_occur: bool
Returns True if the connection was stopped due to an exception.
- 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.FuturesWSClientto run the following example:Futures Websocket: Get the active subscriptions1>>> from kraken.futures import FuturesWSClient 2... 3>>> FuturesWSClient.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 feeds1>>> from kraken.futures import FuturesWSClient 2>>> FuturesWSClient.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 feeds1>>> from kraken.futures import FuturesWSClient 2>>> FuturesWSClient.get_available_private_subscription_feeds() 3[ 4 "trade", "book", "ticker", 5 "ticker_lite", "heartbeat" 6]
- get_nonce() str
Return a new nonce
- 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:
Trueif the credentials are set, elseFalse- Return type:
bool
Futures Websocket: Check if the credentials are set1>>> from kraken.futures import FuturesWSClient 2>>> FuturesWSClient().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.FuturesWSClient.- Parameters:
message (dict) – The message that was send by Kraken via the websocket connection.
- Return type:
None
- async request(method: str, uri: str, post_params: dict | None = None, query_params: dict | None = None, timeout: int = 10, *, auth: bool = True, return_raw: bool = False) dict | list | aiohttp.ClientResponse | Awaitable
Handles the requested requests, by sending the request, handling the response, and returning the message or in case of an error the respective Exception.
- Parameters:
method (str) – The request method, e.g.,
GET,POST, andPUTuri (str) – The endpoint to send the message
post_params (dict, optional) – The query parameter of the request (default:
None)extra_params (str | dict, optional) – Additional query parameter of the request (default:
None)query_params (dict, optional) – The query parameter of the request (default:
None)timeout (int) – Timeout for the request (default:
10)auth (bool) – If the request needs authentication (default:
True)return_raw (bool, optional) – If the response should be returned without parsing. This is used for example when requesting an export of the trade history as .zip archive.
- Raises:
kraken.exceptions.* – If the response contains errors
- Returns:
The response
- Return type:
dict | list | requests.Response
- async start() None
Method to start the websocket connection.
- stop() None
Method to stop the websocket connection.
- 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.FuturesWSClientto run the following example:Futures Websocket: Subscribe to a feed1>>> 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.FuturesWSClient.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.FuturesWSClientto run the following example:Futures Websocket: Unsubscribe from a feed1>>> 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.FuturesWSClient.on_message`()or a custom callback function.