"""Fetch SC 13D / 13G filings (activist and 5% passive stakes) from EDGAR. Quarterly full-index files list every filing: https://www.sec.gov/Archives/edgar/full-index/YYYY/QTRn/form.idx Each is ~40-60 MB fixed-width text. Rows for SC 13D, SC 13D/A, SC 13G and SC 13G/A are kept (filed date, CIK, company, archive path). Amendments are flagged via the /A suffix so an event study can use originals only. Writes data/research/edgar_13d.csv (append-safe per quarter; a quarter already present in the CSV is refetched only with --refresh). PYTHONPATH=. uv run python -m research.lab.fetch_13d [--quarters N] """ from __future__ import annotations import io import os import re import sys import time import pandas as pd import requests from research.lab.fetch_us import _proxy_env OUT = os.path.join("data", "research", "edgar_13d.csv") URL = "https://www.sec.gov/Archives/edgar/full-index/{y}/QTR{q}/form.idx" UA = {"User-Agent": "quant-research/0.1 gahow@example.com", "Accept-Encoding": "gzip"} ROW = re.compile( r"^(?P