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
//! [![github]](https://github.com/wboayue/rust-ibapi) [![crates-io]](https://crates.io/crates/ibapi) [![license]](https://opensource.org/licenses/MIT)
//!
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
//! [license]: https://img.shields.io/badge/License-MIT-blue.svg?style=for-the-badge&labelColor=555555
//!
//! <br>
//!
//! An implementation of the Interactive Brokers [TWS API](https://interactivebrokers.github.io/tws-api/introduction.html) for Rust.
//! This implementation is not a direct port of the official TWS API.
//! It provides a synchronous API that simplifies the development of trading strategies.
//!
//! This is a work in progress and was tested using TWS 10.19. The primary reference for this implementation is the [C# source code](https://github.com/InteractiveBrokers/tws-api-public).
//!
//! The following example gives a flavor of the API style. It is not a trading strategy recommendation and not a complete implementation.
//!
//!```no_run
//! use std::collections::VecDeque;
//!
//! use ibapi::contracts::Contract;
//! use ibapi::market_data::realtime::{BarSize, Bar, WhatToShow};
//! use ibapi::orders::{order_builder, Action, OrderNotification};
//! use ibapi::Client;
//!
//! let client = Client::connect("127.0.0.1:4002", 100).unwrap();
//!
//! let symbol = "TSLA";
//! let contract = Contract::stock(symbol); // defaults to USD and SMART exchange.
//!
//! let bars = client.realtime_bars(&contract, BarSize::Sec5, WhatToShow::Trades, false).unwrap();
//!
//! let mut channel = BreakoutChannel::new(30);
//!
//! for bar in bars {
//! channel.add_bar(&bar);
//!
//! // Ensure enough bars and no open positions.
//! if !channel.ready() || has_position(&client, symbol) {
//! continue;
//! }
//!
//! let action = if bar.close > channel.high() {
//! Action::Buy
//! } else if bar.close < channel.low() {
//! Action::Sell
//! } else {
//! continue;
//! };
//!
//! let order_id = client.next_order_id();
//! let order = order_builder::market_order(action, 100.0);
//!
//! let notices = client.place_order(order_id, &contract, &order).unwrap();
//! for notice in notices {
//! if let OrderNotification::ExecutionData(data) = notice {
//! println!("{} {} shares of {}", data.execution.side, data.execution.shares, data.contract.symbol);
//! } else {
//! println!("{:?}", notice);
//! }
//! }
//! }
//!
//! fn has_position(client: &Client, symbol: &str) -> bool {
//! if let Ok(mut positions) = client.positions() {
//! positions.find(|p| p.contract.symbol == symbol).is_some()
//! } else {
//! false
//! }
//! }
//!
//! struct BreakoutChannel {
//! ticks: VecDeque<(f64, f64)>,
//! size: usize,
//! }
//!
//! impl BreakoutChannel {
//! fn new(size: usize) -> BreakoutChannel {
//! BreakoutChannel {
//! ticks: VecDeque::with_capacity(size + 1),
//! size,
//! }
//! }
//!
//! fn ready(&self) -> bool {
//! self.ticks.len() >= self.size
//! }
//!
//! fn add_bar(&mut self, bar: &Bar) {
//! self.ticks.push_back((bar.high, bar.low));
//!
//! if self.ticks.len() > self.size {
//! self.ticks.pop_front();
//! }
//! }
//!
//! fn high(&self) -> f64 {
//! self.ticks.iter().map(|x| x.0).max_by(|a, b| a.partial_cmp(b).unwrap()).unwrap()
//! }
//!
//! fn low(&self) -> f64 {
//! self.ticks.iter().map(|x| x.1).max_by(|a, b| a.partial_cmp(b).unwrap()).unwrap()
//! }
//! }
//!```
/// Describes items present in an account.
/// TSW API Client.
///
/// The Client establishes the connection to TWS or the Gateway.
/// It manages the routing of messages between TWS and the application.
/// A [Contract](crate::contracts::Contract) object represents trading instruments such as a stocks, futures or options.
///
/// Every time a new request that requires a contract (i.e. market data, order placing, etc.) is sent to the API, the system will try to match the provided contract object with a single candidate. If there is more than one contract matching the same description, the API will return an error notifying you there is an ambiguity. In these cases the API needs further information to narrow down the list of contracts matching the provided description to a single element.
// Describes primary data structures used by the model.
//pub(crate) mod domain;
/// APIs for retrieving market data
pub
/// Data types for building and placing orders.
pub
pub use Error;
pub use Client;
// ToField
pub