Package: aio-kraken-ws
aio-kraken-wsA module to collect ohlc candles from Kraken using WebSockets that is asyncio friendly! Looking for automated trading tools?Botcryptomay interest you.Key featuresSubscribe 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 startedInstallpip install aio-kraken-wsUsage# check tests/learning/log_to_file.py for a complete exampleasyncdefcallback(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"""withopen("candles.txt","a+")asfile:file.write(f"[{pair}:{interval}]({timestamp},{o},{h},{l},{c},{v})\n")kraken_ws=awaitKrakenWs.create(callback)# subscribe to some datasetskraken_ws.subscribe([("XBT/EUR",1),("ETH/EUR",5)])Thecallbackfunction 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.Ifnonew 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() = 120awaitcallback("XBT/EUR",1,60,42.0,57.0,19.0,24.0,150.0)# time.time() = 180awaitcallback("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 activityawaitcallback("XBT/EUR",1,180,10.0,10.0,10.0,10.0,0.0)Error managementAn exception raised by thecallbackwill be logged and it wont stop the streamsIf kraken send an error message, anERRORlog is emitted with the kraken payloadIf kraken send 'Subscription ohlc interval not supported', the related dataset is automatically unsubscribedWarningThecallbackshould takes less than a minute to process. If thecallbacktakes more than a minutes, awarningis emitted and you may lose market data.Kraken WebSocket server manage20 subscriptions maximumper 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-wsautomatically reconnect and re-subscribe to the datasets every 5 minutes.TestsYou can find a working example of KrakenWs intests/learning/log_to_file.py.Run tests locallyClone the repo and install requirementspipinstall-e.[test]Run the suite tests# unit tests - no call to kraken - fastpytest--cov=aio_kraken_ws--cov-report=-vtests/unit# integration tests - actual kraken subscription - slowpytest--cov=aio_kraken_ws--cov-append-v-n8tests/integrationChangelogSeehttps://cdlr75.gitlab.io/aio-kraken-ws/CHANGELOG.html