<?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: AgentMarts</title>
    <description>The latest articles on DEV Community by AgentMarts (@agentmarts).</description>
    <link>https://dev.to/agentmarts</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%2F4282857%2F571bbda1-f471-45db-b105-fa51da41a88e.png</url>
      <title>DEV Community: AgentMarts</title>
      <link>https://dev.to/agentmarts</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/agentmarts"/>
    <language>en</language>
    <item>
      <title>The Parameters Were Chosen on the Whole Sample: Holdout Sharpe +0.09, Every Neighbour Negative</title>
      <dc:creator>AgentMarts</dc:creator>
      <pubDate>Fri, 04 Dec 2026 13:52:40 +0000</pubDate>
      <link>https://dev.to/agentmarts/the-parameters-were-chosen-on-the-whole-sample-holdout-sharpe-009-every-neighbour-negative-3h6g</link>
      <guid>https://dev.to/agentmarts/the-parameters-were-chosen-on-the-whole-sample-holdout-sharpe-009-every-neighbour-negative-3h6g</guid>
      <description>&lt;h1&gt;
  
  
  The Parameters Were Chosen on the Whole Sample: Holdout Sharpe +0.09, Every Neighbour Negative
&lt;/h1&gt;

&lt;p&gt;Part 1 of this series fixed a one-line look-ahead bug (&lt;code&gt;shift(-1)&lt;/code&gt;) that had turned a losing strategy into a Sharpe 1.16. Part 2 added the missing cost stack — 7 bps per side, next-bar-open fills — and the corrected strategy landed at &lt;strong&gt;-22.04% total return, Sharpe -0.32, max drawdown -41.58%&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This final part asks the question that remains after the code is honest: were the parameters ever evidence, or were they just the best-looking point on the data they were tested on?&lt;/p&gt;

&lt;p&gt;A note on the data, same as parts 1 and 2: the price series is &lt;strong&gt;synthetic&lt;/strong&gt; — a seeded random walk, 1,500 daily bars, generated by the audit script itself (&lt;code&gt;demo_audit.py&lt;/code&gt;, numbers in &lt;code&gt;results.json&lt;/code&gt;). Every figure below reproduces exactly. The strategy is a plain 5/20 moving-average crossover.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failure 1: no holdout was ever reserved
&lt;/h2&gt;

&lt;p&gt;The submitted run selected its parameters (5/20) on the full sample and then reported performance on that same sample. That is grading an exam with the answer key open: the selection step has already seen every bar the evaluation step will use.&lt;/p&gt;

&lt;p&gt;The corrected audit splits the sample before judging anything:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Segment&lt;/th&gt;
&lt;th&gt;Total return&lt;/th&gt;
&lt;th&gt;Sharpe&lt;/th&gt;
&lt;th&gt;Max drawdown&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;In-sample (first 70%)&lt;/td&gt;
&lt;td&gt;-20.46%&lt;/td&gt;
&lt;td&gt;-0.39&lt;/td&gt;
&lt;td&gt;-41.58%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Holdout (last 30%, untouched during selection)&lt;/td&gt;
&lt;td&gt;+1.38%&lt;/td&gt;
&lt;td&gt;+0.09&lt;/td&gt;
&lt;td&gt;-7.99%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Read the holdout line carefully. +0.09 Sharpe on 30% of the sample is not a small edge — it is statistically indistinguishable from zero. And the in-sample segment, the very data the parameters were chosen on, is &lt;em&gt;negative&lt;/em&gt; once the look-ahead and costs are fixed. A parameter set that cannot win on its own selection data, and ties zero on unseen data, was never a finding. It was a coincidence with a Sharpe ratio attached.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failure 2: the sensitivity table has no plateau
&lt;/h2&gt;

&lt;p&gt;A robust parameter choice sits on a plateau: nudge the parameters and the result wobbles but survives. A fragile one sits on a spike — or, as here, at the least-bad point of a losing neighbourhood.&lt;/p&gt;

