Repository navigation
Expand file tree
/
Copy pathedgarscrape.py
More file actions
145 lines (118 loc) · 4.99 KB
/
Copy pathedgarscrape.py
File metadata and controls
145 lines (118 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import requests
import pandas as pd
# ==========================================
# 1. CONFIGURATION
# ==========================================
# The SEC requires a User-Agent. Replace with your name/email.
HEADERS = {'User-Agent': 'Matthew McWhorter matthewmac111@gmail.com'}
def get_cik(ticker):
"""Maps a stock ticker to the SEC's internal CIK number."""
url = "https://www.sec.gov/files/company_tickers.json"
r = requests.get(url, headers=HEADERS)
for entry in r.json().values():
if entry['ticker'] == ticker.upper():
return str(entry['cik_str']).zfill(10)
return None
def get_fact_dataframe(json_data, tag_list):
"""
Scans the SEC JSON for the first matching XBRL tag from our list.
Returns a clean DataFrame of that data.
"""
us_gaap = json_data['facts']['us-gaap']
for tag in tag_list:
if tag in us_gaap:
# Found the data!
units = us_gaap[tag]['units']
# Different tags use different units (USD vs shares)
key = 'USD' if 'USD' in units else list(units.keys())[0]
df = pd.DataFrame(units[key])
# Filter for Annual Reports (10-K) only
df = df[df['form'] == '10-K'].copy()
# Filter for full-year coverage (approx 360-375 days)
# This prevents mistakes with "quarterly" data appearing in 10-Ks
if 'start' in df.columns and 'end' in df.columns:
df['start'] = pd.to_datetime(df['start'])
df['end'] = pd.to_datetime(df['end'])
df['days'] = (df['end'] - df['start']).dt.days
df = df[(df['days'] > 360) & (df['days'] < 375)]
# Sort newest first and return
return df.sort_values('end', ascending=False), tag
return pd.DataFrame(), None
def run_analysis(ticker):
print(f"--- FETCHING DEEP HISTORY FOR {ticker.upper()} ---")
# 1. Get CIK
cik = get_cik(ticker)
if not cik:
print("Ticker not found.")
return
# 2. Download the 'Company Facts' JSON (The Master File)
print(f"Downloading XBRL data for CIK {cik}...")
url = f"https://data.sec.gov/api/xbrl/companyfacts/CIK{cik}.json"
r = requests.get(url, headers=HEADERS)
json_data = r.json()
# 3. Extract REVENUE
# (Checking multiple tags because companies change accounting styles)
rev_tags = [
'RevenueFromContractWithCustomerExcludingAssessedTax',
'Revenues',
'SalesRevenueNet',
'SalesRevenueGoodsNet'
]
rev_df, rev_tag_used = get_fact_dataframe(json_data, rev_tags)
# 4. Extract NET INCOME
inc_tags = [
'NetIncomeLoss',
'ProfitLoss',
'NetIncomeLossAvailableToCommonStockholdersBasic'
]
inc_df, inc_tag_used = get_fact_dataframe(json_data, inc_tags)
# 5. Extract EPS (Diluted is safer for valuation)
eps_tags = [
'EarningsPerShareDiluted',
'EarningsPerShareBasic'
]
eps_df, eps_tag_used = get_fact_dataframe(json_data, eps_tags)
# ==========================================
# DISPLAY RESULTS
# ==========================================
if rev_df.empty or inc_df.empty:
print("Error: Could not align financial data.")
return
# Grab the most recent common year
latest_rev = rev_df.iloc[0]
latest_year = latest_rev['end'].year
# Match Net Income to that same year
try:
latest_inc = inc_df[inc_df['end'] == latest_rev['end']].iloc[0]
latest_eps = eps_df[eps_df['end'] == latest_rev['end']].iloc[0]
except IndexError:
print("Data mismatch: Revenue and Income years do not align.")
return
print(f"\n=== FUNDAMENTAL SNAPSHOT ({latest_year} Fiscal Year) ===")
print(f"Revenue: ${latest_rev['val']/1e9:,.2f} B (Tag: {rev_tag_used})")
print(f"Net Income: ${latest_inc['val']/1e9:,.2f} B (Tag: {inc_tag_used})")
print(f"EPS (Diluted): ${latest_eps['val']:.2f}")
# Calculate Profit Margin
margin = (latest_inc['val'] / latest_rev['val']) * 100
print(f"Net Margin: {margin:.2f}%")
# ==========================================
# VALUATION (P/E RATIO)
# ==========================================
print(f"\n--- VALUATION CHECK ---")
try:
price_input = input(f"Enter current stock price for {ticker.upper()}: $")
current_price = float(price_input)
pe_ratio = current_price / latest_eps['val']
print(f"P/E Ratio: {pe_ratio:.2f}x")
# Simple Graham Assessment
if pe_ratio < 15:
print(">> STATUS: Potentially Undervalued (Classic Graham)")
elif pe_ratio > 30:
print(">> STATUS: High Growth Expectations / Overvalued")
else:
print(">> STATUS: Fairly Valued")
except ValueError:
print("Invalid price entered. Skipping valuation.")
# Run it
ticker_input = input("Enter ticker (e.g. AAPL): ")
run_analysis(ticker_input)