<?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: Manel Fernández</title>
    <description>The latest articles on DEV Community by Manel Fernández (@manel_fernndez_9fb74ffe4).</description>
    <link>https://dev.to/manel_fernndez_9fb74ffe4</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%2F4263449%2F44d458d4-e1c5-45d3-8861-ab66018d08b8.png</url>
      <title>DEV Community: Manel Fernández</title>
      <link>https://dev.to/manel_fernndez_9fb74ffe4</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manel_fernndez_9fb74ffe4"/>
    <language>en</language>
    <item>
      <title>Your Backtest Is Probably Lying to You: 4 Point-In-Time Traps in Python Trading Bots</title>
      <dc:creator>Manel Fernández</dc:creator>
      <pubDate>Sat, 28 Nov 2026 17:32:37 +0000</pubDate>
      <link>https://dev.to/manel_fernndez_9fb74ffe4/your-backtest-is-probably-lying-to-you-4-point-in-time-traps-in-python-trading-bots-484a</link>
      <guid>https://dev.to/manel_fernndez_9fb74ffe4/your-backtest-is-probably-lying-to-you-4-point-in-time-traps-in-python-trading-bots-484a</guid>
      <description>&lt;p&gt;Your backtest probably isn't wrong because of the strategy. It's wrong because of &lt;em&gt;time&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Look-ahead bias means using, at decision time, information that wasn't available yet. Everyone knows the textbook version ("don't use tomorrow's close"). The versions that actually bite are quieter. This post walks through the ones I hit while building a small Python trading bot, and the code I now use to catch them. Everything below runs with zero dependencies using &lt;a href="https://github.com/Manelofthh/pit-guard" rel="noopener noreferrer"&gt;pit-guard&lt;/a&gt;, a small MIT library I extracted from that bot.&lt;/p&gt;

&lt;h2&gt;
  
  
  The one rule
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;available_utc&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;decision_utc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every bar and every news item carries the moment &lt;em&gt;you&lt;/em&gt; could first have seen it. If that's later than the decision, the data point doesn't exist for that decision. That's the whole idea. The work is in getting &lt;code&gt;available_utc&lt;/code&gt; right.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 1: today's daily candle
&lt;/h2&gt;

&lt;p&gt;Your bot runs at 15:30 New York. Your data provider happily returns a daily bar dated today, with open, high, low and "close". That close is the last trade so far, not the close. Feed it into a signal and your backtest (which used the real close) and your live run (which used a 15:30 price) are two different systems.&lt;/p&gt;

&lt;p&gt;The fix is to give every daily bar a &lt;code&gt;candle_end_utc&lt;/code&gt; equal to the &lt;strong&gt;official&lt;/strong&gt; close of that session, and refuse it before then:&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;from&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timedelta&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timezone&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pit_guard&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Candle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;validate_candle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nyse_close_utc&lt;/span&gt;

&lt;span class="n"&gt;day&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2026&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;decision&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2026&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;24&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="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tzinfo&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;timezone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 15:30 New York&lt;/span&gt;

&lt;span class="n"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Candle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"AAPL"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;231.4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="n"&gt;candle_end_utc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;nyse_close_utc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="n"&gt;available_utc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;decision&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nf"&gt;timedelta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;minutes&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;validate_candle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decision&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# ['AVAILABLE_BEFORE_CANDLE_END', 'INTRADAY_INCOMPLETE_DAILY_CANDLE']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two codes, two different problems: the bar claims to be available before it could exist, and the session hasn't closed yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 2: "16:00" isn't always the close
&lt;/h2&gt;

&lt;p&gt;Hard-coding 16:00 New York works until the day after Thanksgiving, when the NYSE closes at 13:00. A bar stamped 16:00 on an early-close day is wrong, and anything that compares times against it is wrong too. Same for holidays: a bar dated on a non-session day should never be silently accepted.&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;from&lt;/span&gt; &lt;span class="n"&gt;pit_guard&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;nyse_close_utc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;is_session&lt;/span&gt;

