From 2738b548519ad66a1f34f06a37822787dc5c559f Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sat, 4 Jul 2020 18:59:02 -0400 Subject: [PATCH] Start kraken backend --- piker/brokers/kraken.py | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 piker/brokers/kraken.py diff --git a/piker/brokers/kraken.py b/piker/brokers/kraken.py new file mode 100644 index 00000000..a55080d9 --- /dev/null +++ b/piker/brokers/kraken.py @@ -0,0 +1,44 @@ +""" +Kraken backend. +""" +from typing import List +import json + +import trio +import tractor +from trio_websocket import open_websocket_url + + +if __name__ == '__main__': + + async def stream_quotes( + pairs: List[str] = ['BTC/USD'], + ) -> None: + """Subscribe ohlc quotes for ``pairs``. + + ``pairs`` must be formatted like `crypto/fiat`. + """ + async with open_websocket_url( + 'wss://ws.kraken.com', + ) as ws: + # setup subs + subs = { + 'event': 'subscribe', + 'pair': pairs, + 'subscription': { + 'name': 'ohlc', + # 'name': 'ticker', + # 'name': 'openOrders', + # 'depth': '25', + }, + } + await ws.send_message(json.dumps(subs)) + + while True: + msg = json.loads(await ws.get_message()) + if isinstance(msg, dict) and msg.get('event') == 'heartbeat': + continue + + print(msg) + + trio.run(stream_quotes)