twsapi 0.1.0

Port of Interactive Broker's trading API written in Rust
Documentation
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! Examples of populating fields that define various types of contacts

use crate::core::contract::{ComboLeg, Contract, PositionType};

//==================================================================================================
pub fn eur_gbp_fx() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "EUR".to_string();
    contract.sec_type = "CASH".to_string();
    contract.currency = "GBP".to_string();
    contract.exchange = "IDEALPRO".to_string();

    contract
}

//==================================================================================================
pub fn index() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "DAX".to_string();
    contract.sec_type = "IND".to_string();
    contract.currency = "EUR".to_string();
    contract.exchange = "DTB".to_string();

    contract
}

//==================================================================================================
pub fn cfd() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "IBDE30".to_string();
    contract.sec_type = "cfd".to_string();
    contract.currency = "EUR".to_string();
    contract.exchange = "SMART".to_string();

    contract
}

//==================================================================================================
pub fn european_stock() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "BMW".to_string();
    contract.sec_type = "STK".to_string();
    contract.currency = "EUR".to_string();
    contract.exchange = "SMART".to_string();
    contract.primary_exchange = "IBIS".to_string();
    contract
}

//==================================================================================================
pub fn european_stock2() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "NOKIA".to_string();
    contract.sec_type = "STK".to_string();
    contract.currency = "EUR".to_string();
    contract.exchange = "SMART".to_string();
    contract.primary_exchange = "HEX".to_string();
    contract
}

//==================================================================================================
pub fn option_at_ise() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "COF".to_string();
    contract.sec_type = "OPT".to_string();
    contract.currency = "USD".to_string();
    contract.exchange = "ISE".to_string();
    contract.last_trade_date_or_contract_month = "20190315".to_string();
    contract.right = "P".to_string();
    contract.strike = 105.0;
    contract.multiplier = "100".to_string();
    contract
}

//==================================================================================================
pub fn bond_with_cusip() -> Contract {
    let mut contract = Contract::default();
    // enter CUSIP as symbol
    contract.symbol = "912828C57".to_string();
    contract.sec_type = "BOND".to_string();
    contract.exchange = "SMART".to_string();
    contract.currency = "USD".to_string();

    contract
}

//==================================================================================================
pub fn bond() -> Contract {
    let mut contract = Contract::default();
    contract.con_id = 15960357;
    contract.exchange = "SMART".to_string();

    contract
}

//==================================================================================================
pub fn mutual_fund() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "VINIX".to_string();
    contract.sec_type = "FUND".to_string();
    contract.exchange = "FUNDSERV".to_string();
    contract.currency = "USD".to_string();

    contract
}

//==================================================================================================
pub fn commodity() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "XAUUSD".to_string();
    contract.sec_type = "CMDTY".to_string();
    contract.exchange = "SMART".to_string();
    contract.currency = "USD".to_string();

    contract
}

//==================================================================================================
pub fn usstock() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "AMZN".to_string();
    contract.sec_type = "STK".to_string();
    contract.currency = "USD".to_string();
    //In the API side, NASDAQ is always defined as ISLAND in the exchange field
    contract.exchange = "ISLAND".to_string();
    //stkcontract]
    contract
}

//==================================================================================================
pub fn usstock_with_primary_exch() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "MSFT".to_string();
    contract.sec_type = "STK".to_string();
    contract.currency = "USD".to_string();
    contract.exchange = "SMART".to_string();
    //Specify the Primary Exchange attribute to avoid contract ambiguity
    //(there is an ambiguity because there is also a MSFT contract with primary exchange = "AEB")
    contract.primary_exchange = "ISLAND".to_string();
    //stkcontractwithprimary]
    contract
}

//==================================================================================================
pub fn us_stock_at_smart() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "MSFT".to_string();
    contract.sec_type = "STK".to_string();
    contract.currency = "USD".to_string();
    contract.exchange = "SMART".to_string();
    contract
}

//==================================================================================================
pub fn us_option_contract() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "GOOG".to_string();
    contract.sec_type = "OPT".to_string();
    contract.exchange = "SMART".to_string();
    contract.currency = "USD".to_string();
    contract.last_trade_date_or_contract_month = "20201218".to_string();
    contract.strike = 1180.0;
    contract.right = "C".to_string();
    contract.multiplier = "100".to_string();

    contract
}