&lt;span class="nf"&gt;nyse_close_utc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2026&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;27&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 2026-11-27 18:00 UTC (13:00 New York)&lt;/span&gt;
&lt;span class="nf"&gt;is_session&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2026&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# False, Thanksgiving&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;pit-guard checks &lt;code&gt;candle_end_utc&lt;/code&gt; against the real close for that date and returns &lt;code&gt;CANDLE_END_UTC_MISMATCH_WITH_NYSE_CLOSE&lt;/code&gt; or &lt;code&gt;CANDLE_SESSION_IS_HOLIDAY&lt;/code&gt;. And for years the calendar doesn't cover, it raises &lt;code&gt;CALENDAR_NOT_COVERED&lt;/code&gt; instead of guessing. An unknown calendar is "not verifiable", not "normal day".&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 3: the stale series
&lt;/h2&gt;

&lt;p&gt;Your data source has a bad morning and the latest session is simply missing for some tickers. Nothing errors. Your indicators compute happily on data that ends two days ago, and the bot trades on it.&lt;/p&gt;

&lt;p&gt;I now treat that as a property of the whole series, not of individual bars:&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;from&lt;/span&gt; &lt;span class="n"&gt;pit_guard&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;validate_series&lt;/span&gt;

&lt;span class="n"&gt;accepted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rejected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;validate_series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bars&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decision_utc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# rejected == {"AAPL@series": ["STALE_DAILY_SERIES_MISSING_LATEST_SESSION"]}&lt;/span&gt;
&lt;span class="c1"&gt;# accepted == []   -&amp;gt; ticker excluded from this cycle&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"The bot didn't trade today" is useless. "12 of 100 tickers excluded for a stale series" is a data problem you can go and fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 4: news timestamped when &lt;em&gt;you&lt;/em&gt; fetched it
&lt;/h2&gt;

&lt;p&gt;For news, the useful time is when the item was &lt;strong&gt;published&lt;/strong&gt;, and it must also have been &lt;strong&gt;received&lt;/strong&gt; before the decision. Two failure modes are common:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The provider gives you no reliable publication time, so you fill it with your fetch time. Now every headline looks fresh.&lt;/li&gt;
&lt;li&gt;You re-fetch history later and store it as if you'd had it at the time.
&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="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pit_guard&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;NewsItem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;validate_news&lt;/span&gt;

&lt;span class="nf"&gt;validate_news&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NewsItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"n1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;published_utc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;received_utc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decision&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# ['UNVERIFIABLE_PROVIDER_PUBLICATION_TIME']&lt;/span&gt;

&lt;span class="nf"&gt;validate_news&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NewsItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"n2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;published_utc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;t0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;received_utc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;decision&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;timedelta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hours&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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decision&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# ['RETROSPECTIVE_CAPTURE_AFTER_DECISION']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A publication time &lt;em&gt;after&lt;/em&gt; the receipt time gets &lt;code&gt;EVENT_AFTER_RECEIPT&lt;/code&gt;. That's either a clock problem or a data problem, and neither should be traded on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why reason codes instead of filtering
&lt;/h2&gt;

&lt;p&gt;The tempting implementation is &lt;code&gt;bars = [b for b in bars if is_ok(b)]&lt;/code&gt;. It works, and you lose all information about &lt;em&gt;why&lt;/em&gt; data disappeared. Reason codes cost almost nothing and turn "weird day" into a countable, loggable event. In my bot every rejection is stored with its code, and the first thing I check after a quiet day is the rejection histogram.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use the same definition in backtest and live
&lt;/h2&gt;

&lt;p&gt;The last trap isn't in the data, it's in the code. If your backtester uses a looser notion of "available" than your live loop (say, the backtest uses daily closes and the live loop runs intraday), you're validating a system you don't run. Put the PIT checks in one module and call it from both.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;available_utc&lt;/code&gt; on every data point, timezone-aware, always.&lt;/li&gt;
&lt;li&gt;Daily bars end at the &lt;strong&gt;official&lt;/strong&gt; session close for that date, early closes included.&lt;/li&gt;
&lt;li&gt;Holidays and uncovered calendar years are rejections, not defaults.&lt;/li&gt;
&lt;li&gt;A series missing its latest complete session is stale as a whole.&lt;/li&gt;
&lt;li&gt;News needs a verifiable publication time and a receipt time before the decision.&lt;/li&gt;
&lt;li&gt;Every rejection gets a code.&lt;/li&gt;
&lt;li&gt;Backtest and live share the validation code.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;pit-guard is free and MIT: &lt;a href="https://github.com/Manelofthh/pit-guard" rel="noopener noreferrer"&gt;https://github.com/Manelofthh/pit-guard&lt;/a&gt; (27 tests, no dependencies).&lt;/p&gt;

