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
use log::debug;
use crate::client::blocking::{ClientRequestBuilders, Subscription};
use crate::contracts::{Contract, TagValue};
use crate::messages::OutgoingMessages;
use crate::protocol::{check_version, Features};
use crate::{client::sync::Client, server_versions, Error};
use super::common::{decoders, encoders};
use super::{Bar, DepthMarketDataDescription, MarketDepthBuilder, MarketDepths, RealtimeBarsBuilder, TickByTickBuilder, TickTypes, WhatToShow};
use crate::market_data::{SmartDepth, TradingHours};
use crate::subscriptions::StreamDecoder;
// Validates that server supports the given request.
pub(super) fn validate_tick_by_tick_request(client: &Client, _contract: &Contract, number_of_ticks: i32, ignore_size: bool) -> Result<(), Error> {
check_version(client.server_version(), Features::TICK_BY_TICK)?;
if number_of_ticks != 0 || ignore_size {
check_version(client.server_version(), Features::TICK_BY_TICK_IGNORE_SIZE)?;
}
Ok(())
}
impl Client {
/// Returns a builder for a real-time 5-second bar subscription.
///
/// Defaults to `WhatToShow::Trades` and `TradingHours::Regular`. See
/// [`RealtimeBarsBuilder`] for the chained methods.
///
/// # Examples
///
/// ```no_run
/// use ibapi::client::blocking::Client;
/// use ibapi::contracts::Contract;
/// use ibapi::market_data::realtime::WhatToShow;
/// use ibapi::market_data::TradingHours;
///
/// let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
///
/// let contract = Contract::stock("TSLA").build();
/// let subscription = client
/// .realtime_bars(&contract)
/// .what_to_show(WhatToShow::Trades)
/// .trading_hours(TradingHours::Extended)
/// .subscribe()
/// .expect("realtime bars request failed");
///
/// for (i, bar) in subscription.iter_data().enumerate().take(60) {
/// match bar {
/// Ok(bar) => println!("bar[{i}]: {bar:?}"),
/// Err(e) => { eprintln!("error: {e:?}"); break; }
/// }
/// }
/// ```
pub fn realtime_bars<'a>(&'a self, contract: &'a Contract) -> RealtimeBarsBuilder<'a, Self> {
RealtimeBarsBuilder::new(self, contract)
}
/// Returns a builder for a tick-by-tick real-time subscription.
///
/// Pick the tick stream with the terminal — `.last()` / `.all_last()` /
/// `.bid_ask(IgnoreSize)` / `.mid_point()`. See [`TickByTickBuilder`].
///
/// # Examples
///
/// ```no_run
/// use ibapi::client::blocking::Client;
/// use ibapi::contracts::Contract;
/// use ibapi::market_data::IgnoreSize;
///
/// let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
/// let contract = Contract::stock("AAPL").build();
///
/// let quotes = client
/// .tick_by_tick(&contract, 10)
/// .bid_ask(IgnoreSize::No)
/// .expect("tick-by-tick bid/ask request failed");
///
/// for quote in quotes.iter_data().take(10) {
/// match quote {
/// Ok(quote) => println!("{quote:?}"),
/// Err(e) => { eprintln!("error: {e:?}"); break; }
/// }
/// }
/// ```
pub fn tick_by_tick<'a>(&'a self, contract: &'a Contract, number_of_ticks: i32) -> TickByTickBuilder<'a, Self> {
TickByTickBuilder::new(self, contract, number_of_ticks)
}
/// Returns a builder for a level-2 market-depth (order book) subscription.
///
/// Defaults to `SmartDepth::No`. See [`MarketDepthBuilder`] for the chained
/// methods.
///
/// # Examples
///
/// ```no_run
/// use ibapi::client::blocking::Client;
/// use ibapi::contracts::Contract;
/// use ibapi::market_data::SmartDepth;
///
/// let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
///
/// let contract = Contract::stock("AAPL").build();
/// let subscription = client
/// .market_depth(&contract, 5)
/// .smart_depth(SmartDepth::Yes)
/// .subscribe()
/// .expect("error requesting market depth");
///
/// for row in subscription.iter_data() {
/// match row {
/// Ok(row) => println!("row: {row:?}"),
/// Err(e) => {
/// eprintln!("error: {e:?}");
/// break;
/// }
/// }
/// }
/// ```
pub fn market_depth<'a>(&'a self, contract: &'a Contract, number_of_rows: i32) -> MarketDepthBuilder<'a, Self> {
MarketDepthBuilder::new(self, contract, number_of_rows)
}
/// Requests venues for which market data is returned to market_depth (those with market makers)
///
/// # Examples
///
/// ```no_run
/// use ibapi::client::blocking::Client;
///
/// let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
/// let exchanges = client.market_depth_exchanges().expect("error requesting market depth exchanges");
/// for exchange in &exchanges {
/// println!("{exchange:?}");
/// }
/// ```
pub fn market_depth_exchanges(&self) -> Result<Vec<DepthMarketDataDescription>, Error> {
check_version(self.server_version(), Features::REQ_MKT_DEPTH_EXCHANGES)?;
loop {
let request = encoders::encode_request_market_depth_exchanges()?;
let subscription = self.shared_request(OutgoingMessages::RequestMktDepthExchanges).send_raw(request)?;
let response = subscription.next();
match response {
Some(Ok(message)) => return decoders::decode_market_depth_exchanges(&message),
Some(Err(Error::ConnectionReset)) => {
debug!("connection reset. retrying market_depth_exchanges");
continue;
}
Some(Err(e)) => return Err(e),
None => return Ok(Vec::new()),
}
}
}
/// Switches market data type returned from request_market_data requests to Live, Frozen, Delayed, or FrozenDelayed.
///
/// # Arguments
/// * `market_data_type` - Type of market data to retrieve.
///
/// # Examples
///
/// ```no_run
/// use ibapi::client::blocking::Client;
/// use ibapi::market_data::{MarketDataType};
///
/// let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
///
/// let market_data_type = MarketDataType::Realtime;
/// client.switch_market_data_type(market_data_type).expect("request failed");
/// println!("market data switched: {market_data_type:?}");
/// ```
pub fn switch_market_data_type(&self, market_data_type: crate::market_data::MarketDataType) -> Result<(), Error> {
self.check_server_version(server_versions::REQ_MARKET_DATA_TYPE, "It does not support market data type requests.")?;
let message = crate::market_data::encoders::encode_request_market_data_type(market_data_type)?;
let _ = self.send_shared_request(OutgoingMessages::RequestMarketDataType, message)?;
Ok(())
}
}
/// Subscribe to streaming level-1 market data for the given contract.
pub fn market_data(
client: &Client,
contract: &Contract,
generic_ticks: &[&str],
snapshot: bool,
regulatory_snapshot: bool,
) -> Result<Subscription<TickTypes>, Error> {
let builder = client.request();
let request = encoders::encode_request_market_data(builder.request_id(), contract, generic_ticks, snapshot, regulatory_snapshot)?;
builder.send(request)
}
pub(crate) fn realtime_bars(
client: &Client,
contract: &Contract,
what_to_show: &WhatToShow,
trading_hours: TradingHours,
options: &[TagValue],
) -> Result<Subscription<Bar>, Error> {
let builder = client.request();
let request = encoders::encode_request_realtime_bars(builder.request_id(), contract, what_to_show, trading_hours.use_rth(), options)?;
builder.send(request)
}
pub(crate) fn market_depth(
client: &Client,
contract: &Contract,
number_of_rows: i32,
smart_depth: SmartDepth,
) -> Result<Subscription<MarketDepths>, Error> {
let is_smart_depth = matches!(smart_depth, SmartDepth::Yes);
if is_smart_depth {
check_version(client.server_version(), Features::SMART_DEPTH)?;
}
if !contract.primary_exchange.is_empty() {
check_version(client.server_version(), Features::MKT_DEPTH_PRIM_EXCHANGE)?;
}
let builder = client.request();
let request = encoders::encode_request_market_depth(builder.request_id(), contract, number_of_rows, is_smart_depth)?;
builder.send_with_context(request, client.decoder_context().with_smart_depth(is_smart_depth))
}
pub(crate) fn tick_by_tick<T: StreamDecoder<T>>(
client: &Client,
contract: &Contract,
tick_type: &str,
number_of_ticks: i32,
ignore_size: bool,
) -> Result<Subscription<T>, Error> {
validate_tick_by_tick_request(client, contract, number_of_ticks, ignore_size)?;
let builder = client.request();
let request = encoders::encode_tick_by_tick(builder.request_id(), contract, tick_type, number_of_ticks, ignore_size)?;
builder.send(request)
}
#[cfg(test)]
#[path = "sync_tests.rs"]
mod tests;