Skip to main content

Reactive Websocket for Kraken

Project description

Kraken Websocket

[NOT RELEASED] This is very much a work in progress, despite being on pypi. Most things might be wrongly documented; API will change

Features

  • Reconnect with incremental backoff (per Kraken's recommendation)
  • Automatically reset subscription for private feeds when sequence is out of whack
  • request/response factories e.g. add_order_factory make websocket events feel like calling an API
  • ... but provides more info than a simple request/response; for instance, add_order goes through each stage submitted->pending->open or canceled, emitting a notification at each stage

Installing

pip install bittrade-kraken-websocket or poetry add bittrade-kraken-websocket

General considerations

Observables/Reactivex

The whole library is build with Reactivex.

Though Observables seem complicated at first, they are the best way to handle - and (synchronously) test - complex situations that arise over time, like an invalid sequence of messages or socket disconnection and backoff reconnects.

For simple use cases, they are also rather easy to use as shown in the examples folder or in the Getting Started below

Concurrency

Internally the library uses threads. For your main programme you don't have to worry about threads; you can block the main thread.

Getting started

Connect to the public feeds

from bittrade_kraken_websocket import public_websocket_connection, subscribe_ticker
from bittrade_kraken_websocket.operators import keep_messages_only, filter_new_socket_only

# Prepare connection - note, this is a ConnectableObservable, so it will only trigger connection when we call its ``connect`` method
socket_connection = public_websocket_connection()
# Prepare a feed with only "real" messages, dropping things like status update, heartbeat, etc…
messages = socket_connection.pipe(
    keep_messages_only(),
)
socket_connection.pipe(
    filter_new_socket_only(),
    subscribe_ticker('USDT/USD', messages)
).subscribe(
    print, print, print  # you can do anything with the messages; here we simply print them out
)
socket_connection.connect()

(This script is complete, it should run "as is")

Logging

We use Python's standard logging. You can modify what logs you see as follows:

logging.getLogger('bittrade_kraken_websocket').addHandler(logging.StreamHandler())

Private feeds

Similar to bittrade-kraken-rest, this library attempts to get as little access to sensitive information as possible.

Currently, you need to set the token onto the EnhancedWebsocket; this means we have no access to your Api key and secret. Since the token is connection based and can't be reused, this protects you as much as Kraken's current authentication method allows.

In the future we might even ask you to code your own send_json method instead.

See examples/private_subscription.py for an example of implementation

new_sockets = connection.pipe(
    filter_new_socket_only(),
    operators.map(add_token),
    operators.share(),
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bittrade_kraken_websocket-0.2.2.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file bittrade_kraken_websocket-0.2.2.tar.gz.

File metadata

  • Download URL: bittrade_kraken_websocket-0.2.2.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.5 Linux/5.4.72-microsoft-standard-WSL2

File hashes

Hashes for bittrade_kraken_websocket-0.2.2.tar.gz
Algorithm Hash digest
SHA256 504add46e554484a183ac7ae0cb27bc4b3f186d24bd834b24f71cf0f8f6f5128
MD5 9b0894557b0f7522cf6e330d2b1f4fa2
BLAKE2b-256 f6457a0c352d83d385f3af888ca664e25f2427c2b3152137437489905d340a3f

See more details on using hashes here.

File details

Details for the file bittrade_kraken_websocket-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for bittrade_kraken_websocket-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ab2af04b593caf98e3f68992d8acd2b5736daf7bf4b65f4fb6f3ef307492fa35
MD5 f3645402abf04aad61df9f9efb1fb2d6
BLAKE2b-256 fdf8c916601f69931430e272e21ef40f8e2232709e00393d075c0f819fb30747

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page