Shell
Plan: Historical Market Data Storage (No More Mock Data)
Problem Statement
When the Python backend is unavailable, the app falls back to fake mock data instead of showing real historical prices. This is wrong because:
- Historical prices are immutable - they exist and can be stored
- Mock data can mislead users
- Every past moment's price can be saved as a fallback
Current Architecture
Frontend → Next.js API → Python Backend → Alpaca API
↓
(if backend down)
↓
Mock data (WRONG)
Proposed Architecture
Frontend → Next.js API → Check Supabase first (historical)
↓
Found? → Return stored data
↓
Not found? → Fetch from Alpaca
↓
Store in Supabase for future
↓
Alpaca down? → Return "data unavailable" (NEVER mock)
Implementation Plan
Phase 1: Database Schema
Create Supabase table for historical bars.
Table: market_bars
CREATE TABLE market_bars (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
symbol VARCHAR(20) NOT NULL,
timeframe VARCHAR(10) NOT NULL, -- '1d', '1h', '1w', etc.
timestamp TIMESTAMPTZ NOT NULL,
open NUMERIC(20, 8) NOT NULL,
high NUMERIC(20, 8) NOT NULL,
low NUMERIC(20, 8) NOT NULL,
close NUMERIC(20, 8) NOT NULL,
volume BIGINT,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(symbol, timeframe, timestamp)
);
-- Indexes for fast queries
CREATE INDEX idx_market_bars_symbol_timeframe ON market_bars(symbol, timeframe);
CREATE INDEX idx_market_bars_timestamp ON market_bars(timestamp DESC);
CREATE INDEX idx_market_bars_lookup ON market_bars(symbol, timeframe, timestamp DESC);
Files to create:
web/supabase/migrations/XXX_create_market_bars.sql
Phase 2: Data Fetching Logic
Update bars API to use Supabase + Alpaca hybrid approach.
Logic:
- Parse request (symbol, timeframe, limit)
- Calculate date range needed
- Query Supabase for existing bars in that range
- Identify gaps (missing dates)
- If gaps exist AND within fetchable range:
- Fetch missing data from Alpaca
- Store fetched data in Supabase
- Merge with existing data
- If Alpaca unavailable:
- Return whatever we have from Supabase
- Mark response as "stale" with last update timestamp
- NEVER return mock data
Files to modify:
web/src/app/api/market/bars/route.ts- Main logic changeweb/src/lib/supabase/market-data.ts- New Supabase queries
Phase 3: Alpaca Direct Access (Backup)
Add direct Alpaca API calls from Next.js as fallback when Python backend is down.
Why: Python backend may be down, but Alpaca API is usually available.
Files to create:
web/src/lib/alpaca-client.ts- Direct Alpaca API client for Next.js
Environment variables needed:
ALPACA_API_KEY(already exists)ALPACA_SECRET_KEY(already exists)
Phase 4: Remove Mock Data
Delete all mock data generation code.
Files to modify:
web/src/app/api/market/bars/route.ts- RemovegenerateMockBars()web/src/app/api/market/quotes/route.ts- RemovegenerateMockQuotes()web/src/lib/mock-data-constants.ts- DELETE entire file
Replace with:
- Clear "data unavailable" responses
- Stale data indicators with timestamps
Phase 5: UI Updates
Show clear indicators when data is stale or unavailable.
New states:
- "Live" - Fresh data from Alpaca
- "Cached" - Stored data from Supabase (with timestamp)
- "Unavailable" - No data available
Files to modify:
web/src/components/chat/HomeWidgets.tsx- Show data freshness- Market Watch panel components
Open Questions
- Data retention: How far back should we store? (1 year? 5 years? All available?)
- Timeframes to store: All timeframes or just daily? (1d is most useful for historical)
- Background sync: Should we have a cron job to pre-fetch popular symbols?
- Quotes storage: Should we also store quote snapshots, or just bars?
File Changes Summary
| File | Action | Purpose |
|---|---|---|
web/supabase/migrations/XXX_create_market_bars.sql |
CREATE | Database schema |
web/src/lib/supabase/market-data.ts |
CREATE | Supabase queries for market data |
web/src/lib/alpaca-client.ts |
CREATE | Direct Alpaca API client |
web/src/app/api/market/bars/route.ts |
MODIFY | Hybrid fetch logic |
web/src/app/api/market/quotes/route.ts |
MODIFY | Remove mock data |
web/src/lib/mock-data-constants.ts |
DELETE | No more mock data |
web/src/components/chat/HomeWidgets.tsx |
MODIFY | Stale data indicators |
Benefits
- Real data always - Never show fake prices
- Faster responses - Most historical data served from Supabase
- Reduced API costs - Don't re-fetch immutable historical data
- Offline resilience - Works when Alpaca is down
- Honest UX - Clear indicators when data is stale
Risks & Mitigations
| Risk | Mitigation |
|---|---|
| Supabase storage costs | Start with daily bars only, add others if needed |
| Data inconsistency | Use unique constraint, upsert on conflict |
| Initial backfill time | Lazy loading - fetch on first request |
| Alpaca API changes | Abstract behind our own client interface |