<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: joeschatzman</title>
    <description>The latest articles on DEV Community by joeschatzman (@joeschatzman).</description>
    <link>https://dev.to/joeschatzman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3974932%2F67cf3379-6770-40dc-9c6e-fbe91e4610b8.png</url>
      <title>DEV Community: joeschatzman</title>
      <link>https://dev.to/joeschatzman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joeschatzman"/>
    <language>en</language>
    <item>
      <title>I built a self-hosted algo-trading stack in Python — backtest to live, no black box</title>
      <dc:creator>joeschatzman</dc:creator>
      <pubDate>Wed, 29 Jul 2026 12:47:41 +0000</pubDate>
      <link>https://dev.to/joeschatzman/i-built-a-self-hosted-algo-trading-stack-in-python-backtest-to-live-no-black-box-pln</link>
      <guid>https://dev.to/joeschatzman/i-built-a-self-hosted-algo-trading-stack-in-python-backtest-to-live-no-black-box-pln</guid>
      <description>&lt;p&gt;Every "algo trading platform" I tried made me pick one of two bad options:&lt;/p&gt;

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

&lt;p&gt;I wanted a third option: &lt;strong&gt;bring my own Python strategy and run it on infrastructure I control&lt;/strong&gt; — 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  The strategy is the easy part. It's everything &lt;em&gt;around&lt;/em&gt; it that's hard.
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Backtest-to-live parity (the #1 way backtests lie)
&lt;/h3&gt;

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

&lt;p&gt;The fix is architectural: the &lt;strong&gt;same engine runs both&lt;/strong&gt;. Signal evaluation, position sizing, and risk checks share one implementation — only the &lt;em&gt;data source&lt;/em&gt; and the &lt;em&gt;execution target&lt;/em&gt; 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.&lt;/p&gt;

&lt;p&gt;If your "live trader" is a separate implementation from your backtester, you don't actually know what you're deploying.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Broker abstraction — Alpaca vs Interactive Brokers
&lt;/h3&gt;

&lt;p&gt;These two brokers could not be more different to integrate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Alpaca&lt;/strong&gt; is a clean REST API. Keys in, JSON out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IBKR&lt;/strong&gt; is a socket API through TWS or IB Gateway that has to be &lt;em&gt;running&lt;/em&gt;, with session quirks, contract qualification, and a forced daily restart.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your strategy code talks directly to either one, you're locked in. The fix is a thin &lt;strong&gt;broker interface&lt;/strong&gt; — &lt;code&gt;place_order&lt;/code&gt;, &lt;code&gt;get_positions&lt;/code&gt;, &lt;code&gt;get_quote&lt;/code&gt;, &lt;code&gt;get_bars&lt;/code&gt; — with adapters behind it. Strategy code never sees broker-specific objects.&lt;/p&gt;

&lt;p&gt;A few real IBKR gotchas I hit building the adapter, in case they save you time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Contracts must be "qualified"&lt;/strong&gt; (populate &lt;code&gt;conId&lt;/code&gt;) before you can request market data or
place orders — otherwise &lt;code&gt;reqTickers&lt;/code&gt; throws.&lt;/li&gt;
&lt;li&gt;IBKR returns &lt;code&gt;1.7976931348623157e+308&lt;/code&gt; (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.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;daily gateway restart&lt;/strong&gt; means a 24/7 live strategy needs reconnection handling.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of that is in the strategy. All of it has to be abstracted away so the strategy stays portable.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Risk management as a first-class citizen — not an afterthought
&lt;/h3&gt;

&lt;p&gt;Bugs happen. A runaway loop or a bad signal shouldn't drain an account. So risk lives &lt;em&gt;in the&lt;br&gt;
execution path&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pre-trade checks&lt;/strong&gt; — position limits, buying power, max order size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A runtime drawdown circuit breaker&lt;/strong&gt; — halts trading when equity draws down past a threshold.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If risk is a wrapper you can forget to call, it's not risk management.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Be honest about what's still hard
&lt;/h3&gt;

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

&lt;h2&gt;
  
  
  What I ended up building: AlgoDeploy
&lt;/h2&gt;

&lt;p&gt;I packaged all of the above into &lt;strong&gt;&lt;a href="https://algo-deploy.com" rel="noopener noreferrer"&gt;AlgoDeploy&lt;/a&gt;&lt;/strong&gt; — backtest,&lt;br&gt;
risk-manage, and go live on &lt;strong&gt;your own Alpaca or Interactive Brokers account&lt;/strong&gt; (US equities,&lt;br&gt;
single-leg options, crypto). You build strategies in a no-code dashboard, a YAML config, or&lt;br&gt;
Python — all the same engine underneath. It runs on your machine with your keys; nothing is&lt;br&gt;
routed through a hosted backend.&lt;/p&gt;

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

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

&lt;p&gt;👉 &lt;a href="https://algo-deploy.com" rel="noopener noreferrer"&gt;https://algo-deploy.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>fintech</category>
      <category>showdev</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>How I Built a Python Backtesting Engine with 623 Tests</title>
      <dc:creator>joeschatzman</dc:creator>
      <pubDate>Mon, 08 Jun 2026 23:16:17 +0000</pubDate>
      <link>https://dev.to/joeschatzman/how-i-built-a-python-backtesting-engine-with-623-tests-1njo</link>
      <guid>https://dev.to/joeschatzman/how-i-built-a-python-backtesting-engine-with-623-tests-1njo</guid>
      <description>&lt;p&gt;I spent the last year building a backtesting and live trading engine in Python. It started as a personal tool — I was tired of rewriting the same plumbing every time I wanted to test a strategy. Here's how it turned into a real product.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Every time I wanted to backtest a trading strategy, I ended up writing the same boilerplate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pull OHLCV data from somewhere&lt;/li&gt;
&lt;li&gt;Wire up indicators&lt;/li&gt;
&lt;li&gt;Write entry/exit logic&lt;/li&gt;
&lt;li&gt;Track positions, P&amp;amp;L, drawdowns&lt;/li&gt;
&lt;li&gt;Build some kind of report&lt;/li&gt;
&lt;li&gt;Hope nothing broke when I changed one thing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I tried the existing tools. Some were abandoned. Some forced you into their API patterns. Some were notebooks-only with no path to live execution. None of them let me go from "I have an idea" to "it's running live" without rewriting half the code.&lt;/p&gt;

&lt;p&gt;So I built my own.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Built
&lt;/h2&gt;

&lt;p&gt;AlgoDeploy is a Python engine that covers the full loop:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Strategy definition&lt;/strong&gt; → &lt;strong&gt;Backtesting&lt;/strong&gt; → &lt;strong&gt;Risk management&lt;/strong&gt; → &lt;strong&gt;Live execution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The key design decision was making strategies declarative. Most strategies don't need custom Python — they're just indicator conditions, entry rules, exit rules, and position sizing. So I built a YAML config layer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;symbol&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;SPY&lt;/span&gt;
&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;2020-01-01&lt;/span&gt;
&lt;span class="na"&gt;end&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;2025-01-01&lt;/span&gt;
&lt;span class="na"&gt;capital&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100000&lt;/span&gt;

&lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;close &amp;lt; sma(40)&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;rsi(14) &amp;lt; &lt;/span&gt;&lt;span class="m"&gt;30&lt;/span&gt;

&lt;span class="na"&gt;exit&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;hard_stop&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;2%&lt;/span&gt;
  &lt;span class="na"&gt;trailing&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;3%&lt;/span&gt;
  &lt;span class="na"&gt;take_profit&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;8%&lt;/span&gt;

&lt;span class="na"&gt;position_size&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5%&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That config is a complete strategy. No Python required. But if you need custom logic, you can drop to Python and use the same engine programmatically.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture
&lt;/h2&gt;

&lt;p&gt;The engine has a few distinct layers, and keeping them separate was the most important decision I made:&lt;/p&gt;

&lt;h3&gt;
  
  
  Indicators
&lt;/h3&gt;

&lt;p&gt;11 built-in indicators (ATR, EMA, SMA, RSI, MACD, Bollinger, VWAP, Beta, OBV, Stochastic, ADX), all computed as pandas Series from OHLCV data. The indicator layer is stateless — it takes a DataFrame and returns Series. This means you can test indicators independently, swap them out, or add custom ones without touching anything else.&lt;/p&gt;

&lt;h3&gt;
  
  
  Comparators
&lt;/h3&gt;

&lt;p&gt;Entry conditions are built from composable comparator functions: &lt;code&gt;greater_than&lt;/code&gt;, &lt;code&gt;less_than&lt;/code&gt;, &lt;code&gt;crosses_above&lt;/code&gt;, &lt;code&gt;crosses_below&lt;/code&gt;, &lt;code&gt;within_pct_of&lt;/code&gt;, &lt;code&gt;between&lt;/code&gt;. In YAML, you write &lt;code&gt;close &amp;gt; sma(20)&lt;/code&gt;. Under the hood, it's parsed into a comparator chain that evaluates on each bar.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exit Rules
&lt;/h3&gt;

&lt;p&gt;Hard stops, trailing stops, take-profit targets, time-based exits, and scale-outs. Each exit rule is independent — you can stack them, and the first one that triggers wins. This was important because exit logic is where most backtesting frameworks get messy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Risk Layer
&lt;/h3&gt;

&lt;p&gt;This runs independently from strategy logic. Drawdown limits, daily loss caps, position-level risk caps, exposure constraints, and a "require stop" rule that rejects any order without a stop price. The risk layer can veto any trade the strategy wants to make.&lt;/p&gt;

&lt;p&gt;Keeping risk separate from strategy means you can swap strategies without re-implementing risk checks, and you can tighten risk rules without touching strategy code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Position Sizing
&lt;/h3&gt;

&lt;p&gt;Kelly Criterion, volatility-scaled (target a dollar risk per trade), and fixed fractional (risk X% of equity per trade). Each sizer takes the current portfolio state and returns a position size. Again, independent from everything else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing
&lt;/h2&gt;

&lt;p&gt;The project has 623 automated tests. That's not a vanity metric — it's a survival mechanism.&lt;/p&gt;

&lt;p&gt;When you're dealing with financial calculations, a subtle bug in position sizing or exit logic can silently produce wrong results. You won't see it in a stack trace. You'll see it when your backtest returns don't match reality.&lt;/p&gt;

&lt;p&gt;So I test aggressively:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unit tests for every indicator, comparator, exit rule, position sizer, and risk check&lt;/li&gt;
&lt;li&gt;Integration tests for the backtest engine (known inputs → verified outputs)&lt;/li&gt;
&lt;li&gt;End-to-end tests for the full pipeline: config → data → backtest → report&lt;/li&gt;
&lt;li&gt;Edge cases: zero-volume bars, gaps, splits, single-bar strategies, empty universes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The test suite runs in CI on every commit. If I change how trailing stops calculate, I know within seconds whether anything else broke.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Dashboard
&lt;/h2&gt;

&lt;p&gt;I built a web UI on top of the engine because not everything needs to be done in code. The dashboard lets you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configure strategies with dropdowns and form fields (no YAML or Python needed)&lt;/li&gt;
&lt;li&gt;Run backtests and see results in real-time via WebSocket updates&lt;/li&gt;
&lt;li&gt;View metrics: total return, CAGR, Sharpe, Sortino, max drawdown, win rate, profit factor&lt;/li&gt;
&lt;li&gt;Compare against benchmarks (SPY, QQQ, etc.)&lt;/li&gt;
&lt;li&gt;Save and load strategy configurations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The dashboard is a local web app — it talks to the same Python engine underneath. Nothing goes to the cloud.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reports
&lt;/h2&gt;

&lt;p&gt;Backtest results export as self-contained HTML files. One file, no server needed, you can email it or archive it. They include equity curves, drawdown charts, trade logs, and all the summary metrics.&lt;/p&gt;

&lt;h2&gt;
  
  
  Live Trading
&lt;/h2&gt;

&lt;p&gt;The engine connects to Alpaca for live execution. The same strategy that ran in a backtest can run live with one config change. The live runner follows a state machine: scan universe → evaluate entry rules → risk check → size position → execute. It logs every decision for audit.&lt;/p&gt;

&lt;p&gt;There's also a paper trading mode that logs orders without placing them, so you can validate before risking real money.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Learned
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Declarative first, escape to code.&lt;/strong&gt; Most users don't want to write Python for a simple moving average crossover. But some users need custom logic. Supporting both from the same engine was the right call.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Risk is not part of strategy.&lt;/strong&gt; This was the biggest architectural win. When risk checks are independent, you can reason about them separately. "Does my strategy generate good signals?" and "Am I risking too much?" are different questions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test financial code like your money depends on it.&lt;/strong&gt; Because eventually, it does. A 0.1% rounding error in position sizing compounds fast.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reports need to be portable.&lt;/strong&gt; Nobody wants to spin up a Jupyter server to share backtest results. Self-contained HTML files solved this completely.&lt;/p&gt;

&lt;h2&gt;
  
  
  Current Status
&lt;/h2&gt;

&lt;p&gt;The engine is feature-complete and I'm looking for beta testers to stress-test it before public launch. If you're interested in algo trading and want to try it, the site is at &lt;a href="https://algo-deploy.com" rel="noopener noreferrer"&gt;algo-deploy.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy to answer questions about the architecture, the testing approach, or anything else in the comments.&lt;/p&gt;

</description>
      <category>python</category>
      <category>showdev</category>
      <category>software</category>
      <category>devtool</category>
    </item>
  </channel>
</rss>