&lt;p&gt;It comes from InversionBot V4, a self-hosted Python bot template for the Trading 212 &lt;strong&gt;demo&lt;/strong&gt; API with a full PIT data layer (30+ reason codes), fail-closed orders, Top-K ranking, a monthly Walk-Forward optimizer and 91 offline tests: &lt;a href="https://buy.stripe.com/7sY28k1mGgsh6LH8MJdby01" rel="noopener noreferrer"&gt;https://buy.stripe.com/7sY28k1mGgsh6LH8MJdby01&lt;/a&gt;. It's an engineering template, not financial advice, and makes no promise of returns.&lt;/p&gt;

</description>
      <category>python</category>
      <category>datascience</category>
      <category>testing</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Building a Trading Bot That Doesn't Lie to Itself: Point-In-Time Data, Fail-Closed Orders and Walk-Forward Tuning in Python</title>
      <dc:creator>Manel Fernández</dc:creator>
      <pubDate>Tue, 24 Nov 2026 21:07:07 +0000</pubDate>
      <link>https://dev.to/manel_fernndez_9fb74ffe4/building-a-trading-bot-that-doesnt-lie-to-itself-point-in-time-data-fail-closed-orders-and-53ah</link>
      <guid>https://dev.to/manel_fernndez_9fb74ffe4/building-a-trading-bot-that-doesnt-lie-to-itself-point-in-time-data-fail-closed-orders-and-53ah</guid>
      <description>&lt;p&gt;&lt;strong&gt;Disclosure:&lt;/strong&gt; I'm packaging this project as a paid template. This post is about the engineering, and every technique below works without it. Nothing here is financial advice, and I'm not claiming the strategy is profitable.&lt;/p&gt;




&lt;p&gt;I've spent the last few months building a small quant bot in Python that runs on a Debian VM under systemd, scans 100 liquid US stocks every 15 minutes and sends market orders to a &lt;strong&gt;Trading 212 practice (demo) account&lt;/strong&gt;. The strategy is deliberately boring: trend and momentum scoring, a −3 % stop-loss and a trailing stop.&lt;/p&gt;

&lt;p&gt;The interesting part was everything around the strategy. Three things bit me, and I suspect they bite most hobby bots:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Look-ahead bias&lt;/strong&gt; — using data at decision time that wasn't available yet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-idempotent execution&lt;/strong&gt; — the same order going out twice.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parameters tuned once and never re-validated.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's what I ended up with for each.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Point-In-Time (PIT) data: one rule, enforced everywhere
&lt;/h2&gt;

&lt;p&gt;The rule is short:&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;# Every data point carries the moment it became available.&lt;/span&gt;
&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;available_utc&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;decision_utc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What makes it hard is that "available" is not the timestamp most data sources give you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Daily candles.&lt;/strong&gt; A daily bar dated today isn't complete until the session closes. If your bot runs at 15:30 New York time and uses today's daily candle, its "close" is a price from the future. So each candle gets a &lt;code&gt;candle_end_utc&lt;/code&gt;, checked against the &lt;em&gt;real&lt;/em&gt; NYSE close for that date (holidays and early closes included), and an &lt;code&gt;available_utc&lt;/code&gt; that can't be earlier than that close. An incomplete intraday daily candle is rejected, not "used as best effort".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;News.&lt;/strong&gt; A headline's useful timestamp is when it was &lt;em&gt;published&lt;/em&gt;, and it must also have been &lt;em&gt;received&lt;/em&gt; before the decision. If the provider's publication time can't be verified, the item is rejected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stale series.&lt;/strong&gt; If the latest completed session is missing from a ticker's series, that ticker is excluded from the cycle rather than evaluated on old data.&lt;/p&gt;

