5-Minute Bars Silently Cap Out at 60 Days

yfinance hands back an empty frame, not an error, for 5-minute bars older than 60 days. How I found it and the guardrail that now catches it.

The data layer for my paper-trading stack was the part I stopped thinking about. It pulled intraday history, cached it to disk, and handed a frame to the strategy. No errors, no warnings, no drama — for weeks. Then I ran a backtest over a longer window and it came back completely flat: zero trades, zero signals, nothing. The frames were not truncated, not partially filled, not stale. They were empty. And nothing in the entire call stack had complained. That empty return is the whole point of this post, and it points at a hard limit you need to know before you design around it: yfinance caps 5-minute and finer bars at 60 days. If you want the wider context for how this box and its services are wired, my self-hosting security notes cover the rest of the setup.

The failure looked like a strategy problem

Here is the shape of the mistake, because it is a shape I keep seeing. A backtest returns no trades. The natural reflex is to look at the strategy: thresholds too tight, indicators mis-specified, entry conditions that never fire in the tested regime. So you spend time tuning logic that was never broken, because the input to that logic was empty.

The tell was in the plumbing, not the strategy. I had a few stages between the download and the backtest:

  • a fetch step that called yfinance for a symbol and a timeframe,
  • a cache write that persisted the result to disk so I would not hammer the source,
  • a loader that read the cache back and passed it into the strategy,
  • and a report step that printed trade count and P&L.

Every one of those stages was “successful”. The fetch returned a frame. The cache wrote a file. The loader read the file. The strategy iterated over it — over zero rows — and correctly produced zero trades. Zero trades is a valid outcome for a strategy. Nothing raised. The pipeline was honest at every step and wrong at the top.

That is what makes this class of bug expensive: it does not look like a bug. It looks like a result.

What the 60-day cap actually is

The limit is not a bug in yfinance, and it is not a rate limit. It is the resolution-to-history tradeoff baked into the underlying source. Finer bars are heavier: 5-minute bars for a year is a very different amount of data than daily bars for three years, and the service behind the library does not want to serve arbitrarily deep intraday history. So there is a window, and for 5-minute and finer bars that window is 60 days.

Inside the window, everything behaves normally. You ask for 5-minute bars inside the last 60 days and you get them, cleanly, with the columns you expect. Outside the window, you do not get an error. You get an empty result.

The practical consequence is a cliff, and it is a cliff with no fence:

  • 5-minute bars, inside 60 days — fine.
  • 5-minute bars, reaching past 60 days — empty, no exception, no message.
  • Daily bars (1d) over the same long span — fine, because the daily interval is not subject to that intraday cap.

So “yfinance is broken for long history” is the wrong conclusion, and “just add a try/except” is the wrong fix. The library is fine. The request asked for data that does not exist at that resolution on that side of a boundary, and the API answered with nothing rather than with a refusal.

Why there is no exception to catch

This is the part that gets people, and it got me. My mental model of a failed fetch was: something throws. Timeout, HTTP error, malformed response, parse failure — all of those produce an exception, and my code had handling for all of them. But “you asked for a window I will not serve at this resolution” comes back as a successful call with an empty payload. From the code’s point of view, that is indistinguishable from “this symbol genuinely had no bars in that range” — which is a legitimate and expected state for illiquid instruments, halted tickers, or a range that falls entirely on weekends and holidays.

So the library has no way to shout at you without being wrong in the other direction. Either it raises on empty, and then every legitimately empty range becomes an exception you have to special-case, or it returns empty and lets you decide. It chose the second. That is defensible design, and it means the burden of noticing moved into my code.

The mental shortcut to kill is this one: “no exception means the fetch worked.” No. No exception means the call worked. Whether the data is there is a separate question, and it is my question to answer.

The diagnosis I should have done first

Once I suspected the input rather than the strategy, the diagnosis was almost embarrassingly quick. The sequence that actually found it:

  1. Print the row count of the frame at the point of use, before the strategy touches it — not the file size, not the byte count of the cached blob, the number of rows the strategy will iterate.
  2. Print the same row count for a request over a short, recent window on the same symbol and interval. That one came back populated.
  3. Compare. Same symbol, same resolution, different window: one populated, one empty. That immediately implicates the window, not the ticker.
  4. Ask the same long window at the daily interval. Populated.

Four steps, and the fourth one is the one that names the culprit: the exact range that returns nothing at 5-minute resolution returns a full series at 1d. Interval-dependent, range-dependent, error-free. That is the 60-day intraday cap, seen from the outside.

The reason I did not do this first is worth naming, because it is a habit and not an accident. When a numeric pipeline produces a plausible-looking number — zero trades, a flat equity curve, a null metric — I read it as output and move on to interpreting it. I should read it as input first. Any time a downstream stage produces an unremarkable result, the highest-value question is “how many rows did it actually see?” and I asked that question far too late.

The guardrail: assert on data, not on exit codes

The fix that mattered most was not the download change. It was making an empty frame impossible to pass silently. Whoever consumes this data next — me in three weeks, a cron job, a backtest I half-remember writing — should hit a wall, not a flat curve.

The principle is simple: check the data length, not just for exceptions. Concretely:

  • After the fetch, assert the frame is non-empty and that the row count is plausible for the window requested.
  • Treat “empty” as a hard failure at the fetch boundary, and log the symbol, the interval, and the requested range in the failure message — the three things you need to reconstruct the request later.
  • Never let an empty frame reach a cache write. An empty file poisons everything downstream of it, and it does so quietly, because the loader cannot tell “cache says nothing happened” from “cache says nothing exists”.
  • Make the error message say what to do about it: if the interval is intraday and the range reaches past the cap, say so in the message instead of just “no data”.

