Most personal algorithmic trading codebases start the same way: a vectorized Pandas notebook that produces an impressive equity curve, followed by a completely separate live script wired to broker websockets. Almost immediately, live fills diverge from the backtest.
The culprit is rarely a bug in the math itself. It is architectural drift — two codepaths interpreting timestamps, partial fills, and market state slightly differently.
One event core, two clocks
Instead of translating a vectorized notebook into a live daemon, invert the structure: write the strategy and risk engine against a single stream of immutable events, and swap only the event source and execution sink.
- In research mode, a Parquet reader replays historical ticks and bars into the queue in strict timestamp order.
- In live mode, an
asynciowebsocket adapter pushes market updates onto the exact same queue.
Because the strategy can only see events popped from the queue up to current simulated time , lookahead leakage becomes structurally difficult to introduce by accident.
from dataclasses import dataclass
from typing import Literal, Protocol
@dataclass(frozen=True, slots=True)
class BarEvent:
ts_ns: int
symbol: str
bid: float
ask: float
@dataclass(frozen=True, slots=True)
class OrderIntent:
ts_ns: int
symbol: str
side: Literal["buy", "sell"]
qty: float
limit_price: float
class ExecutionSink(Protocol):
async def submit(self, order: OrderIntent) -> None: ...
Modeling execution friction honestly
A deterministic loop also forces you to model fill cost explicitly instead of assuming mid-price fills. If your order crosses half the spread plus a slippage term proportional to order size relative to top-of-book depth , your realized entry penalty is:
Even a rough empirical calibration for and will immediately filter out fragile high-turnover strategies that only look profitable when fills are assumed free.
Running lean on a shared VPS
When the execution loop shares a Linux VPS with monitoring, static sites, or research jobs, operational hygiene matters:
- Journal every state transition to an append-only SQLite WAL table so restarts recover open orders idempotently.
- Enforce hard risk bounds outside the strategy class (max gross notional, max daily drawdown, kill switch on stale feeds).
- Keep the hot path allocation-light using
slots=Truedataclasses and pre-allocated rolling arrays so GC pauses never line up with volatile market windows.