-
Notifications
You must be signed in to change notification settings - Fork 56
/
reqs.rs
174 lines (152 loc) · 4.13 KB
/
reqs.rs
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
use uuid::Uuid;
#[derive(Serialize, Deserialize, Debug)]
pub struct Order {
side: OrderSide,
client_oid: Option<Uuid>,
product_id: String,
#[serde(flatten)]
_type: OrderType,
#[serde(flatten)]
stop: Option<OrderStop>,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub enum OrderSide {
Buy,
Sell,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
#[serde(rename_all = "camelCase")]
pub enum OrderType {
Limit {
price: f64,
size: f64,
post_only: bool,
#[serde(flatten)]
time_in_force: Option<OrderTimeInForce>,
},
Market {
#[serde(flatten)]
_type: MarketType,
},
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
#[serde(rename_all = "camelCase")]
pub enum MarketType {
Size { size: f64 },
Funds { funds: f64 },
}
impl Order {
pub fn market(
product_id: &str,
side: OrderSide,
size: f64,
) -> Self {
Order {
product_id: product_id.to_owned(),
client_oid: None,
side,
_type: OrderType::Market {
_type: MarketType::Size { size },
},
stop: None,
}
}
pub fn buy_market(product_id: &str, size: f64) -> Self {
Self::market(&product_id.to_owned(), OrderSide::Buy, size)
}
pub fn sell_market(product_id: &str, size: f64) -> Self {
Self::market(&product_id.to_owned(), OrderSide::Sell, size)
}
pub fn limit(
product_id: &str,
side: OrderSide,
size: f64,
price: f64,
post_only: bool
) -> Self {
Order {
product_id: product_id.to_owned(),
client_oid: None,
side,
_type: OrderType::Limit {
price,
size,
post_only,
time_in_force: None,
},
stop: None,
}
}
pub fn buy_limit(product_id: &str, size: f64, price: f64, post_only: bool) -> Self {
Self::limit(&product_id.to_owned(), OrderSide::Buy, size, price, post_only)
}
pub fn sell_limit(product_id: &str, size: f64, price: f64, post_only: bool) -> Self {
Self::limit(&product_id.to_owned(), OrderSide::Sell, size, price, post_only)
}
pub fn client_oid(self, client_oid: Uuid) -> Self {
let client_oid = Some(client_oid);
Order{client_oid, .. self }
}
pub fn time_in_force(self, time_in_force: OrderTimeInForce) -> Self {
match self._type {
OrderType::Limit {price, size, post_only, ..} => {
let _type = OrderType::Limit {price, size, post_only, time_in_force: Some(time_in_force)};
Order{_type, .. self}
},
_ => panic!("time_in_force is for limit orders only")
}
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "time_in_force")]
pub enum OrderTimeInForce {
GTC,
GTT {
cancel_after: OrderTimeInForceCancelAfter,
},
IOC,
FOK,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub enum OrderTimeInForceCancelAfter {
Min,
Hour,
Day,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OrderStop {
stop_price: f64,
_type: OrderStopType,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub enum OrderStopType {
Loss,
Entry,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_order_builder() {
let o = Order::buy_limit("BTC-USD", 10.0, 100.0, true);
assert!(o.client_oid.is_none());
match &o._type {
OrderType::Limit {time_in_force: None, ..} => assert!(true),
_ => assert!(false)
}
let o = Order::buy_limit("BTC-USD", 10.0, 100.0, true)
.client_oid(Uuid::nil())
.time_in_force(OrderTimeInForce::GTC);
assert!(o.client_oid.is_some());
match &o._type {
OrderType::Limit {time_in_force: Some(OrderTimeInForce::GTC), ..} => assert!(true),
_ => assert!(false)
}
}
}