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)