Skip to content

Price Storage: Source-Keyed Bars, Adjustment As Fetched

Status: accepted, amended 2026-09-29, 2026-10-30 and 2026-11-28.

Context: get_prices returns a FrameData that is cached and discarded, so no price history is written down. ADR-0005 exists because adjusted history is rewritten by corporate actions, and the road map assumed the reconstructable answer was to store unadjusted prices plus a corporate-action record. Checked against the live API, that option does not exist. yfinance with auto_adjust=False returns Open, High, Low, Close, Adj Close, Volume, Dividends, Stock Splits, and the OHLC columns are already split-back-adjusted - NVDA traded near 1200 USD on 2024-06-05 and Yahoo reports 118.37 - so auto_adjust=False withholds only the dividend adjustment. Truly raw prices are not obtainable from Yahoo. Separately, CoinGecko's OHLC endpoint never returns daily candles: 7 and 30 days come back at 4-hour spacing, 90 and 365 days at 96-hour spacing, days=max requires a paid key, and the payload carries no volume. Its market_chart endpoint does give daily points with volume and market cap, but close-only.

Decision: - Bars are stored as fetched, one current vintage, overwritten on re-fetch. A split rewrite scales every price before the split by one constant, so consecutive ratios are unchanged and the adjustment across the split date is exactly what makes that day's return correct. The rewrite that ADR-0005 guards against does not move returns; only the dividend adjustment does, and that is controlled by storing both close and adj_close side by side so price return and total return are both computable and neither convention has to be guessed at. - Every bar carries fetched_at, so a vintage is identifiable even though only one is kept. - Corporate actions are stored as their own rows, not as bar columns. They are events, they are overwhelmingly zero, and they are the only record of why history changed. - A Restatement row is written when a re-fetch changes an already-stored bar by more than float noise and no corporate action explains it. Yahoo does occasionally correct bad ticks, and that is a silent rewrite which does move returns. One small table turns an invisible problem into a visible one, which is the reason ADR-0005 exists. - A bar's key is (instrument, source, bar_date). Source is part of the key, not a tiebreak, so two vendors disagreeing on a close each keep their row. Vendor disagreement becomes queryable rather than destructive, and is resolved at read time, not at write time. - bar_date is the session date in the venue's own calendar. Yahoo already stamps bars in venue time - UTC for BTC-USD, Asia/Tokyo for 7203.T, America/New_York for US equities - and CoinGecko's daily points land on 00:00 UTC, so the two vendors agree on what a crypto day is. - No time-of-day is stored. A session close is a property of a venue's calendar, derivable from Scope, not a fact about a bar; if a lead-lag correction is ever wanted, an exchange-calendar table is its home and it can be added without touching stored bars. - Instrument carries identity_source, a single value, and price_sources, an ordered preference. The first source holding a bar for a date supplies it, and the bar row records which one did. The list is ordered rather than singular because fallback is per date, not per instrument: Yahoo may carry a coin only from its listing date while CoinGecko covers the earlier stretch at lower fidelity. - Crypto bars therefore come from Yahoo where Yahoo has the pair, and from CoinGecko market_chart otherwise, while identity and market cap always come from CoinGecko. - A CoinGecko-sourced bar leaves adj_close null rather than mirroring close. Equal by coincidence and equal by definition should not look the same. - Amended 2026-09-29: a CoinGecko daily bar for date D is the price at 00:00 UTC of D, the point CoinGecko itself labels D. It is therefore roughly D-1's close, not D's. bars_from_market_chart keeps each UTC day's first point, so the trailing point the API appends at the fetch time never becomes a bar, and hourly and daily windows yield the same bars except on the first day of an hourly window, which has no 00:00 point. - Amended 2026-09-29: the daily sweep takes a markets listing's current_price as D's bar only when fetched within a configurable window after 00:00 UTC of D (bar_window_hours in cointoss.yaml, default three hours), so the bar stays the 00:00 price the definition above names. A sweep outside the window still resolves Instruments and records membership, and writes no bars; the missing day is left for a backfill. A re-sweep never overwrites a bar already stored for its date from the same source, because a later price is not a new vintage of the 00:00 price: fetched_at is the real fetch time, and re-sweeps file no Restatements. - Amended 2026-09-29: Bar carries an optional market_cap where the source reports one, as CoinGecko's markets and market_chart do, so "market cap always comes from CoinGecko" is now stored on CoinGecko's bars. It is not a price and takes no part in restatement detection: it is price times a circulating supply the vendor re-estimates freely, and comparing it would file supply revisions as rewritten history. - Amended 2026-09-29: "the two vendors agree on what a crypto day is" holds for the date label only. Both call the UTC day D by the same date, but CoinGecko's bar for D is the 00:00 price while Yahoo's BTC-USD bar for D closes at the end of D, so the two series are about a day apart and a per-date fallback between them splices series offset by a day. Resolved by the amendment of 2026-10-30 below. - Amended 2026-10-30: each source declares a Session Offset, the number of days by which the date it stamps on a bar leads the session that bar closes: 0 for Yahoo, 1 for CoinGecko, whose bar stamped D+1 is the 00:00 price that closes D. Bars stay stored as fetched, under the stamped date. Reads that resolve an Instrument's bars through its Price Sources apply the offset, so the series they return is keyed by the session closed and a per-date fallback from Yahoo to CoinGecko splices like with like. Reads of one source's bars return them as stored. Relabelling CoinGecko bars in storage was rejected because it rewrites the system of record; never splicing was rejected because it makes per-date fallback impossible. - Amended 2026-10-30: the crypto Price Sources order stays Yahoo first, CoinGecko second, as decided above. A coin's Yahoo pair is found by trying a hand-kept ticker for its CoinGecko id, if it has one, then {SYMBOL}-USD, then the {SYMBOL}{n}-USD tickers Yahoo search lists for {SYMBOL} USD (#36), and a ticker is accepted only when Yahoo's close for D and CoinGecko's bar for D+1 differ by under 3% in median over the last 30 sessions both hold; the first accepted is the pair. The tolerance was 2%, loosened to 3% for SHIB, which differs by 2.7% (#36). A refused ticker is logged; a coin with none accepted stores nothing, and is tried again on the next backfill. An accepted pair is recorded as a Yahoo External Reference and a Ticker History row at source yahoo. Yahoo history is taken back to the pair's listing (period="max"), which reaches past CoinGecko's 365-day window, and only for dates before today UTC, so no Provisional Bar is written by a backfill. - Amended 2026-11-28: stock bars follow the first decision literally, overwritten on re-fetch. A stock's first bars are its Yahoo period="max" history; after that, a daily sweep after the US close re-fetches a trailing window of about ten calendar days and writes the sessions not yet stored, so the sessions Yahoo returns are the calendar and no exchange calendar is needed. When the window shows a split or a dividend, the stock's whole history is re-fetched and overwritten, with Corporate Action rows written and a Restatement filed for any change they do not explain: Yahoo has by then rescaled every earlier OHLC for a split, or every earlier adj_close for a dividend, so appending only would put a false return of the split ratio into stored history. CoinGecko bars keep the never-overwrite rule of 2026-09-29, which exists because a later CoinGecko price is a different price, not a new vintage of the same one; a Yahoo re-fetch is a new vintage. The daily window covers the current members of every universe and each former member with a stored bar in the last 30 days, so a stock that leaves a universe but still trades keeps its bars, and a delisted one, which Yahoo answers with no history, drops out of the window 30 days after its last bar without cointoss modelling a delisting. - Amended 2026-11-28, in building #40: a stock's daily window reaches back to its latest stored bar when that is older than the window, so a machine down longer than the window leaves no gap whichever of the stock sweep and the backfill runs first; the backfill runs the same fetch. A dividend dated after a bar explains that bar's adj_close falling relative to an unchanged close, which is what Yahoo's re-fetch after a dividend looks like, so it files no Restatement; a rise, or a move beside an unexplained close, still does.

