finagg.sec package
Subpackages
Submodules
finagg.sec.api module
An implementation of the Securities and Exchange Commission’s (SEC) EDGAR API.
The SEC EDGAR API provides methods for retrieving XBRL data (e.g., earnings per share) from financial statements and SEC filing submission histories (e.g., 10-Q/10-K forms). The SEC EDGAR API is one of the few APIs that provides historical and current company financial data for free.
An SEC EDGAR API user agent declaration is required to use this API.
The user agent should be of format FIRST_NAME LAST_NAME E_MAIL
. You
can pass your user agent directly to the implemented API getters, or you
can set the SEC_API_USER_AGENT
environment variable to have the user agent
be passed to the implemented API getters for you.
Alternatively, running finagg sec install
(or the broader
finagg install
) will prompt you to enter an SEC EDGAR API user agent
and will automatically store it in an .env
file in your current working
directory. The environment variables set in that .env
file will be loaded
into your shell upon using finagg
(whether that be through the Python
interface or through the CLI tools).
See the official SEC EDGAR API docs for more info on the SEC API.
- class finagg.sec.api.Concept[source]
Bases:
TypedDict
A collection of XBRL tag (data field), SEC EDGAR taxonomy that tag is in, and units for that tag.
- taxonomy: str
Valid SEC EDGAR taxonomy. See https://www.sec.gov/info/edgar/edgartaxonomies.shtml for taxonomies.
- class finagg.sec.api.Frame[source]
Bases:
dict
A frame is just a
Concept
with a flag indicating if it supports “instantaneous” frame data.
- class finagg.sec.api.CompanyConcept[source]
Bases:
API
Get all XBRL disclosures for a single company and concept (a taxonomy and tag) in a single dataframe.
The module variable
finagg.sec.api.company_concept
is an instance of this API implementation and is the most popular interface for querying this API.See also
finagg.sec.api.popular_concepts
: For a list of popular companyconcepts that can be used with this method. The concepts described within this member are the most widely available concepts for SEC filers.
- url: ClassVar[str] = 'https://data.sec.gov/api/xbrl/companyconcept/CIK{cik}/{taxonomy}/{tag}.json'
Request API URL.
- classmethod get(tag: str, /, *, cik: None | str = None, ticker: None | str = None, taxonomy: str = 'us-gaap', units: str = 'USD', cache: bool = True, user_agent: None | str = None) DataFrame [source]
Return all XBRL disclosures for a single company (CIK) and concept (a taxonomy and tag) in a single dataframe.
- Parameters:
tag – Valid tag within the given
taxonomy
.cik – Company SEC CIK. Mutually exclusive with
ticker
.ticker – Company ticker. Mutually exclusive with
cik
.taxonomy – Valid SEC EDGAR taxonomy. See https://www.sec.gov/info/edgar/edgartaxonomies.shtml for taxonomies.
units – Currency to view results in.
cache – Whether to cache the response from the API.
user_agent – Self-declared bot header. Defaults to the value found in the
SEC_API_USER_AGENT
environment variable.
- Returns:
Dataframe with normalized column names.
- Raises:
ValueError – If both a
cik
andticker
are provided or neither are provided.
Examples
>>> finagg.sec.api.company_concept.get( ... "EarningsPerShareBasic", ... ticker="AAPL", ... taxonomy="us-gaap", ... units="USD/shares", ... ).head(5) start end value accn fy fp ... 0 2006-10-01 2007-09-29 4.04 0001193125-09-214859 2009 FY ... 1 2006-10-01 2007-09-29 4.04 0001193125-10-012091 2009 FY ... 2 2007-09-30 2008-06-28 4.20 0001193125-09-153165 2009 Q3 ... 3 2008-03-30 2008-06-28 1.21 0001193125-09-153165 2009 Q3 ... 4 2007-09-30 2008-09-27 5.48 0001193125-09-214859 2009 FY ...
- classmethod get_multiple_original(concepts: list[finagg.sec.api.Concept], *, cik: None | str = None, ticker: None | str = None, form: None | str = None, start: None | str = None, end: None | str = None, cache: bool = True, user_agent: None | str = None) DataFrame [source]
Return original (not amended) XBRL disclosures for a single company using a set of company concepts.
- Parameters:
concepts – Company concepts to retrieve.
cik – Company SEC CIK. Mutually exclusive with
ticker
.ticker – Company ticker. Mutually exclusive with
cik
.form – SEC filing form type to include.
"10-Q"
is for quarterly filing forms and"10-K"
is for annual filing forms. Ignored if leftNone
.start – The start date of the observation period. Defaults to the first recorded date.
end – The end date of the observation period. Defaults to the last recorded date.
cache – Whether to cache the response from the API.
user_agent – Self-declared bot header. Defaults to the value found in the
SEC_API_USER_AGENT
environment variable.
- Returns:
Pivoted dataframe with a tag per column.
Examples
>>> finagg.sec.api.company_concept.get_multiple_original( ... finagg.sec.api.popular_concepts, ... ticker="AAPL", ... ).head(5) fy fp tag end value ... 0 2009 Q3 Assets 2009-06-27 4.814000e+10 ... 1 2010 Q1 Assets 2009-12-26 5.392600e+10 ... 2 2010 Q2 Assets 2010-03-27 5.705700e+10 ... 3 2010 Q3 Assets 2010-06-26 6.472500e+10 ... 4 2011 Q1 Assets 2010-12-25 8.674200e+10 ...
- class finagg.sec.api.CompanyFacts[source]
Bases:
API
Get all XBRL disclosures for a single company.
The module variable
finagg.sec.api.company_facts
is an instance of this API implementation and is the most popular interface for querying this API.- classmethod download_zip(*, chunk_size: int = 1000000, user_agent: None | str = None) ZipFile [source]
Download all XBRL disclosures for all companies in a zip file.
A progress bar displays the download progress as the download could take a long time because the zip file is more than 1GB. The zip file is updated by the SEC at approximately 3am ET nightly.
- Parameters:
chunk_size – Chunk size to stream and write the zip file with.
user_agent – Self-declared bot header. Defaults to the value found in the
SEC_API_USER_AGENT
environment variable.
- Returns:
A zip file object representing the downloaded zip file.
- classmethod get(*, cik: None | str = None, ticker: None | str = None, cache: bool = True, user_agent: None | str = None) DataFrame [source]
Return all XBRL disclosures for a single company (CIK).
- Parameters:
cik – Company SEC CIK. Mutually exclusive with ticker.
ticker – Company ticker. Mutually exclusive with cik.
cache – Whether to cache the response from the API.
user_agent – Self-declared bot header. Defaults to the value found in the
SEC_API_USER_AGENT
environment variable.
- Returns:
Dataframe with normalized column names.
- Raises:
ValueError – If both a
cik
andticker
are provided or neither are provided.
Examples
>>> finagg.sec.api.company_facts.get(ticker="AAPL").head(5) end value accn fy fp form ... 0 2009-06-27 8.9582e+08 0001193125-09-153165 2009 Q3 10-Q ... 1 2009-10-16 9.0068e+08 0001193125-09-214859 2009 FY 10-K ... 2 2009-10-16 9.0068e+08 0001193125-10-012091 2009 FY 10-K/A ... 3 2010-01-15 9.0679e+08 0001193125-10-012085 2010 Q1 10-Q ... 4 2010-04-09 9.0994e+08 0001193125-10-088957 2010 Q2 10-Q ...
- class finagg.sec.api.Exchanges[source]
Bases:
API
SEC-registered ticker info with exchange data.
This API implementation is very similar to
Tickers
. It returns all the same data in addition to the exchanges that each company is on.The module variable
finagg.sec.api.exchanges
is an instance of this API implementation and is the most popular interface for querying this API.- classmethod get(*, cache: bool = True, user_agent: None | str = None) DataFrame [source]
Get a dataframe containing all SEC-registered ticker info.
- Parameters:
cache – Whether to cache the response from the API.
user_agent – Self-declared SEC bot header. Defaults to the value found in the
SEC_API_USER_AGENT
environment variable.
- Returns:
A dataframe containing company names, their SEC CIKs, and their ticker symbols.
Examples
>>> finagg.sec.api.exchanges.get().head(5) cik name ticker exchange 0 320193 Apple Inc. AAPL Nasdaq 1 789019 MICROSOFT CORP MSFT Nasdaq 2 1652044 Alphabet Inc. GOOGL Nasdaq 3 1018724 AMAZON COM INC AMZN Nasdaq 4 1045810 NVIDIA CORP NVDA Nasdaq
- class finagg.sec.api.Frames[source]
Bases:
API
Get all company filings for one particular fact that most closely matches the requested calendrical period.
The module variable
finagg.sec.api.frames
is an instance of this API implementation and is the most popular interface for querying this API.See also
popular_frames
: For a list of popular company framesthat can be used with this method. The frames described within this member are the most widely available frames for SEC filers.
- url: ClassVar[str] = 'https://data.sec.gov/api/xbrl/frames/{taxonomy}/{tag}/{units}/CY{year}{quarter}.json'
Request API URL.
- classmethod get(tag: str, year: int | str, /, quarter: None | int | str = None, *, instant: bool = True, taxonomy: str = 'us-gaap', units: str = 'USD', cache: bool = True, user_agent: None | str = None) DataFrame [source]
Get one fact for each reporting entity that most closely fits the calendrical period requested.
- Parameters:
tag – Valid tag within the given
taxonomy
.year – Year to retrieve.
quarter – Quarter to retrieve data for. Most data is only provided at a quarterly rate, so this should be provided for most cases.
instant – Whether to get instantaneous data for the frame (data that most closely matches a frame’s year and quarter without a time buffer). This flag should be enabled for most cases. See
popular_frames
for which tags should beinstant
.taxonomy – Valid SEC EDGAR taxonomy. See https://www.sec.gov/info/edgar/edgartaxonomies for taxonomies.
units – Units to view results in.
cache – Whether to cache the response from the API.
user_agent – Self-declared bot header. Defaults to the value found in the
SEC_API_USER_AGENT
environment variable.
- Returns:
Dataframe with slightly improved column names.
Examples
>>> finagg.sec.api.frames.get( ... "EarningsPerShareBasic", ... 2020, ... quarter=3, ... instant=False, ... taxonomy="us-gaap", ... units="USD-per-shares", ... ).head(5) accn cik entity loc ... 0 0001104659-21-118843 1750 AAR CORP. US-IL ... 1 0001104659-21-133629 1800 ABBOTT LABORATORIES US-IL ... 2 0001264931-20-000235 1961 WORLDS INC. US-MA ... 3 0001564590-21-055818 2098 ACME UNITED CORP US-CT ... 4 0000002178-22-000033 2178 ADAMS RESOURCES & ENERGY, INC. US-TX ...
- class finagg.sec.api.Submissions[source]
Bases:
API
Get a company’s metadata and all its recent SEC filings.
Not all the metadata typically found with the submissions API is supported by this API. Only common company metadata (e.g., company name, industry code, fiscal year end date, etc.) is returned by this implementation.
The module variable
finagg.sec.api.submissions
is an instance of this API implementation and is the most popular interface for querying this API.- classmethod download_zip(*, chunk_size: int = 1000000, user_agent: None | str = None) ZipFile [source]
Download all SEC filings for all companies in a zip file.
A progress bar displays the download progress as the download could take a long time because the zip file is more than 1GB. The zip file is updated by the SEC at approximately 3am ET nightly.
- Parameters:
chunk_size – Chunk size to stream and write the zip file with.
user_agent – Self-declared bot header. Defaults to the value found in the
SEC_API_USER_AGENT
environment variable.
- Returns:
A zip file object representing the downloaded zip file.
- classmethod get(*, cik: None | str = None, ticker: None | str = None, cache: bool = True, user_agent: None | str = None) SubmissionsResult [source]
Return a company’s metadata and all its recent SEC filings.
- Parameters:
cik – Company SEC CIK. Mutually exclusive with
ticker
.ticker – Company ticker. Mutually exclusive with
cik
.cache – Whether to cache the response from the API.
user_agent – Self-declared SEC bot header. Defaults to the value found in the
SEC_API_USER_AGENT
environment variable.
- Returns:
Company metadata and a dataframe with recent SEC filings.
- Raises:
ValueError – If both a
cik
andticker
are provided or neither are provided.
Examples
>>> out = finagg.sec.api.submissions.get(ticker="AAPL") >>> out["metadata"] {'cik': '0000320193', 'entityType': 'operating', 'sic': '3571', ...}
- class finagg.sec.api.Tickers[source]
Bases:
API
SEC-registered ticker info.
This is a broader method in comparison to
get_ticker_set()
.get_ticker_set()
will get all the tickers that have popular fundamentals data available through the SEC EDGAR API for the previous year, while this method will get all tickers and CIKs that have ever had data available via the SEC EDGAR API.The module variable
finagg.sec.api.tickers
is an instance of this API implementation and is the most popular interface for querying this API.- classmethod get(*, cache: bool = True, user_agent: None | str = None) DataFrame [source]
Get a dataframe containing all SEC-registered ticker info.
- Parameters:
cache – Whether to cache the response from the API.
user_agent – Self-declared SEC bot header. Defaults to the value found in the
SEC_API_USER_AGENT
environment variable.
- Returns:
A dataframe containing company names, their SEC CIKs, and their ticker symbols.
Examples
>>> finagg.sec.api.tickers.get().head(5) cik ticker title 0 320193 AAPL Apple Inc. 1 789019 MSFT MICROSOFT CORP 2 1652044 GOOGL Alphabet Inc. 3 1018724 AMZN AMAZON COM INC 4 1067983 BRK-B BERKSHIRE HATHAWAY INC
- finagg.sec.api.company_concept
The most popular way for accessing the
CompanyConcept
API implementation.
- finagg.sec.api.company_facts
The most popular way for accessing the
CompanyFacts
API implementation.
- finagg.sec.api.submissions
The most popular way for accessing the
Submissions
API implementation.
- finagg.sec.api.popular_frames: list[finagg.sec.api.Frame]
Company frames that have high availability and are relatively popular for fundamental analysis. Includes things like earnings per share, current assets, etc.. Frames are in valid format for usage with the
Frames
API implementation.
- finagg.sec.api.popular_concepts: list[finagg.sec.api.Concept]
Company concepts that have high availability and are relatively popular for fundamental analysis. Includes things like earnings per share, current assets, etc.. Concepts contain all the parameters required for usage with the
CompanyConcept
API implementation.
- finagg.sec.api.compute_financial_ratios(df: DataFrame, /) DataFrame [source]
Compute financial ratios in-place for a dataframe using XBRL financial tags.
- Parameters:
df – Dataframe with common XBRL financial tags (e.g., “Assets”, “Liabilities”, etc.).
- Returns:
df
but with additional columns that’re normalized financial ratios.
- finagg.sec.api.filter_original_filings(df: DataFrame, /, *, form: None | str = None, units: None | str = None) DataFrame [source]
Get all original filing rows as determined by the filing date and tag for a period.
- Parameters:
df – Dataframe without mixed original and amendment rows.
form – Only keep rows with form type
form
. Most popular choices include"10-K"
for annual and"10-Q"
for quarterly. This parameter is ignored if leftNone
.units – Only keep rows with units
units
if notNone
.
- Returns:
Dataframe with rows that only correspond to original filings.
Examples
Only get a company’s original quarterly earnings-per-share filings.
>>> df = finagg.sec.api.company_concept.get( ... "EarningsPerShareBasic", ... ticker="AAPL", ... taxonomy="us-gaap", ... units="USD/shares", ... ) >>> finagg.sec.api.filter_original_filings( ... df, ... form="10-Q", ... units="USD/shares" ... ).head(5) fy fp ... 0 2009 Q3 ... 1 2010 Q1 ... 2 2010 Q2 ... 3 2010 Q3 ... 4 2011 Q1 ...
- finagg.sec.api.get_cik(ticker: str, /, *, user_agent: None | str = None) str [source]
Return a company’s SEC CIK from its ticker.
- Parameters:
ticker – A company’s ticker symbol.
user_agent – Self-declared SEC bot header. Defaults to the value found in the
SEC_API_USER_AGENT
environment variable.
- Returns:
The company’s corresponding SEC CIK.
Examples
Get Apple’s SEC CIK from its ticker.
>>> finagg.sec.api.get_cik("AAPL") == "0000320193" True
- finagg.sec.api.get_ticker(cik: str, /, *, user_agent: None | str = None) str [source]
Return a company’s ticker from its SEC CIK.
- Parameters:
cik – A company’s 10-character SEC CIK.
user_agent – Self-declared SEC bot header. Defaults to the value found in the
SEC_API_USER_AGENT
environment variable.
- Returns:
The company’s corresponding ticker.
Examples
Get Apple’s ticker from its SEC CIK.
>>> finagg.sec.api.get_ticker("0000320193") == "AAPL" True
- finagg.sec.api.get_ticker_set(*, user_agent: None | str = None) set[str] [source]
Get the set of tickers that published data for popular concepts during any of the quarters for the previous year.
This effectively gets the set of tickers whose data is at least somewhat available through the SEC EDGAR API.
- Parameters:
user_agent – Self-declared SEC bot header. Defaults to the value found in the
SEC_API_USER_AGENT
environment variable.- Returns:
Set of tickers whose data is at least somewhat available through the SEC EDGAR API.
Examples
>>> tickers = finagg.sec.api.get_ticker_set() >>> "AAPL" in tickers True >>> "MSFT" in tickers True >>> "GOOG" in tickers True
- finagg.sec.api.group_and_pivot_filings(df: DataFrame, /, *, form: None | str = None) DataFrame [source]
Helper for grouping filings into a pivoted dataframe such that each tag has its own column.
Tags are grouped according to fiscal year and fiscal period. The newest filing date is used when tags have different filing dates for the same fiscal year and fiscal period. Tags are forward-filled to fill gaps in filings.
- Parameters:
df – “Melted” dataframe where each filing is a row.
form – Type of form to group.
"10-K"
sets the index to the fiscal year and filing date whereas"10-Q"
sets the index to the fiscal year, fiscal period, and filing date. If not provided, the form type is chosen by inspecting the first element of the"form"
column.
- Returns:
A pivoted dataframe where each column is a tag.
Examples
Get all XBRL filings from a company, unintelligently select the first unique filings for each XBRL tag, and then group all filings into a pivoted table.
>>> df = finagg.sec.api.company_facts.get(ticker="AAPL") >>> df = finagg.sec.api.filter_original_filings(df, form="10-K") >>> finagg.sec.api.group_and_pivot_filings(df, form="10-K") AccountsPayableCurrent AccountsReceivableNetCurrent ... fy filed ... 2009 2009-10-27 5.520000e+09 2.422000e+09 ... 2010 2010-10-27 5.601000e+09 3.361000e+09 ... 2011 2011-10-26 1.201500e+10 5.510000e+09 ... 2012 2012-10-31 1.463200e+10 5.369000e+09 ... 2013 2013-10-30 2.117500e+10 1.093000e+10 ...
finagg.sec.sql module
SEC SQLAlchemy interfaces.
- finagg.sec.sql.metadata
The metadata associated with all SQL tables defined in this module.
- finagg.sec.sql.submissions
SQL table for storing raw data as managed by
finagg.sec.feat.submissions
(an alias forfinagg.sec.feat.Submissions
).
- finagg.sec.sql.tags
SQL table for storing raw data as managed by
finagg.sec.feat.tags
(an alias forfinagg.sec.feat.Tags
).
- finagg.sec.sql.annual
SQL table for storing refined data as managed by
finagg.sec.feat.annual
(an alias forfinagg.sec.feat.Annual
).
- finagg.sec.sql.normalized_annual
SQL table for storing refined data as managed by
finagg.sec.feat.Annual.normalized
(an alias forfinagg.sec.feat.NormalizedAnnual
).
- finagg.sec.sql.quarterly
SQL table for storing refined data as managed by
finagg.sec.feat.quarterly
(an alias forfinagg.sec.feat.Quarterly
).
- finagg.sec.sql.normalized_quarterly
SQL table for storing refined data as managed by
finagg.sec.feat.Quarterly.normalized
(an alias forfinagg.sec.feat.NormalizedQuarterly
).
- finagg.sec.sql.get_cik(ticker: str, /, *, engine: None | Engine = None) str [source]
Use raw SQL data to find a company’s SEC CIK from its ticker symbol.
This is the preferred method for getting a company’s SEC CIK if raw SQL data is installed. This method is a convenience over
finagg.sec.api.get_cik()
for repeatedly getting company SEC CIKs without having to query the SEC EDGAR API. Usefinagg.sec.api.get_cik()
if you want to get a company’s ticker symbol without installing or accessing locally installed raw SQL data.- Parameters:
ticker – A company’s ticker symbol.
engine – Feature store database engine. Defaults to the engine at
finagg.backend.engine
.
- Returns:
The company’s corresponding SEC CIK.
Examples
Get Apple’s SEC CIK from its ticker.
>>> finagg.sec.sql.get_cik("AAPL") == "0000320193" True
- finagg.sec.sql.get_metadata(*, cik: None | str = None, ticker: None | str = None, engine: None | Engine = None) dict[str, Any] [source]
Return a company’s metadata (its SEC CIK, ticker, name, and industry code) from its SEC CIK or its ticker symbol.
A convenient method for getting a company’s metadata using raw SQL data. This method is a convenience over
finagg.sec.api.submissions
for repeatedly getting company metadata without having to query the SEC EDGAR API. Usefinagg.sec.api.submissions
if you want to get a company’s metadata without installing or accessing locally installed raw SQL data.- Parameters:
cik – Company SEC CIK. Mutually exclusive with
ticker
.ticker – Company ticker. Mutually exclusive with
cik
.engine – Feature store database engine. Defaults to the engine at
finagg.backend.engine
.
- Returns:
A company’s metadata as a dictionary.
Examples
>>> finagg.sec.sql.get_metadata(ticker="MSFT") {'cik': '0000789019', 'ticker': 'MSFT', 'name': 'microsoft corp', 'sic': '7372'}
- finagg.sec.sql.get_ticker(cik: str, /, *, engine: None | Engine = None) str [source]
Use raw SQL data to find a company’s ticker from its SEC CIK.
This is the preferred method for getting a company’s ticker if raw SQL data is installed. This method is a convenience over
finagg.sec.api.get_ticker()
for repeatedly getting company tickers without having to query the SEC EDGAR API. Usefinagg.sec.api.get_ticker()
if you want to get a company’s ticker symbol without installing or accessing locally installed raw SQL data.- Parameters:
cik – A company’s SEC CIK.
engine – Feature store database engine. Defaults to the engine at
finagg.backend.engine
.
- Returns:
The company’s corresponding ticker symbol.
Examples
Get Apple’s ticker from its SEC CIK.
>>> finagg.sec.sql.get_ticker("0000320193") == "AAPL" True
- finagg.sec.sql.get_tickers_in_industry(*, ticker: None | str = None, code: None | str = None, level: Literal[2, 3, 4] = 2, engine: None | Engine = None) set[str] [source]
Get a set of tickers that all share the same industry using raw SQL data.
This method is convenient for finding tickers within the same industry so they can be compared. A related and common pattern is to use
finagg.sec.feat.Quarterly.normalized
to get industry-normalized features for a particular company. Similar to other methods in this submodule, this will only return tickers that have raw SQL data associated with them.- Parameters:
ticker – Company ticker. Lookup the industry associated with this company. Mutually exclusive with
code
.code – Industry SIC code to use for industry lookup. Mutually exclusive with
ticker
.level –
Industry level to find tickers within. The industry used according to
ticker
orcode
is subsampled according to this value. Options include:2 = major group (e.g., furniture and fixtures)
3 = industry group (e.g., office furnitures)
4 = industry (e.g., wood office furniture)
engine – Feature store database engine. Defaults to the engine at
finagg.backend.engine
.
- Returns:
A set of tickers that all share the same industry as denoted by another ticker (using the
ticker
arg) or an industry SIC code (using thecode
arg).
Examples
>>> "ETSY" in finagg.sec.sql.get_tickers_in_industry(ticker="MSFT") True
Module contents
Top-level SEC interface.