&lt;p&gt;The design choice I'd recommend most: &lt;strong&gt;make every rejection explicit&lt;/strong&gt;. I ended up with 30+ reason codes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INTRADAY_INCOMPLETE_DAILY_CANDLE
CANDLE_END_UTC_MISMATCH_WITH_NYSE_CLOSE
CANDLE_SESSION_IS_HOLIDAY
STALE_DAILY_SERIES_MISSING_LATEST_SESSION
UNVERIFIABLE_PROVIDER_PUBLICATION_TIME
EVENT_AFTER_RECEIPT
RETROSPECTIVE_CAPTURE_AFTER_DECISION
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"The bot didn't trade today" tells you nothing. "14 tickers excluded with &lt;code&gt;STALE_DAILY_SERIES_MISSING_LATEST_SESSION&lt;/code&gt;" tells you the data provider had a bad morning.&lt;/p&gt;

&lt;p&gt;The same validator also checks the &lt;strong&gt;portfolio snapshot&lt;/strong&gt;: used + reserved + available capital must add up to the configured cap, no position may exceed the per-position cap, and there can be at most 10 open positions. A snapshot that fails is rejected, not patched.&lt;/p&gt;

&lt;p&gt;Finally, the backtester and the live loop share &lt;strong&gt;the same definition of "available"&lt;/strong&gt;. If your backtest is looser than production, you're testing a different system from the one you run.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Idempotent and fail-closed order execution
&lt;/h2&gt;

