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
//! Types related to Contracts
use std::fmt::{Display, Error, Formatter};

use num_derive::FromPrimitive;
use serde::{Deserialize, Serialize};
// 0.2.6 (the trait)

use crate::core::common::TagValue;

//==================================================================================================
#[repr(i32)]
#[derive(Serialize, Deserialize, Clone, Copy, FromPrimitive, Debug)]
pub enum PositionType {
    SamePos = 0,
    //open/close leg value is same as combo
    OpenPos = 1,
    //open
    ClosePos = 2,
    //close
    UnknownPos = 3, //unknown
}

impl Display for PositionType {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        match *self {
            PositionType::SamePos => write!(f, "SamePos"),
            PositionType::OpenPos => write!(f, "OpenPos"),
            PositionType::ClosePos => write!(f, "ClosePos"),
            PositionType::UnknownPos => write!(f, "UnknownPos"),
        }
    }
}

impl Default for PositionType {
    fn default() -> Self {
        PositionType::SamePos
    }
}

//==================================================================================================
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct ComboLeg {
    pub con_id: i32,
    pub ratio: f64,
    pub action: String,
    // BUY /SELL / SSHORT
    pub exchange: String,
    pub open_close: PositionType,
    // for stock legs when doing short sale
    pub short_sale_slot: i32,
    pub designated_location: String,
    pub exempt_code: i32,
}

impl ComboLeg {
    pub fn new(
        con_id: i32,
        ratio: f64,
        action: String,
        exchange: String,
        open_close: PositionType,
        short_sale_slot: i32,
        designated_location: String,
        exempt_code: i32,
    ) -> Self {
        ComboLeg {
            con_id,
            ratio,
            action,
            exchange,
            open_close,
            short_sale_slot,
            designated_location,
            exempt_code,
        }
    }
}

impl Display for ComboLeg {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        write!(
            f,
            "con_id: {},
            ratio: {},
            action: {},
            exchange: {},
            open_close: {},
            short_sale_slot: {},
            designated_location: {},
            exempt_code: {}",
            self.con_id,
            self.ratio,
            self.action,
            self.exchange,
            self.open_close,
            self.short_sale_slot,
            self.designated_location,
            self.exempt_code
        )
    }
}

//==================================================================================================
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct DeltaNeutralContract {
    pub con_id: i32,
    pub delta: f64,
    pub price: f64,
}

impl DeltaNeutralContract {
    pub fn new(con_id: i32, delta: f64, price: f64) -> Self {
        DeltaNeutralContract {
            con_id,
            delta,
            price,
        }
    }
}

impl Display for DeltaNeutralContract {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        write!(
            f,
            "con_id: {}, delta: {}, price: {},",
            self.con_id, self.delta, self.price,
        )
    }
}

//==================================================================================================
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Contract {
    pub con_id: i32,
    pub symbol: String,
    pub sec_type: String,
    pub last_trade_date_or_contract_month: String,
    pub strike: f64,
    pub right: String,
    pub multiplier: String,
    pub exchange: String,
    pub primary_exchange: String,
    // pick an actual (ie non - aggregate) exchange that the contract trades on.DO NOT SET TO SMART.
    pub currency: String,
    pub local_symbol: String,
    pub trading_class: String,
    pub include_expired: bool,
    pub sec_id_type: String,
    // CUSIP; SEDOL; ISIN;RIC
    pub sec_id: String,

    //combos
    pub combo_legs_descrip: String,
    // received in open order 14 and up for all combos
    pub combo_legs: Vec<ComboLeg>,
    pub delta_neutral_contract: Option<DeltaNeutralContract>,
}

impl Contract {
    pub fn new(
        con_id: i32,
        symbol: String,
        sec_type: String,
        last_trade_date_or_contract_month: String,
        strike: f64,
        right: String,
        multiplier: String,
        exchange: String,
        primary_exchange: String,
        currency: String,
        local_symbol: String,
        trading_class: String,
        include_expired: bool,
        sec_id_type: String,
        sec_id: String,
        combo_legs_descrip: String,
        combo_legs: Vec<ComboLeg>,
        delta_neutral_contract: Option<DeltaNeutralContract>,
    ) -> Self {
        Contract {
            con_id,
            symbol,
            sec_type,
            last_trade_date_or_contract_month,
            strike,
            right,
            multiplier,
            exchange,
            primary_exchange,
            currency,
            local_symbol,
            trading_class,
            include_expired,
            sec_id_type,
            sec_id,
            combo_legs_descrip,
            combo_legs,
            delta_neutral_contract,
        }
    }
}

impl Display for Contract {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        write!(
            f,
            "con_id: {},
             symbol: {},
             sec_type: {},
             last_trade_date_or_contract_month: {},
             strike: {},
             right: {},
             multiplier: {},
             exchange: {},
             primary_exchange: {},
             currency: {},
             local_symbol: {},
             trading_class: {},
             include_expired: {},
             sec_id_type: {},
             sec_id: {},
             combo_legs_descrip: {},
             combo_legs: [{}],
             delta_neutral_contract: [{:?}],
            ",
            self.con_id,
            self.symbol,
            self.sec_type,
            self.last_trade_date_or_contract_month,
            self.strike,
            self.right,
            self.multiplier,
            self.exchange,
            self.primary_exchange,
            self.currency,
            self.local_symbol,
            self.trading_class,
            self.include_expired,
            self.sec_id_type,
            self.sec_id,
            self.combo_legs_descrip,
            self.combo_legs
                .iter()
                .map(|x| { format!("{}", x.to_string()) })
                .collect::<Vec<String>>()
                .join(","),
            self.delta_neutral_contract
        )
    }
}

