← Back to Blog

Building My First Latency-Sensitive Trading Bot in Python

#Trading Bot#python

Peter Bieda

Author

When I started experimenting with live market feeds, I didn’t fully appreciate how much latency matters. My first trading bot used asyncio in Python to handle streaming tick data from a WebSocket feed.

stream_ticks.py
import asyncio
import aiohttp
import time

async def stream_ticks():
    async with aiohttp.ClientSession() as session:
        async with session.ws_connect("wss://api.exchange.example/ticks") as ws:
            async for msg in ws:
                tick = msg.json()
                print(f"{tick['symbol']} {tick['price']:.2f} @ {time.time():.6f}")

asyncio.run(stream_ticks())

It worked — until it didn’t. Under real load, I saw 200–300 ms of lag due to slow event handling.
That taught me to treat every microsecond as part of the strategy, not an afterthought.

Takeaway: Understanding the event loop and profiling async behavior was my first real exposure to latency engineering — an essential skill for trading systems.