Source file child_order.ml
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
open Common
let json_of_order product_code order side size : Json.t =
match order with
| Limit price ->
`Assoc
[
("product_code", `String product_code);
("child_order_type", `String "LIMIT");
("price", `String (string_of_float price));
("side", `String (string_of_side side));
("size", `String (string_of_float size));
]
| Market ->
`Assoc
[
("product_code", `String product_code);
("child_order_type", `String "MARKET");
("side", `String (string_of_side side));
("size", `String (string_of_float size));
]
type placed_order = {
id : int;
product_code : string;
side : side;
child_order_type : order_type;
size : float;
child_order_state : string;
child_order_date : Datetime.t;
}
let order_of_json json =
let open Json.Util in
let id = member "id" json |> to_int in
let product_code = member "product_code" json |> to_string in
let side = member "side" json |> to_string |> Common.side_of_string in
let child_order_type_str = member "child_order_type" json |> to_string in
let child_order_type =
match child_order_type_str with
| "LIMIT" ->
let price = member "price" json |> to_float in
Limit price
| "MARKET" -> Market
| other -> failwith (!%"order_of_json: '%s'" other)
in
let child_order_state = member "child_order_state" json |> to_string in
let size = member "size" json |> to_float in
let child_order_date =
member "child_order_date" json |> to_string |> Datetime.from_string
in
{
id;
product_code;
side;
child_order_type;
size;
child_order_state;
child_order_date;
}
let orders_of_json json =
let open Json.Util in
to_list json |> List.map order_of_json