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 asyncio websocket 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 tt, 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 s/2s/2 plus a slippage term proportional to order size qq relative to top-of-book depth VV, your realized entry penalty is:

Δp=s2+κ(qV)γ\Delta p = \frac{s}{2} + \kappa \left(\frac{q}{V}\right)^\gamma

Even a rough empirical calibration for κ\kappa and γ\gamma 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:

  1. Journal every state transition to an append-only SQLite WAL table so restarts recover open orders idempotently.
  2. Enforce hard risk bounds outside the strategy class (max gross notional, max daily drawdown, kill switch on stale feeds).
  3. Keep the hot path allocation-light using slots=True dataclasses and pre-allocated rolling arrays so GC pauses never line up with volatile market windows.