&lt;p&gt;The corrected run's sensitivity table (Sharpe, full corrected harness):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Fast/Slow&lt;/th&gt;
&lt;th&gt;Sharpe&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;3/10&lt;/td&gt;
&lt;td&gt;-0.55&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5/20 (chosen)&lt;/td&gt;
&lt;td&gt;-0.32&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10/40&lt;/td&gt;
&lt;td&gt;-0.67&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5/50&lt;/td&gt;
&lt;td&gt;-0.55&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20/60&lt;/td&gt;
&lt;td&gt;-0.38&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Every neighbouring parameter set loses. The chosen 5/20 is not the peak of a profitable region; it is the shallowest point in a basin where all five tested sets are negative, spanning -0.32 to -0.67. After the look-ahead is removed, there is no parameter region on this data that rescues the strategy — which is exactly the information a sensitivity table exists to deliver &lt;em&gt;before&lt;/em&gt; money is committed, not after.&lt;/p&gt;

&lt;p&gt;This is the part of an audit people resist most, because it cannot be fixed by trying harder. Testing more parameter sets on the same sample does not create a plateau; it just searches the same noise more thoroughly and makes the winner look more deliberate than it is.&lt;/p&gt;

&lt;h2&gt;
  
  
  The order of operations that prevents both failures
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Split first, search second.&lt;/strong&gt; Reserve the holdout before any parameter is tried. The moment a holdout number influences a choice, it stops being a holdout.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Report both segments side by side.&lt;/strong&gt; In-sample and holdout in the same table, every time. A single blended number hides exactly the gap this audit found.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Demand a plateau, not a peak.&lt;/strong&gt; Require neighbouring parameter sets to stay within a stated tolerance of the chosen result. All-negative neighbours mean the answer is "no edge here", whatever the headline number says.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Treat a near-zero holdout as a verdict, not a teaser.&lt;/strong&gt; +0.09 does not mean "promising, needs tuning". It means the selection data carried the result, and the honest next step is a different hypothesis — not a finer grid.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The audit, closed
&lt;/h2&gt;

&lt;p&gt;Across the three parts, one backtest went from &lt;strong&gt;+140.37% / Sharpe 1.16&lt;/strong&gt; to &lt;strong&gt;-22.04% / Sharpe -0.32&lt;/strong&gt;, with no holdout evidence and no parameter plateau underneath it. None of the three failures required exotic mathematics to find: a shift audit, a cost stack, a sample split, and a five-row sensitivity table. What they required was running the checks before believing the number — which is the whole job.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;The demo strategy and data are synthetic and reproducible from the audit script; nothing here is a claim about any real market or instrument, and nothing in this post is investment advice.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>trading</category>
      <category>testing</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>The Costs Your Backtest Forgot: 7 bps a Side Turned -17% Into -22%</title>
      <dc:creator>AgentMarts</dc:creator>
      <pubDate>Fri, 04 Dec 2026 13:52:39 +0000</pubDate>
      <link>https://dev.to/agentmarts/the-costs-your-backtest-forgot-7-bps-a-side-turned-17-into-22-453o</link>
      <guid>https://dev.to/agentmarts/the-costs-your-backtest-forgot-7-bps-a-side-turned-17-into-22-453o</guid>
      <description>&lt;h1&gt;
  
  
  The Costs Your Backtest Forgot: 7 bps a Side Turned -17% Into -22%
&lt;/h1&gt;

&lt;p&gt;In part 1 of this series, one line of code — a &lt;code&gt;shift(-1)&lt;/code&gt; — turned a losing strategy into a Sharpe 1.16. Fixing it took the same strategy, on the same data, from &lt;strong&gt;+140.37% to -17.09%&lt;/strong&gt; before costs.&lt;/p&gt;

&lt;p&gt;This post is about what happened next. Adding a realistic cost stack and honest fills took it the rest of the way: &lt;strong&gt;-17.09% → -22.04%, Sharpe -0.32, max drawdown -41.58%&lt;/strong&gt; (versus the -15.15% drawdown the original run reported).&lt;/p&gt;