That last point matters more than it sounds. A future-me staring at “empty frame for SPY 5m” will start debugging the ticker. A future-me staring at “empty frame: 5m requests cannot reach past 60 days — use 1d for long history or narrow the range” fixes it in ten seconds. Error messages are documentation that gets read at exactly the right moment.

The fix: two timeframes, two jobs

The structural fix was to stop pretending one interval serves every question. The questions I actually had were different in kind, and they deserved different resolutions:

  • Long-horizon context and backtests — daily bars. 1d is not subject to the intraday cap, so it covers the multi-year span I want for regime context, and the reduced row count makes iteration fast.
  • Recent execution detail — 5-minute bars, but only over a window I know is inside 60 days, and never with a range that reaches past it.

That split is not a workaround, it is the correct model. Daily and intraday data answer different questions. A multi-year backtest at 5-minute resolution is not something I can have from this source, and honestly it is not something I need: the decisions that depend on multi-year context are not decisions that need 5-minute granularity. The decisions that need 5-minute granularity are about the last several weeks.

Where the split actually changes things, in order of how often I hit it:

  1. Backtest ranges. Any range older than the cap is expressed at 1d. If I catch myself writing code that asks for intraday bars over a long span, that is a design smell, not a data problem.
  2. Cache keys. The cache key must include the interval, not just the symbol. Two different intervals over overlapping ranges are two different datasets, and collapsing them into one key is how a daily frame ends up served to a function that expects intraday bars.
  3. Backfill vs. refresh. Backfill is a daily-interval operation. Refresh is an intraday operation over a short recent window. Keeping them as separate jobs with separate intervals removes the temptation to reach past the cap “just for the backfill”.
  4. Anything that genuinely needs long intraday history. That is the one case the notes are blunt about: switch to a paid or other provider. No amount of retry logic makes the source serve resolution it does not keep.

The decision, and why I did not just retry

There was a fork here, and I want to be explicit about taking the boring side of it. The tempting move is to loop: request the long 5-minute range, get nothing, split it into smaller chunks, request again, get nothing again because the chunks are still past the cap, and so on until something returns. That pattern feels like resilience and is actually a way to hide a design error behind retries while burning time and hammering a source that already told you its answer.

I went the other way:

  • Keep the source. It is free, it is convenient, and it serves everything I need at the resolutions where it is valid.
  • Change what I ask it for. Long history at 1d, recent detail at intraday resolution, never a request that crosses the boundary.
  • Pay for a provider only when I have a concrete question that genuinely requires deep intraday history — and so far I do not.

The reasoning is that the constraint is a property of the source, not a transient failure. Retrying a boundary is not resilience; it is confusion with a for-loop. Once I understood that the cap is resolution-dependent and permanent, the design question stopped being “how do I get around this” and became “which resolution does each of my questions actually need”. That question has a clean answer, and it made the pipeline simpler than it was before the bug.

A checklist before you build on any intraday feed

If you are about to put an intraday data source under a strategy, a dashboard, or a cron job, these are the checks I now run before writing any consuming code:

  • Find the resolution cliff before you hit it. For every interval you plan to use, know the maximum history the source will serve. Write it down next to the interval.
  • Prove the boundary empirically. Fetch a short recent window and a long window for the same symbol and interval, and compare row counts. If the long window is empty and the short one is not, you have found the cap.
  • Distinguish “call succeeded” from “data arrived”. Log row counts at the fetch boundary, the cache boundary, and the point of use. If those three numbers disagree, you have a plumbing bug.
  • Fail loudly on empty at the boundary. Do not write empty frames to cache. Do not hand empty frames to strategies. An empty frame is an error state, and it should read like one.
  • Include the interval in the cache key. Overlapping ranges at different resolutions are different datasets.
  • Put the interval in the failure message. “No data” is a riddle. “No data at this interval for this range, and here is the cap” is a fix.
  • Pick a resolution per question, not per pipeline. Long context does not need fine granularity. Fine granularity does not need long history.

What I would do differently

Two things, and the first one is not really about yfinance.

I would instrument the fetch boundary on day one — a row count logged at fetch, at cache write, and at use, with the interval in every line. That single habit would have turned a mystery into a one-line log read, and it costs almost nothing to add. The reason I did not is that I was optimizing for the interesting part — the strategy — and treating the plumbing as solved because it had never visibly failed. Plumbing that has never visibly failed is not the same as plumbing that is correct.

The second: I would refuse to accept a flat numeric result as a result until I have checked the row count that produced it. Zero trades, a null return, a flat curve — all of those are downstream echoes. Checking the input is cheap, and it is the difference between debugging a strategy for an afternoon and fixing a two-line request.

The outcome

After the split, the backtests are populated and the strategy code is unchanged — which is the strongest evidence that the strategy was never the problem. The pipeline now fails fast and loudly if a frame comes back empty, with a message that names the interval and the cap, so the next time something upstream changes and this returns nothing, I will know in one line instead of one afternoon.

The general lesson is bigger than one library. A silent empty return is the trap, and it is a trap that scales: every source I build on has some boundary where it stops answering and starts returning nothing, and almost none of them treat crossing that boundary as an error. The defense is the same everywhere — know the boundary, check the length, and make emptiness loud.

Related

Filed under: Business & Policy, Automation