<?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: xiaolong</title>
    <description>The latest articles on DEV Community by xiaolong (@xiaolong_208ee1253b5cbd6d_51).</description>
    <link>https://dev.to/xiaolong_208ee1253b5cbd6d_51</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%2F4266226%2F13400ba8-67b3-46f9-ac49-6e1a74981abe.png</url>
      <title>DEV Community: xiaolong</title>
      <link>https://dev.to/xiaolong_208ee1253b5cbd6d_51</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xiaolong_208ee1253b5cbd6d_51"/>
    <language>en</language>
    <item>
      <title>How I caught look-ahead bias in my own backtests — and built a free checker</title>
      <dc:creator>xiaolong</dc:creator>
      <pubDate>Thu, 26 Nov 2026 08:19:37 +0000</pubDate>
      <link>https://dev.to/xiaolong_208ee1253b5cbd6d_51/how-i-caught-look-ahead-bias-in-my-own-backtests-and-built-a-free-checker-6i3</link>
      <guid>https://dev.to/xiaolong_208ee1253b5cbd6d_51/how-i-caught-look-ahead-bias-in-my-own-backtests-and-built-a-free-checker-6i3</guid>
      <description>&lt;h1&gt;
  
  
  How I caught look-ahead bias in my own backtests — and built a free checker
&lt;/h1&gt;

&lt;p&gt;A backtest that looks too good is usually telling you something about the backtest, not about the market.&lt;/p&gt;

&lt;p&gt;I learned this the boring way, by auditing our own research pipeline. The code looked fine. Every signal had its lag and every join had a date filter. The audit still found a leak: our fundamentals were filtered by SEC &lt;strong&gt;filing date&lt;/strong&gt;, and a filing date is just a calendar day. Treated as midnight, a report that was actually published after the close that evening looked "known" from the start of the day, hours before anyone could have read it. The same audit flagged two neighbours of that bug. The daily bars were raw, so a stock split looks like a giant one-day move unless it is handled separately. And a stock list can't prove it still contains the companies that later delisted.&lt;/p&gt;

&lt;p&gt;None of these show up when you read the code line by line. They are about &lt;strong&gt;time&lt;/strong&gt;, what was known &lt;em&gt;when&lt;/em&gt;, and code review is bad at seeing time. So we started checking timestamps instead of reading code, and eventually turned that check into a small free API.&lt;/p&gt;

&lt;p&gt;This article walks through:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;what look-ahead bias is, in plain words&lt;/li&gt;
&lt;li&gt;why it makes backtests look so good&lt;/li&gt;
&lt;li&gt;a small runnable example where one missing &lt;code&gt;.shift(1)&lt;/code&gt; turns a random walk into a "Sharpe 3.28" strategy&lt;/li&gt;
&lt;li&gt;how to catch it automatically by auditing timestamps, using the free NorthStar Backtest Audit API&lt;/li&gt;
&lt;li&gt;the before/after comparison&lt;/li&gt;
&lt;li&gt;where to get the code&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Everything below runs on &lt;strong&gt;synthetic&lt;/strong&gt; prices. Nothing here is a trading idea, a recommendation or a performance claim.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What look-ahead bias is (plain version)
&lt;/h2&gt;

&lt;p&gt;Look-ahead bias is when a backtest makes a decision using information that did not exist yet at the moment the decision was made.&lt;/p&gt;

&lt;p&gt;In real life you only know the past. At 09:30 on a Monday you know Friday's close, but not Monday's. A backtest is just a loop over historical rows, though, and every row is in memory, including future ones. Nothing stops the code from reading tomorrow's newspaper. It takes one index that is off by one.&lt;/p&gt;

