Full-Trade-Stream-Latency-Test
This code assumes your machine is using the EST timezone. We also recommend syncing your system clock with a time server located somewhere in NJ/NY.
python
import asyncio
import json
import time
import websockets
# This only works on Python 3.11, not 3.12!
async def stream_trades():
async with websockets.connect('ws://127.0.0.1:25520/v1/events') as websocket:
req = {}
req['msg_type'] = 'STREAM_BULK'
req['sec_type'] = 'OPTION'
req['req_type'] = 'TRADE'
req['add'] = True
req['id'] = 0
await websocket.send(req.__str__())
count = 0
while True:
response = await websocket.recv()
try:
count += 1
obj = json.loads(response)
if obj['header']['type'] == "TRADE" and count % 100 == 0:
print('latency: ' + str(get_ms_of_day() - int(obj['trade']['ms_of_day'])))
except Exception as e:
print(e)
print(response)
exit(1)
# print(response)
def get_ms_of_day():
# Add + 1000 to this return statement if you experience negative latencies in the 900s.
return (int(time.time() * 1000) % 86400000) - 14400000
asyncio.get_event_loop().run_until_complete(stream_trades())
text
latency: 3
latency: 47
latency: 25
latency: 16
latency: 4
latency: 20
latency: 18
latency: 17
latency: 37
latency: 21
latency: 5
latency: 48
latency: 26
latency: 38
latency: 47
latency: 1