//==================================================================================================
pub fn option_at_box() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "GOOG".to_string();
    contract.sec_type = "OPT".to_string();
    contract.exchange = "BOX".to_string();
    contract.currency = "USD".to_string();
    contract.last_trade_date_or_contract_month = "20201218".to_string();
    contract.strike = 1180.0;
    contract.right = "C".to_string();
    contract.multiplier = "100".to_string();

    contract
}

//==================================================================================================
/// Option contracts require far more information since there are many
/// contracts having the exact same attributes such as symbol, currency,
/// strike, etc. This can be overcome by adding more details such as the
//' trading class
pub fn option_with_trading_class() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "SANT".to_string();
    contract.sec_type = "OPT".to_string();
    contract.exchange = "MEFFRV".to_string();
    contract.currency = "EUR".to_string();
    contract.last_trade_date_or_contract_month = "20190621".to_string();
    contract.strike = 7.5;
    contract.right = "C".to_string();
    contract.multiplier = "100".to_string();
    contract.trading_class = "SANEU".to_string();

    contract
}

//==================================================================================================
/// Using the contract's own symbol (local_symbol) can greatly simplify a
/// contract description. Watch out for the spaces within the local symbol!
pub fn option_with_local_symbol() -> Contract {
    let mut contract = Contract::default();
    //Watch out for the spaces within the local symbol!
    contract.local_symbol = "C DBK  DEC 20  1600".to_string();
    contract.sec_type = "OPT".to_string();
    contract.exchange = "DTB".to_string();
    contract.currency = "EUR".to_string();

    contract
}

//==================================================================================================
/// Dutch Warrants (IOPTs) can be defined using the local symbol or conid
pub fn dutch_warrant() -> Contract {
    let mut contract = Contract::default();
    contract.local_symbol = "B881G".to_string();
    contract.sec_type = "IOPT".to_string();
    contract.exchange = "SBF".to_string();
    contract.currency = "EUR".to_string();

    contract
}

//==================================================================================================
/// Future contracts also require an expiration date but are less
/// complicated than options.
pub fn simple_future() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "ES".to_string();
    contract.sec_type = "FUT".to_string();
    contract.exchange = "GLOBEX".to_string();
    contract.currency = "USD".to_string();
    contract.last_trade_date_or_contract_month = "202009".to_string();

    contract
}

//==================================================================================================
/// Rather than giving expiration dates we can also provide the local symbol
/// attributes such as symbol, currency, strike, etc.
pub fn future_with_local_symbol() -> Contract {
    let mut contract = Contract::default();
    contract.sec_type = "FUT".to_string();
    contract.exchange = "GLOBEX".to_string();
    contract.currency = "USD".to_string();
    contract.local_symbol = "ESU0".to_string();

    contract
}

//==================================================================================================
pub fn future_with_multiplier() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "DAX".to_string();
    contract.sec_type = "FUT".to_string();
    contract.exchange = "DTB".to_string();
    contract.currency = "EUR".to_string();
    contract.last_trade_date_or_contract_month = "201903".to_string();
    contract.multiplier = "5".to_string();

    contract
}

//==================================================================================================
/// Note the space in the symbol!
pub fn wrong_contract() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = " IJR ".to_string();
    contract.con_id = 9579976;
    contract.sec_type = "STK".to_string();
    contract.exchange = "SMART".to_string();
    contract.currency = "USD".to_string();
    contract
}

//==================================================================================================
pub fn futures_on_options() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "ES".to_string();
    contract.sec_type = "FOP".to_string();
    contract.exchange = "GLOBEX".to_string();
    contract.currency = "USD".to_string();
    contract.last_trade_date_or_contract_month = "20190315".to_string();
    contract.strike = 2900.0;
    contract.right = "C".to_string();
    contract.multiplier = "50".to_string();

    contract
}

//==================================================================================================
/// It is also possible to deine contracts based on their ISIN (IBKR STK
/// sample).
pub fn by_isin() -> Contract {
    let mut contract = Contract::default();
    contract.sec_id_type = "ISIN".to_string();
    contract.sec_id = "US45841N1072".to_string();
    contract.exchange = "SMART".to_string();
    contract.currency = "USD".to_string();
    contract.sec_type = "STK".to_string();
    contract
}

