DEV Community

Cover image for I built a self-hosted algo-trading stack in Python — backtest to live, no black box
joeschatzman
joeschatzman

Posted on

I built a self-hosted algo-trading stack in Python — backtest to live, no black box

Every "algo trading platform" I tried made me pick one of two bad options:

  1. A hosted black box — upload your strategy to someone else's cloud, run it on their runtime, pay a monthly fee, and hope their fills resemble reality.
  2. Roll your own — weeks of plumbing (broker APIs, data feeds, order lifecycle, risk management) before you place a single live trade.

I wanted a third option: bring my own Python strategy and run it on infrastructure I control — my machine, my broker keys, no cloud lock-in. Here's what that actually takes, the parts that bit me, and the tool I ended up building.

The strategy is the easy part. It's everything around it that's hard.

1. Backtest-to-live parity (the #1 way backtests lie)

The most common way a backtest lies to you: your live code path differs from your backtest code path. If your backtest fills at the bar close but live fills at next-bar-open with slippage, your equity curve is fiction.

The fix is architectural: the same engine runs both. Signal evaluation, position sizing, and risk checks share one implementation — only the data source and the execution target swap out. A vectorized pass gives you speed for research; an event-driven pass gives you realism (next-bar fills, slippage, commissions) and is the exact code that runs live.

If your "live trader" is a separate implementation from your backtester, you don't actually know what you're deploying.

2. Broker abstraction — Alpaca vs Interactive Brokers

These two brokers could not be more different to integrate:

  • Alpaca is a clean REST API. Keys in, JSON out.
  • IBKR is a socket API through TWS or IB Gateway that has to be running, with session quirks, contract qualification, and a forced daily restart.

If your strategy code talks directly to either one, you're locked in. The fix is a thin broker interfaceplace_order, get_positions, get_quote, get_bars — with adapters behind it. Strategy code never sees broker-specific objects.

A few real IBKR gotchas I hit building the adapter, in case they save you time:

  • Contracts must be "qualified" (populate conId) before you can request market data or place orders — otherwise reqTickers throws.
  • IBKR returns 1.7976931348623157e+308 (max double, its "UNSET" sentinel) for empty price fields — a market order will report a bogus ~1.8e308 limit price if you don't strip it.
  • The daily gateway restart means a 24/7 live strategy needs reconnection handling.

None of that is in the strategy. All of it has to be abstracted away so the strategy stays portable.

3. Risk management as a first-class citizen — not an afterthought

Bugs happen. A runaway loop or a bad signal shouldn't drain an account. So risk lives in the
execution path
:

  • Pre-trade checks — position limits, buying power, max order size.
  • A runtime drawdown circuit breaker — halts trading when equity draws down past a threshold.

If risk is a wrapper you can forget to call, it's not risk management.

4. Be honest about what's still hard

No backtest fully models slippage, fill timing, or market impact — configurable slippage and commission modeling narrows the gap but doesn't close it. And live-trading reliability (the IBKR gateway restart, reconnection, monitoring) is genuine operational work. Anyone selling you "backtest = live" is selling you something.

What I ended up building: AlgoDeploy

I packaged all of the above into AlgoDeploy — backtest,
risk-manage, and go live on your own Alpaca or Interactive Brokers account (US equities,
single-leg options, crypto). You build strategies in a no-code dashboard, a YAML config, or
Python — all the same engine underneath. It runs on your machine with your keys; nothing is
routed through a hosted backend.

It's a one-time license (you own the version you buy), source-available, with a 7-day free trial. Honest scope: IBKR is US equities + single-leg options + crypto today; multi-leg spreads, futures, and forex are on the roadmap.

If you've ever wanted to run your own strategies without renting a platform or building all
the plumbing yourself, I'd genuinely value your feedback — especially on what's missing for
your workflow.

👉 https://algo-deploy.com

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.