Considered Options: - Bitemporal storage keyed on (instrument, source, bar_date, fetched_as_of), every vintage retained. Perfectly reproducible, and storage multiplies every time a daily job re-fetches an overlapping range. - Current vintage plus a corporate-action log presented as a reconstruction mechanism. Any earlier vintage is derivable, since splits are what rewrote it and the factors are held - but nothing is expected to use the reconstruction, so presenting it as a feature overstates it. The corporate-action table is kept for its own sake. - Store only Adj Close. Smallest, and it permanently conflates price return with total return. - One field for primary source, serving both identity and prices. Rejected once the CoinGecko granularity limits were measured: identity from CoinGecko and bars from Yahoo is the systematic case for every crypto instrument, not an exception. - bar_date plus session_close_utc. One nullable column makes cross-market lag explicit, and it smears a calendar problem across the largest table in the schema.

Consequences: Bars on one bar_date are not contemporaneous - Tokyo's close precedes New York's by fourteen hours and crypto's UTC midnight sits between - so a covariance over a mixed universe correlates windows that do not overlap and systematically understates cross-market correlation. This is accepted, and is near-universal practice. A stored crypto series can be heterogeneous, with some dates carrying full OHLCV from Yahoo and others carrying a CoinGecko close with null open, high and low, so consumers must tolerate null OHL and rely on close. CoinGecko's free tier reaches back 365 days, so it can only ever backfill a recent window. fetch_coin_ohlc is unusable for daily history and market_chart is not yet wired into the adapter.