//==================================================================================================
/// Or their con_id (EUR.uSD sample).
/// Note: passing a contract containing the con_id can cause problems if one of
/// the other provided attributes does not match 100% with what is in IB's
/// database. This is particularly important for contracts such as Bonds which
/// may change their description from one day to another.
/// If the con_id is provided, it is best not to give too much information as
/// in the example below.
pub fn by_con_id() -> Contract {
    let mut contract = Contract::default();
    contract.sec_type = "CASH".to_string();
    contract.con_id = 12087792;
    contract.exchange = "IDEALPRO".to_string();
    contract
}

//==================================================================================================
/// Ambiguous contracts are great to use with
/// Contract::req_contract_details. This way
/// you can query the whole option chain for an underlying. Bear in mind that
/// there are pacing mechanisms in place which will delay any further responses
/// from the TWS to prevent abuse.
pub fn option_for_query() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "FISV".to_string();
    contract.sec_type = "OPT".to_string();
    contract.exchange = "SMART".to_string();
    contract.currency = "USD".to_string();

    contract
}

//==================================================================================================
pub fn option_combo_contract() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "DBK".to_string();
    contract.sec_type = "BAG".to_string();
    contract.currency = "EUR".to_string();
    contract.exchange = "DTB".to_string();

    let mut leg1 = ComboLeg::default();
    leg1.con_id = 317960956; //DBK JUN 21 2019 C
    leg1.ratio = 1.0;
    leg1.action = "BUY".to_string();
    leg1.exchange = "DTB".to_string();

    let mut leg2 = ComboLeg::default();
    leg2.con_id = 334216780; //DBK MAR 15 2019 C
    leg2.ratio = 1.0;
    leg2.action = "SELL".to_string();
    leg2.exchange = "DTB".to_string();

    contract.combo_legs = vec![];
    contract.combo_legs.push(leg1);
    contract.combo_legs.push(leg2);
    //bagoptcontract]
    contract
}

//==================================================================================================
/// STK Combo contract
/// Leg 1: 43645865 - IBKR's STK
/// Leg 2: 9408 - McDonald's STK
pub fn stock_combo_contract() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "IBKR,MCD".to_string();
    contract.sec_type = "BAG".to_string();
    contract.currency = "USD".to_string();
    contract.exchange = "SMART".to_string();

    let mut leg1 = ComboLeg::default();
    leg1.con_id = 43645865; //IBKR STK
    leg1.ratio = 1.0;
    leg1.action = "BUY".to_string();
    leg1.exchange = "SMART".to_string();

    let mut leg2 = ComboLeg::default();
    leg2.con_id = 9408; //MCD STK
    leg2.ratio = 1.0;
    leg2.action = "SELL".to_string();
    leg2.exchange = "SMART".to_string();

    contract.combo_legs = vec![];
    contract.combo_legs.push(leg1);
    contract.combo_legs.push(leg2);

    contract
}

//==================================================================================================
/// CBOE volatility Index Future combo contract
pub fn future_combo_contract() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "VIX".to_string();
    contract.sec_type = "BAG".to_string();
    contract.currency = "USD".to_string();
    contract.exchange = "CFE".to_string();

    let mut leg1 = ComboLeg::default();
    leg1.con_id = 438391466; // VIX FUT 201903
    leg1.ratio = 1.0;
    leg1.action = "BUY".to_string();
    leg1.exchange = "CFE".to_string();
    leg1.exempt_code = -1;
    leg1.open_close = PositionType::SamePos;

    let mut leg2 = ComboLeg::default();
    leg2.con_id = 394987014; // VIX FUT 201904
    leg2.ratio = 1.0;
    leg2.action = "SELL".to_string();
    leg2.exchange = "CFE".to_string();
    leg2.exempt_code = -1;
    leg2.open_close = PositionType::SamePos;

    contract.combo_legs = vec![];
    contract.combo_legs.push(leg1);
    contract.combo_legs.push(leg2);

    contract
}

//==================================================================================================
pub fn smart_future_combo_contract() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "WTI".to_string(); // WTI,COIL spread. Symbol can be defined as first leg symbol ("WTI") or currency ("USD")
    contract.sec_type = "BAG".to_string();
    contract.currency = "USD".to_string();
    contract.exchange = "SMART".to_string();

    let mut leg1 = ComboLeg::default();
    leg1.con_id = 55928698; // WTI future June 2017
    leg1.ratio = 1.0;
    leg1.action = "BUY".to_string();
    leg1.exchange = "IPE".to_string();

    let mut leg2 = ComboLeg::default();
    leg2.con_id = 55850663; // COIL future June 2017
    leg2.ratio = 1.0;
    leg2.action = "SELL".to_string();
    leg2.exchange = "IPE".to_string();

    contract.combo_legs = vec![];
    contract.combo_legs.push(leg1);
    contract.combo_legs.push(leg2);

    contract
}

