-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdump-dolt-dividends.rkt
70 lines (57 loc) · 2.27 KB
/
dump-dolt-dividends.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
#lang racket/base
(require db
gregor
racket/cmdline
racket/string
racket/system)
(define base-folder (make-parameter "/var/tmp/dolt/stocks"))
(define start-date (make-parameter (~t (-months (today) 1) "yyyy-MM-dd")))
(define end-date (make-parameter (~t (today) "yyyy-MM-dd")))
(define db-user (make-parameter "user"))
(define db-name (make-parameter "local"))
(define db-pass (make-parameter ""))
(command-line
#:program "racket dump-dolt-dividends.rkt"
#:once-each
[("-b" "--base-folder") folder
"Base dolt folder. Defaults to /var/tmp/dolt/stocks"
(base-folder folder)]
[("-e" "--end-date") end
"Final date for history retrieval. Defaults to today"
(end-date end)]
[("-n" "--db-name") name
"Database name. Defaults to 'local'"
(db-name name)]
[("-p" "--db-pass") password
"Database password"
(db-pass password)]
[("-s" "--start-date") start
"Earliest date for history retrieval. Defaults to today"
(start-date start)]
[("-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 dividend-file (string-append (base-folder) "/dividend-" (end-date) ".csv"))
(call-with-output-file* dividend-file
(λ (out)
(displayln "act_symbol,ex_date,amount" out)
(for-each (λ (row)
(displayln (string-join (vector->list row) ",") out))
(query-rows dbc "
select
act_symbol::text,
ex_date::text,
trunc(amount, 5)::text
from
yahoo.dividend
where
ex_date >= $1::text::date and
ex_date <= $2::text::date
"
(start-date)
(end-date))))
#:exists 'replace)
(system (string-append "cd " (base-folder) "; /usr/local/bin/dolt table import -u --continue dividend dividend-" (end-date) ".csv"))
(system (string-append "cd " (base-folder) "; /usr/local/bin/dolt add dividend; "
"/usr/local/bin/dolt commit -m 'dividend " (end-date) " update'; /usr/local/bin/dolt push --silent"))