&lt;p&gt;Common ways it sneaks in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Same-bar signals.&lt;/strong&gt; A signal computed from today's close, used to trade at today's open.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A missing &lt;code&gt;.shift(1)&lt;/code&gt;&lt;/strong&gt; after a rolling indicator, so row &lt;em&gt;t&lt;/em&gt; of the signal already contains bar &lt;em&gt;t&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fundamentals joined by period end instead of filing date.&lt;/strong&gt; A quarter ending March 31 is usually not public until late April or May. Join it on March 31 and the backtest reads earnings weeks early.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Restated data.&lt;/strong&gt; Using today's corrected numbers for a date when only the original numbers had been published.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Universe chosen after the fact.&lt;/strong&gt; Testing "stocks that exist today" over the last ten years. That is survivorship bias, a form of look-ahead: the list itself encodes who survived.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overlapping ML labels.&lt;/strong&gt; If the label is a 5-day forward return, the last training labels reach into the test period. The model has partly seen the test set.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Why it makes the backtest look so good
&lt;/h2&gt;

&lt;p&gt;Markets are noisy. A real edge, if there is one, is small, and most of each day's move is unpredictable. Look-ahead bias swaps part of that unpredictable move for the answer.&lt;/p&gt;

&lt;p&gt;If the signal can see even part of the bar it trades, it is right more often than chance, losses shrink and the equity curve smooths out. Every number you would normally use to judge a strategy gets better together: return, Sharpe ratio, hit rate, drawdown. That is what makes it dangerous. A leaky backtest does not look broken. It looks &lt;em&gt;excellent&lt;/em&gt;, and excellent results are the ones we are least motivated to question.&lt;/p&gt;

&lt;p&gt;Then the strategy goes live, where the future really is unavailable, and performance falls back to whatever the honest version would have shown.&lt;/p&gt;

&lt;p&gt;A useful rule of thumb: when a simple idea on daily bars shows a Sharpe ratio above 2 or 3, check the timing before anything else.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. A real example: &lt;code&gt;bad_backtest.py&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;To make this concrete, the companion GitHub repository has a deliberately broken backtest in &lt;code&gt;sample_data/bad_backtest.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The setup is intentionally boring:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prices:&lt;/strong&gt; a seeded random walk with &lt;em&gt;no drift&lt;/em&gt;. There is nothing to predict, by construction. Each day has an open at 09:30 and a close at 16:00 New York time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strategy:&lt;/strong&gt; a toy trend filter. "Hold the asset during the session if price is above its 20-day average." Enter at the open, exit at the close.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the core of it:&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="n"&gt;sma&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"close"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rolling&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;above&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"close"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;sma&lt;/span&gt;          &lt;span class="c1"&gt;# uses close[t] and the SMA up to t&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;leak&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# BUG: row t already contains information from 16:00 on day t,&lt;/span&gt;
    &lt;span class="c1"&gt;# but we act on it at 09:30 on day t.&lt;/span&gt;
    &lt;span class="n"&gt;signal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;above&lt;/span&gt;
    &lt;span class="n"&gt;feature_ts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"close_ts"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# FIX: at the open of day t we only know close[t-1].&lt;/span&gt;
    &lt;span class="n"&gt;signal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;above&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="n"&gt;feature_ts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"close_ts"&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="n"&gt;session_ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"close"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"open"&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="n"&gt;strat_ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;session_ret&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The leaky version decides whether to hold during day &lt;em&gt;t&lt;/em&gt; by checking whether day &lt;em&gt;t&lt;/em&gt;'s &lt;strong&gt;close&lt;/strong&gt; is above its average, and then pretends to enter at day &lt;em&gt;t&lt;/em&gt;'s &lt;strong&gt;open&lt;/strong&gt;, six and a half hours earlier. If today's close is above the average, today was more likely an up day, so the strategy "knows" to be in on up days.&lt;/p&gt;

