Guide · Algorithmic trading
Trading bot architecture: low latency, backtesting and testing
I built high-frequency trading bots for cross-market arbitrage between 2020 and 2025. The lesson: strategy is the easy part. What decides whether a bot survives is architecture, risk controls and how thoroughly you test before real money is involved.
The building blocks
- Market data ingestion: streaming connections (WebSocket), not polling. By the time a REST poll returns, the opportunity may be gone.
- Normalization: every venue speaks a different format. Convert to one internal model at the edge.
- In-memory state: order books and positions live in memory. No database on the hot path.
- Strategy engine: pure, deterministic functions from state to signals, which makes them easy to test.
- Risk manager: a separate component that can veto any order. Never part of the strategy.
- Execution: order placement, acknowledgements, retries and reconciliation.
Low-latency principles
- Keep the hot path in memory and allocation-light. Persist asynchronously.
- Don't block the event loop. In Node.js, move heavy maths to worker threads or a Rust module; keep Node.js for I/O and orchestration.
- Host close to the venue. Network distance often costs more than your code.
- Handle the ugly cases: reconnections, sequence gaps, stale books and rate limits. They happen daily.
- Measure p99, not averages. The slow tail is when you lose money.
How to test a trading bot
Testing is layered, and each layer catches what the previous one can't:
- Unit tests for strategy and risk logic. Because they're pure functions, edge cases are cheap to cover.
- Backtesting on historical data, with fees, slippage and realistic fill assumptions. No free lunches.
- Deterministic replay of recorded live sessions through the real pipeline, to reproduce bugs exactly.
- Simulation with latency and failures: delayed messages, disconnects, rejected orders.
- Paper trading against live data without real orders, to validate the whole system end to end.
- Small live canary with tight limits and a kill switch, before scaling up.
// Same input, same output: trivial to unit test and to replay.
export function spreadSignal({ bidA, askB, feeRate, minEdge }) {
const edge = (bidA - askB) / askB - 2 * feeRate;
return edge > minEdge ? { action: "arbitrage", edge } : null;
}Risk controls that aren't optional
- Maximum position and order size per instrument
- Maximum daily loss, with automatic stop
- Circuit breakers on abnormal prices, spreads or error rates
- A manual kill switch that works even if the strategy is misbehaving
- Continuous reconciliation against the venue's balances and orders
The same principles (event-driven ingestion, in-memory state, replayable logs) apply to any real-time system, from analytics pipelines to monitoring platforms.
FAQ
Is Node.js fast enough for a trading bot?
For most strategies, yes. Node.js handles many concurrent market data streams well thanks to its event-driven model. The rule is never to block the event loop: move CPU-heavy calculations to worker threads or a Rust module, and keep the hot path in memory.
What's the difference between backtesting and paper trading?
Backtesting runs your strategy against historical data to see how it would have performed. Paper trading runs it live against real-time market data without placing real orders. You need both: backtests find bad ideas cheaply, and paper trading finds the problems history can't, like latency, reconnections and data gaps.
Why do trading bots that look profitable in backtests lose money live?
The usual causes are overfitting to historical data, ignoring fees and slippage, assuming instant fills, and missing real-world failures like dropped connections and stale prices. A realistic simulation models all of these.
What risk controls should every trading bot have?
Position and order-size limits, a maximum daily loss, circuit breakers on abnormal prices or error rates, a manual kill switch, and continuous reconciliation of what the bot thinks it holds against what the venue reports.