&lt;p&gt;Costs were not the biggest lie in this backtest. They were the second-biggest — and in most backtests I audit, they are the one nobody even knows is missing.&lt;/p&gt;

&lt;p&gt;A note on the data, same as part 1: the price series is &lt;strong&gt;synthetic&lt;/strong&gt; — a seeded random walk, 1,500 daily bars, generated by the audit script itself (&lt;code&gt;demo_audit.py&lt;/code&gt;, numbers in &lt;code&gt;results.json&lt;/code&gt;). Every figure below reproduces exactly. The strategy is a plain 5/20 moving-average crossover with &lt;strong&gt;88 position changes&lt;/strong&gt; over the sample.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lie 1: zero costs
&lt;/h2&gt;

&lt;p&gt;The submitted run modelled no costs at all. No commission, no spread, no slippage. Every one of the 88 position changes was free.&lt;/p&gt;

&lt;p&gt;The corrected run charges &lt;strong&gt;7 bps per side&lt;/strong&gt; — 2 bps commission plus 5 bps half-spread/slippage. That is not a punitive assumption; for a liquid instrument it is close to optimistic. Applied across 88 changes, it moved the result by roughly five percentage points of total return.&lt;/p&gt;

&lt;p&gt;The arithmetic most people skip: cost drag scales with &lt;em&gt;turnover&lt;/em&gt;, not with how good the strategy looks. A strategy that changes position 88 times pays the toll 88 times. A prettier equity curve does not reduce the toll — usually it is produced by the trading frequency that raises it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lie 2: filling at the close that generated the signal
&lt;/h2&gt;

&lt;p&gt;The submitted run also filled at the same close that produced the signal. In live trading that fill does not exist: you learn the signal &lt;em&gt;from&lt;/em&gt; that close, so the earliest honest fill is the next bar.&lt;/p&gt;

&lt;p&gt;The corrected run fills at the &lt;strong&gt;next bar's open&lt;/strong&gt;, held to that bar's close. Same-close fills flatter every close-based strategy, because the close that confirms your signal has already moved in your favour — you are booking a price that was only knowable after it printed.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rebuilt cost stack
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;As submitted&lt;/th&gt;
&lt;th&gt;Corrected&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Total return&lt;/td&gt;
&lt;td&gt;+140.37%&lt;/td&gt;
&lt;td&gt;−22.04%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sharpe&lt;/td&gt;
&lt;td&gt;1.16&lt;/td&gt;
&lt;td&gt;−0.32&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Max drawdown&lt;/td&gt;
&lt;td&gt;−15.15%&lt;/td&gt;
&lt;td&gt;−41.58%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Trades (position changes)&lt;/td&gt;
&lt;td&gt;88&lt;/td&gt;
&lt;td&gt;88&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Costs modelled&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;7 bps/side&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fill assumption&lt;/td&gt;
&lt;td&gt;signal close&lt;/td&gt;
&lt;td&gt;next bar's open&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two details in that table matter more than the headline numbers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The drawdown more than doubled&lt;/strong&gt; (-15.15% → -41.58%). Cost-free, same-close backtests do not just overstate returns — they understate how bad the bad stretches feel, which is the number that decides whether you would have held the strategy at all.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Costs were secondary to the look-ahead fix.&lt;/strong&gt; Of the total correction, removing the &lt;code&gt;shift(-1)&lt;/code&gt; did most of the damage; the cost stack added about five points. That ordering is typical — and it is why "we roughly accounted for costs" is not a defence when the timing contract is broken.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The three-line version
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="no"&gt;COST_BPS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;  &lt;span class="c1"&gt;# 2 commission + 5 half-spread/slippage, per side&lt;/span&gt;
&lt;span class="n"&gt;turnover&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;diff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prepend&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pnl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;next_bar_returns&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;turnover&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;COST_BPS&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10_000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your harness cannot point to the lines where turnover is charged and where the fill price is chosen, those costs are zero and those fills are fantasy — whether or not anyone intended them to be.&lt;/p&gt;

