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
use error::Error;
use serde::de::Deserialize;
use serde_json;
use std::collections::HashMap;
use chrono::*;
use std::fmt;
use serde;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Level {
    pub ok: bool,
    #[serde(rename="instanceId")]
    pub instance_id: i64,
    pub account: String,
    pub instructions: HashMap<String, String>,
    pub tickers: Vec<String>,
    pub venues: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct HeartBeatResponse {
    pub ok: bool,
    pub error: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct VenueHeartBeatResponse {
    pub ok: bool,
    pub venue: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct StockListResponse {
    pub ok: bool,
    pub symbols: Vec<StockSymbol>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct StockSymbol {
    pub name: String,
    pub symbol: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OrderbookResponse {
    pub ok: bool,
    pub venue: String,
    pub symbol: String,
    pub bids: Option<Vec<BidAsk>>,
    pub asks: Option<Vec<BidAsk>>,
    pub ts: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BidAsk {
    pub price: u64,
    pub qty: u64,
    #[serde(rename="isBuy")]
    pub is_buy: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct QuoteResponse {
    pub ok: bool,
    pub symbol: String,
    pub venue: String,
    pub bid: Option<u64>,
    pub ask: Option<u64>,
    #[serde(rename="bidSize")]
    pub bid_size: u64,
    #[serde(rename="askSize")]
    pub ask_size: u64,
    #[serde(rename="bidDepth")]
    pub bid_depth: u64,
    #[serde(rename="askDepth")]
    pub ask_depth: u64,
    pub last: Option<u64>,
    #[serde(rename="lastSize")]
    pub last_size: Option<u64>,
    #[serde(rename="lastTrade")]
    pub last_trade: Option<DateTime<UTC>>,
    #[serde(rename="quoteTime")]
    pub quote_time: Option<DateTime<UTC>>,
}

#[derive(Debug, Clone)]
pub enum OrderDirection {
    Buy,
    Sell,
}

impl fmt::Display for OrderDirection {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match self {
            &OrderDirection::Buy => "buy",
            &OrderDirection::Sell => "sell",
        };
        write!(f, "{}", s)
    }
}

impl serde::Serialize for OrderDirection {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        serializer.serialize_str(&self.to_string())
    }
}


impl serde::Deserialize for OrderDirection {
    fn deserialize<D>(deserializer: &mut D) -> Result<OrderDirection, D::Error>
        where D: serde::de::Deserializer
    {
        struct OrderDirectionVisitor;

        impl serde::de::Visitor for OrderDirectionVisitor {
            type Value = OrderDirection;

            fn visit_str<E>(&mut self, value: &str) -> Result<OrderDirection, E>
                where E: serde::de::Error
            {
                match value {
                    "buy" => Ok(OrderDirection::Buy),
                    "sell" => Ok(OrderDirection::Sell),
                    _ => Err(serde::de::Error::custom("expected Buy or Sell")),
                }
            }
        }

        deserializer.deserialize(OrderDirectionVisitor)
    }
}


#[derive(Debug, Clone)]
pub enum OrderType {
    Limit,
    Market,
    FillOrKill,
    ImmediateOrCancel,
}

impl fmt::Display for OrderType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match self {
            &OrderType::Market => "market",
            &OrderType::Limit => "limit",
            &OrderType::FillOrKill => "fill-or-kill",
            &OrderType::ImmediateOrCancel => "immediate-or-cancel",

        };
        write!(f, "{}", s)
    }
}

impl serde::Serialize for OrderType {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        serializer.serialize_str(&self.to_string())
    }
}


impl serde::Deserialize for OrderType {
    fn deserialize<D>(deserializer: &mut D) -> Result<OrderType, D::Error>
        where D: serde::de::Deserializer
    {
        struct OrderTypeVisitor;

        impl serde::de::Visitor for OrderTypeVisitor {
            type Value = OrderType;

            fn visit_str<E>(&mut self, value: &str) -> Result<OrderType, E>
                where E: serde::de::Error
            {
                match value {
                    "limit" => Ok(OrderType::Limit),
                    "market" => Ok(OrderType::Market),
                    "fill-or-kill" => Ok(OrderType::FillOrKill),
                    "immediate-or-cancel" => Ok(OrderType::ImmediateOrCancel),
                    _ => Err(serde::de::Error::custom("expected known order type")),
                }
            }
        }

        deserializer.deserialize(OrderTypeVisitor)
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Order {
    pub account: String,
    pub venue: String,
    pub stock: String,
    pub price: u64,
    pub qty: u64,
    pub direction: OrderDirection,
    #[serde(rename="orderType")]
    pub order_type: OrderType,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OrderResponse {
    pub ok: bool,
    pub symbol: String,
    pub venue: String,
    pub direction: Option<OrderDirection>,
    #[serde(rename="originalQty")]
    pub original_qty: u64,
    pub qty: u64,
    pub price: u64,
    #[serde(rename="orderType")]
    pub order_type: OrderType,
    pub id: u64,
    pub account: String,
    pub ts: DateTime<UTC>,
    pub fills: Vec<Fill>,
    #[serde(rename="totalFilled")]
    pub total_filled: u64,
    pub open: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Fill {
    pub price: u64,
    pub qty: u64,
    pub ts: DateTime<UTC>,
}

pub fn parse_response<T: Deserialize>(buf: &str) -> Result<T, Error> {
    let l: T = try!(serde_json::from_str(&buf));
    Ok(l)
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::*;
    use serde_json;
    use std::collections::HashMap;

    #[test]
    fn test_encode_order() {
        let o = Order {
            account: "tstacc".to_string(),
            venue: "testven".to_string(),
            stock: "BOOK".to_string(),
            price: 1000,
            qty: 5000,
            direction: OrderDirection::Buy,
            order_type: OrderType::ImmediateOrCancel,
        };
        let res = serde_json::to_string(&o).unwrap();
        assert!(res.len() > 0);
    }

    #[test]
    fn test_encode_level_field_names() {
        let l = Level {
            ok: true,
            instance_id: 998,
            account: "tacc".to_string(),
            instructions: HashMap::new(),
            tickers: vec![],
            venues: vec![],
        };
        let res_string = serde_json::to_string(&l).unwrap();
        assert!(res_string.contains("instanceId"));
    }

    #[test]
    fn test_bid_ask_encode_field_name() {
        let ba = BidAsk {
            price: 1009,
            qty: 100,
            is_buy: true,
        };
        let res_string = serde_json::to_string(&ba).unwrap();
        assert!(res_string.contains("isBuy"));
    }

    #[test]
    fn test_quote_response_fieled_name() {
        let qr = QuoteResponse {
            ok: true,
            symbol: "BOOK".to_string(),
            venue: "MYEX".to_string(),
            bid: Some(101),
            ask: Some(105),
            bid_size: 50,
            ask_size: 1,
            bid_depth: 9,
            ask_depth: 9,
            last: Some(102),
            last_size: Some(3),
            last_trade: Some("2015-12-04T09:02:16.680986205Z".parse().unwrap()),
            quote_time: Some("2015-12-04T09:02:16.680986205Z".parse().unwrap()),
        };
        let res_string = serde_json::to_string(&qr).unwrap();
        assert!(res_string.contains("bidSize"));
        assert!(res_string.contains("askSize"));
        assert!(res_string.contains("bidDepth"));
        assert!(res_string.contains("askDepth"));
        assert!(res_string.contains("lastSize"));
        assert!(res_string.contains("lastTrade"));
        assert!(res_string.contains("quoteTime"));
    }


    #[test]
    fn test_order_response_field_name() {
        let or = OrderResponse {
            ok: true,
            direction: None,
            symbol: "BOOK".to_string(),
            venue: "MYEX".to_string(),
            original_qty: 10,
            qty: 9,
            price: 1045,
            order_type: OrderType::Limit,
            id: 99803,
            account: "MYACC".to_string(),
            ts: "2015-12-04T09:02:16.680986205Z".parse().unwrap(),
            fills: vec![],
            total_filled: 1,
            open: true,
        };
        let res_string = serde_json::to_string(&or).unwrap();
        println!("{:?}", res_string);
        assert!(res_string.contains("originalQty"));
        assert!(res_string.contains("orderType"));
        assert!(res_string.contains("totalFilled"));
    }


    #[test]
    fn test_decode_date_time() {
        let dt_one: DateTime<UTC> = "2015-12-04T09:02:16.680986205Z".parse().unwrap();
        println!("dt = {:?}", dt_one);
        let dt_two: DateTime<UTC> = "2015-07-05T22:16:18+00:00".parse().unwrap();
        println!("dt = {:?}", dt_two);
    }

    #[test]
    fn test_decode_order_response() {
        let o_json = "{\"account\": \"testacc\", \"price\": 26382757, \"id\": 2138, \"open\": \
                      false, \"venue\": \"TESTEX\", \"orderType\": \"limit\", \"qty\": 0, \
                      \"direction\": \"buy\", \"fills\": [{\"qty\": 5000, \"ts\": \
                      \"2016-06-02T16:20:53.024563Z\", \"price\": 26382747}], \"totalFilled\": \
                      5000, \"originalQty\": 5000, \"symbol\": \"FOOBAR\", \"ts\": \
                      \"2016-06-02T16:20:53.024542Z\", \"ok\": true}";

        let o: OrderResponse = parse_response(&o_json).unwrap();
        assert!(o.ok);
    }
}