Skip to content

Instantly share code, notes, and snippets.

@nevinaragam
Created September 18, 2021 18:19
Show Gist options
  • Save nevinaragam/d059a7ad1234d128207538bf3015cd3d to your computer and use it in GitHub Desktop.
Save nevinaragam/d059a7ad1234d128207538bf3015cd3d to your computer and use it in GitHub Desktop.
Kraken Message Counter
import json
import logging
import numpy as np
import time
import websocket
# Set logging parameters
log_format = "%(asctime)s: %(message)s"
logging.basicConfig(format=log_format, level=logging.INFO, datefmt="%H:%M:%S")
# Set exchange
EXCHANGE = "Kraken"
# Set websocket
SOCKET = "wss://ws.kraken.com"
# Set seconds to run test
RUNTIME = 15
# Set subscription message
subscribe = {
"event": "subscribe",
"pair": ["XBT/USD"],
"subscription": {
"name": "book"
}
}
# Define callback functions for websocket connection
def on_open(ws):
logging.info(f"{EXCHANGE} Socket open")
ws.send(json.dumps(subscribe, ensure_ascii=False).encode('utf8'))
def on_message(ws, message):
global count
global counts
global starttime
endtime = time.time()
interval = (endtime - starttime)
count += 1
if interval >= 1:
logging.info(f'{count} messages received in {interval} seconds.')
starttime = endtime
counts.append(count)
count = 0
if (endtime - begin) >= RUNTIME:
ws.close()
def on_close(ws):
logging.info(f"{EXCHANGE} Socket closed")
ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_message=on_message, on_close=on_close)
if __name__ == "__main__":
count = 0
counts = []
stop_interval = 0
starttime = time.time()
begin = time.time()
ws.run_forever()
if len(counts) != 0:
logging.info(f"Average messages per second: {np.average(counts)}")
else:
logging.info(f"No Messages received")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment