Spot Websocket

The examples presented below serve to demonstrate the usage of the Spot websocket clients provided by python-kraken-sdk to access Kraken’s Websocket API v2.

For questions, feedback, additions, suggestions for improvement or problems python-kraken-sdk/discussions or python-kraken-sdk/issues may be helpful.

Example access and usage for Kraken Spot Websocket API
  1# !/usr/bin/env python3
  2# -*- mode: python; coding: utf-8 -*-
  3#
  4# Copyright (C) 2023 Benjamin Thomas Schwertfeger
  5# All rights reserved.
  6# https://github.com/btschwertfeger
  7#
  8
  9"""
 10Module that provides an example usage for the KrakenSpotWebsocketClient.
 11It uses the Kraken Websocket API v2.
 12"""
 13
 14from __future__ import annotations
 15
 16import asyncio
 17import logging
 18import os
 19
 20from kraken.spot import SpotWSClient
 21
 22logging.basicConfig(
 23    format="%(asctime)s %(module)s,line: %(lineno)d %(levelname)8s | %(message)s",
 24    datefmt="%Y/%m/%d %H:%M:%S",
 25    level=logging.INFO,
 26)
 27logging.getLogger("requests").setLevel(logging.WARNING)
 28logging.getLogger("urllib3").setLevel(logging.WARNING)
 29
 30clients = []
 31
 32
 33class Client(SpotWSClient):
 34    """Can be used to create a custom trading strategy"""
 35
 36    async def on_message(self: Client, message: dict) -> None:
 37        """Receives the websocket messages"""
 38        if message.get("method") == "pong" or message.get("channel") == "heartbeat":
 39            return
 40
 41        print(message)
 42        # now you can access lots of methods, for example to create an order:
 43        # if self._is_auth:  # only if the client is authenticated …
 44        #     await self.send_message(
 45        #         message={
 46        #             "method": "add_order",
 47        #             "params": {
 48        #                 "limit_price": 1234.56,
 49        #                 "order_type": "limit",
 50        #                 "order_userref": 123456789,
 51        #                 "order_qty": 1.0,
 52        #                 "side": "buy",
 53        #                 "symbol": "BTC/USD",
 54        #                 "validate": True,
 55        #             },
 56        #         }
 57        #     )
 58        # ... it is also possible to call regular REST endpoints
 59        # but using the websocket messages is more efficient.
 60        # You can also un-/subscribe here using self.subscribe/self.unsubscribe.
 61
 62
 63async def main() -> None:
 64    key: str = os.getenv("SPOT_API_KEY")
 65    secret: str = os.getenv("SPOT_SECRET_KEY")
 66
 67    try:
 68        # Public/unauthenticated websocket client
 69        client: Client = Client()  # only use this one if you don't need private feeds
 70        clients.append(client)
 71        await client.start()
 72        # print(client.public_channel_names)  # list public subscription names
 73
 74        await client.subscribe(
 75            params={"channel": "ticker", "symbol": ["BTC/USD", "DOT/USD"]},
 76        )
 77        await client.subscribe(
 78            params={"channel": "book", "depth": 25, "symbol": ["BTC/USD"]},
 79        )
 80        # await client.subscribe(params={"channel": "ohlc", "symbol": ["BTC/USD"]})
 81        await client.subscribe(
 82            params={
 83                "channel": "ohlc",
 84                "interval": 15,
 85                "snapshot": False,
 86                "symbol": ["BTC/USD", "DOT/USD"],
 87            },
 88        )
 89        await client.subscribe(params={"channel": "trade", "symbol": ["BTC/USD"]})
 90
 91        # wait because unsubscribing is faster than unsubscribing ... (just for that example)
 92        await asyncio.sleep(3)
 93        # print(client.active_public_subscriptions) # … to list active subscriptions
 94        await client.unsubscribe(
 95            params={"channel": "ticker", "symbol": ["BTC/USD", "DOT/USD"]},
 96        )
 97        # ...
 98
 99        if key and secret:
100            # Per default, the authenticated client starts two websocket connections,
101            # one for authenticated and one for public messages. If there is no need
102            # for a public connection, it can be disabled using the ``no_public``
103            # parameter.
104            client_auth = Client(key=key, secret=secret, no_public=True)
105            clients.append(client_auth)
106            await client_auth.start()
107            # print(client_auth.private_channel_names)  # … list private channel names
108            # when using the authenticated client, you can also subscribe to public feeds
109            await client_auth.subscribe(params={"channel": "executions"})
110
111            await asyncio.sleep(5)
112            await client_auth.unsubscribe(params={"channel": "executions"})
113
114        while not client.exception_occur:  # and not client_auth.exception_occur:
115            await asyncio.sleep(6)
116    finally:
117        # Stop the sessions properly.
118        for _client in clients:
119            await _client.close()
120
121
122if __name__ == "__main__":
123    asyncio.run(main())
124
125# ============================================================
126# Alternative - as ContextManager:
127
128# from kraken.spot import SpotWSClient
129# import asyncio
130
131
132# async def on_message(message: dict) -> None:
133#     print(message)
134
135
136# async def main() -> None:
137#     async with SpotWSClient(callback=on_message) as session:
138#         await session.subscribe(params={"channel": "ticker", "symbol": ["BTC/USD"]})
139
140#     while True:
141#         await asyncio.sleep(6)
142
143
144# if __name__ == "__main__":
145#     try:
146#         asyncio.run(main())
147#     except KeyboardInterrupt:
148#         pass