A fully automated stock and crypto trading bot using Django, Python, and the Alpaca and Binance APIs — the technical architecture, what worked, and what we would do differently with LangChain today.
Architecture: Two APIs, One Brain
The trading bot connects two market data sources: Alpaca for US equities (with paper trading mode that is invaluable for development) and Binance for crypto pairs. Both provide REST APIs for order management and WebSocket APIs for real-time price streams. The Django backend serves as the orchestration layer — managing strategy state, logging trades, and exposing a REST API for the GUI. The core trading logic lives in a strategy engine that consumes the WebSocket price streams and emits buy/sell signals based on configurable rule sets. Strategy modules are plug-and-play: you can swap RSI momentum for mean reversion without touching the order execution layer.
WebSocket Connections: Handling Reconnects and Stale Data
The hardest part of any real-time trading system is not the trading logic — it is connection management. Binance WebSocket streams disconnect randomly, especially during high-volume market events. We implemented an exponential backoff reconnect strategy with a maximum of five retries before triggering an alert and falling back to REST polling. Stale data detection runs on every price tick: if the last-updated timestamp is more than three seconds old, the strategy engine pauses all orders and waits for a fresh stream. This sounds conservative, but acting on stale prices during a flash crash can blow a portfolio in minutes.
The Python GUI: Monitoring Without a Browser
We built a Python desktop GUI using Tkinter with live P&L charts rendered by Matplotlib. The GUI connects to the Django backend via local REST calls and polls every second for updated position data. Buttons allow the trader to pause all strategies, override a single position, or trigger an emergency flatten that closes all open orders. Running Matplotlib inside Tkinter required threading to prevent the charting library from blocking the UI event loop — a common Python gotcha. The result is a clean, standalone desktop app that any trader can run without needing a web browser or an AWS account.
What We Would Add Today: LangChain and Sentiment Analysis
If we rebuilt this today, we would integrate LangChain with OpenAI GPT-4o for real-time news sentiment analysis using Pinecone as the vector store. The strategy would work like this: fetch Reuters and Bloomberg RSS headlines every 60 seconds, embed them with OpenAI, query Pinecone for historically similar market events, and use the retrieved context to bias the strategy engine's risk tolerance. A negative earnings surprise for a holding would instantly reduce position size by 50%. LangChain's structured output tools make this pipeline surprisingly concise — roughly 200 lines of Python to go from headline to strategy adjustment.
“The hardest part of a real-time trading system is not the trading logic — it is connection management and stale-data detection.”





Leave a Comment