Skip to content

svdro/coding_sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Coding Sample

This is a basic python wrapper that provides a uniform interface for interacting with public crypto exchange websocket streams using the asyncio framework.

Features

  • Supports the Kraken and Binance websocket apis
  • Implementation of Orderbook and Trades Streams
  • Common Interface for interacting with ws-streams
    • maps symbol names (which differ between exchanges) to a standardized format (e.g: btcusdt)
    • abstracts subscribing/unsubscribing and handling ws-events
    • synchronizes binance orderbook (Diff. Depth) stream with rest api orderbook (depth) snapshots
  • Websocket messages are parsed and returned as standardized "python dataclasses" events
  • Handles maintaining and closing multiple ws-connections

Example

import asyncio
from ws_apis import KrakenWebsocket,  StreamType

async def main():
  ws = KrakenWebsocket(StreamType.TRADES, "ethusdt")
  await ws.connect()
  try:
    for _ in range(10):
      event = await ws.recv()
      print(event)
  finally:
    await ws.cleanup()

if __name__ == "__main__":
  asyncio.run(main())

Example using async context managers

import asyncio
from ws_apis import BinanceWebsocket, StreamType

async def main():
  async with BinanceWebsocket(StreamType.BOOK, "ethusdt", depth=100) as ws:
    for _ in range(10):
      event = await ws.recv()
      print(event)

Example subscribing to multiple websocket streams

import asyncio
from ws_apis import WsManager, Subscription, OrderbookEvent

async def main():
  subs = [
    Subscription("kraken", "adaeur", "book", 500),
    Subscription("binance", "adausdt", "book", 500),
    Subscription("kraken", "adausdt", "trades"),
    Subscription("binance", "adausdt", "trades"),
    ]
  
  async with WsManager(subs) as wsm:
    for _ in range(50):
      event = await wsm.recv()
      stream_name = "book" if isinstance(event, OrderbookEvent) else "trades"
      print(f"{event.ts_exchange} {stream_name:5s} {event.exch_name:7s} {event.symbol:8s}")

About

A rudimentary python wrapper for binance and kraken ws-apis

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages