-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathincome-statement-transform-load.rkt
283 lines (270 loc) · 17 KB
/
income-statement-transform-load.rkt
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
#lang racket/base
(require db
gregor
gregor/period
html-parsing
racket/cmdline
racket/list
racket/sequence
racket/string
sxml
threading)
(define (income-statement-figure xexp #:period period #:date date #:entry entry)
(let*-values ([(section-id period-offset) (case period
['annual (values "annual_income_statement" 1)]
['quarterly (values "quarterly_income_statement" 0)])]
[(col) (case date
['most-recent 2]
['second-most-recent 3]
['third-most-recent 4]
['fourth-most-recent 5]
['fifth-most-recent 6])]
[(table-id row thead-tbody th-td) (case entry
['date (values 1 1 `thead `th)]
['sales (values 1 1 `tbody `td)]
['cost-of-goods (values 1 2 `tbody `td)]
['gross-profit (values 1 3 `tbody `td)]
['selling-administrative-depreciation-amortization-expenses (values 1 4 `tbody `td)]
['income-after-depreciation-and-amortization (values 1 5 `tbody `td)]
['non-operating-income (values 1 6 `tbody `td)]
['interest-expense (values 1 7 `tbody `td)]
['pretax-income (values 1 8 `tbody `td)]
['income-taxes (values 1 9 `tbody `td)]
['minority-interest (values 1 10 `tbody `td)]
['investment-gains (values 1 11 `tbody `td)]
['other-income (values 1 12 `tbody `td)]
['income-from-continuing-operations (values 1 13 `tbody `td)]
['extras-and-discontinued-operations (values 1 14 `tbody `td)]
['net-income (values 1 15 `tbody `td)]
['income-before-depreciation-and-amortization (values 2 1 `tbody `td)]
['depreciation-and-amortization (values 2 2 `tbody `td)]
['average-shares (values (+ 2 period-offset) 1 `tbody `td)]
['diluted-eps-before-non-recurring-items (values (+ 2 period-offset) 2 `tbody `td)]
['diluted-net-eps (values (+ 2 period-offset) 3 `tbody `td)])])
(~>
; (define in-file (open-input-file "/var/tmp/zacks/income-statement/2018-05-08/AA.income-statement.html"))
; (define in-xexp (html->xexp in-file))
; (webscraperhelper '(td (@ (class "alpha")) "Sales") in-xexp)
((sxpath `(// (div (@ (equal? (id ,section-id))))
(table ,table-id) ,thead-tbody (tr ,row) (,th-td ,col))) xexp)
(flatten _)
(last _)
(string-trim _)
(string-replace _ "," ""))))
(define base-folder (make-parameter "/var/tmp/zacks/income-statement"))
(define folder-date (make-parameter (today)))
(define db-user (make-parameter "user"))
(define db-name (make-parameter "local"))
(define db-pass (make-parameter ""))
(command-line
#:program "racket income-statement-transform-load.rkt"
#:once-each
[("-b" "--base-folder") folder
"Zacks income statement base folder. Defaults to /var/tmp/zacks/income-statement"
(base-folder folder)]
[("-d" "--folder-date") date
"Zacks income statement folder date. Defaults to today"
(folder-date (iso8601->date date))]
[("-n" "--db-name") name
"Database name. Defaults to 'local'"
(db-name name)]
[("-p" "--db-pass") password
"Database password"
(db-pass password)]
[("-u" "--db-user") user
"Database user name. Defaults to 'user'"
(db-user user)])
(define dbc (postgresql-connect #:user (db-user) #:database (db-name) #:password (db-pass)))
(define insert-counter 0)
(define insert-success-counter 0)
(define insert-failure-counter 0)
(parameterize ([current-directory (string-append (base-folder) "/" (~t (folder-date) "yyyy-MM-dd") "/")])
(for ([p (sequence-filter (λ (p) (string-contains? (path->string p) ".income-statement.html")) (in-directory (current-directory)))])
(let* ([file-name (path->string p)]
[ticker-symbol (string-replace (string-replace file-name (path->string (current-directory)) "") ".income-statement.html" "")])
(call-with-input-file file-name
(λ (in) (let ([xexp (html->xexp in)])
; we assume that if we have an updated value that is very close to our extract date that the whole set of data is bad.
(with-handlers ([exn:fail? (λ (e) (displayln (string-append "Failed to extract a date from " ticker-symbol)))])
(cond [(< 15 (period-ref (date-period-between (parse-date (income-statement-figure xexp #:period 'quarterly
#:date 'most-recent #:entry 'date)
"M/dd/yy")
(folder-date)
(list 'days))
'days))
(for-each (λ (period-date)
(with-handlers ([exn:fail? (λ (e) (displayln (string-append "Failed to process "
ticker-symbol
" for date "
(~t (folder-date) "yyyy-MM-dd")))
(displayln e)
(rollback-transaction dbc)
(set! insert-failure-counter (add1 insert-failure-counter)))])
(set! insert-counter (add1 insert-counter))
(start-transaction dbc)
(query-exec dbc "
-- We use the common table expression below in order to check if we're receiving
-- bad values from Zacks. When the new fiscal year occurs, Zacks apparently has a
-- bug where values from the prior year are copied into the new year column. We
-- guard against that by checking data against the previous year's data and not
-- inserting it if it is exactly the same by trying to insert a NULL act_symbol.
-- A better way might be with a stored procedure that can return a meaningful error.
with should_not_insert as (
select
bool_and(
sales = $4::text::decimal * 1e6 and
cost_of_goods = $5::text::decimal * 1e6 and
gross_profit = $6::text::decimal * 1e6 and
selling_administrative_depreciation_amortization_expenses = $7::text::decimal * 1e6 and
income_after_depreciation_and_amortization = $8::text::decimal * 1e6 and
non_operating_income = $9::text::decimal * 1e6 and
interest_expense = $10::text::decimal * 1e6 and
pretax_income = $11::text::decimal * 1e6 and
income_taxes = $12::text::decimal * 1e6 and
minority_interest = $13::text::decimal * 1e6 and
investment_gains = $14::text::decimal * 1e6 and
other_income = $15::text::decimal * 1e6 and
income_from_continuing_operations = $16::text::decimal * 1e6 and
extras_and_discontinued_operations = $17::text::decimal * 1e6 and
net_income = $18::text::decimal * 1e6 and
case $3
when 'annual' then income_before_depreciation_and_amortization = $19::text::decimal * 1e6
when 'quarterly' then income_before_depreciation_and_amortization is null
end and
case $3
when 'annual' then depreciation_and_amortization = $20::text::decimal * 1e6
when 'quarterly' then depreciation_and_amortization is null
end and
average_shares = $21::text::decimal * 1e6 and
diluted_eps_before_non_recurring_items = $22::text::decimal and
diluted_net_eps = $23::text::decimal
) as sni
from
zacks.income_statement
where
act_symbol = $1 and
case $3
when 'annual' then
period = 'Year'::zacks.statement_period and
date = $2::text::date - interval '1 year'
when 'quarterly' then
period = 'Quarter'::zacks.statement_period and
date = $2::text::date + interval '1 day' - interval '3 months' - interval '1 day'
end
)
insert into zacks.income_statement
(
act_symbol,
date,
period,
sales,
cost_of_goods,
gross_profit,
selling_administrative_depreciation_amortization_expenses,
income_after_depreciation_and_amortization,
non_operating_income,
interest_expense,
pretax_income,
income_taxes,
minority_interest,
investment_gains,
other_income,
income_from_continuing_operations,
extras_and_discontinued_operations,
net_income,
income_before_depreciation_and_amortization,
depreciation_and_amortization,
average_shares,
diluted_eps_before_non_recurring_items,
diluted_net_eps
) values (
case (select sni from should_not_insert)
when true then NULL
else $1
end,
$2::text::date,
case $3
when 'annual' then 'Year'::zacks.statement_period
when 'quarterly' then 'Quarter'::zacks.statement_period
end,
$4::text::decimal * 1e6,
$5::text::decimal * 1e6,
$6::text::decimal * 1e6,
$7::text::decimal * 1e6,
$8::text::decimal * 1e6,
$9::text::decimal * 1e6,
$10::text::decimal * 1e6,
$11::text::decimal * 1e6,
$12::text::decimal * 1e6,
$13::text::decimal * 1e6,
$14::text::decimal * 1e6,
$15::text::decimal * 1e6,
$16::text::decimal * 1e6,
$17::text::decimal * 1e6,
$18::text::decimal * 1e6,
case $3
when 'annual' then $19::text::decimal * 1e6
when 'quarterly' then null
end,
case $3
when 'annual' then $20::text::decimal * 1e6
when 'quarterly' then null
end,
$21::text::decimal * 1e6,
$22::text::decimal,
$23::text::decimal
) on conflict (act_symbol, date, period) do nothing;
"
ticker-symbol
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'date)
(symbol->string (first period-date))
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'sales)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'cost-of-goods)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'gross-profit)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'selling-administrative-depreciation-amortization-expenses)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'income-after-depreciation-and-amortization)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'non-operating-income)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'interest-expense)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'pretax-income)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'income-taxes)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'minority-interest)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'investment-gains)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'other-income)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'income-from-continuing-operations)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'extras-and-discontinued-operations)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'net-income)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'income-before-depreciation-and-amortization)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'depreciation-and-amortization)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'average-shares)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'diluted-eps-before-non-recurring-items)
(income-statement-figure xexp #:period (first period-date) #:date (second period-date)
#:entry 'diluted-net-eps))
(commit-transaction dbc)
(set! insert-success-counter (add1 insert-success-counter))))
(cartesian-product (list 'annual 'quarterly)
(list 'fifth-most-recent 'fourth-most-recent 'third-most-recent 'second-most-recent 'most-recent)))]
[else (displayln (string-append "Skipping " ticker-symbol " as the data is most likely using the wrong date."))]))))))))
(disconnect dbc)
(displayln (string-append "Attempted to insert " (number->string insert-counter) " rows. "
(number->string insert-success-counter) " were successful. "
(number->string insert-failure-counter) " failed."))