-
Notifications
You must be signed in to change notification settings - Fork 4
/
SecFiling.py
442 lines (399 loc) · 22 KB
/
SecFiling.py
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import requests
from bs4 import BeautifulSoup as BSoup
import pandas as pd
import csv
import requests
import os
from datetime import datetime, timedelta
from abc import ABC, abstractmethod
import traceback
class SecFiling(ABC):
"""Holds the data of a generic SEC filing.
Expected to be subclassed by 'SecFiling10K' and 'SecFiling10Q'. Also expected to be a utility to CanslimParams, i.e.
driven by it.
Before running this, make sure my edgar_idx.db is up-to-date. analyze_stockdata.get_list_sec_filings() will
update the 'idx' table (i.e. the database with the relevant urls), and analyze_stockdata.get_cik_ticker_lookup_db()
will update the lookup table for CIK number - ticker symbol correlation.
"""
def __init__(self, ticker):
self.ticker = ticker
self.xbrlInstance = None
self.contextIds = {}
self.currentContextId = ""
self.errorLog = []
self.currentSE = None
self.currentNI = None
self.reportDate = None
def __del__(self):
self.contextIds.clear()
del self.errorLog[:]
def getReportDate(self):
return self.reportDate
def download(self, cik, co_name, filing_type, filing_date, filing_link, downloadPath = "SECDATA"):
"""Retrieves a SEC filing from EDGAR. Follow by a call to 'load()'.
Also a roundabout way of getting the filename for this data file.
"""
# inspired by http://kaikaichen.com/?p=681
saveas = '_'.join([cik, co_name, filing_type, str(filing_date)])
saveDir = os.path.join(downloadPath, co_name)
self.fname = os.path.join (saveDir, saveas)
## Only download if the file doesn't exist yet (at the expected path)
if not os.path.exists(self.fname):
url = 'https://www.sec.gov/Archives/' + filing_link.strip()
if not os.path.exists(saveDir):
os.makedirs(saveDir)
with open("logfile.txt", "a+") as logfile:
with open(self.fname, 'wb') as f:
try:
f.write(requests.get('%s' % url).content)
logfile.write('{:s} - downloaded and saved as {:s}\n'.format(url, self.fname))
except BaseException as be:
self.errorLog.append("SecFiling: Unable to download from url: {:s}".format(url))
self.errorLog.append(str(be))
return (self.fname)
def load(self, fname):
"""Loads the data of SEC filing."""
f = open (fname, "r")
## TODO: Do I need to have the whole file in memory?
wholeFile = f.read ()
f.close ()
soup = BSoup (wholeFile, "lxml")
## Extract some basic info from the SEC-header section
sec_header = soup.find("sec-header")
try:
for l in sec_header.text.split("\n"):
if 'conformed period of report' in l.lower():
self.reportDate = datetime.strptime((l.split(":")[1]).strip(), "%Y%m%d")
if 'standard industrial classification' in l.lower():
self.stdIndustrialClass = l.split(":")[1].strip()
self.errorLog.append("SecFiling: Report date: {:s}, industrial class: {:s}". format(str(self.reportDate), self.stdIndustrialClass))
except:
self.errorLog.append("SecFiling: Unable to find SEC-header in file.")
return False
## Find the XBRL Instance document. It has the description-tag "XBRL INSTANCE DOCUMENT" within its document-tag.
doc_tag = soup.find_all("document")
try:
for tag in doc_tag:
desc_tag = tag.find("description")
if desc_tag:
description = desc_tag.get_text().lower()
if (description.startswith("xbrl instance document")):
self.xbrlInstance = tag
break
## Try again, in case the first attempt didn't find an XBRL Document
if not self.xbrlInstance:
for tag in doc_tag:
## Sometimes the description seems to be (mis-)named by the default instance document file named.
## Don't 'break', to give the first if statement priority.
## The "ex-..." seems to more reliably be in the TYPE-tag than the Description-tag
desc_tag = tag.find("type")
if desc_tag:
description = desc_tag.get_text().lower()
if description.startswith("EX-101.INS".lower()):
self.xbrlInstance = tag
break
desc_tag = tag.find("description")
if desc_tag:
description = desc_tag.get_text().lower()
if description.startswith("EX-101.INS".lower()):
self.xbrlInstance = tag
break
## Try yet again, in case the second attempt didn't find an XBRL Document either
if not self.xbrlInstance:
for tag in doc_tag:
## In the case of inline XBRL tags, there is no Instance document, but the SEC extracts one.
## Try and find that instead, by finding an XML-tag within a description tag of "IDEA: XBRL DOCUMENT".
if (description.startswith("idea: xbrl document")):
self.errorLog.append("SecFiling: Trying to fall back on extracted instance document.\n")
## I think I just want the first occurrence of this. It seems that that is the XBRL Doc.
if not self.xbrlInstance:
xml_tags = tag.find("xml")
#self.errorLog.append(xml_tags)
if xml_tags:
self.xbrlInstance = tag
break
except:
self.errorLog.append("SecFiling: Unable to find document-tags in file.")
return False
if not self.xbrlInstance:
self.errorLog.append("SecFiling: ERROR: unable to find Instance document for {:s}.\n".format(self.fname))
return False
## 'findAll('us-gaap:earningspersharebasic')' doesn't seem to work... so find them manually
self.all_tags = self.xbrlInstance.findAll()
if not self.all_tags:
self.errorLog.append("SecFiling: No tags found!")
return False
return True
def save(self):
"""Saves the data to file."""
pass
def getEps(self):
"""Retrieve the current EPS from the filing."""
possible_tags = [\
## The standard tag, used by most (MMM uses all lower-case)
'us-gaap:EarningsPerShareBasic'.lower()\
## NRP uses this tag (wtf?!?)
,'us-gaap:IncomeLossFromContinuingOperationsPerOutstandingLimitedPartnershipUnitBasicNetOfTax'.lower()\
## Used by OSK
,'us-gaap:IncomeLossFromContinuingOperationsPerBasicShare'.lower()\
## WK uses this tag:
,'us-gaap:EarningsPerShareBasicAndDiluted'.lower()\
]
all_eps_tags = []
try:
for tag in self.all_tags:
if tag.name.strip().lower() in possible_tags:
all_eps_tags.append(tag)
self.currentEps = self.getCurrentValue(all_eps_tags)
except:
self.errorLog.append("SecFiling: Unable to find the EPS information in the filing.\n")
return None
return self.currentEps
def getSales(self, contextId = ""):
"""Retrieves the current Sales data from the filing.
WARNING! The 'currentContextRef' member must have been set first (e.g. by calling getEps or getRoe
before calling this function) for this function to work properly. It seems to be very tricky to
figure out the currentContextRef by itself."""
if not contextId:
if not self.currentContextId:
self.errorLog.append("SecFiling: ERROR! current contextId is not set!\n")
return -99.0
else:
contextId = self.currentContextId
## Collect a list of possible tags to check for to get sales. There are a lot, but so far
## there are no collisions within a statement for these.
possible_tags = [\
## The filings seem to use either 'Revenues' or 'SalesRevenuesNet' to indicate the net sales amount
'us-gaap:Revenues'.lower()\
,'us-gaap:SalesRevenuesNet'.lower()\
## Some of AAPL's statements use this tag:
,'us-gaap:salesrevenuenet'.lower()\
## Some of AMG's (and JNJ, OSK) statements use this tags:
,'us-gaap:RevenueFromContractWithCustomerExcludingAssessedTax'.lower()\
## Some of AMG's statements also sometimes use this tag:
,'us-gaap:AssetManagementFees1'.lower()\
## The 10-Q of CRVL for 2018-Q1 (filing date 2017/12/31) uses this tag:
,'us-gaap:SalesRevenueServicesNet'.lower()\
## NRP uses this tag (hopefully it's the right one for sales)
,'us-gaap:IncomeLossFromContinuingOperationsIncludingPortionAttributableToNoncontrollingInterest'.lower()\
## INS uses this tag:
,'us-gaap:RevenueFromContractWithCustomerIncludingAssessedTax'.lower()\
## JNJ's 10-Q for 2018Q2 uses this tag?!?:
,'us-gaap:NewAccountingPronouncementsAndChangesInAccountingPrinciplesTextBlock'.lower()\
]
all_sales_tags = []
try:
for tag in self.all_tags:
## The filings seem to use either 'Revenues' or 'SalesRevenuesNet' to indicate the net sales amount
## But the 'Revenues' tag also contains other stuff, need to filter it by its contextref attribute
if tag.name.strip().lower() in possible_tags:
if (tag.attrs)['contextref'] == contextId:
all_sales_tags.append(tag)
# if 'us-gaap:Revenues'.lower() == tag.name.strip().lower():
# if (tag.attrs)['contextref'] == contextId:
# all_sales_tags.append(tag)
# elif 'us-gaap:SalesRevenuesNet'.lower() == tag.name.strip().lower():
# if (tag.attrs)['contextref'] == contextId:
# all_sales_tags.append(tag)
# Some of AAPL's statements use this tag:
# elif 'us-gaap:salesrevenuenet'.lower() == tag.name.strip().lower():
# if (tag.attrs)['contextref'] == contextId:
# all_sales_tags.append(tag)
# Some of AMG's (and JNJ, OSK) statements use one of these two tags:
# elif 'us-gaap:RevenueFromContractWithCustomerExcludingAssessedTax'.lower() == tag.name.strip().lower():
# if (tag.attrs)['contextref'] == contextId:
# all_sales_tags.append(tag)
# elif 'us-gaap:AssetManagementFees1'.lower() == tag.name.strip().lower():
# if (tag.attrs)['contextref'] == contextId:
# all_sales_tags.append(tag)
# The 10-Q of CRVL for 2018-Q1 (filing date 2017/12/31) uses this tag:
# elif 'us-gaap:SalesRevenueServicesNet'.lower() == tag.name.strip().lower():
# if (tag.attrs)['contextref'] == contextId:
# all_sales_tags.append(tag)
# elif 'us-gaap:IncomeLossFromContinuingOperationsIncludingPortionAttributableToNoncontrollingInterest'.lower() == tag.name.strip().lower():
# NRP uses this tag (hopefully it's the right one for sales)
# if (tag.attrs)['contextref'] == contextId:
# all_sales_tags.append(tag)
# INS uses this tag:
# elif 'us-gaap:RevenueFromContractWithCustomerIncludingAssessedTax'.lower() == tag.name.strip().lower():
# if (tag.attrs)['contextref'] == contextId:
# all_sales_tags.append(tag)
# JNJ's 10-Q for 2018Q2 uses this tag?!?:
# elif 'us-gaap:NewAccountingPronouncementsAndChangesInAccountingPrinciplesTextBlock'.lower() == tag.name.strip().lower():
# if (tag.attrs)['contextref'] == contextId:
# all_sales_tags.append(tag)
except BaseException as be:
self.errorLog.append("SecFiling: Unable to find Sales data in filing.")
self.errorLog.append(be)
return None
if not all_sales_tags:
self.errorLog.append("SecFiling: Sales/Revenue information not found.")
self.currentSales = self.getCurrentValue(all_sales_tags)
return self.currentSales
def getRoe(self):
"""Retrieves the current Return on Equity from the filing."""
## First find the stockholders' equity
currentSE = self.getStockholdersEquity()
## Then find the net income
currentNI = self.getNetIncome()
## Return on equity = net income / stockholders' equity
self.currentRoe = 0.0
if self.currentSE and self.currentNI and self.currentSE > 0.0:
self.currentRoe = self.currentNI / self.currentSE
# Multiply by 100 to turn it into a percentage.
return self.currentRoe * 100.0
def getCurrentValue(self, tagList, duration, eps):
"""Finds the tag for the current date from a list of tags, and returns its value."""
prevDiff = timedelta(9999, 0, 0) ## days, seconds, microseconds (!)
prevContextRefLen = 100000
current = None
try:
for tag in tagList:
## Find the quarter that ended closest to the reportDate, make sure it's a quarter
## TODO: figure out how to do this for the 10-K's
contextRef = str((tag.attrs)['contextref'])
## The various 'us-gaap:<quantity-of-interest>' tags can occur multiple times for the same time frame,
## but usually distinguish between contextRef names (i.e. the contextref attributes are different, but
## denote the same time frame). It seems that I generally want the contextRef with the shortest name
## (i.e. least special one)?
contextRefLen = len(contextRef)
if contextRef in self.contextIds:
tag_dates = self.contextIds[contextRef]
else:
tag_dates = self.getStartEndDateForContext(contextRef)
self.contextIds[contextRef] = tag_dates
try:
diff = self.reportDate - tag_dates[1]
## Find the closest enddate to the ReportDate, i.e. the 'current' date
if abs(diff.days) < abs(prevDiff.days): ## found a date closer to the reportDate
prevDiff = diff
## Reset the length of the contextref string
prevContextRefLen = 100000
## found another candidate, do all the checks
if abs(diff.days) <= abs(prevDiff.days): ## found a date closer to the reportDate
## If the date is an instantaneous date, only look at the 'diff'
if tag_dates[1] == tag_dates[0]:
if contextRefLen < prevContextRefLen:
prevContextRefLen = contextRefLen
current = tag
prevDiff = diff
## If a date range is given, make sure it's about a year long
elif abs(abs(tag_dates[1] - tag_dates[0]).days - duration) < eps:
if contextRefLen < prevContextRefLen:
prevContextRefLen = contextRefLen
current = tag
prevDiff = diff
except BaseException as be:
self.errorLog.append(str(be))
return None
if current:
self.currentContextId = (current.attrs)['contextref']
else:
self.errorLog.append("SecFiling: Failed to determine the value for current contextId")
self.errorLog.append("SecFiling: Received tagList: \n" + str(tagList))
self.errorLog.append("SecFiling: Stored contextIds: \n" + str(self.contextIds))
return None
except Exception as ex:
self.errorLog.append("SecFiling: Unable to determine the current value:")
self.errorLog.append(type(ex).__name__)
self.errorLog.append(ex)
self.errorLog.append(traceback.print_exc())
return None
return float(current.text)
def getStockholdersEquity(self):
"""Returns the value saved in self.currentSE.
This attribute is populated by getRoe(). It is 'None' until that function is called.
Useful for debugging purposes.
"""## First find the stockholders' equity
all_se_tags = []
self.currentSE = None
try:
for tag in self.all_tags:
## Check for all possible Stockholders' Equity- indicating tags
if 'us-gaap:StockholdersEquity'.lower() == tag.name.strip():
all_se_tags.append(tag)
elif 'us-gaap:StockholdersEquityIncludingPortionAttributableToNoncontrollingInterest'.lower() \
== tag.name.strip():
all_se_tags.append(tag)
if not all_se_tags:
self.errorLog.append("SecFiling: Stockholders' equity information not found.")
self.currentSE = self.getCurrentValue(all_se_tags)
except:
if not self.currentSE:
self.errorLog.append("SecFiling: Unable to find Stockholders' Equity in filing.")
return None
return self.currentSE
def getNetIncome(self):
"""Returns the value saved in self.currentNI.
This attribute is populated by getRoe(). It is 'None' until that function is called.
Useful for debugging purposes.
"""
## Then find the net income
all_ni_tags = []
self.currentNI = None
try:
for tag in self.all_tags:
## Check for all possible Net Income- indicating tags
if 'us-gaap:NetIncomeLoss'.lower() == tag.name.strip():
all_ni_tags.append(tag)
elif 'us-gaap:NetIncomeLossAvailableToCommonStockholdersBasic'.lower() == tag.name.strip():
all_ni_tags.append(tag)
elif 'us-gaap:ProfitLoss'.lower() == tag.name.strip():
all_ni_tags.append(tag)
if not all_ni_tags:
self.errorLog.append("SecFiling: Net income information not found.")
self.currentNI = self.getCurrentValue(all_ni_tags)
except:
if not self.currentNI:
self.errorLog.append("SecFiling: Unable to find Net Income in filing.")
return None
return self.currentNI
def getStartEndDateForContext(self, contextRef):
"""Looks up the date for the given contextref/id.
This used to be a method under the SecFiling class. For some reason (me, Jupyter notebooks, other),
I would get an AttributeError that SecFiling10Q did not have this attribute when calling, e.g. 'getEps()
on a SecFiling10Q object. So I nested it here under 'getCurrentValue'. """
## Some filings use 'xbrli:context' as the tag
periodXml = self.xbrlInstance.find("xbrli:context", {"id" : contextRef})
## Some filings just use 'context' as the tag
if not periodXml:
periodXml = self.xbrlInstance.find("context", {"id" : contextRef})
if periodXml:
## We found a context-tag, try to determine the dates. Again, some use 'xbrli:<tag>', some just
## use '<tag>'. Also, some contexts are instantaneous ('instant'), and some indicate a date range
## Search for each case, and search for all known variants of the tag names, just in case some
## filings mix between them.
startdate = periodXml.find("xbrli:startdate") ## or just 'startdate'
if not startdate:
startdate = periodXml.find("startdate")
enddate = periodXml.find("xbrli:enddate") ## or just 'enddate'
if not enddate:
enddate = periodXml.find("enddate")
instant = periodXml.find("xbrli:instant") ## instantaneous date, not a date range
if not instant:
instant = periodXml.find("instant") ## instantaneous date, not a date range
## Presumably, there is either an 'instant' or a 'startdate' + 'enddate'
if instant:
startdate = instant
enddate = startdate
else:
self.errorLog.append("SecFiling: ERROR: Unable to find dates for contextref '{:s}'".format(contextRef))
return None
try:
sd = datetime.strptime(startdate.text, "%Y-%m-%d")
except:
sd = None
try:
ed = datetime.strptime(enddate.text, "%Y-%m-%d")
except:
ed = None
return [sd, ed]
def getCurrentContextId(self):
return self.currentContextId
def printErrors(self):
return ", ".join(str(e) for e in self.errorLog)
def popErrors(self):
"""Returns a string of all the errors, separated by '\n', and clears the error log."""
errString = "\n".join(str(e) for e in self.errorLog)
self.errorLog = []
return errString