-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponse-messages.rkt
1116 lines (1101 loc) · 49.6 KB
/
response-messages.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#lang racket/base
(require gregor
racket/contract
racket/list
racket/match
racket/string
"base-structs.rkt")
(provide
(contract-out
[struct account-value-rsp
((key string?)
(value string?)
(currency string?)
(account-name string?))]
[struct commission-report-rsp
((execution-id string?)
(commission rational?)
(currency string?)
(realized-pnl (or/c rational? #f))
(yield (or/c rational? #f))
(yield-redemption-date (or/c integer? #f)))]
[struct contract-details-rsp
((request-id integer?)
(symbol string?)
(security-type (or/c 'stk 'opt 'fut 'cash 'bond 'cfd 'fop 'war 'iopt 'fwd 'bag
'ind 'bill 'fund 'fixed 'slb 'news 'cmdty 'bsk 'icu 'ics #f))
(expiry (or/c date? #f))
(strike (or/c rational? #f))
(right (or/c 'call 'put #f))
(exchange string?)
(currency string?)
(local-symbol string?)
(market-name string?)
(trading-class string?)
(contract-id integer?)
(minimum-tick-increment rational?)
(md-size-multiplier integer?)
(multiplier string?)
(order-types (listof string?))
(valid-exchanges (listof string?))
(price-magnifier integer?)
(underlying-contract-id integer?)
(long-name string?)
(primary-exchange string?)
(contract-month string?)
(industry string?)
(category string?)
(subcategory string?)
(time-zone-id string?)
(trading-hours (listof string?))
(liquid-hours (listof string?))
(ev-rule string?)
(ev-multiplier string?)
(security-ids hash?)
(agg-group integer?)
(underlying-symbol string?)
(underlying-security-type (or/c 'stk 'opt 'fut 'cash 'bond 'cfd 'fop 'war 'iopt 'fwd 'bag
'ind 'bill 'fund 'fixed 'slb 'news 'cmdty 'bsk 'icu 'ics #f))
(market-rule-ids (listof string?))
(real-expiry (or/c date? #f)))]
[struct err-rsp
((id integer?)
(error-code integer?)
(error-msg string?))]
[struct execution-rsp
((request-id integer?)
(order-id integer?)
(contract-id integer?)
(symbol string?)
(security-type (or/c 'stk 'opt 'fut 'cash 'bond 'cfd 'fop 'war 'iopt 'fwd 'bag
'ind 'bill 'fund 'fixed 'slb 'news 'cmdty 'bsk 'icu 'ics #f))
(expiry (or/c date? #f))
(strike (or/c rational? #f))
(right (or/c 'call 'put #f))
(multiplier (or/c rational? #f))
(exchange string?)
(currency string?)
(local-symbol string?)
(trading-class string?)
(execution-id string?)
(timestamp moment?)
(account string?)
(executing-exchange string?)
(side string?)
(shares rational?)
(price rational?)
(perm-id integer?)
(client-id integer?)
(liquidation integer?)
(cumulative-quantity integer?)
(average-price rational?)
(order-reference string?)
(ev-rule string?)
(ev-multiplier (or/c rational? #f))
(model-code string?))]
[struct historical-data-rsp
((request-id integer?)
(start-moment moment?)
(end-moment moment?)
(bars (listof bar?)))]
[struct market-data-rsp
((request-id integer?)
(type symbol?)
(value rational?))]
[struct next-valid-id-rsp
((order-id integer?))]
[struct open-order-rsp
((order-id integer?)
(contract-id integer?)
(symbol string?)
(security-type (or/c 'stk 'opt 'fut 'cash 'bond 'cfd 'fop 'war 'iopt 'fwd 'bag
'ind 'bill 'fund 'fixed 'slb 'news 'cmdty 'bsk 'icu 'ics #f))
(expiry (or/c date? #f))
(strike (or/c rational? #f))
(right (or/c 'call 'put #f))
(multiplier (or/c rational? #f))
(exchange string?)
(currency string?)
(local-symbol string?)
(trading-class string?)
(action (or/c 'buy 'sell 'sshort))
(total-quantity rational?)
(order-type string?)
(limit-price (or/c rational? #f))
(aux-price (or/c rational? #f))
(time-in-force (or/c 'day 'gtc 'opg 'ioc 'gtd 'gtt 'auc 'fok 'gtx 'dtc))
(oca-group string?)
(account string?)
(open-close (or/c 'open 'close #f))
(origin (or/c 'customer 'firm))
(order-ref string?)
(client-id integer?) ; new
(perm-id integer?) ; new
(outside-rth boolean?)
(hidden boolean?)
(discretionary-amount (or/c rational? #f))
(good-after-time (or/c moment? #f))
(advisor-group string?)
(advisor-method string?)
(advisor-percentage string?)
(advisor-profile string?)
(model-code string?)
(good-till-date (or/c date? #f))
(rule-80-a string?)
(percent-offset (or/c rational? #f))
(settling-firm string?)
(short-sale-slot (or/c 0 1 2))
(designated-location string?)
(exempt-code integer?)
(auction-strategy (or/c 'match 'improvement 'transparent #f))
(starting-price (or/c rational? #f))
(stock-ref-price (or/c rational? #f))
(delta (or/c rational? #f))
(stock-range-lower (or/c rational? #f))
(stock-range-upper (or/c rational? #f))
(display-size (or/c integer? #f))
(block-order boolean?)
(sweep-to-fill boolean?)
(all-or-none boolean?)
(minimum-quantity (or/c integer? #f))
(oca-type integer?)
(electronic-trade-only boolean?)
(firm-quote-only boolean?)
(nbbo-price-cap (or/c rational? #f))
(parent-id integer?)
(trigger-method integer?)
(volatility (or/c rational? #f))
(volatility-type (or/c integer? #f))
(delta-neutral-order-type string?)
(delta-neutral-aux-price (or/c rational? #f))
(delta-neutral-contract-id (or/c integer? #f))
(delta-neutral-settling-firm string?)
(delta-neutral-clearing-account string?)
(delta-neutral-clearing-intent string?)
(delta-neutral-open-close (or/c 'open 'close #f))
(delta-neutral-short-sale boolean?)
(delta-neutral-short-sale-slot (or/c integer? #f))
(delta-neutral-designated-location string?)
(continuous-update integer?)
(reference-price-type (or/c integer? #f))
(trailing-stop-price (or/c rational? #f))
(trailing-percent (or/c rational? #f))
(basis-points (or/c rational? #f))
(basis-points-type (or/c integer? #f))
(combo-legs-description string?)
(combo-legs (listof combo-leg?))
(order-combo-legs (listof rational?))
(smart-combo-routing-params hash?)
(scale-init-level-size (or/c integer? #f))
(scale-subs-level-size (or/c integer? #f))
(scale-price-increment (or/c rational? #f))
(scale-price-adjust-value (or/c rational? #f))
(scale-price-adjust-interval (or/c integer? #f))
(scale-profit-offset (or/c rational? #f))
(scale-auto-reset boolean?)
(scale-init-position (or/c integer? #f))
(scale-init-fill-quantity (or/c integer? #f))
(scale-random-percent boolean?)
(hedge-type string?)
(hedge-param string?)
(opt-out-smart-routing boolean?)
(clearing-account string?)
(clearing-intent (or/c 'ib 'away 'pta #f))
(not-held boolean?)
(delta-neutral-underlying-contract-id (or/c integer? #f))
(delta-neutral-underlying-delta (or/c rational? #f))
(delta-neutral-underlying-price (or/c rational? #f))
(algo-strategy string?)
(algo-strategy-params hash?)
(solicited boolean?)
(what-if boolean?)
(status string?)
(initial-margin-before (or/c rational? #f))
(maintenance-margin-before (or/c rational? #f))
(equity-with-loan-before (or/c rational? #f))
(initial-margin-change (or/c rational? #f))
(maintenance-margin-change (or/c rational? #f))
(equity-with-loan-change (or/c rational? #f))
(initial-margin-after (or/c rational? #f))
(maintenance-margin-after (or/c rational? #f))
(equity-with-loan-after (or/c rational? #f))
(commission (or/c rational? #f))
(minimum-commission (or/c rational? #f))
(maximum-commission (or/c rational? #f))
(commission-currency string?)
(warning-text string?)
(randomize-size boolean?)
(randomize-price boolean?)
(reference-contract-id (or/c integer? #f))
(is-pegged-change-amount-decrease boolean?)
(pegged-change-amount (or/c rational? #f))
(reference-change-amount (or/c rational? #f))
(reference-exchange-id string?)
(conditions (listof condition?))
(adjusted-order-type (or/c 'mkt 'lmt 'stp 'stp-limit 'rel 'trail 'box-top 'fix-pegged 'lit 'lmt-+-mkt
'loc 'mit 'mkt-prt 'moc 'mtl 'passv-rel 'peg-bench 'peg-mid 'peg-mkt 'peg-prim
'peg-stk 'rel-+-lmt 'rel-+-mkt 'snap-mid 'snap-mkt 'snap-prim 'stp-prt
'trail-limit 'trail-lit 'trail-lmt-+-mkt 'trail-mit 'trail-rel-+-mkt 'vol
'vwap 'quote 'ppv 'pdv 'pmv 'psv #f))
(trigger-price (or/c rational? #f))
(trail-stop-price (or/c rational? #f))
(limit-price-offset (or/c rational? #f))
(adjusted-stop-price (or/c rational? #f))
(adjusted-stop-limit-price (or/c rational? #f))
(adjusted-trailing-amount (or/c rational? #f))
(adjusted-trailing-unit integer?)
(soft-dollar-tier-name string?)
(soft-dollar-tier-value string?)
(soft-dollar-tier-display-name string?)
(cash-quantity rational?)
(dont-use-auto-price-for-hedge boolean?)
(is-oms-container boolean?)
(discretionary-up-to-limit-price boolean?)
(use-price-management-algo boolean?))]
[struct order-status-rsp
((order-id integer?)
(status (or/c 'pending-submit 'pending-cancel 'pre-submitted 'submitted
'api-cancelled 'cancelled 'filled 'inactive))
(filled rational?)
(remaining rational?)
(average-fill-price rational?)
(perm-id integer?)
(parent-id integer?)
(last-fill-price rational?)
(client-id integer?)
(why-held string?)
(market-cap-price rational?))]
[struct portfolio-value-rsp
((contract-id integer?)
(symbol string?)
(security-type (or/c 'stk 'opt 'fut 'cash 'bond 'cfd 'fop 'war 'iopt 'fwd 'bag
'ind 'bill 'fund 'fixed 'slb 'news 'cmdty 'bsk 'icu 'ics #f))
(expiry (or/c date? #f))
(strike (or/c rational? #f))
(right (or/c 'call 'put #f))
(multiplier (or/c rational? #f))
(exchange string?)
(currency string?)
(local-symbol string?)
(trading-class string?)
(position rational?)
(market-price rational?)
(market-value rational?)
(average-cost rational?)
(unrealized-pnl rational?)
(realized-pnl rational?)
(account-name string?))])
parse-msg)
(struct account-value-rsp
(key
value
currency
account-name)
#:transparent)
(struct commission-report-rsp
(execution-id
commission
currency
realized-pnl
yield
yield-redemption-date)
#:transparent)
(struct contract-details-rsp
(request-id
symbol
security-type
expiry
strike
right
exchange
currency
local-symbol
market-name
trading-class
contract-id
minimum-tick-increment
md-size-multiplier
multiplier
order-types
valid-exchanges
price-magnifier
underlying-contract-id
long-name
primary-exchange
contract-month
industry
category
subcategory
time-zone-id
trading-hours
liquid-hours
ev-rule
ev-multiplier
security-ids
agg-group
underlying-symbol
underlying-security-type
market-rule-ids
real-expiry)
#:transparent)
(struct err-rsp
(id
error-code
error-msg)
#:transparent)
(struct execution-rsp
(request-id
order-id
contract-id
symbol
security-type
expiry
strike
right
multiplier
exchange
currency
local-symbol
trading-class
execution-id
timestamp
account
executing-exchange
side
shares
price
perm-id
client-id
liquidation
cumulative-quantity
average-price
order-reference
ev-rule
ev-multiplier
model-code)
#:transparent)
(struct historical-data-rsp
(request-id
start-moment
end-moment
bars)
#:transparent)
(struct market-data-rsp
(request-id
type
value)
#:transparent)
(struct next-valid-id-rsp
(order-id)
#:transparent)
(struct open-order-rsp
(order-id
contract-id
symbol
security-type
expiry
strike
right
multiplier
exchange
currency
local-symbol
trading-class
action
total-quantity
order-type
limit-price
aux-price
time-in-force
oca-group
account
open-close
origin
order-ref
client-id ; new
perm-id ; new
outside-rth
hidden
discretionary-amount
good-after-time
advisor-group
advisor-method
advisor-percentage
advisor-profile
model-code
good-till-date
rule-80-a
percent-offset
settling-firm
short-sale-slot
designated-location
exempt-code
auction-strategy
starting-price
stock-ref-price
delta
stock-range-lower
stock-range-upper
display-size
block-order
sweep-to-fill
all-or-none
minimum-quantity
oca-type
electronic-trade-only
firm-quote-only
nbbo-price-cap
parent-id
trigger-method
volatility
volatility-type
delta-neutral-order-type
delta-neutral-aux-price
delta-neutral-contract-id
delta-neutral-settling-firm
delta-neutral-clearing-account
delta-neutral-clearing-intent
delta-neutral-open-close
delta-neutral-short-sale
delta-neutral-short-sale-slot
delta-neutral-designated-location
continuous-update
reference-price-type
trailing-stop-price
trailing-percent
basis-points
basis-points-type
combo-legs-description
combo-legs
order-combo-legs
smart-combo-routing-params
scale-init-level-size
scale-subs-level-size
scale-price-increment
scale-price-adjust-value
scale-price-adjust-interval
scale-profit-offset
scale-auto-reset
scale-init-position
scale-init-fill-quantity
scale-random-percent
hedge-type
hedge-param
opt-out-smart-routing
clearing-account
clearing-intent
not-held
delta-neutral-underlying-contract-id
delta-neutral-underlying-delta
delta-neutral-underlying-price
algo-strategy
algo-strategy-params
solicited
what-if
status
initial-margin-before
maintenance-margin-before
equity-with-loan-before
initial-margin-change
maintenance-margin-change
equity-with-loan-change
initial-margin-after
maintenance-margin-after
equity-with-loan-after
commission
minimum-commission
maximum-commission
commission-currency
warning-text
randomize-size
randomize-price
reference-contract-id
is-pegged-change-amount-decrease
pegged-change-amount
reference-change-amount
reference-exchange-id
conditions
adjusted-order-type
trigger-price
trail-stop-price
limit-price-offset
adjusted-stop-price
adjusted-stop-limit-price
adjusted-trailing-amount
adjusted-trailing-unit
soft-dollar-tier-name
soft-dollar-tier-value
soft-dollar-tier-display-name
cash-quantity
dont-use-auto-price-for-hedge
is-oms-container
discretionary-up-to-limit-price
use-price-management-algo)
#:transparent)
(struct order-status-rsp
(order-id
status
filled
remaining
average-fill-price
perm-id
parent-id
last-fill-price
client-id
why-held
market-cap-price)
#:transparent)
(struct portfolio-value-rsp
(contract-id
symbol
security-type
expiry
strike
right
multiplier
exchange
currency
local-symbol
trading-class
position
market-price
market-value
average-cost
unrealized-pnl
realized-pnl
account-name)
#:transparent)
; ensure string->number conversions try to exactly represent the provided decimal
(read-decimal-as-inexact #f)
; convert a byte string message received over the wire to the appropriate structure
(define/contract (parse-msg str)
(-> bytes? (or/c account-value-rsp?
commission-report-rsp?
contract-details-rsp?
err-rsp?
execution-rsp?
historical-data-rsp?
(listof string?)
market-data-rsp?
moment?
next-valid-id-rsp?
open-order-rsp?
order-status-rsp?
portfolio-value-rsp?))
(match (string-split (bytes->string/utf-8 str) "\0")
; tick price
[(list-rest "1" "6" request-id type value details)
(market-data-rsp
(string->number request-id)
(hash-ref tick-type-hash (string->number type))
(string->number value))]
; tick size
[(list "2" "6" request-id type value)
(market-data-rsp
(string->number request-id)
(hash-ref tick-type-hash (string->number type))
(string->number value))]
; order status
[(list "3" order-id status filled remaining average-fill-price perm-id parent-id
last-fill-price client-id why-held market-cap-price)
(order-status-rsp
(string->number order-id)
(match status
["PendingSubmit" 'pending-submit]
["PendingCancel" 'pending-cancel]
["PreSubmitted" 'pre-submitted]
["Submitted" 'submitted]
["ApiCancelled" 'api-cancelled]
["Cancelled" 'cancelled]
["Filled" 'filled]
["Inactive" 'inactive])
(string->number filled)
(string->number remaining)
(string->number average-fill-price)
(string->number perm-id)
(string->number parent-id)
(string->number last-fill-price)
(string->number client-id)
why-held
(string->number market-cap-price))]
; generic error message
; ignoring error responses with version < 2
[(list "4" "2" id error-code message) (err-rsp (string->number id) (string->number error-code) message)]
; open order
[(list-rest "5" details)
(let* ([combo-legs-index 78]
[combo-legs-size (string->number (list-ref details combo-legs-index))]
[order-combo-legs-index (+ 1 combo-legs-index (* 8 combo-legs-size))]
[order-combo-legs-size (string->number (list-ref details order-combo-legs-index))]
[smart-combo-routing-params-index (+ 1 order-combo-legs-index order-combo-legs-size)]
[smart-combo-routing-params-size (string->number (list-ref details smart-combo-routing-params-index))]
[scale-init-level-size-index (+ 1 smart-combo-routing-params-index (* 2 smart-combo-routing-params-size))]
[hedge-type-index (if (equal? "" (list-ref details (+ scale-init-level-size-index 2)))
(+ scale-init-level-size-index 3)
(+ scale-init-level-size-index 10))]
[opt-out-smart-routing-index (if (equal? "" (list-ref details hedge-type-index))
(+ hedge-type-index 1)
(+ hedge-type-index 2))]
[delta-neutral-contract-indicator (string->number (list-ref details (+ 4 opt-out-smart-routing-index)))]
[algo-strategy-index (if (equal? 0 delta-neutral-contract-indicator)
(+ opt-out-smart-routing-index 5)
(+ opt-out-smart-routing-index 9))]
[algo-strategy-params-size (if (equal? "" (list-ref details algo-strategy-index))
0
(string->number (list-ref details (+ 1 algo-strategy-index))))]
[solicited-index (+ 1 algo-strategy-index (if (< 0 algo-strategy-params-size) 1 0) (* 2 algo-strategy-params-size))]
[conditions-index (if (equal? "PEG BENCH" (list-ref details 14))
(+ 24 solicited-index)
(+ 19 solicited-index))]
[conditions-size (string->number (list-ref details conditions-index))]
[conditions-offsets (foldl (λ (i res)
(append res (list (match (list-ref details (last res))
["1" (+ 7 (last res))]
["3" (+ 4 (last res))]
["4" (+ 4 (last res))]
["5" (+ 5 (last res))]
["6" (+ 6 (last res))]
["7" (+ 6 (last res))]))))
(list (+ 1 conditions-index))
(range conditions-size))]
[adjusted-order-type-index (+ (last conditions-offsets) (if (< 0 conditions-size) 2 0))])
(open-order-rsp
(string->number (list-ref details 0)) ; order-id
(string->number (list-ref details 1)) ; contract-id
(list-ref details 2) ; symbol
(string->symbol (string-downcase (list-ref details 3))) ; security-type
(if (equal? "" (list-ref details 4))
#f (parse-date (list-ref details 4) "yyyyMMdd")) ; expiry
(string->number (list-ref details 5)) ; strike
(match (list-ref details 6)
["C" 'call]
["P" 'put]
[_ #f]) ; right
(string->number (list-ref details 7)) ; multiplier
(list-ref details 8) ; exchange
(list-ref details 9) ; currency
(list-ref details 10) ; local-symbol
(list-ref details 11) ; trading-class
(string->symbol (string-downcase (list-ref details 12))) ; action
(string->number (list-ref details 13)) ; total-quantity
(list-ref details 14) ; order-type
(string->number (list-ref details 15)) ; limit-price
(string->number (list-ref details 16)) ; aux-price
(string->symbol (string-downcase (list-ref details 17))) ; time-in-force
(list-ref details 18) ; oca-group
(list-ref details 19) ; account
(match (list-ref details 20)
["O" 'open]
["C" 'close]
[_ #f]) ; open-close
(match (list-ref details 21)
["0" 'customer]
["1" 'firm]) ; origin
(list-ref details 22) ; order-ref
(string->number (list-ref details 23)) ; client-id
(string->number (list-ref details 24)) ; perm-id
(if (equal? "1" (list-ref details 25)) #t #f) ; outside-rth
(if (equal? "1" (list-ref details 26)) #t #f) ; hidden
(string->number (list-ref details 27)) ; discretionary-amount
; take out the time-zone-name as srfi/19 does not handle this. assume local time zone
(if (equal? "" (list-ref details 28))
#f (parse-moment (second (regexp-match #px"([0-9]{8} [0-9]{2}:[0-9]{2}:[0-9]{2}) [A-Z]+"
(list-ref details 28))) "yyyyMMdd HH:mm:ss")) ; good-after-time
; deprecated shares allocation
(list-ref details 30) ; advisor-group
(list-ref details 31) ; advisor-method
(list-ref details 32) ; advisor-percentage
(list-ref details 33) ; advisor-profile
(list-ref details 34) ; model-code
(if (equal? "" (list-ref details 35))
#f (parse-date (list-ref details 35) "yyyyMMdd")) ; good-till-date
(list-ref details 36) ; rule-80-a
(string->number (list-ref details 37)) ; percent-offset
(list-ref details 38) ; settling-firm
(string->number (list-ref details 39)) ; short-sale-slot
(list-ref details 40) ; designated-location
(string->number (list-ref details 41)) ; exempt-code
(match (list-ref details 42)
["1" 'match]
["2" 'improvement]
["3" 'transparent]
[_ #f]) ; auction-strategy
(string->number (list-ref details 43)) ; starting-price
(string->number (list-ref details 44)) ; stock-ref-price
(string->number (list-ref details 45)) ; delta
(string->number (list-ref details 46)) ; stock-range-lower
(string->number (list-ref details 47)) ; stock-range-upper
(string->number (list-ref details 48)) ; display-size
(if (equal? "1" (list-ref details 49)) #t #f) ; block-order
(if (equal? "1" (list-ref details 50)) #t #f) ; sweep-to-fill
(if (equal? "1" (list-ref details 51)) #t #f) ; all-or-none
(string->number (list-ref details 52)) ; minimum-quantity
(string->number (list-ref details 53)) ; oca-type
(if (equal? "1" (list-ref details 54)) #t #f) ; electronic-trade-only
(if (equal? "1" (list-ref details 55)) #t #f) ; firm-quote-only
(string->number (list-ref details 56)) ; nbbo-price-cap
(string->number (list-ref details 57)) ; parent-id
(string->number (list-ref details 58)) ; trigger-method
(string->number (list-ref details 59)) ; volatility
(string->number (list-ref details 60)) ; volatility-type
(list-ref details 61) ; delta-neutral-order-type
(string->number (list-ref details 62)) ; delta-neutral-aux-price
(string->number (list-ref details 63)) ; delta-neutral-contract-id
(list-ref details 64) ; delta-neutral-settling-firm
(list-ref details 65) ; delta-neutral-clearing-account
(list-ref details 66) ; delta-neutral-clearing-intent
(match (list-ref details 67)
["O" 'open]
["C" 'close]
[_ #f]) ; delta-neutral-open-close
(if (equal? "1" (list-ref details 68)) #t #f) ; delta-neutral-short-sale
(string->number (list-ref details 69)) ; delta-neutral-short-sale-slot
(list-ref details 70) ; delta-neutral-designated-location
(string->number (list-ref details 71)) ; continuous-update
(string->number (list-ref details 72)) ; reference-price-type
(string->number (list-ref details 73)) ; trailing-stop-price
(string->number (list-ref details 74)) ; trailing-percent
(string->number (list-ref details 75)) ; basis-points
(string->number (list-ref details 76)) ; basis-points-type
(list-ref details 77) ; combo-legs-description
(map (λ (i) (combo-leg
(string->number (list-ref details (+ 1 (* i 8) combo-legs-index))) ; contract-id
(string->number (list-ref details (+ 2 (* i 8) combo-legs-index))) ; ratio
(string->symbol (string-downcase (list-ref details (+ 3 (* i 8) combo-legs-index)))) ; action
(list-ref details (+ 4 (* i 8) combo-legs-index)) ; exchange
(match (list-ref details (+ 5 (* i 8) combo-legs-index))
["0" 'same]
["1" 'open]
["2" 'close]
[_ #f]) ; open-close
(string->number (list-ref details (+ 6 (* i 8) combo-legs-index))) ; short-sale-slot
(list-ref details (+ 7 (* i 8) combo-legs-index)) ; designated-location
(string->number (list-ref details (+ 8 (* i 8) combo-legs-index))) ; exempt-code
))
(range combo-legs-size)) ; combo-legs
(map (λ (i) (string->number (list-ref details (+ 1 (* i 1) order-combo-legs-index))))
(range order-combo-legs-size)) ; order-combo-legs
(apply hash (take (drop details (+ 1 smart-combo-routing-params-index))
(* 2 smart-combo-routing-params-size))) ; smart-combo-routing-params
(string->number (list-ref details scale-init-level-size-index)) ; scale-init-level-size
(string->number (list-ref details (+ 1 scale-init-level-size-index))) ; scale-subs-level-size
(string->number (list-ref details (+ 2 scale-init-level-size-index))) ; scale-price-increment
(if (equal? "" (list-ref details (+ 2 scale-init-level-size-index)))
#f (string->number (list-ref details (+ 3 scale-init-level-size-index)))) ; scale-price-adjust-value
(if (equal? "" (list-ref details (+ 2 scale-init-level-size-index)))
#f (string->number (list-ref details (+ 4 scale-init-level-size-index)))) ; scale-price-adjust-interval
(if (equal? "" (list-ref details (+ 2 scale-init-level-size-index)))
#f (string->number (list-ref details (+ 5 scale-init-level-size-index)))) ; scale-profit-offset
(if (equal? "" (list-ref details (+ 2 scale-init-level-size-index)))
#f (if (equal? "1" (list-ref details (+ 6 scale-init-level-size-index))) #t #f)) ; scale-auto-reset
(if (equal? "" (list-ref details (+ 2 scale-init-level-size-index)))
#f (string->number (list-ref details (+ 7 scale-init-level-size-index)))) ; scale-init-position
(if (equal? "" (list-ref details (+ 2 scale-init-level-size-index)))
#f (string->number (list-ref details (+ 8 scale-init-level-size-index)))) ; scale-init-fill-quantity
(if (equal? "" (list-ref details (+ 2 scale-init-level-size-index)))
#f (if (equal? "1" (list-ref details (+ 9 scale-init-level-size-index))) #t #f)) ; scale-random-percent
(list-ref details hedge-type-index) ; hedge-type
(if (equal? "" (list-ref details hedge-type-index))
"" (list-ref details (+ 1 hedge-type-index))) ; hedge-param
(if (equal? "1" (list-ref details opt-out-smart-routing-index)) #t #f) ; opt-out-smart-routing
(list-ref details (+ 1 opt-out-smart-routing-index)) ; clearing-account
(string->symbol (string-downcase (list-ref details (+ 2 opt-out-smart-routing-index)))) ; clearing-intent
(if (equal? "1" (list-ref details (+ 3 opt-out-smart-routing-index))) #t #f) ; not-held
(if (equal? 0 delta-neutral-contract-indicator)
#f (string->number (list-ref details (+ 5 opt-out-smart-routing-index)))) ; delta-neutral-underlying-contract-id
(if (equal? 0 delta-neutral-contract-indicator)
#f (string->number (list-ref details (+ 6 opt-out-smart-routing-index)))) ; delta-neutral-underlying-delta
(if (equal? 0 delta-neutral-contract-indicator)
#f (string->number (list-ref details (+ 7 opt-out-smart-routing-index)))) ; delta-neutral-underlying-price
(list-ref details algo-strategy-index) ; algo-strategy
(apply hash (take (drop details (+ 2 algo-strategy-index))
(* 2 algo-strategy-params-size))) ; algo-strategy-params
(if (equal? "1" (list-ref details solicited-index)) #t #f) ; solicited
(if (equal? "1" (list-ref details (+ 1 solicited-index))) #t #f) ; what-if
(list-ref details (+ 2 solicited-index)) ; status
; there are some bits in the Java code that attempt to use Double.MAX as a null value.
; usually, these are later removed, but in a few cases like the ones below, they are
; not removed and need to be explicitly handled as they are sent over the wire.
(if (equal? "1.7976931348623157E308" (list-ref details (+ 3 solicited-index)))
#f (string->number (list-ref details (+ 3 solicited-index)))) ; initial-margin-before
(if (equal? "1.7976931348623157E308" (list-ref details (+ 4 solicited-index)))
#f (string->number (list-ref details (+ 4 solicited-index)))) ; maintenance-margin-before
(if (equal? "1.7976931348623157E308" (list-ref details (+ 5 solicited-index)))
#f (string->number (list-ref details (+ 5 solicited-index)))) ; equity-with-loan-before
(if (equal? "1.7976931348623157E308" (list-ref details (+ 6 solicited-index)))
#f (string->number (list-ref details (+ 6 solicited-index)))) ; initial-margin-change
(if (equal? "1.7976931348623157E308" (list-ref details (+ 7 solicited-index)))
#f (string->number (list-ref details (+ 7 solicited-index)))) ; maintenance-margin-change
(if (equal? "1.7976931348623157E308" (list-ref details (+ 8 solicited-index)))
#f (string->number (list-ref details (+ 8 solicited-index)))) ; equity-with-loan-change
(if (equal? "1.7976931348623157E308" (list-ref details (+ 9 solicited-index)))
#f (string->number (list-ref details (+ 9 solicited-index)))) ; initial-margin-after
(if (equal? "1.7976931348623157E308" (list-ref details (+ 10 solicited-index)))
#f (string->number (list-ref details (+ 10 solicited-index)))) ; maintenance-margin-after
(if (equal? "1.7976931348623157E308" (list-ref details (+ 11 solicited-index)))
#f (string->number (list-ref details (+ 11 solicited-index)))) ; equity-with-loan-after
(string->number (list-ref details (+ 12 solicited-index))) ; commission
(string->number (list-ref details (+ 13 solicited-index))) ; minimum-commission
(string->number (list-ref details (+ 14 solicited-index))) ; maximum-commission
(list-ref details (+ 15 solicited-index)) ; commission-currency
(list-ref details (+ 16 solicited-index)) ; warning-text
(if (equal? "1" (list-ref details (+ 17 solicited-index))) #t #f) ; randomize-size
(if (equal? "1" (list-ref details (+ 18 solicited-index))) #t #f) ; randomize-price
(if (equal? "PEG BENCH" (list-ref details 14))
(string->number (list-ref details (+ 19 solicited-index))) #f) ; reference-contract-id
(if (equal? "PEG BENCH" (list-ref details 14))
(if (equal? "1" (list-ref details (+ 20 solicited-index))) #t #f) #f) ; is-pegged-change-amount-decrease
(if (equal? "PEG BENCH" (list-ref details 14))
(string->number (list-ref details (+ 21 solicited-index))) #f) ; pegged-change-amount
(if (equal? "PEG BENCH" (list-ref details 14))
(string->number (list-ref details (+ 22 solicited-index))) #f) ; reference-change-amount
(if (equal? "PEG BENCH" (list-ref details 14))
(list-ref details (+ 23 solicited-index)) "") ; reference-exchange-id
(map (λ (i)
(match (list-ref details i)
["1" (condition
'price ; type
; we have a default case below because we have received "n" before, which shouldn't be possible,
; but the Java library treats everything not "a" as the 'or case
(match (list-ref details (+ 1 i)) ["a" 'and] ["o" 'or] [_ 'or]) ; boolean-operator
(match (list-ref details (+ 2 i)) ["0" 'less-than] ["1" 'greater-than]) ; comparator
(string->number (list-ref details (+ 3 i))) ; price
(string->number (list-ref details (+ 4 i))) ; contract-id
(list-ref details (+ 5 i)) ; exchange
(match (list-ref details (+ 6 i))
["0" 'default]
["1" 'double-bid/ask]
["2" 'last]
["3" 'double-last]
["4" 'bid/ask]
["7" 'last-of-bid/ask]
["8" 'mid-point]) ; trigger-method
#f ; security-type
#f)] ; symbol
["3" (condition
'time ; type
(match (list-ref details (+ 1 i)) ["a" 'and] ["o" 'or]) ; boolean-operator
(match (list-ref details (+ 2 i)) ["0" 'less-than] ["1" 'greater-than]) ; comparator
(parse-moment (second (regexp-match #px"([0-9]{8} [0-9]{2}:[0-9]{2}:[0-9]{2}) [A-Z]+"
(list-ref details (+ 3 i)))) "yyyyMMdd HH:mm:ss") ; time
#f ; contract-id
#f ; exchange
#f ; trigger-method
#f ; security-type
#f)] ; symbol
["4" (condition
'margin ; type
(match (list-ref details (+ 1 i)) ["a" 'and] ["o" 'or]) ; boolean-operator
(match (list-ref details (+ 2 i)) ["0" 'less-than] ["1" 'greater-than]) ; comparator
(string->number (list-ref details (+ 3 i))) ; margin-percent
#f ; contract-id
#f ; exchange
#f ; trigger-method
#f ; security-type
#f)] ; symbol
["5" (condition
'execution
(match (list-ref details (+ 1 i)) ["a" 'and] ["o" 'or]) ; boolean-operator
#f ; comparator
#f ; value
#f ; contract-id
(list-ref details (+ 3 i)) ; exchange
#f ; trigger-method
(string->symbol (string-downcase (list-ref details (+ 2 i)))) ; security-type
(list-ref details (+ 4 i)))] ; symbol
["6" (condition
'volume ; type
(match (list-ref details (+ 1 i)) ["a" 'and] ["o" 'or]) ; boolean-operator
(match (list-ref details (+ 2 i)) ["0" 'less-than] ["1" 'greater-than]) ; comparator
(string->number (list-ref details (+ 3 i))) ; volume
(string->number (list-ref details (+ 4 i))) ; contract-id
(list-ref details (+ 5 i)) ; exchange
#f ; trigger-method
#f ; security-type
#f)] ; symbol
["7"(condition
'percent-change ; type
(match (list-ref details (+ 1 i)) ["a" 'and] ["o" 'or]) ; boolean-operator
(match (list-ref details (+ 2 i)) ["0" 'less-than] ["1" 'greater-than]) ; comparator
(string->number (list-ref details (+ 3 i))) ; percent-change
(string->number (list-ref details (+ 4 i))) ; contract-id
(list-ref details (+ 5 i)) ; exchange
#f ; trigger-method
#f ; security-type
#f)])) ; symbol
(drop-right conditions-offsets 1)) ; conditions
(if (equal? "None" (list-ref details adjusted-order-type-index))
#f (string->symbol (string-downcase (string-replace (list-ref details adjusted-order-type-index) "_" "-")))) ; adjusted-order-type
(if (equal? "1.7976931348623157E308" (list-ref details (+ 1 adjusted-order-type-index)))
#f (string->number (list-ref details (+ 1 adjusted-order-type-index)))) ; trigger-price
(if (equal? "1.7976931348623157E308" (list-ref details (+ 2 adjusted-order-type-index)))
#f (string->number (list-ref details (+ 2 adjusted-order-type-index)))) ; trail-stop-price
(if (equal? "1.7976931348623157E308" (list-ref details (+ 3 adjusted-order-type-index)))
#f (string->number (list-ref details (+ 3 adjusted-order-type-index)))) ; limit-price-offset
(if (equal? "1.7976931348623157E308" (list-ref details (+ 4 adjusted-order-type-index)))
#f (string->number (list-ref details (+ 4 adjusted-order-type-index)))) ; adjusted-stop-price
(if (equal? "1.7976931348623157E308" (list-ref details (+ 5 adjusted-order-type-index)))
#f (string->number (list-ref details (+ 5 adjusted-order-type-index)))) ; adjusted-stop-limit-price
(if (equal? "1.7976931348623157E308" (list-ref details (+ 6 adjusted-order-type-index)))
#f (string->number (list-ref details (+ 6 adjusted-order-type-index)))) ; adjusted-trailing-amount
(string->number (list-ref details (+ 7 adjusted-order-type-index))) ; adjustable-trailing-unit
(list-ref details (+ 8 adjusted-order-type-index)) ; soft-dollar-tier-name
(list-ref details (+ 9 adjusted-order-type-index)) ; soft-dollar-tier-val
(list-ref details (+ 10 adjusted-order-type-index)) ; soft-dollar-tier-display-name
(string->number (list-ref details (+ 11 adjusted-order-type-index))) ; cash-quantity
(if (equal? "1" (list-ref details (+ 12 adjusted-order-type-index))) #t #f) ; dont-use-auto-price-for-hedge
(if (equal? "1" (list-ref details (+ 13 adjusted-order-type-index))) #t #f) ; is-oms-container
(if (equal? "1" (list-ref details (+ 14 adjusted-order-type-index))) #t #f) ; discretionary-up-to-limit-price
(if (equal? "1" (list-ref details (+ 15 adjusted-order-type-index))) #t #f) ; use-price-management-algo
))]
; account value
[(list "6" version key value currency account-name) (account-value-rsp key value currency account-name)]
; portfolio value
[(list-rest "7" version details) (portfolio-value-rsp
(string->number (list-ref details 0)) ; contract-id
(list-ref details 1) ; symbol
(string->symbol (string-downcase (list-ref details 2))) ; security-type
(if (equal? "" (list-ref details 3))
#f (parse-date (list-ref details 3) "yyyyMMdd")) ; expiry
(string->number (list-ref details 4)) ; strike
(match (list-ref details 5)
["C" 'call]
["P" 'put]
[_ #f]) ; right
(string->number (list-ref details 6)) ; multiplier
(list-ref details 7) ; exchange
(list-ref details 8) ; currency
(list-ref details 9) ; local-symbol
(list-ref details 10) ; trading-class
(string->number (list-ref details 11)) ; position
(string->number (list-ref details 12)) ; market-price
(string->number (list-ref details 13)) ; market-value
(string->number (list-ref details 14)) ; average-cost
(string->number (list-ref details 15)) ; unrealized-pnl
(string->number (list-ref details 16)) ; realized-pnl
(list-ref details 17) ; account-name
)]
; next valid id
[(list "9" version order-id) (next-valid-id-rsp (string->number order-id))]
; contract details
[(list-rest "10" version details)
(let* ([security-ids-index 30]
[security-ids-size (string->number (list-ref details security-ids-index))]
[agg-group-index (+ 1 security-ids-index (* 2 security-ids-size))])
(contract-details-rsp