//==================================================================================================
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct ContractDetails {
    pub contract: Contract,
    pub market_name: String,
    pub min_tick: f64,
    pub order_types: String,
    pub valid_exchanges: String,
    pub price_magnifier: i32,
    pub under_con_id: i32,
    pub long_name: String,
    pub contract_month: String,
    pub industry: String,
    pub category: String,
    pub subcategory: String,
    pub time_zone_id: String,
    pub trading_hours: String,
    pub liquid_hours: String,
    pub ev_rule: String,
    pub ev_multiplier: f64,
    pub md_size_multiplier: i32,
    pub agg_group: i32,
    pub under_symbol: String,
    pub under_sec_type: String,
    pub market_rule_ids: String,
    pub sec_id_list: Vec<TagValue>,
    pub real_expiration_date: String,
    pub last_trade_time: String,

    // BOND values
    pub cusip: String,
    pub ratings: String,
    pub desc_append: String,
    pub bond_type: String,
    pub coupon_type: String,
    pub callable: bool,
    pub putable: bool,
    pub coupon: f64,
    pub convertible: bool,
    pub maturity: String,
    pub issue_date: String,
    pub next_option_date: String,
    pub next_option_type: String,
    pub next_option_partial: bool,
    pub notes: String,
}

impl ContractDetails {
    pub fn new(
        contract: Contract,
        market_name: String,
        min_tick: f64,
        order_types: String,
        valid_exchanges: String,
        price_magnifier: i32,
        under_con_id: i32,
        long_name: String,
        contract_month: String,
        industry: String,
        category: String,
        subcategory: String,
        time_zone_id: String,
        trading_hours: String,
        liquid_hours: String,
        ev_rule: String,
        ev_multiplier: f64,
        md_size_multiplier: i32,
        agg_group: i32,
        under_symbol: String,
        under_sec_type: String,
        market_rule_ids: String,
        sec_id_list: Vec<TagValue>,
        real_expiration_date: String,
        last_trade_time: String,
        cusip: String,
        ratings: String,
        desc_append: String,
        bond_type: String,
        coupon_type: String,
        callable: bool,
        putable: bool,
        coupon: f64,
        convertible: bool,
        maturity: String,
        issue_date: String,
        next_option_date: String,
        next_option_type: String,
        next_option_partial: bool,
        notes: String,
    ) -> Self {
        ContractDetails {
            contract,
            market_name,
            min_tick,
            order_types,
            valid_exchanges,
            price_magnifier,
            under_con_id,
            long_name,
            contract_month,
            industry,
            category,
            subcategory,
            time_zone_id,
            trading_hours,
            liquid_hours,
            ev_rule,
            ev_multiplier,
            md_size_multiplier,
            agg_group,
            under_symbol,
            under_sec_type,
            market_rule_ids,
            sec_id_list,
            real_expiration_date,
            last_trade_time,
            cusip,
            ratings,
            desc_append,
            bond_type,
            coupon_type,
            callable,
            putable,
            coupon,
            convertible,
            maturity,
            issue_date,
            next_option_date,
            next_option_type,
            next_option_partial,
            notes,
        }
    }
}

impl Display for ContractDetails {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        write!(
            f,
            "contract: {},
            market_name: {},
            min_tick: {},
            order_types: {},
            valid_exchanges: {},
            price_magnifier: {},
            under_con_id: {},
            long_name: {},
            contract_month: {},
            industry: {},
            category: {},
            subcategory: {},
            time_zone_id: {},
            trading_hours: {},
            liquid_hours: {},
            ev_rule: {},
            ev_multiplier: {},
            md_size_multiplier: {},
            agg_group: {},
            under_symbol: {},
            under_sec_type: {},
            market_rule_ids: {},
            sec_id_list: {},
            real_expiration_date: {},
            last_trade_time: {},
            cusip: {},
            ratings: {},
            desc_append: {},
            bond_type: {},
            coupon_type: {},
            callable: {},
            putable: {},
            coupon: {},
            convertible: {},
            maturity: {},
            issue_date: {},
            next_option_date: {},
            next_option_type: {},
            next_option_partial: {},
            notes: {},",
            self.contract,
            self.market_name,
            self.min_tick,
            self.order_types,
            self.valid_exchanges,
            self.price_magnifier,
            self.under_con_id,
            self.long_name,
            self.contract_month,
            self.industry,
            self.category,
            self.subcategory,
            self.time_zone_id,
            self.trading_hours,
            self.liquid_hours,
            self.ev_rule,
            self.ev_multiplier,
            self.md_size_multiplier,
            self.agg_group,
            self.under_symbol,
            self.under_sec_type,
            self.market_rule_ids,
            self.sec_id_list
                .iter()
                .map(|x| { format!("{}", x.to_string()) })
                .collect::<Vec<String>>()
                .join(","),
            self.real_expiration_date,
            self.last_trade_time,
            self.cusip,
            self.ratings,
            self.desc_append,
            self.bond_type,
            self.coupon_type,
            self.callable,
            self.putable,
            self.coupon,
            self.convertible,
            self.maturity,
            self.issue_date,
            self.next_option_date,
            self.next_option_type,
            self.next_option_partial,
            self.notes
        )
    }
}

//==================================================================================================
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct ContractDescription {
    pub contract: Contract,
    pub derivative_sec_types: Vec<String>, // type: list of strings
}

impl ContractDescription {
    pub fn new(contract: Contract, derivative_sec_types: Vec<String>) -> Self {
        ContractDescription {
            contract,
            derivative_sec_types,
        }
    }
}

impl Display for ContractDescription {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        write!(
            f,
            "contract: {}, derivative_sec_types: ({})",
            self.contract,
            self.derivative_sec_types
                .iter()
                .map(|x| { x.to_owned() })
                .collect::<Vec<String>>()
                .join(",")
        )
    }
}