-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
168 lines (123 loc) · 4.44 KB
/
script.py
File metadata and controls
168 lines (123 loc) · 4.44 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-
"""
SEC ``Filing`` Scraper
"""
# import modules
import requests
import pandas as pd
import matplotlib as plot
# create request header
headers = {'User-Agent': "sammyhasan17@gmail.com"}
# get all companies data
companyTickers = requests.get(
"https://www.sec.gov/files/company_tickers.json",
headers=headers
# todo update tickers to competitors only (list in peers.txt)
)
# review response / keys
print(companyTickers.json().keys())
# format response to dictionary and get first key/value
firstEntry = companyTickers.json()['0']
# parse CIK // without leading zeros
directCik = companyTickers.json()['0']['cik_str']
# dictionary to dataframe
companyData = pd.DataFrame.from_dict(companyTickers.json(),
orient='index')
# add leading zeros to CIK
companyData['cik_str'] = companyData['cik_str'].astype(
str).str.zfill(10)
# review data
print(companyData[:20])
cik = companyData[0:1].cik_str[0]
# get company specific filing metadata
filingMetadata = requests.get(
f'https://data.sec.gov/submissions/CIK{cik}.json',
headers=headers
)
# review json
print(filingMetadata.json().keys())
filingMetadata.json()['filings']
filingMetadata.json()['filings'].keys()
filingMetadata.json()['filings']['recent']
filingMetadata.json()['filings']['recent'].keys()
# dictionary to dataframe
allForms = pd.DataFrame.from_dict(
filingMetadata.json()['filings']['recent']
)
# review columns
allForms.columns
allForms[['accessionNumber', 'reportDate', 'form']].head(50)
# 10-Q metadata
allForms.iloc[11]
# get company facts data
companyFacts = requests.get(
f'https://data.sec.gov/api/xbrl/companyfacts/CIK{cik}.json',
headers=headers
)
#review data
companyFacts.json().keys()
companyFacts.json()['facts']
companyFacts.json()['facts'].keys()
# filing metadata
companyFacts.json()['facts']['dei'][
'EntityCommonStockSharesOutstanding']
companyFacts.json()['facts']['dei'][
'EntityCommonStockSharesOutstanding'].keys()
companyFacts.json()['facts']['dei'][
'EntityCommonStockSharesOutstanding']['units']
companyFacts.json()['facts']['dei'][
'EntityCommonStockSharesOutstanding']['units']['shares']
companyFacts.json()['facts']['dei'][
'EntityCommonStockSharesOutstanding']['units']['shares'][0]
# concept data // financial statement line items
companyFacts.json()['facts']['us-gaap']
companyFacts.json()['facts']['us-gaap'].keys()
# different amounts of data available per concept
companyFacts.json()['facts']['us-gaap']['AccountsPayable']
companyFacts.json()['facts']['us-gaap']['Revenues']
companyFacts.json()['facts']['us-gaap']['Assets']
# get company concept data
companyConcept = requests.get(
(
f'https://data.sec.gov/api/xbrl/companyconcept/CIK{cik}'
f'/us-gaap/Assets.json'
),
headers=headers
)
# review data
companyConcept.json().keys()
companyConcept.json()['units']
companyConcept.json()['units'].keys()
companyConcept.json()['units']['USD']
companyConcept.json()['units']['USD'][0]
# parse assets from single filing
companyConcept.json()['units']['USD'][0]['val']
# get all filings data
assetsData = pd.DataFrame.from_dict((
companyConcept.json()['units']['USD']))
# review data
assetsData.columns
assetsData.form
# get assets from 10Q forms and reset index
assets10Q = assetsData[assetsData.form == '10-Q']
assets10Q = assets10Q.reset_index(drop=True)
# plot
assets10Q.plot(x='end', y='val')
##########################################################################
print(companyData)
# Define the list of CIKs to keep
target_ciks = ['0000945841','0000091142','0001852345', '0000795403', '0001821806','0000077360', '0001833197','0001834622']
# Format and filter CIKs
filteredData = companyData[companyData['cik_str'].isin(target_ciks)]
print(filteredData)
# Loop through the filtered CIKs and get filing metadata
for cik in filteredData['cik_str']:
print(f"Fetching data for CIK: {cik}")
filingMetadata = requests.get(
f'https://data.sec.gov/submissions/CIK{cik}.json',
headers=headers
)
data = filingMetadata.json()
print(f"Company Name: {data.get('name', 'Unknown')}")
print(f"Latest Filing Date: {data.get('filings', {}).get('recent', {}).get('dateFiled', ['N/A'])[0]}")
print("-" * 40)