<?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.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>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>