&lt;p&gt;The fix is one &lt;code&gt;.shift(1)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On the same synthetic prices, the leaky backtest reports:&lt;/p&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;Leaky&lt;/th&gt;
&lt;th&gt;Fixed&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;82.2%&lt;/td&gt;
&lt;td&gt;0.5%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Annualized Sharpe&lt;/td&gt;
&lt;td&gt;3.28&lt;/td&gt;
&lt;td&gt;0.07&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hit rate&lt;/td&gt;
&lt;td&gt;63.0%&lt;/td&gt;
&lt;td&gt;50.8%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A Sharpe of 3.28 on a driftless random walk. Every bit of it comes from the bug. The fixed version looks exactly like noise, which is what the data is.&lt;/p&gt;

&lt;h3&gt;
  
  
  The part that matters: a decision log
&lt;/h3&gt;

&lt;p&gt;Notice that the backtest also records &lt;code&gt;feature_ts&lt;/code&gt;. Besides P&amp;amp;L, each run writes a &lt;strong&gt;decision log&lt;/strong&gt;, one row per decision:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;event_time&lt;/code&gt;: when the decision is acted on (here, the 09:30 open of day &lt;em&gt;t&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;feature_time&lt;/code&gt;: the timestamp of the newest data point that went into it
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;decision_log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"event_time"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"open_ts"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isoformat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="s2"&gt;"feature_time"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;feature_ts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isoformat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A decision is only honest if &lt;code&gt;feature_time &amp;lt; event_time&lt;/code&gt;. In the leaky run, row 0 says "acted at 2022-01-28 09:30 using data stamped 2022-01-28 16:00". Written down like that, the bug is obvious. Buried in a pandas expression, it was not.&lt;/p&gt;

&lt;p&gt;This is the main idea of the article: &lt;strong&gt;you can't easily review code for time, but you can check timestamps.&lt;/strong&gt; If every decision writes down &lt;em&gt;when it acted&lt;/em&gt; and &lt;em&gt;when its newest input became known&lt;/em&gt;, catching look-ahead becomes a simple comparison.&lt;/p&gt;

&lt;p&gt;One detail is easy to get wrong: &lt;code&gt;feature_time&lt;/code&gt; should be when the data became &lt;strong&gt;available&lt;/strong&gt;, not the label it carries. A daily bar dated 2022-01-28 is available at 16:00 that day, not at midnight. A quarterly report for the period ending March 31 is available on its filing date, not on March 31.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Checking it with the NorthStar Backtest Audit API
&lt;/h2&gt;

&lt;p&gt;You could write that comparison yourself in five lines, and for a single script you probably should. We built a small, free API for it anyway, for two reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Independence.&lt;/strong&gt; If the code that has the bug also checks for it, the same mistake can easily hide in both places. A separate checker that only sees your timestamps can't share your bug.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;More than one rule.&lt;/strong&gt; Besides the per-row timing rule, it checks train/test label overlap (purging), an optional minimum lag, and an embargo after test folds. Those are easy to get slightly wrong by hand.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is a plain HTTP endpoint with no API key and no sign-up. The limit is 100 requests per IP per day, up to 2000 rows and 64 KB per request. A minimal call:&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="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;

&lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="c1"&gt;# acted Monday 09:30 using Monday's 16:00 close: impossible&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"event_time"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2024-03-01T09:30:00-05:00"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"feature_time"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2024-03-01T16:00:00-05:00"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;# acted Monday 09:30 using the previous Friday's close: fine&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"event_time"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2024-03-04T09:30:00-05:00"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"feature_time"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2024-03-01T16:00:00-05:00"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://app.xuanduzhixuan.com/audit/lookahead"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"rows"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Trimmed response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"ok"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"failed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"feature_status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"failed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"purge_status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"not_checked"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"violations"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"row"&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="nl"&gt;"reason"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"feature_not_available_in_time"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"rows"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"disclaimer"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Data tool only. Not investment advice."&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Row 0 is flagged, row 1 passes. &lt;code&gt;purge_status&lt;/code&gt; is &lt;code&gt;not_checked&lt;/code&gt; because these rows have no train/test fold information, so for pure signal timing the field to read is &lt;code&gt;feature_status&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Timestamps must include a timezone (or be Unix seconds). A naive &lt;code&gt;2024-03-01 09:30&lt;/code&gt; is rejected instead of guessed, because guessing the timezone is a classic way to introduce exactly the bug you are looking for.&lt;/p&gt;

&lt;h3&gt;
  
  
  Running it on the example
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;audit_demo.py&lt;/code&gt; in the repo runs both backtests, sends each decision log to the endpoint and prints what the backtest claims next to what the audit finds. This is real output against the live API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LEAKY backtest (uses today's close at today's open)
  backtest says : total return 82.2%, Sharpe 3.28, hit rate 63.0%
  audit says    : feature_status = 'failed', 481 of 481 decisions used data from the future
  e.g. row 0: acted at 2022-01-28T09:30:00-05:00 using data stamped 2022-01-28T16:00:00-05:00

FIXED backtest (signal shifted by one bar)
  backtest says : total return 0.5%, Sharpe 0.07, hit rate 50.8%
  audit says    : feature_status = 'passed', 0 of 480 decisions used data from the future
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every single decision in the leaky run fails, and the audit points to the exact row and the two timestamps that conflict.&lt;/p&gt;

&lt;h3&gt;
  
  
  The ML version: label overlap
&lt;/h3&gt;

&lt;p&gt;The second half of the demo covers a bug that is subtler and very common in ML backtests. Each sample's label is the return over the next 5 sessions. The data is split 70/30 into train and test by time. That looks clean, but the last few training samples have labels that are only known &lt;em&gt;after&lt;/em&gt; the test period has started.&lt;/p&gt;

&lt;p&gt;For this check, each row also carries &lt;code&gt;label_end&lt;/code&gt; (when the label is fully known), &lt;code&gt;split&lt;/code&gt; (&lt;code&gt;train&lt;/code&gt; or &lt;code&gt;test&lt;/code&gt;) and a fold id:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NAIVE  split: 346 train rows, 149 test rows
  audit says    : status = 'failed', purge_status = 'failed'
  4 training rows have labels that overlap the test period (10 train/test pairs, reason: 'label_overlap')
  e.g. train row 345: label known only at 2023-05-08T16:00:00-04:00, test starts at 2023-05-03T09:30:00-04:00

PURGED split: 342 train rows, 149 test rows
  audit says    : status = 'passed', purge_status = 'passed'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Dropping the four overlapping training rows (purging) fixes it. Four rows out of 346 sounds harmless, but with more overlap, longer horizons or many folds, this kind of leak can inflate cross-validation scores noticeably.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Before and after
&lt;/h2&gt;

&lt;p&gt;Here is everything side by side. Same synthetic prices, same strategy idea, same code apart from the fixes:&lt;/p&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;Leaky backtest&lt;/th&gt;
&lt;th&gt;Fixed backtest&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;The fix&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;&lt;code&gt;signal.shift(1)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Total return&lt;/td&gt;
&lt;td&gt;82.2%&lt;/td&gt;
&lt;td&gt;0.5%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Annualized Sharpe&lt;/td&gt;
&lt;td&gt;3.28&lt;/td&gt;
&lt;td&gt;0.07&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hit rate&lt;/td&gt;
&lt;td&gt;63.0%&lt;/td&gt;
&lt;td&gt;50.8%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Timing audit (&lt;code&gt;feature_status&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;failed: 481 of 481 decisions&lt;/td&gt;
&lt;td&gt;passed: 0 of 480&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&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;Naive ML split&lt;/th&gt;
&lt;th&gt;Purged ML split&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;The fix&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;drop the 4 training rows whose labels reach into the test period&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Train / test rows&lt;/td&gt;
&lt;td&gt;346 / 149&lt;/td&gt;
&lt;td&gt;342 / 149&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Purge audit (&lt;code&gt;purge_status&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;failed: 10 overlapping train/test pairs&lt;/td&gt;
&lt;td&gt;passed&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two things are worth noticing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fixes are tiny.&lt;/strong&gt; One &lt;code&gt;.shift(1)&lt;/code&gt;. Four dropped rows. Neither change would stand out in a code review, and neither original bug did either. With timing bugs, the size of the code change tells you nothing about the size of the effect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The honest result is the boring one.&lt;/strong&gt; After the fix, the "strategy" looks exactly like what it is: a coin flip on a random walk. If your fixed backtest still shows an edge, good, but that is not proof. The audit only says that the timestamps you logged are consistent. It does not say the strategy works.&lt;/p&gt;

&lt;h3&gt;
  
  
  What "passed" does and does not mean
&lt;/h3&gt;

&lt;p&gt;The checker is deliberately narrow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It only sees the timestamps you send.&lt;/strong&gt; It does not read or run your code. If your decision log is wrong, for example a daily bar stamped at midnight instead of 16:00, the audit will pass a leaky backtest. Logging &lt;em&gt;availability&lt;/em&gt; time correctly is your job.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"Passed" means "no violations in the checked scope."&lt;/strong&gt; It does not cover survivorship bias, unadjusted splits, restated fundamentals or unrealistic fills. Those are the data problems our own audit kept running into, and you need point-in-time data and separate checks for them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You can reimplement it.&lt;/strong&gt; The rules are simple: strict feature-before-event per row, an optional minimum lag, label-interval overlap per fold, and an optional embargo. If you would rather not send timestamps anywhere, write the same checks locally. What matters is that your backtest &lt;em&gt;emits a decision log&lt;/em&gt; at all, so timing becomes something you can test.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A practical habit that came out of this: put the timing check in CI. Every backtest run writes its decision log, and the build fails if any row has &lt;code&gt;feature_time &amp;gt;= event_time&lt;/code&gt;. It costs a few lines and catches the next missing &lt;code&gt;.shift(1)&lt;/code&gt; before anyone gets excited about a chart.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Get the code
&lt;/h2&gt;

&lt;p&gt;Everything in this article is in one small repository:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GitHub: &lt;a href="https://github.com/xiaoxue-cyber/northstar-backtest-audit-demo" rel="noopener noreferrer"&gt;northstar-backtest-audit-demo&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;sample_data/bad_backtest.py&lt;/code&gt;: the leaky and fixed backtests, and the decision log&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;audit_demo.py&lt;/code&gt;: runs both, calls the audit API, prints the comparison above&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;notebook.ipynb&lt;/code&gt;: the same walkthrough as a notebook that runs in Google Colab
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/xiaoxue-cyber/northstar-backtest-audit-demo.git
&lt;span class="nb"&gt;cd&lt;/span&gt; northstar-backtest-audit-demo
pip install -r requirements.txt
python audit_demo.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The demo makes 4 requests to the free API, out of 100 per IP per day.&lt;/p&gt;

&lt;p&gt;Further reading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://app.xuanduzhixuan.com/docs/audit.html?utm_source=blog&amp;amp;utm_medium=article&amp;amp;utm_campaign=backtest_audit" rel="noopener noreferrer"&gt;Backtest Audit API docs&lt;/a&gt;: request format, all rules, limits&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://app.xuanduzhixuan.com/data-sources?utm_source=blog&amp;amp;utm_medium=article&amp;amp;utm_campaign=backtest_audit" rel="noopener noreferrer"&gt;How our point-in-time data is built&lt;/a&gt;: sources, filing-date timestamps, delisted companies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you find a mistake in the demo, open an issue. I would rather fix the example than have it teach the wrong thing.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Disclosure: I'm part of the NorthStar team, which runs the Backtest Audit API. The API is free and needs no key. All data in this article and the repository is synthetic. This is a data-tool article, not investment advice.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>backtesting</category>
      <category>python</category>
      <category>quant</category>
      <category>algotrading</category>
    </item>
  </channel>
</rss>
