Skip to main content

aio-kraken-ws pipeline status coverage report PEP8

A module to collect ohlc candles from Kraken using WebSockets that is asyncio friendly! Looking for automated trading tools? Botcrypto may interest you.

Key features

  • Subscribe to kraken data using a single WebSocket.
  • Trigger a callback that is coroutine on each new closed candles from kraken.
  • Easy subscribe/unsubscribe to datasets, i.e. [(pair, Unit Time)] ex: [('XBT/EUR', 1)].
  • Callback is regularly triggered at each end of the UT intervals, whatever is the number of data received by kraken.

Getting started

Install

pip install aio-kraken-ws

Usage

# check tests/learning/log_to_file.py for a complete example
async def callback(pair, interval, timestamp, o, h, l, c, v):
    """ A coroutine handling new candles.

    :param str pair:
    :param int interval: time in minutes
    :param int timestamp: candle open timestamp.
    :param float o: open price
    :param float h: high price
    :param float l: low price
    :param float c: close price
    :param float v: volume
    """
    with open("candles.txt", "a+") as file:
        file.write(f"[{pair}:{interval}]({timestamp},{o},{h},{l},{c},{v})\n")

kraken_ws = await KrakenWs.create(callback)
# subscribe to some datasets
kraken_ws.subscribe([("XBT/EUR", 1), ("ETH/EUR", 5)])

The callback function is called for each dataset at the end of each dataset's unit time interval.

E.g. if subscription start at 4h42.05 to the dataset ("XBT/EUR", 1), then callback is triggered at 4h43.00, at 4h44.00, at 4h45.00, etc... For ("XBT/EUR", 60), it would be at 5h00.00, at 6h00.00, etc... It's possible to get at most 10ms delay between the exact UT interval ending and the actual datetime of the call.

If no new data were received from Kraken during an interval, the callback is triggered with the latest known close price and v=0, as it's described in the following example.

E.g.

kraken_ws.subscribe([("XBT/EUR", 1)])
# time.time() = 120
await callback("XBT/EUR", 1, 60, 42.0, 57.0, 19.0, 24.0, 150.0)
# time.time() = 180
await callback("XBT/EUR", 1, 120, 19.0, 24.0, 8.0, 10.0, 13.0)
# time.time() = 240 : no data received in 60s, i.e. no activity
await callback("XBT/EUR", 1, 180, 10.0, 10.0, 10.0, 10.0, 0.0)

Error management

  • An exception raised by the callback will be logged and it wont stop the streams
  • If kraken send an error message, an ERROR log is emitted with the kraken payload
  • If kraken send 'Subscription ohlc interval not supported', the related dataset is automatically unsubscribed

Warning

The callback should takes less than a minute to process. If the callback takes more than a minutes, a warning is emitted and you may lose market data.

Kraken WebSocket server manage 20 subscriptions maximum per connection. Above 20 subscriptions, you may not receive all desired data.

Hopfully, aio-kraken-ws manage this limitation for you! A new websocket connection is open every 20 subscriptions.

Moreover, after 24h a subscriptions seem to expire and no more market data is receive. To ensure we do not lose the stream of market data, aio-kraken-ws automatically reconnect and re-subscribe to the datasets every 5 minutes.

Tests

You can find a working example of KrakenWs in tests/learning/log_to_file.py.

Run tests locally

Clone the repo and install requirements

pip install -e .[test]

Run the suite tests

# unit tests - no call to kraken - fast
pytest --cov=aio_kraken_ws --cov-report= -v tests/unit

# integration tests - actual kraken subscription - slow
pytest --cov=aio_kraken_ws --cov-append -v -n 8 tests/integration

Changelog

See https://cdlr75.gitlab.io/aio-kraken-ws/CHANGELOG.html

Metadata

Release files for aio-kraken-ws 0.3.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aio-kraken-ws 0.3.9
File Size Uploaded
aio-kraken-ws-0.3.9.tar.gz 19.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aio-kraken-ws 0.3.9
File Interpreter ABI Platform
aio_kraken_ws-0.3.9-py3-none-any.whl Python 3 none any Details

Total release size: 36.8 kB

Release files / aio-kraken-ws-0.3.9.tar.gz

Download URL aio-kraken-ws-0.3.9.tar.gz
Size 19.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b2cfed36996a458984fd0ff7d2af2a8256eadc453429e5657b1b0b3a15e932cb
BLAKE2b-256 checksum
How to use checksums
82a835c0de20c11a1ab8f21c7c06e68ddcb4487b74535c7d7c168ac4fe6ad043
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Release files / aio_kraken_ws-0.3.9-py3-none-any.whl

Download URL aio_kraken_ws-0.3.9-py3-none-any.whl
Size 17.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d8d18ab29a10e8338897533c374aad7218cf4ef2f8fc65a38c81abebd09f5b7
BLAKE2b-256 checksum
How to use checksums
87d0317731210f53c83cf9082057ea960e2755a3c074134ff99b08901629cd81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Release history Release notifications | RSS feed

This release

0.3.9 This release

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page