This folder contains standalone Python scripts to initialize and backfill core financial tables.
dim_tickerfrom yfinance metadatafact_price_dailyfrom yfinance historical pricesfact_financialsfrom Alpha Vantage quarterly statements
dim_ticker.pyfact_price_daily.pyfact_financials.py- ticker input files:
tickers.csv,index_tickers.csv
+-----------------------------+
| ticker files |
| tickers.csv / index_... |
+-----------------------------+
|
v
+-----------------------------------------------+
| Python seed scripts |
| - dim_ticker.py |
| - fact_price_daily.py |
| - fact_financials.py |
+-----------------------------------------------+
| |
v v
+------------------+ +------------------+
| yfinance | | Alpha Vantage |
+------------------+ +------------------+
| |
+------------+------------+
|
v
+------------------------+
| PostgreSQL (upserts) |
+------------------------+
- Python 3.11+
- PostgreSQL access
- Alpha Vantage API key (for
fact_financials.py)
Install dependencies in your virtual environment:
pip install yfinance pandas requests psycopg2-binary python-dotenvSet these variables in your shell or local .env file:
| Variable | Purpose |
|---|---|
PG_HOST |
PostgreSQL host |
PG_PORT |
PostgreSQL port |
PG_DB |
target database |
PG_USER |
database user |
PG_PASSWORD |
database password |
PG_SCHEMA |
target schema (default public) |
ALPHAVANTAGE_API_KEY |
required for fact_financials.py |
Optional tuners:
YF_SLEEP_SECONDSALPHAVANTAGE_MAX_CALLS_PER_MINSLEEP_AFTER_TICKER
- Seed tickers first.
- Seed historical prices.
- Seed quarterly financials.
python data/seed/dim_ticker.py --from-file data/seed/tickers.csv --create-tableUsing a fixed period:
python data/seed/fact_price_daily.py --from-file data/seed/tickers.csv --period 5y --create-tableUsing explicit dates:
python data/seed/fact_price_daily.py --from-file data/seed/tickers.csv --start 2024-01-01 --end 2026-01-01 --create-tablepython data/seed/fact_financials.py --from-file data/seed/tickers.csv --create-table- Scripts use bulk upsert semantics (
ON CONFLICT ... DO UPDATE). - Table creation is optional and controlled by
--create-table. fact_financials.pyincludes a rate limiter for Alpha Vantage.fact_price_daily.pyfetches in ticker batches and sleeps between batches.
- If yfinance starts throttling, reduce batch size and increase sleep.
- If Alpha Vantage throttles, lower
--max-calls-per-min. - Ensure
dim_tickerexists before seeding facts (foreign key dependency).