Skip to Content
While25SDK docs
ExamplesMonitors

New SEC filings for a watchlist

Every 30 minutes, fetches the SEC's filing index for a few companies through the platform's open-data registry and sends a notice for each 8-K, 10-Q or 10-K filed since the last check, with a link to the filing. It checks the columns it needs against `describe()` at start, and treats each refusal code the way the guide says to.

Every 30 minutes, fetches the SEC's filing index for a few companies through the platform's open-data registry and sends a notice for each 8-K, 10-Q or 10-K filed since the last check, with a link to the filing. It checks the columns it needs against describe() at start, and treats each refusal code the way the guide says to.

Shows: Monitor, data.catalog(), data.describe(), data.fetch(..., symbol=), df.attrs["provenance"], open-data refusal codes (FETCH_FAILED, NOT_FOUND, AMBIGUOUS, CONTACT_REQUIRED), pandas, @on.schedule.

View on GitHub

monitors/sec_filings_watch.py
"""New SEC filings for a watchlist

Every 30 minutes, fetches the SEC's filing index for a few companies through
the platform's open-data registry and sends a notice for each 8-K, 10-Q or
10-K filed since the last check, with a link to the filing. It checks the
columns it needs against `describe()` at start, and treats each refusal code
the way the guide says to.

Shows: Monitor, data.catalog(), data.describe(), data.fetch(..., symbol=), df.attrs["provenance"], open-data refusal codes (FETCH_FAILED, NOT_FOUND, AMBIGUOUS, CONTACT_REQUIRED), pandas, @on.schedule.
"""
from datetime import date, timedelta

import while25
from while25 import Monitor, on

DATASET = "sec.company_filings"
WATCH = ["AAPL", "MSFT", "NVDA", "AMZN"]
FORMS = {"8-K", "10-Q", "10-K"}


NEEDS = ("filing_date", "report_type", "report_url")


def new_filings(df, since):
    """Rows of a wanted form filed on or after `since`, newest first."""
    if df.empty:
        return df
    rows = df[df["report_type"].isin(FORMS) & (df["filing_date"] >= since)]
    return rows.sort_values("filing_date", ascending=False)


class SecFilingsWatch(Monitor):
    id = "sec_filings_watch"
    label = "SEC filings watch"

    def on_start(self, ctx):
        ids = [d["id"] for d in while25.data.catalog("filings")]
        if DATASET not in ids:
            raise RuntimeError(f"{DATASET} is not in the open-data catalog: {ids}")
        columns = {c["name"] for c in while25.data.describe(DATASET)["columns"]}
        missing = [c for c in NEEDS if c not in columns]
        if missing:
            raise RuntimeError(f"{DATASET} no longer has {missing}")
        self.since = {s: date.today() - timedelta(days=1) for s in WATCH}

    @on.schedule(every="30m")
    def check(self, ctx):
        for sym in WATCH:
            try:
                df = while25.data.fetch(DATASET, symbol=sym)
            except while25.While25Error as e:
                if e.code == "FETCH_FAILED":        # the source is busy: next pass
                    while25.log(f"{sym}: {e.message}", "warn")
                    continue
                if e.code in ("NOT_FOUND", "AMBIGUOUS", "NOT_COVERED"):
                    while25.log(f"{sym}: {e.code}: {e.message}", "warn")
                    continue
                raise                               # CONTACT_REQUIRED and the rest stop the run
            for _, row in new_filings(df, self.since[sym]).iterrows():
                while25.notify(f"{sym} filed a {row['report_type']} on {row['filing_date']}: "
                               f"{row['report_url']}", title="SEC filing", symbol=f"EQ:US:{sym}")
            self.since[sym] = date.today()
            src = df.attrs.get("provenance", {}).get("provider", "the source")
            while25.log(f"{sym}: {len(df)} filings read from {src}")