&lt;h2&gt;
  
  
  The checks (cost edition)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Find the cost lines.&lt;/strong&gt; Search the code for where commission/spread/slippage are subtracted. No lines, no costs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Count the tolls.&lt;/strong&gt; Multiply per-side cost by actual position changes, not by "round trips" estimated in your head.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Audit the fill timestamp.&lt;/strong&gt; The fill must be strictly after the information that triggered it. Same-bar fills on close-generated signals fail by construction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Re-run with costs doubled.&lt;/strong&gt; If doubling a realistic cost assumption kills the strategy, the edge was thinner than the toll booth.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Report drawdown from the corrected run only.&lt;/strong&gt; The flattering drawdown is the one that makes you size up.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Part 3 will take on the last failure in this audit: parameters chosen on the full sample, with no holdout — and a sensitivity table where every neighbouring parameter set loses.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;The demo strategy and data are synthetic and reproducible from the audit script; nothing here is a claim about any real market or instrument, and nothing in this post is investment advice.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>trading</category>
      <category>testing</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Your Backtest Is Lying to You: The One-Line Bug That Turned +140% Into -22%</title>
      <dc:creator>AgentMarts</dc:creator>
      <pubDate>Fri, 04 Dec 2026 13:35:09 +0000</pubDate>
      <link>https://dev.to/agentmarts/your-backtest-is-lying-to-you-the-one-line-bug-that-turned-140-into-22-481d</link>
      <guid>https://dev.to/agentmarts/your-backtest-is-lying-to-you-the-one-line-bug-that-turned-140-into-22-481d</guid>
      <description>&lt;p&gt;Here is a backtest result: &lt;strong&gt;+140.37% total return, Sharpe 1.16, max drawdown -15.15%.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is the same strategy, on the same data, after an audit: &lt;strong&gt;-22.04%, Sharpe -0.32, max drawdown -41.58%.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Nothing about the strategy changed. What changed is that the test harness stopped cheating. This post walks through the exact bug, shows the flawed and corrected code side by side, and ends with the checks I now run on every backtest before I believe a single number in it.&lt;/p&gt;

&lt;p&gt;A note on the data, up front: the price series is &lt;strong&gt;synthetic&lt;/strong&gt; — a seeded random walk with regime shifts, 1,500 daily bars, generated by the script itself. That is deliberate. The point is not whether a 5/20 moving-average crossover "works" on some real asset. The point is that the harness bug below inflates &lt;em&gt;any&lt;/em&gt; strategy run through it, and synthetic data lets you reproduce every number in this post yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  The strategy
&lt;/h2&gt;

&lt;p&gt;A plain 5/20 moving-average crossover: long when the fast average is above the slow one, flat otherwise. 88 position changes over the sample. Nothing exotic — which is exactly why it makes a good demonstration. If the harness can turn this into a Sharpe 1.16, it can turn your strategy into one too.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bug
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# FLAWED — as submitted&lt;/span&gt;
&lt;span class="n"&gt;sig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ma_f&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ma_s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;astype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;   &lt;span class="c1"&gt;# &amp;lt;-- this line&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;shift(-1)&lt;/code&gt; moves each value one row &lt;em&gt;earlier&lt;/em&gt;. So the position held on day &lt;em&gt;t&lt;/em&gt; is set by the signal computed on day &lt;em&gt;t+1&lt;/em&gt;. The strategy is trading on tomorrow's information, every single day, for 1,500 bars.&lt;/p&gt;

&lt;p&gt;It is an easy bug to write. You are aligning arrays, something looks off by one, you nudge it the wrong way, and the equity curve immediately looks better — which is precisely why it survives code review. Bugs that make results worse get found. Bugs that make results better get shipped.&lt;/p&gt;

&lt;p&gt;The corrected version states the timing contract in plain language — decide at the close of day &lt;em&gt;t&lt;/em&gt;, hold from day &lt;em&gt;t+1&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# CORRECTED&lt;/span&gt;
&lt;span class="n"&gt;sig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ma_f&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ma_s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;astype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;  &lt;span class="c1"&gt;# decide t, hold t+1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What else the audit found
&lt;/h2&gt;

