Futures Websockets
- class kraken.futures.KrakenFuturesWSClient(key: str = '', secret: str = '', url: str = '', callback: Any | None = None, sandbox: bool = False)
Bases:
KrakenBaseFuturesAPIClass 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 not implemented yet, please open an issue: 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 KrakenFuturesWSClient 3 4async def main() -> None: 5 6 # Create a custom bot 7 class Bot(KrakenFuturesWSClient): 8 async def on_message(self, event: dict) -> None: 9 print(event) 10 11 bot = Bot() # unauthenticated 12 auth_bot = Bot( # authenticated 13 key="api-key", 14 secret="secret-key" 15 ) 16 17 # now you can subscribe to channels using 18 await bot.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 loop = asyncio.new_event_loop() 29 asyncio.set_event_loop(loop) 30 try: 31 asyncio.run(main()) 32 except KeyboardInterrupt: 33 loop.close() 34 35 36 from kraken.futures import KrakenFuturesWSClient
Futures Websocket: Create the websocket client as context manager1import asyncio 2from kraken.futures import KrakenFuturesWSClient 3 4async def on_message(msg): 5 print(msg) 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.KrakenFuturesWSClientto run the following example:Futures Websocket: Get the active subscriptions1>>> 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 feeds1>>> 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 feeds1>>> from kraken.futures import KrakenFuturesWSClient 2>>> KrakenFuturesWSClient.get_available_private_subscription_feeds() 3[ 4 "trade", "book", "ticker", 5 "ticker_lite", "heartbeat" 6]
- 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 KrakenFuturesWSClient 2>>> KrakenFuturesWSClient().is_auth() 3False
- async on_message(msg: 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:
msg (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:
ValueError – If the parameters don’t match the requirements set by the Kraken API
Initialize your client as described in
kraken.futures.KrakenFuturesWSClientto 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.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:
ValueError – If the parameters don’t match the requirements set by the Kraken API
Initialize your client as described in
kraken.futures.KrakenFuturesWSClientto 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.KrakenFuturesWSClient.on_message`()or a custom callback function.