-
Notifications
You must be signed in to change notification settings - Fork 24
/
save-missed-trades-from-file.rkt
125 lines (116 loc) · 3.25 KB
/
save-missed-trades-from-file.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
#lang racket/base
(require db
gregor
racket/cmdline
racket/list
racket/port
racket/string)
(define account (make-parameter ""))
(define filename (make-parameter ""))
(define db-name (make-parameter "local"))
(define db-pass (make-parameter ""))
(define db-user (make-parameter "user"))
(command-line
#:program "racket save-missed-trades-from-file.rkt"
#:once-each
[("-a" "--account") acct
"Account for these trades. The export uses account alias and not account number."
(account acct)]
[("-f" "--csv-file") csv-file
"CSV file name for saving."
(filename csv-file)]
[("-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)))
(call-with-input-file (filename)
(λ (in)
(define csv-string (port->string in))
(define lines (map (λ (str) (string-split str "," #:trim? #f)) (string-split csv-string "\n")))
(for-each
(λ (line)
(writeln line)
(if (or (equal? "Drill Down" (first line))
(equal? "BAG" (second line)))
(displayln "Skipping header and BAG lines")
(query-exec dbc "
insert into ibkr.execution (
order_id,
contract_id,
execution_id,
timestamp,
account,
executing_exchange,
side,
shares,
price,
perm_id,
client_id,
liquidation,
cumulative_quantity,
average_price,
order_reference,
model_code
) values (
0,
case
when $1 = '' then
(select
contract_id
from
ibkr.contract
where
symbol = $14 and
security_type = 'STK')
else
(select
contract_id
from
ibkr.contract
where
symbol = $1 and
expiry = $2::text::date and
strike = $3::text::numeric and
\"right\" = $4::text::ibkr.\"right\")
end,
$5,
($6 || ' ' || $7 || '+0')::timestamptz,
$8,
$9,
$10,
$11::text::numeric,
$12::text::numeric,
case
when $13 = '' then null
else $13::text::integer
end,
0,
0,
$11::text::numeric,
$12::text::numeric,
'',
''
) on conflict (execution_id) do nothing;
"
(list-ref line 40) ; Trading Class
(list-ref line 2) ; Last Trading Day
(list-ref line 3) ; Strike
(string-upcase (list-ref line 4)) ; Put/Call
(list-ref line 20) ; ID
(list-ref line 13) ; Date
(list-ref line 12) ; Time
(account)
(list-ref line 14) ; Exch
(list-ref line 6) ; Action
(list-ref line 8) ; Quantity
(list-ref line 11) ; Price
(list-ref line 30) ; Vol Link
(list-ref line 10) ; Fin Instrument
)))
lines)))