I am writing a code in python that streams real-time data from the Coinbase Exchange.
def on_open(ws):
print('opened connection')
subscribe_message ={
"type": "subscribe",
"channels": [
{
"name": "ticker",
"product_ids": [
"BTC-USD"
]
}
]
}
print(subscribe_message)
ws.send(json.dumps(subscribe_message))
def on_message(ws,message):
js=json.loads(message)
if js['type']=='ticker':
print(js['time'])
socket = "wss://ws-feed.exchange.coinbase.com"
ws = websocket.WebSocketApp(socket,on_open=on_open, on_message=on_message)
ws.run_forever()
I would like to:
Measure the latency between the time I make the request to the server and the time I receive the message, and compare it with the time information stored in
js['time'].Run this code as a daemon and save all the outputs in a .txt or later analysis.
In point 1, I want to measure the latency. I need efficiency and very less latency to extract the data upon which to make trading decisions. This is because the rate at which the orderbook updates is greater than the frequency at which I send and receive data from the exchange. So, by the time I receive information about the orderbook from the websocket and my script takes a decision, the orderbook is already changed. So, I want to know the latency, to be able to estimate the real-time status of the orderbook at the time my script takes a decision.
In point 2, I just want to extract the real-time bid/ask prices for the currency pair BTC/USD. Of course, I can do this in python and save the output in a .txt file. For just the bid/ask spread, it's very easy. But eventually, I will use this same code (with some modification) to extract all the information about the order book (24h, 7/7). 1 week of data is 28GB and the script to store the data in python is likely gonna consume a lot of CPU (I am running the script on AWS).
I have read that tcpdump would be perfect for these tasks but I really don't know how to start. I have identified the interface and when I run
sudo tcpdump -i en0 -nfrom which I get of course all the data traffic.
Can anyone explain to me briefly how can I address the two points above? For example, step zero would be to filter all the packets sent and received to/from Coinbase. How can I identify the IP of the server of Coinbase that is sending me the packets?