-
Notifications
You must be signed in to change notification settings - Fork 8
/
Rkt.rkt
46 lines (38 loc) · 1.91 KB
/
Rkt.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
#lang typed/racket
(require racket/performance-hint)
(define: *NUM_RECORDS* : Integer (* 50 1000 444))
(struct: rkt-mem-trade ([trade-id : Integer] [client-id : Integer] [venue-id : Integer]
[instrument-code : Integer] [price : Integer] [quantity : Integer]
[side : Char])
#:mutable)
(define (make-trade i) (rkt-mem-trade 1 1 1 1 1 1 #\a))
(define: trades : (Vectorof rkt-mem-trade) (build-vector *NUM_RECORDS* make-trade))
(define (init-trades)
(for ([i (in-range 0 *NUM_RECORDS*)])
(let ([trade-ref (vector-ref trades i)])
(begin (set-rkt-mem-trade-trade-id! trade-ref i)
(set-rkt-mem-trade-client-id! trade-ref 1)
(set-rkt-mem-trade-venue-id! trade-ref 123)
(set-rkt-mem-trade-instrument-code! trade-ref 321)
(set-rkt-mem-trade-price! trade-ref i)
(set-rkt-mem-trade-quantity! trade-ref i)
(if (odd? i)
(set-rkt-mem-trade-side! trade-ref #\S)
(set-rkt-mem-trade-side! trade-ref #\B))))))
(define-inline (perform-run run-num)
(let (
[start-t (current-inexact-milliseconds)]
[buy-cost 0]
[sell-cost 0])
(begin
(init-trades)
(for ([i (in-range 0 *NUM_RECORDS*)])
(let ([trade-ref (vector-ref trades i)])
(if (equal? (rkt-mem-trade-side trade-ref) #\B)
(set! buy-cost (+ buy-cost (* (rkt-mem-trade-price trade-ref) (rkt-mem-trade-quantity trade-ref))))
(set! sell-cost (+ sell-cost (* (rkt-mem-trade-price trade-ref) (rkt-mem-trade-quantity trade-ref)))))))
(printf "Run ~v had duration ~v ms~%" run-num (inexact->exact (floor (- (current-inexact-milliseconds) start-t))) )
(printf "buycost = ~v sellCost = ~v ~%" buy-cost sell-cost))))
(define (run)
(time(for ([i (in-range 0 5)]) (perform-run i))))
(run)