//==================================================================================================
pub fn inter_cmdty_futures_contract() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "CL.BZ".to_string(); //symbol is 'local symbol' of intercommodity spread.
    contract.sec_type = "BAG".to_string();
    contract.currency = "USD".to_string();
    contract.exchange = "NYMEX".to_string();

    let mut leg1 = ComboLeg::default();
    leg1.con_id = 47207310; //CL Dec'16 @NYMEX
    leg1.ratio = 1.0;
    leg1.action = "BUY".to_string();
    leg1.exchange = "NYMEX".to_string();

    let mut leg2 = ComboLeg::default();
    leg2.con_id = 47195961; //BZ Dec'16 @NYMEX
    leg2.ratio = 1.0;
    leg2.action = "SELL".to_string();
    leg2.exchange = "NYMEX".to_string();

    contract.combo_legs = vec![];
    contract.combo_legs.push(leg1);
    contract.combo_legs.push(leg2);

    contract
}

//==================================================================================================
pub fn news_feed_for_query() -> Contract {
    let mut contract = Contract::default();
    contract.sec_type = "NEWS".to_string();
    contract.exchange = "BRFG".to_string(); //Briefing Trader

    contract
}

//==================================================================================================
pub fn brfgbroadtape_news_feed() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "BRFG:BRFG_ALL".to_string();
    contract.sec_type = "NEWS".to_string();
    contract.exchange = "BRFG".to_string();

    contract
}

//==================================================================================================
pub fn djnlbroadtape_news_feed() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "DJNL:DJNL_ALL".to_string();
    contract.sec_type = "NEWS".to_string();
    contract.exchange = "DJNL".to_string();

    contract
}

//==================================================================================================
pub fn djtopbroadtape_news_feed() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "DJTOP:ASIAPAC".to_string();
    contract.sec_type = "NEWS".to_string();
    contract.exchange = "DJTOP".to_string();

    contract
}

//==================================================================================================
pub fn brfupdnbroadtape_news_feed() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "BRFUPDN:BRF_ALL".to_string();
    contract.sec_type = "NEWS".to_string();
    contract.exchange = "BRFUPDN".to_string();

    contract
}

//==================================================================================================
pub fn cont_fut() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "ES".to_string();
    contract.sec_type = "CONTFUT".to_string();
    contract.exchange = "GLOBEX".to_string();

    contract
}

//==================================================================================================
pub fn cont_and_expiring_fut() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "ES".to_string();
    contract.sec_type = "FUT+CONTFUT".to_string();
    contract.exchange = "GLOBEX".to_string();

    contract
}

//==================================================================================================
pub fn jefferies_contract() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "AAPL".to_string();
    contract.sec_type = "STK".to_string();
    contract.exchange = "JEFFALGO".to_string();
    contract.currency = "USD".to_string();

    contract
}

//==================================================================================================
pub fn csfbcontract() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "IBKR".to_string();
    contract.sec_type = "STK".to_string();
    contract.exchange = "CSFBALGO".to_string();
    contract.currency = "USD".to_string();

    contract
}

//==================================================================================================
pub fn usstock_cfd() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "IBM".to_string();
    contract.sec_type = "cfd".to_string();
    contract.currency = "USD".to_string();
    contract.exchange = "SMART".to_string();

    contract
}

//==================================================================================================
pub fn european_stock_cfd() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "BMW".to_string();
    contract.sec_type = "cfd".to_string();
    contract.currency = "EUR".to_string();
    contract.exchange = "SMART".to_string();

    contract
}

//==================================================================================================
pub fn cash_cfd() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "EUR".to_string();
    contract.sec_type = "cfd".to_string();
    contract.currency = "USD".to_string();
    contract.exchange = "SMART".to_string();

    contract
}

//==================================================================================================
pub fn qbalgo_contract() -> Contract {
    let mut contract = Contract::default();
    contract.symbol = "ES".to_string();
    contract.sec_type = "FUT".to_string();
    contract.exchange = "QBALGO".to_string();
    contract.currency = "USD".to_string();
    contract.last_trade_date_or_contract_month = "202009".to_string();

    contract
}