-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_to_form13f.go
278 lines (228 loc) · 5.96 KB
/
xml_to_form13f.go
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
package insidervizforms
import (
"encoding/csv"
"encoding/xml"
"io"
"net/http"
"os"
"strings"
"github.com/Matterhorn-Studios/insidervizforms/iv_models"
"github.com/PuerkitoBio/goquery"
)
func GetThirteenFFromUrl(url string, accNum string, userAgent string) (iv_models.DB_Form13F_Base, error) {
var thirteenF iv_models.DB_Form13F_Base
// create the http request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return thirteenF, err
}
req.Header.Set("User-Agent", userAgent)
req.Host = "www.sec.gov"
// create the http client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode != 200 {
return thirteenF, err
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return thirteenF, err
}
// find the links
curIdx := 0
links := make([]string, 2)
doc.Find("a").Each(func(i int, s *goquery.Selection) {
if strings.Contains(s.Text(), ".xml") {
links[curIdx] = "https://www.sec.gov" + s.AttrOr("href", "")
curIdx++
}
})
// try the first
if len(links) < 2 {
return thirteenF, err
}
var infoTable InfoTable
var primaryDoc Form13_PrimaryDoc
firstLinkPrimaryDoc, err := parsePrimaryDoc(links[0], userAgent)
if err != nil {
// first link is info table
firstLinkInfoTable, err := parseInfoTable(links[0], userAgent)
if err != nil {
return thirteenF, err
}
secondLinkPrimaryDoc, err := parsePrimaryDoc(links[1], userAgent)
if err != nil {
return thirteenF, err
}
infoTable = firstLinkInfoTable
primaryDoc = secondLinkPrimaryDoc
} else {
// first link is primary doc
primaryDoc = firstLinkPrimaryDoc
secondLinkInfoTable, err := parseInfoTable(links[1], userAgent)
if err != nil {
return thirteenF, err
}
infoTable = secondLinkInfoTable
}
// populate 13f
thirteenF.AccessionNumber = accNum
thirteenF.PeriodOfReport = primaryDoc.Header.FilerInfo.PeriodOfReport
thirteenF.ReporterName = primaryDoc.FormData.CoverPage.FilingManager.Name
thirteenF.Cik = primaryDoc.Header.FilerInfo.Filer.Credentials.Cik
thirteenF.Url = url
// go over entries
total := 0.0
for _, entry := range infoTable.Entries {
var nHolding iv_models.DB_Form13F_Holding
cik, err := convertCusipToCik(entry.Cusip)
if err == nil {
nHolding.Name = entry.NameOfIssuer
nHolding.Cik = cik
nHolding.Shares = float32(entry.SharesOrAmt.SshPrnamt)
nHolding.NetTotal = float32(entry.Value) * 1000
total += entry.Value * 1000
thirteenF.Holdings = append(thirteenF.Holdings, nHolding)
}
}
thirteenF.TotalHeld = float32(total)
return thirteenF, nil
}
// CUSIP
type tableEntry struct {
Cik string
CusipSix string
CusipEight string
}
func convertCusipToCik(cusip string) (string, error) {
// read the csv file
file, err := os.Open("data/mapping.csv")
if err != nil {
return "", err
}
r := csv.NewReader(file)
// remove last character from cusip
cusipCheck := cusip[:len(cusip)-1]
for {
record, err := r.Read()
if err != nil {
return "", err
}
var curTable tableEntry
curTable.Cik = strings.TrimSuffix(record[0], ".0")
curTable.CusipSix = record[1]
curTable.CusipEight = record[2]
for len(curTable.Cik) < 10 {
curTable.Cik = "0" + curTable.Cik
}
if curTable.CusipEight == cusipCheck {
return curTable.Cik, nil
}
}
}
// INFO TABLE
func parseInfoTable(uri string, userAgent string) (InfoTable, error) {
var doc InfoTable
// create the http request
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return doc, err
}
req.Header.Set("User-Agent", userAgent)
req.Host = "www.sec.gov"
// create the http client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode != 200 {
return doc, err
}
// get the data from res body
data, err := io.ReadAll(resp.Body)
if err != nil {
return doc, err
}
// parse the data
err = xml.Unmarshal(data, &doc)
if err != nil {
return doc, err
}
return doc, nil
}
type InfoTable struct {
XMLName xml.Name `xml:"informationTable"`
Entries []InfoTableEntry `xml:"infoTable"`
}
type InfoTableEntry struct {
XMLName xml.Name `xml:"infoTable"`
NameOfIssuer string `xml:"nameOfIssuer"`
Cusip string `xml:"cusip"`
Value float64 `xml:"value"`
SharesOrAmt SharesOrAmt `xml:"shrsOrPrnAmt"`
}
type SharesOrAmt struct {
XMLName xml.Name `xml:"shrsOrPrnAmt"`
SshPrnamt float64 `xml:"sshPrnamt"`
}
// PRIMARY DOC
func parsePrimaryDoc(uri string, userAgent string) (Form13_PrimaryDoc, error) {
var doc Form13_PrimaryDoc
// create the http request
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return doc, err
}
req.Header.Set("User-Agent", userAgent)
req.Host = "www.sec.gov"
// create the http client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode != 200 {
return doc, err
}
// get the data from res body
data, err := io.ReadAll(resp.Body)
if err != nil {
return doc, err
}
// parse the data
err = xml.Unmarshal(data, &doc)
if err != nil {
return doc, err
}
return doc, nil
}
type Form13_PrimaryDoc struct {
XMLName xml.Name `xml:"edgarSubmission"`
Header HeaderData `xml:"headerData"`
FormData FormData `xml:"formData"`
}
type FormData struct {
XMLName xml.Name `xml:"formData"`
CoverPage CoverPage `xml:"coverPage"`
}
type CoverPage struct {
XMLName xml.Name `xml:"coverPage"`
FilingManager FilingManager `xml:"filingManager"`
}
type FilingManager struct {
XMLName xml.Name `xml:"filingManager"`
Name string `xml:"name"`
}
type HeaderData struct {
XMLName xml.Name `xml:"headerData"`
FilerInfo FilerInfo `xml:"filerInfo"`
}
type FilerInfo struct {
XMLName xml.Name `xml:"filerInfo"`
PeriodOfReport string `xml:"periodOfReport"`
Filer Filer `xml:"filer"`
}
type Filer struct {
XMLName xml.Name `xml:"filer"`
Credentials Credentials `xml:"credentials"`
}
type Credentials struct {
XMLName xml.Name `xml:"credentials"`
Cik string `xml:"cik"`
}