-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvolume-extract.rkt
84 lines (70 loc) · 2.61 KB
/
volume-extract.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
#lang racket/base
(require db
net/url
racket/cmdline
racket/file
racket/list
racket/port
racket/string
srfi/19 ; Time Data Types and Procedures
tasks
threading
"list-partition.rkt")
(define (download-volume symbols)
(make-directory* (string-append "/var/tmp/iex/volume/" (date->string (current-date) "~1")))
(call-with-output-file (string-append "/var/tmp/iex/volume/" (date->string (current-date) "~1") "/"
(first symbols) "-" (last symbols) ".json")
(λ (out)
(~> (string-append "https://cloud.iexapis.com/stable/stock/market/batch?symbols=" (string-join symbols ",")
"&types=volume-by-venue&token=" (api-token))
(string->url _)
(get-pure-port _)
(copy-port _ out)))
#:exists 'replace))
(define api-token (make-parameter ""))
(define db-user (make-parameter "user"))
(define db-name (make-parameter "local"))
(define db-pass (make-parameter ""))
(command-line
#:program "racket volume-extract.rkt"
#:once-each
[("-n" "--db-name") name
"Database name. Defaults to 'local'"
(db-name name)]
[("-p" "--db-pass") password
"Database password"
(db-pass password)]
[("-t" "--api-token") token
"IEX Cloud API Token"
(api-token token)]
[("-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 symbols (query-list dbc "
select
act_symbol
from
nasdaq.symbol
where
is_test_issue = false and
is_next_shares = false and
nasdaq_symbol !~ '[-\\$\\+\\*#!@%\\^=~]' and
case when nasdaq_symbol ~ '[A-Z]{4}[L-Z]'
then security_name !~ '(Note|Preferred|Right|Unit|Warrant)'
else true
end and
last_seen = (select max(last_seen) from nasdaq.symbol)
order by
act_symbol;
"))
(disconnect dbc)
(define grouped-symbols (list-partition symbols 100 100))
(define delay-interval 10)
(define delays (map (λ (x) (* delay-interval x)) (range 0 (length grouped-symbols))))
(with-task-server (for-each (λ (l) (schedule-delayed-task (λ () (download-volume (first l)))
(second l)))
(map list grouped-symbols delays))
; add a final task that will halt the task server
(schedule-delayed-task (λ () (schedule-stop-task)) (* delay-interval (length delays)))
(run-tasks))