feat: add Polygon grouped daily endpoint for broad market data

Two tiers of market data:
1. Per-ticker prev bars (existing 50 sources, 15-min cadence) for
   watchlist detail — trading decisions, stop-loss, position sizing
2. Grouped daily (new single source, once per day) for broad market
   context — correlation analysis, sector rotation, competitive intel

Changes:
- Add grouped_daily endpoint to PolygonMarketAdapter with auto date
  calculation (previous trading day, skip weekends)
- Add fetch_global_market_sources() to scheduler for sources without
  company_id, scheduled once daily (86400s cadence)
- Update _persist_market_items to use item-level ticker from T field
  and look up company_id dynamically for grouped daily bars
- Migration 020: make company_id nullable on sources and
  market_snapshots tables, add grouped daily source row
- Fix backtest replay to query market_snapshots data->>'c' for prices
This commit is contained in:
Celes Renata
2026-04-15 22:38:18 +00:00
parent ea6c2b3f54
commit 4501bbebd4
4 changed files with 133 additions and 4 deletions
@@ -0,0 +1,25 @@
-- Migration 020: Add grouped daily market data source and allow
-- global (non-company-specific) sources and market snapshots.
-- Allow market_snapshots to store bars for tickers not in our companies table
ALTER TABLE market_snapshots ALTER COLUMN company_id DROP NOT NULL;
-- Allow sources to exist without a company (for global market data sources)
ALTER TABLE sources DROP CONSTRAINT sources_company_id_fkey;
ALTER TABLE sources ALTER COLUMN company_id DROP NOT NULL;
ALTER TABLE sources ADD CONSTRAINT sources_company_id_fkey
FOREIGN KEY (company_id) REFERENCES companies(id) ON DELETE CASCADE;
-- Add the grouped daily source (fetches ALL US stock bars in one API call)
INSERT INTO sources (source_type, source_name, config, active, company_id)
SELECT
'market_api',
'Polygon Grouped Daily',
'{"endpoint": "grouped_daily", "provider": "polygon", "adjusted": true}'::jsonb,
true,
NULL
WHERE NOT EXISTS (
SELECT 1 FROM sources
WHERE source_type = 'market_api'
AND config->>'endpoint' = 'grouped_daily'
);