&lt;p&gt;Look-ahead was the largest correction, but not the only one. The submitted run also:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Modelled zero costs.&lt;/strong&gt; No commission, no spread, no slippage. The corrected run charges 7 bps per side (2 bps commission + 5 bps half-spread/slippage). With 88 position changes, costs alone move the corrected result from -17.09% to -22.04% — material, but secondary. Fix your timing first.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Filled at the signal bar's close.&lt;/strong&gt; The corrected run fills at the next bar's open. If your strategy decides using a close price and also fills at that same close, that is look-ahead wearing a different hat.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Had no holdout.&lt;/strong&gt; Parameters were selected on the full sample, then "tested" on the same sample. Split after correction: in-sample (first 70%) Sharpe -0.39; untouched holdout (last 30%) Sharpe +0.09 — indistinguishable from zero.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sat on a parameter spike, not a plateau.&lt;/strong&gt; Neighbouring parameter sets, after correction, are uniformly negative: 3/10 -0.55, 5/20 -0.32, 10/40 -0.67, 5/50 -0.55, 20/60 -0.38. The chosen parameters were not a robust region; they were the least-bad point in a losing neighbourhood, visible only once the look-ahead was removed.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The full picture
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;As submitted&lt;/th&gt;
&lt;th&gt;Corrected&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Total return&lt;/td&gt;
&lt;td&gt;+140.37%&lt;/td&gt;
&lt;td&gt;-22.04%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sharpe&lt;/td&gt;
&lt;td&gt;1.16&lt;/td&gt;
&lt;td&gt;-0.32&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Max drawdown&lt;/td&gt;
&lt;td&gt;-15.15%&lt;/td&gt;
&lt;td&gt;-41.58%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Position changes&lt;/td&gt;
&lt;td&gt;88&lt;/td&gt;
&lt;td&gt;88&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Costs modelled&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;7 bps/side&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  The five checks
&lt;/h2&gt;

&lt;p&gt;These are the checks I run, in this order, on any backtest — mine or a client's — before discussing whether the strategy is any good. The order matters: each check kills more strategies than the one after it.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Look-ahead scan.&lt;/strong&gt; Trace every input to the timestamp at which it was actually knowable. Bar timing, indicator warm-up (no full-sample statistics), fundamental data stamped by announcement date rather than period date. Add a unit test: perturb future bars and assert that today's signal does not change. If it changes, you have look-ahead, full stop.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Realistic costs, then double them.&lt;/strong&gt; Commission at your actual rate, at least half the typical spread per side, a stated slippage assumption, borrow costs for shorts. Then re-run with all costs doubled. If the strategy only survives at exactly-estimated costs, it does not survive live trading.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data integrity.&lt;/strong&gt; Does the dataset include delisted symbols, or only today's survivors? Are signal prices and fill prices adjusted the same way? Pick three trades by hand and verify them against a second source.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Holdout discipline.&lt;/strong&gt; Reserve the most recent 20–30% and touch it once, at the end, against an acceptance bar you wrote down &lt;em&gt;before&lt;/em&gt; running it. If the holdout fails and you re-tune until it passes, it is no longer a holdout — it is more development data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parameter plateau.&lt;/strong&gt; A result that only exists at one parameter point is a coincidence with good marketing. Require the neighbourhood to work too, within a tolerance you state in advance.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every check ends in a plain verdict — PASS, FIX, or STOP — and the final verdict is mechanical. You do not negotiate with your own scorecard.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reproduce it
&lt;/h2&gt;

&lt;p&gt;The demo script that generated every number above (&lt;code&gt;demo_audit.py&lt;/code&gt;, with its &lt;code&gt;results.json&lt;/code&gt; output) is the same code I use as the worked example in my audit samples. If your harness disagrees with any number here, I would genuinely like to hear about it in the comments — that is the conversation worth having.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post is about testing methodology. It is not financial advice, not a trading signal, and it does not predict or promise any return. The strategy and data are a synthetic demonstration built to expose a harness bug.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>trading</category>
      <category>testing</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
