-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassignment2.py
More file actions
405 lines (311 loc) · 15.9 KB
/
Copy pathassignment2.py
File metadata and controls
405 lines (311 loc) · 15.9 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
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
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 1 20:27:28 2017
@author: williamtsao
Dependencies:
Beautiful Soup:
$ pip install beautifulsoup4
joblib:
$ pip install joblib
"""
import re
import xml.etree.ElementTree as ET
import urllib
#import time
#t0 = time.time()
try:
from bs4 import BeautifulSoup as BS
except ImportError:
print("Fatal Failure: Please download dependency BeautifulSoup\n pip install beautifulsoup4")
quit()
def getNewXML(url):
res = urllib.request.urlopen(url)
html = BS(res, 'html.parser')
tag = html.find("a", string=re.compile('.*\.xml$'))
xmlUrl = 'https://www.sec.gov'+tag['href']
xmlRes = urllib.request.urlopen(xmlUrl)
tree = ET.parse(xmlRes)
root = tree.getroot()
return root
def handleTable1(basicInfo, root):
transactionList = []
for transaction in root.findall('nonDerivativeTransaction'):
#newRecord gets all basicInfo
newRecord = []
for ele in basicInfo:
newRecord.append(ele)
newRecord.append('N')#for transaction with nonDerivativeSecurities
try:
#append transaction title
newRecord.append('"'+transaction.find('securityTitle').find('value').text+'"')
#append transaction coding
newRecord.append(transaction.find('transactionCoding').find('transactionCode').text)
transactionAmount = transaction.find('transactionAmounts')
#append amount of shares
newRecord.append(transactionAmount.find('transactionShares').find('value').text)
#append price per share
newRecord.append(transactionAmount.find('transactionPricePerShare').find('value').text)
#append Acquired or Disposed of
newRecord.append(transactionAmount.find('transactionAcquiredDisposedCode').find('value').text)
#append ownership (Direct or Indirect)
newRecord.append(transaction.find('ownershipNature').find('directOrIndirectOwnership').find('value').text)
except AttributeError:
continue
else:
#append 8 N/A to the end for fields meant for table 2 transactions
for i in range(0,6):
newRecord.append('N/A')
#append this transaction to transactionList
transactionList.append(newRecord)
return transactionList
def handleTable2(basicInfo, root):
transactionList = []
for transaction in root.findall('derivativeTransaction'):
#newRecord gets all basicInfo
newRecord = []
for ele in basicInfo:
newRecord.append(ele)#for transaction with derivativeSecurities
newRecord.append('D')
#append 6 N/A in front for fields meant for table 1 transactions
for i in range(0,6):
newRecord.append('N/A')
try:
#append transaction title
newRecord.append('"'+transaction.find('securityTitle').find('value').text+'"')
#append transaction coding
newRecord.append(transaction.find('transactionCoding').find('transactionCode').text)
transactionAmount = transaction.find('transactionAmounts')
#append amount of shares
newRecord.append(transactionAmount.find('transactionShares').find('value').text)
#append price per share
newRecord.append(transactionAmount.find('transactionPricePerShare').find('value').text)
#append Acquired or Disposed of
newRecord.append(transactionAmount.find('transactionAcquiredDisposedCode').find('value').text)
#append ownership (Direct or Indirect)
newRecord.append(transaction.find('ownershipNature').find('directOrIndirectOwnership').find('value').text)
except AttributeError:
continue;
else:
#append this transaction to transactionList
transactionList.append(newRecord)
return transactionList
#=================================================================================================================================
table = []
heading = [
'Reporting Name',
'Accession Number',
'Issuer Name' ,
'Relationship of Reporting Person to Issuer',
'Transaction with Derivative (D) or Non-Derivative (N) Securities',
'Non-Derivative Securities: Title',
'Non-Derivative Securities: Transaction Code',
'Non-Derivative Securities: Amount (shares)',
'Non-Derivative Securities: Price (per share)',
'Non-Derivative Securities: Acquired (A) or Disposed Of (D)',
'Non-Derivative Securities: Ownership Form: Direct (D) or Indirect (I)',
'Derivative Securities: Title',
'Derivative Securities: Transaction Code',
'Derivative Securities: Amount (shares)',
'Derivative Securities: Price (per share)',
'Derivative Securities: Acquired (A) or Disposed Of (D)',
'Derivative Securities: Ownership Form: Direct (D) or Indirect (I)',
'Year', 'Month', 'Day', 'Time']
#transactionCode:
#A: Grant, award, or other acquisition of securities from the company (such as an option)
#K: Equity swaps and similar hedging transactions
#P: Purchase of securities on an exchange or from another person
#S: Sale of securities on an exchange or to another person
#D: Sale or transfer of securities back to the company
#F: Payment of exercise price or tax liability using portion of securities received from the company
#M: Exercise or conversion of derivative security received from the company (such as an option)
#G: Gift of securities by or to the insider
#V: A transaction voluntarily reported on Form 4
#J: Other (accompanied by a footnote describing the transaction)
table.append(heading)
#Pages to run (100 entries per page)
pageToRun = 10;
# Try to run in parallel
try:
from joblib import Parallel, delayed
except ImportError:
#Dependency not fulfilled, run sequencial instead
#Sequential implementation
print("Warning: Please download dependency joblib for (much) faster performance\n pip install joblib")
index = 0
while index < pageToRun:
url = 'https://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&CIK=&type=&company=&dateb=&owner=include&start='+str(index*100)+'&count=100&output=atom'
response = urllib.request.urlopen(url)
index += 1
tree = ET.parse(response)
root = tree.getroot()
for entry in root.findall('{http://www.w3.org/2005/Atom}entry'):
#each Entry is a record
record = []
#title contains multiple fields that needs to be parsed
#format of title: form_type - entity_name (cik) (action)
title = entry.find('{http://www.w3.org/2005/Atom}title').text
parsedTitle = title.split(' - ', 1)
#Only retrieve form 4 data (insider trading)
if parsedTitle[0] != '4':
continue
#parsing out cik and action
temp = (re.findall(r'\((.*?)\)', parsedTitle[1]))
action = temp[-1]
cik = temp[-2]
#Only retrieve records labelled (Reporting)
#There is 1 reporting file and 1 issuer file per transaction (two identical forms)
#Retrieving both will result in processing two identical forms
if action != 'Reporting':
continue
reportingName = parsedTitle[1][:-(len(cik)+len(action)+6)]
#quote around name with "," to ensure correct rendering when writing to csv
reportingName = '"'+reportingName+'"'
#Append Reporting Name
record.append(reportingName)
#get the accNum
accNum = entry.find('{http://www.w3.org/2005/Atom}id').text
accNum = accNum.split('accession-number=', 1)[1].replace('-','')
record.append(accNum)
#entryUrl: url to html page with next XML file's location
entryUrl = entry.find('{http://www.w3.org/2005/Atom}link').get('href')
#getNewXML returns root of next XML file
r = getNewXML(entryUrl)
#append issuerName
issuerName = '"'+r.find('issuer').find('issuerName').text+'"'
record.append(issuerName)
#Reporting person's relationship to issuer
relation = r.find('reportingOwner').find('reportingOwnerRelationship')
temp = ''
if not (relation.find('isDirector') == None or relation.find('isDirector').text.lower() == 'false' or relation.find('isDirector').text =='0'):
temp += 'D '
if not (relation.find('isOfficer') == None or relation.find('isOfficer').text.lower() == 'false' or relation.find('isOfficer').text == '0'):
temp += 'O '
if not (relation.find('isTenPercentOwner') == None or relation.find('isTenPercentOwner').text.lower() == 'false' or relation.find('isTenPercentOwner').text =='0'):
temp += 'T '
#append reportingOwnerRelationship
#D for director, O for officer, T for Ten Percent Owner
record.append(temp)
#record now has the basic informatiion of this entry.
#Each record represents 1 transaction
#A sigle transaction can either be with Non-Derivative Sec or Devrivative Sec.
#An entry can have multiple transactions.
recordList = []
#print(cik)
if r.find('nonDerivativeTable') != None:
for i in handleTable1(record, r.find('nonDerivativeTable')):
recordList.append(i)
#Since nonDerivativeTable can hold multiple transactions, it returns a list of records
if r.find('derivativeTable') != None:
for i in handleTable2(record, r.find('derivativeTable')):
recordList.append(i)
#Since derivativeTable can hold multiple transactions, it returns a list of records
for record in recordList:
datetime = entry.find('{http://www.w3.org/2005/Atom}updated').text
parse = datetime.split('T', 1)
date = parse[0].split('-', 2)
Time = parse[1].split('-', 1)[0]
for ele in date:
record.append(ele)
record.append(Time)
table.append(record)
#print('page done', index)
#=================================================================================================================================
# Parallel
else:
import multiprocessing
def processPage(i):
t = []
url = 'https://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&CIK=&type=&company=&dateb=&owner=include&start='+str(i*100)+'&count=100&output=atom'
response = urllib.request.urlopen(url)
i += 1
tree = ET.parse(response)
root = tree.getroot()
for entry in root.findall('{http://www.w3.org/2005/Atom}entry'):
#each Entry is a record
record = []
#title contains multiple fields that needs to be parsed
#format of title: form_type - entity_name (cik) (action)
title = entry.find('{http://www.w3.org/2005/Atom}title').text
parsedTitle = title.split(' - ', 1)
#Only retrieve form 4 data (insider trading)
if parsedTitle[0] != '4':
continue
#parsing out cik and action
temp = (re.findall(r'\((.*?)\)', parsedTitle[1]))
action = temp[-1]
cik = temp[-2]
#Only retrieve records labelled (Reporting)
#There is 1 reporting file and 1 issuer file per transaction (two identical forms)
#Retrieving both will result in processing two identical forms
if action != 'Reporting':
continue
reportingName = parsedTitle[1][:-(len(cik)+len(action)+6)]
#quote around name with "," to ensure correct rendering when writing to csv
reportingName = '"'+reportingName+'"'
#Append Reporting Name
record.append(reportingName)
#get the accNum
accNum = entry.find('{http://www.w3.org/2005/Atom}id').text
accNum = accNum.split('accession-number=', 1)[1].replace('-','')
record.append(accNum)
#entryUrl: url to html page with next XML file's location
entryUrl = entry.find('{http://www.w3.org/2005/Atom}link').get('href')
#getNewXML returns root of next XML file
r = getNewXML(entryUrl)
#append issuerName
issuerName = '"'+r.find('issuer').find('issuerName').text+'"'
record.append(issuerName)
#Reporting person's relationship to issuer
relation = r.find('reportingOwner').find('reportingOwnerRelationship')
temp = ''
if not (relation.find('isDirector') == None or relation.find('isDirector').text.lower() == 'false' or relation.find('isDirector').text =='0'):
temp += 'D '
if not (relation.find('isOfficer') == None or relation.find('isOfficer').text.lower() == 'false' or relation.find('isOfficer').text == '0'):
temp += 'O '
if not (relation.find('isTenPercentOwner') == None or relation.find('isTenPercentOwner').text.lower() == 'false' or relation.find('isTenPercentOwner').text =='0'):
temp += 'T '
#append reportingOwnerRelationship
#D for director, O for officer, T for Ten Percent Owner
record.append(temp)
#record now has the basic informatiion of this entry.
#Each record represents 1 transaction
#A sigle transaction can either be with Non-Derivative Sec or Devrivative Sec.
#An entry can have multiple transactions.
recordList = []
#print(cik)
if r.find('nonDerivativeTable') != None:
for i in handleTable1(record, r.find('nonDerivativeTable')):
recordList.append(i)
#Since nonDerivativeTable can hold multiple transactions, it returns a list of records
if r.find('derivativeTable') != None:
for i in handleTable2(record, r.find('derivativeTable')):
recordList.append(i)
#Since derivativeTable can hold multiple transactions, it returns a list of records
for record in recordList:
datetime = entry.find('{http://www.w3.org/2005/Atom}updated').text
parse = datetime.split('T', 1)
date = parse[0].split('-', 2)
time = parse[1].split('-', 1)[0]
for ele in date:
record.append(ele)
record.append(time)
t.append(record)
return t
pRange = range(pageToRun)
coresCount = multiprocessing.cpu_count()
#print("Running with "+str(coresCount)+" cores")
res = []
res.append(Parallel(n_jobs=coresCount)(delayed(processPage)(i) for i in pRange))
for tableList in res:
for tables in tableList:
for records in tables:
table.append(records)
#=================================================================================================================================
f = open('lateest_filing_at_sec.csv', 'w')
for record in table:
row = ",".join(record)
row = row + "\n"
f.write(row)
f.close()
#t1 = time.time();
#print(t1-t0)