&lt;p&gt;The naive loop looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;strategy says BUY AAPL → POST order → timeout (did it fill?) → next cycle: BUY AAPL again
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My executor runs three checks before any buy:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Read the broker portfolio&lt;/strong&gt; (&lt;code&gt;GET /equity/portfolio&lt;/code&gt;). If the ticker is already held, the buy is blocked. The broker is the source of truth.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read the local order log.&lt;/strong&gt; Orders I submitted that aren't in the portfolio yet still count, so broker lag can't cause a duplicate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;If the portfolio can't be read, block the buy.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Number 3 is the one I got wrong. My first version looked roughly like this:&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;def&lt;/span&gt; &lt;span class="nf"&gt;get_open_positions&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;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;PORTFOLIO_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;resp&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;          &lt;span class="c1"&gt;# ← HTTP 429? "you hold nothing"&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;          &lt;span class="c1"&gt;# ← DNS failure? "you hold nothing"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An empty list means "no positions", so the duplicate check passes and the total-capital check sees 0 € invested. A transient network error quietly turns every safety guard off. That's &lt;strong&gt;fail-open&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The fix is to make "I don't know" a distinct state:&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;class&lt;/span&gt; &lt;span class="nc"&gt;PortfolioUnavailableError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_open_positions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strict&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;PORTFOLIO_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;resp&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;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;PortfolioUnavailableError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"HTTP {resp.status_code}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;PortfolioUnavailableError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;strict&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;strict&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;PortfolioUnavailableError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;exc&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# in the executor&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"BUY"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"INCREASE"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;positions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_open_positions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strict&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;PortfolioUnavailableError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;block&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"PORTFOLIO_NOT_VERIFIABLE"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# persisted for audit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some details that matter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Exits are treated differently.&lt;/strong&gt; Missing a buy because the broker is flaky costs nothing. Being unable to even attempt a stop-loss exit is worse, so sells don't go through the same block.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The caller has to cooperate too.&lt;/strong&gt; My cycle runner read the portfolio once at the start and passed the list down. After a failed read it was passing &lt;code&gt;[]&lt;/code&gt;, which skips the executor's own check. Now it passes &lt;code&gt;None&lt;/code&gt;, which forces the executor to fetch again, strictly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Blocked orders are logged&lt;/strong&gt; just like sent ones. The "no"s are as important to audit as the "yes"es.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Expect a test to break.&lt;/strong&gt; When I fixed this, an existing test that "successfully placed a buy" started failing. Its mock returned garbage for the portfolio call and the buy only went through because of the fail-open bug.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The tests I added cover HTTP 429, 503, network errors, timeouts, a non-list JSON body, a verified empty portfolio (buy allowed) and a verified portfolio that already holds the ticker (buy blocked).&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Top-K ranking and Walk-Forward tuning
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Top-K.&lt;/strong&gt; Candidates aren't bought in scan order. Each cycle collects every candidate, ranks them by conviction and expected return, then fills slots top-down: at most 10 positions, minimum lot 50 €, lot capped by the per-position limit and by free capital. Every skipped candidate is logged with its reason.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Walk-Forward.&lt;/strong&gt; Once a month a systemd timer runs the optimizer:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Take the last 60 sessions.&lt;/li&gt;
&lt;li&gt;Split them: the first 75 % in-sample, the last 25 % out-of-sample.&lt;/li&gt;
&lt;li&gt;Grid-search lot sizes and trailing-stop parameters on the in-sample part only (198 combinations at "coarse" resolution).&lt;/li&gt;
&lt;li&gt;Re-run the best in-sample configuration on the out-of-sample part.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The fitness function penalises risk and costs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fitness = net realized PnL − 1.5 × max drawdown − frictions (FX fees + slippage)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the part I think matters most, the &lt;strong&gt;adoption gate&lt;/strong&gt;. New parameters go live only if, out of sample:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;net PnL &amp;gt; 0,&lt;/li&gt;
&lt;li&gt;profit factor ≥ 1.10,&lt;/li&gt;
&lt;li&gt;fitness ≥ the current baseline's fitness on the same out-of-sample window.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Otherwise the current parameters stay. Most months "no change" is the right answer, and the system is designed to accept that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Operational lesson.&lt;/strong&gt; The optimizer once failed on a DNS error while fetching data, and since it was a monthly timer, it simply didn't run again for a month. The unit now has &lt;code&gt;Restart=on-failure&lt;/code&gt;, &lt;code&gt;RestartSec=30min&lt;/code&gt;, &lt;code&gt;StartLimitBurst=3&lt;/code&gt; and the timer has &lt;code&gt;Persistent=true&lt;/code&gt;, so it retries and catches up after a reboot.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell anyone building one
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Make time a first-class field: &lt;code&gt;available_utc&lt;/code&gt; on everything, checked against a real market calendar.&lt;/li&gt;
&lt;li&gt;Give every rejected data point and blocked order a reason code.&lt;/li&gt;
&lt;li&gt;Treat "I don't know the state" as its own state, and never let it become "empty".&lt;/li&gt;
&lt;li&gt;Test the failure paths (429, timeouts, bad payloads), not just the happy path.&lt;/li&gt;
&lt;li&gt;Re-tune parameters on a schedule, but make adoption hard.&lt;/li&gt;
&lt;li&gt;Use the same validation code in backtest and live.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Known limitations (being honest)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It runs against the &lt;strong&gt;demo&lt;/strong&gt; environment only.&lt;/li&gt;
&lt;li&gt;Market data comes from Yahoo Finance via &lt;code&gt;yfinance&lt;/code&gt;, which is unofficial and has no SLA.&lt;/li&gt;
&lt;li&gt;The engine supports news sentiment, but no news provider is wired in by default.&lt;/li&gt;
&lt;li&gt;Position values for capital caps use quantity × average price from the broker, with no FX conversion.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I've open-sourced a small, dependency-free version of the two guards (PIT checks + fail-closed portfolio reads, 27 tests, MIT): &lt;a href="https://github.com/Manelofthh/pit-guard" rel="noopener noreferrer"&gt;https://github.com/Manelofthh/pit-guard&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy to answer questions about any of it — especially if you've solved the PIT problem for news differently.&lt;/p&gt;




&lt;p&gt;I've packaged the full project as a self-hosted template (Python 3.12+, systemd installer, 91 offline tests, EN/ES docs, demo-only): &lt;a href="https://buy.stripe.com/7sY28k1mGgsh6LH8MJdby01" rel="noopener noreferrer"&gt;https://buy.stripe.com/7sY28k1mGgsh6LH8MJdby01&lt;/a&gt;. It's an engineering template, not financial advice, and comes with no promise of returns.&lt;/p&gt;

</description>
      <category>python</category>
      <category>fintech</category>
      <category>testing</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
