Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update StockFinancial model (net income, EPS, and parent net income loss) #817

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/rest/stocks-stock_financials.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
client = RESTClient() # POLYGON_API_KEY environment variable is used

financials = []
for f in client.vx.list_stock_financials("AAPL"):
for f in client.vx.list_stock_financials("AAPL", filing_date="2024-11-01"):
financials.append(f)
print(financials)
101 changes: 59 additions & 42 deletions polygon/rest/models/financials.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@

@modelclass
class DataPoint:
"An individual financial data point."
formula: Optional[str] = None
"Represents a single financial data point."
label: Optional[str] = None
order: Optional[int] = None
unit: Optional[str] = None
value: Optional[float] = None
derived_from: Optional[list] = None
formula: Optional[str] = None
source: Optional[dict] = None
xpath: Optional[str] = None

@staticmethod
def from_dict(d):
return DataPoint(**d)
return DataPoint(
label=d.get("label"),
order=d.get("order"),
unit=d.get("unit"),
value=d.get("value"),
derived_from=d.get("derived_from"),
formula=d.get("formula"),
source=d.get("source"),
xpath=d.get("xpath"),
)


@modelclass
Expand Down Expand Up @@ -286,43 +297,42 @@ def from_dict(d):

@modelclass
class Financials:
"Contains financial data."
balance_sheet: Optional[Dict[str, DataPoint]] = None
cash_flow_statement: Optional[CashFlowStatement] = None
comprehensive_income: Optional[ComprehensiveIncome] = None
income_statement: Optional[IncomeStatement] = None
"""
Contains data for:
- balance_sheet
- cash_flow_statement
- comprehensive_income
- income_statement
Each is a dict of { 'SomeTag': DataPoint }, e.g. { 'NetIncomeLoss': DataPoint(...) }
"""

balance_sheet: Optional[dict] = None
cash_flow_statement: Optional[dict] = None
comprehensive_income: Optional[dict] = None
income_statement: Optional[dict] = None

@staticmethod
def from_dict(d):
def parse_statement(x):
if not x or not isinstance(x, dict):
return None
return {k: DataPoint.from_dict(v) for k, v in x.items()}

return Financials(
balance_sheet=(
None
if "balance_sheet" not in d
else {
k: DataPoint.from_dict(v) for (k, v) in d["balance_sheet"].items()
}
),
cash_flow_statement=(
None
if "cash_flow_statement" not in d
else CashFlowStatement.from_dict(d["cash_flow_statement"])
),
comprehensive_income=(
None
if "comprehensive_income" not in d
else ComprehensiveIncome.from_dict(d["comprehensive_income"])
),
income_statement=(
None
if "income_statement" not in d
else IncomeStatement.from_dict(d["income_statement"])
),
balance_sheet=parse_statement(d.get("balance_sheet")),
cash_flow_statement=parse_statement(d.get("cash_flow_statement")),
comprehensive_income=parse_statement(d.get("comprehensive_income")),
income_statement=parse_statement(d.get("income_statement")),
)


@modelclass
class StockFinancial:
"StockFinancial contains historical financial data for a stock ticker."
"""
StockFinancial contains historical financial data for a stock ticker.
"""

# Existing fields (unchanged):
cik: Optional[str] = None
company_name: Optional[str] = None
end_date: Optional[str] = None
Expand All @@ -336,17 +346,24 @@ class StockFinancial:

@staticmethod
def from_dict(d):
return StockFinancial(
cik=d.get("cik", None),
company_name=d.get("company_name", None),
end_date=d.get("end_date", None),
filing_date=d.get("filing_date", None),
"""
Create a StockFinancial, preserving all old behavior, but also pulling out
a few commonly used fields from the income_statement and comprehensive_income
so they can be accessed directly at the top level.
"""
sf = StockFinancial(
cik=d.get("cik"),
company_name=d.get("company_name"),
end_date=d.get("end_date"),
filing_date=d.get("filing_date"),
financials=(
None if "financials" not in d else Financials.from_dict(d["financials"])
Financials.from_dict(d["financials"]) if "financials" in d else None
),
fiscal_period=d.get("fiscal_period", None),
fiscal_year=d.get("fiscal_year", None),
source_filing_file_url=d.get("source_filing_file_url", None),
source_filing_url=d.get("source_filing_url", None),
start_date=d.get("start_date", None),
fiscal_period=d.get("fiscal_period"),
fiscal_year=d.get("fiscal_year"),
source_filing_file_url=d.get("source_filing_file_url"),
source_filing_url=d.get("source_filing_url"),
start_date=d.get("start_date"),
)

return sf
6 changes: 0 additions & 6 deletions test_rest/mocks/vX/reference/financials.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@
"unit": "USD",
"order": 400
},
"other_than_fixed_noncurrent_assets": {
"label": "Other Than Fixed Noncurrent Assets",
"value": 1.6046e+10,
"unit": "USD",
"order": 500
},
"noncurrent_liabilities": {
"label": "Noncurrent Liabilities",
"value": 1.1716e+10,
Expand Down
234 changes: 0 additions & 234 deletions test_rest/test_financials.py

This file was deleted.

Loading