> ## Documentation Index
> Fetch the complete documentation index at: https://calendar-effects.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# etl/prices_download.py

> Download daily prices for every in-scope retailer in config/retailers.csv, plus the market index (common.study.MARKET_TICKER), from Yahoo Finance, over the study period.

`etl/prices_download.py` · chapter [Building a row](https://calendar-effects.mintlify.site/tour/03-building-a-row.md), file 1 · 48 lines · [open in the repository](https://github.sfu.ca/aba395/Calendar-Effects/blob/main/etl/prices_download.py)

## What it does

Download daily prices for every in-scope retailer in config/retailers.csv, plus the
market index (common.study.MARKET\_TICKER), from Yahoo Finance, over the study period.

Run once, from anywhere:   python3 etl/prices\_download.py
Raw files land in raw/prices/\<TICKER>.csv and are never committed.

To add a stock, add a row to config/retailers.csv and run this again.
If Yahoo blocks you, wait about 10 minutes and run it again. Tickers that are
already saved are skipped, so a rerun only fetches the missing ones.

## What is inside

* **`TICKERS`** · [line 20](https://github.sfu.ca/aba395/Calendar-Effects/blob/main/etl/prices_download.py#L20)
* **`OUT_DIR`** · [line 21](https://github.sfu.ca/aba395/Calendar-Effects/blob/main/etl/prices_download.py#L21)

## Connections

**Uses:** [`src/common/contracts.py`](https://calendar-effects.mintlify.site/reference/src/common/contracts), [`src/common/settings.py`](https://calendar-effects.mintlify.site/reference/src/common/settings), [`src/common/study.py`](https://calendar-effects.mintlify.site/reference/src/common/study)

## Source

```python title="etl/prices_download.py" lines expandable theme={null}
"""Download daily prices for every in-scope retailer in config/retailers.csv, plus the
market index (common.study.MARKET_TICKER), from Yahoo Finance, over the study period.

Run once, from anywhere:   python3 etl/prices_download.py
Raw files land in raw/prices/<TICKER>.csv and are never committed.

To add a stock, add a row to config/retailers.csv and run this again.
If Yahoo blocks you, wait about 10 minutes and run it again. Tickers that are
already saved are skipped, so a rerun only fetches the missing ones.
"""
import time
from datetime import timedelta

import yfinance as yf

from common.contracts import price_tickers
from common.settings import RAW_DIR
from common.study import STUDY_END, STUDY_START

TICKERS = price_tickers()
OUT_DIR = RAW_DIR / 'prices'

OUT_DIR.mkdir(parents=True, exist_ok=True)
missing = []

for t in TICKERS:
    path = OUT_DIR / (t + '.csv')
    if path.exists() and path.stat().st_size > 1000:
        print(t, 'already downloaded, skipping')
        continue

    # auto_adjust=True: 'Close' is already adjusted for splits and dividends.
    # end is exclusive, so the day after STUDY_END gives every trading day up to it.
    df = yf.download(t, start=STUDY_START.isoformat(), end=(STUDY_END + timedelta(days=1)).isoformat(),
                     auto_adjust=True, progress=False)
    if df.empty:
        print(t, 'EMPTY: Yahoo may be blocking us')
        missing.append(t)
        continue

    df.columns = df.columns.get_level_values(0)  # drop the extra ticker level yfinance adds
    df['ticker'] = t
    df.reset_index().to_csv(path, index=False)
    print(t, len(df))
    time.sleep(1)  # be polite to Yahoo

if missing:
    raise SystemExit('Missing ' + ', '.join(missing) + '. Wait 10 minutes and run again.')
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.