ibcore 0.2.1

Standalone IB Gateway integration layer wrapping ibapi — diagnostic events, structured errors, market data snapshots
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
//! Real-time market data streaming.
//!
//! Provides typed market data tick events ([`TickEvent`]) and the
//! [`TickStream`] wrapper that manages the IB subscription lifecycle.

use ibapi::contracts::tick_types::TickType;
use ibapi::market_data::realtime::TickTypes;
use futures::StreamExt;
use futures::stream::BoxStream;
use ibapi::subscriptions::SubscriptionItemStreamExt;

use crate::errors::IbError;

/// A typed tick event from a real-time market data stream.
///
/// Maps ibapi's heterogeneous [`TickTypes`] into a flat, consumer-friendly
/// enum. Each variant carries the data relevant to that kind of tick.
#[derive(Debug, PartialEq)]
pub enum TickEvent {
    /// Bid, ask, or last price update.
    Price {
        tick_type: TickType,
        price: f64,
    },
    /// Size update (bid size, ask size, last size, volume).
    Size {
        tick_type: TickType,
        size: f64,
    },
    /// Combined price + size (common for option bid/ask).
    PriceSize {
        price_tick_type: TickType,
        price: f64,
        size: f64,
    },
    /// Option Greeks + IV computation.
    Greeks {
        computation_type: i32,
        implied_volatility: f64,
        delta: f64,
        gamma: f64,
        theta: f64,
        vega: f64,
        option_price: f64,
        underlying_price: f64,
    },
    /// Generic numeric tick (e.g. option IV, historical vol, shortable).
    Generic {
        tick_type: TickType,
        value: f64,
    },
    /// Textual market data string (e.g. last timestamp, RT volume, halted).
    String {
        tick_type: TickType,
        value: String,
    },
    /// Snapshot completed (only for snapshot subscriptions).
    SnapshotEnd,
    /// Active market data type announced for this subscription.
    MarketDataType(ibapi::market_data::MarketDataType),
}

/// Map ibapi's [`TickTypes`] into our domain [`TickEvent`].
impl From<TickTypes> for TickEvent {
    fn from(tick: TickTypes) -> Self {
        match tick {
            TickTypes::Price(price) => TickEvent::Price {
                tick_type: price.tick_type,
                price: price.price,
            },
            TickTypes::Size(size) => TickEvent::Size {
                tick_type: size.tick_type,
                size: size.size,
            },
            TickTypes::String(s) => TickEvent::String {
                tick_type: s.tick_type,
                value: s.value,
            },
            TickTypes::Generic(g) => TickEvent::Generic {
                tick_type: g.tick_type,
                value: g.value,
            },
            TickTypes::OptionComputation(opt) => TickEvent::Greeks {
                computation_type: opt.tick_attribute.unwrap_or(0),
                implied_volatility: opt.implied_volatility.unwrap_or(0.0),
                delta: opt.delta.unwrap_or(0.0),
                gamma: opt.gamma.unwrap_or(0.0),
                theta: opt.theta.unwrap_or(0.0),
                vega: opt.vega.unwrap_or(0.0),
                option_price: opt.option_price.unwrap_or(0.0),
                underlying_price: opt.underlying_price.unwrap_or(0.0),
            },
            TickTypes::SnapshotEnd => TickEvent::SnapshotEnd,
            TickTypes::MarketDataType(md) => TickEvent::MarketDataType(md),
            TickTypes::PriceSize(ps) => TickEvent::PriceSize {
                price_tick_type: ps.price_tick_type,
                price: ps.price,
                size: ps.size,
            },
            TickTypes::RequestParameters(_) => {
                // Request parameters are not user-facing — skip
                TickEvent::Generic {
                    tick_type: TickType::Unknown,
                    value: 0.0,
                }
            }
        }
    }
}

/// A live market data tick stream for a single contract.
///
/// Wraps an ibapi subscription and maps each item into a typed [`TickEvent`].
/// Dropping the stream cancels the IB subscription.
pub struct TickStream {
    /// Inner boxed stream to hide the concrete ibapi subscription type.
    inner: BoxStream<'static, Result<ibapi::market_data::realtime::TickTypes, ibapi::Error>>,
}

impl TickStream {
    /// Create a new stream from any boxed stream of TickTypes items.
    ///
    /// Primarily for testing. For production use, construct via
    /// [`IbClient::tick_stream`](crate::IbClient::tick_stream).
    pub fn new(
        inner: BoxStream<'static, Result<ibapi::market_data::realtime::TickTypes, ibapi::Error>>,
    ) -> Self {
        Self { inner }
    }

    /// Create from an ibapi market data subscription (used internally).
    pub fn from_subscription(
        sub: ibapi::subscriptions::Subscription<ibapi::market_data::realtime::TickTypes>,
    ) -> Self {
        Self {
            inner: sub.filter_data().boxed(),
        }
    }

    /// Receive the next tick event (or error / stream-end).
    ///
    /// Returns `None` when the stream ends (connection closed / cancellation).
    pub async fn next(&mut self) -> Option<Result<TickEvent, IbError>> {
        match self.inner.next().await {
            Some(Ok(tick)) => Some(Ok(TickEvent::from(tick))),
            Some(Err(e)) => Some(Err(IbError::from(e))),
            None => None,
        }
    }
}

// ── Tests ──────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use ibapi::contracts::tick_types::TickType;
    use ibapi::market_data::realtime::{
        TickGeneric, TickPrice, TickPriceSize, TickSize, TickString,
    };
    use ibapi::market_data::MarketDataType;
    use ibapi::contracts::OptionComputation;

    // ── TickEvent variant construction tests ──

    #[test]
    fn price_event_has_tick_type_and_price() {
        let e = TickEvent::Price {
            tick_type: TickType::Bid,
            price: 450.25,
        };
        assert_eq!(
            e,
            TickEvent::Price {
                tick_type: TickType::Bid,
                price: 450.25,
            }
        );
    }

    #[test]
    fn size_event_has_tick_type_and_size() {
        let e = TickEvent::Size {
            tick_type: TickType::BidSize,
            size: 100.0,
        };
        assert_eq!(
            e,
            TickEvent::Size {
                tick_type: TickType::BidSize,
                size: 100.0,
            }
        );
    }

    #[test]
    fn price_size_event_has_price_and_size() {
        let e = TickEvent::PriceSize {
            price_tick_type: TickType::Ask,
            price: 451.00,
            size: 200.0,
        };
        assert_eq!(
            e,
            TickEvent::PriceSize {
                price_tick_type: TickType::Ask,
                price: 451.00,
                size: 200.0,
            }
        );
    }

    #[test]
    fn greeks_event_has_all_fields() {
        let e = TickEvent::Greeks {
            computation_type: 10,
            implied_volatility: 0.35,
            delta: 0.5,
            gamma: 0.05,
            theta: -0.02,
            vega: 0.10,
            option_price: 5.20,
            underlying_price: 450.00,
        };
        match e {
            TickEvent::Greeks {
                computation_type,
                implied_volatility,
                delta,
                gamma,
                theta,
                vega,
                option_price,
                underlying_price,
            } => {
                assert_eq!(computation_type, 10);
                assert!((implied_volatility - 0.35).abs() < 1e-10);
                assert!((delta - 0.5).abs() < 1e-10);
                assert!((gamma - 0.05).abs() < 1e-10);
                assert!((theta - -0.02).abs() < 1e-10);
                assert!((vega - 0.10).abs() < 1e-10);
                assert!((option_price - 5.20).abs() < 1e-10);
                assert!((underlying_price - 450.00).abs() < 1e-10);
            }
            _ => panic!("expected Greeks variant"),
        }
    }

    #[test]
    fn generic_event_has_tick_type_and_value() {
        let e = TickEvent::Generic {
            tick_type: TickType::OptionImpliedVol,
            value: 0.28,
        };
        match e {
            TickEvent::Generic { tick_type, value } => {
                assert_eq!(tick_type, TickType::OptionImpliedVol);
                assert!((value - 0.28).abs() < 1e-10);
            }
            _ => panic!("expected Generic variant"),
        }
    }

    #[test]
    fn string_event_has_tick_type_and_value() {
        let e = TickEvent::String {
            tick_type: TickType::LastTimestamp,
            value: "1234567890".into(),
        };
        match e {
            TickEvent::String { tick_type, value } => {
                assert_eq!(tick_type, TickType::LastTimestamp);
                assert_eq!(value, "1234567890");
            }
            _ => panic!("expected String variant"),
        }
    }

    #[test]
    fn snapshot_end_is_unit_variant() {
        let e = TickEvent::SnapshotEnd;
        assert_eq!(e, TickEvent::SnapshotEnd);
    }

    #[test]
    fn market_data_type_event() {
        let e = TickEvent::MarketDataType(MarketDataType::Realtime);
        match e {
            TickEvent::MarketDataType(md) => assert_eq!(md, MarketDataType::Realtime),
            _ => panic!("expected MarketDataType variant"),
        }
    }

    // ── From<TickTypes> mapping tests ──

    #[test]
    fn from_tick_price_maps_to_price_event() {
        let tick = TickTypes::Price(TickPrice {
            tick_type: TickType::Bid,
            price: 100.50,
            attributes: Default::default(),
        });
        let event = TickEvent::from(tick);
        assert_eq!(
            event,
            TickEvent::Price {
                tick_type: TickType::Bid,
                price: 100.50,
            }
        );
    }

    #[test]
    fn from_tick_size_maps_to_size_event() {
        let tick = TickTypes::Size(TickSize {
            tick_type: TickType::BidSize,
            size: 500.0,
        });
        let event = TickEvent::from(tick);
        assert_eq!(
            event,
            TickEvent::Size {
                tick_type: TickType::BidSize,
                size: 500.0,
            }
        );
    }

    #[test]
    fn from_tick_price_size_maps_to_price_size_event() {
        let tick = TickTypes::PriceSize(TickPriceSize {
            price_tick_type: TickType::Ask,
            price: 101.00,
            attributes: Default::default(),
            size_tick_type: TickType::AskSize,
            size: 300.0,
        });
        let event = TickEvent::from(tick);
        assert_eq!(
            event,
            TickEvent::PriceSize {
                price_tick_type: TickType::Ask,
                price: 101.00,
                size: 300.0,
            }
        );
    }

    #[test]
    fn from_tick_string_maps_to_string_event() {
        let tick = TickTypes::String(TickString {
            tick_type: TickType::LastTimestamp,
            value: "1700000000".into(),
        });
        let event = TickEvent::from(tick);
        match event {
            TickEvent::String { tick_type, value } => {
                assert_eq!(tick_type, TickType::LastTimestamp);
                assert_eq!(value, "1700000000");
            }
            _ => panic!("expected String variant"),
        }
    }

    #[test]
    fn from_tick_generic_maps_to_generic_event() {
        let tick = TickTypes::Generic(TickGeneric {
            tick_type: TickType::OptionImpliedVol,
            value: 0.35,
        });
        let event = TickEvent::from(tick);
        match event {
            TickEvent::Generic { tick_type, value } => {
                assert_eq!(tick_type, TickType::OptionImpliedVol);
                assert!((value - 0.35).abs() < 1e-10);
            }
            _ => panic!("expected Generic variant"),
        }
    }

    #[test]
    fn from_tick_option_computation_maps_to_greeks() {
        let opt = OptionComputation {
            field: TickType::Bid,
            tick_attribute: Some(10),
            implied_volatility: Some(0.35),
            delta: Some(0.5),
            gamma: Some(0.05),
            theta: Some(-0.02),
            vega: Some(0.10),
            underlying_price: Some(450.00),
            option_price: Some(5.20),
            present_value_dividend: None,
        };
        let tick = TickTypes::OptionComputation(opt);
        let event = TickEvent::from(tick);
        match event {
            TickEvent::Greeks {
                computation_type,
                implied_volatility,
                delta,
                gamma,
                theta,
                vega,
                option_price,
                underlying_price,
            } => {
                assert_eq!(computation_type, 10);
                assert!((implied_volatility - 0.35).abs() < 1e-10);
                assert!((delta - 0.5).abs() < 1e-10);
                assert!((gamma - 0.05).abs() < 1e-10);
                assert!((theta - -0.02).abs() < 1e-10);
                assert!((vega - 0.10).abs() < 1e-10);
                assert!((option_price - 5.20).abs() < 1e-10);
                assert!((underlying_price - 450.00).abs() < 1e-10);
            }
            _ => panic!("expected Greeks variant"),
        }
    }

    #[test]
    fn from_snapshot_end_maps_to_snapshot_end() {
        let tick = TickTypes::SnapshotEnd;
        let event = TickEvent::from(tick);
        assert_eq!(event, TickEvent::SnapshotEnd);
    }

    #[test]
    fn from_market_data_type_maps_to_market_data_type() {
        let tick = TickTypes::MarketDataType(MarketDataType::Realtime);
        let event = TickEvent::from(tick);
        assert_eq!(event, TickEvent::MarketDataType(MarketDataType::Realtime));
    }

    #[test]
    fn from_request_parameters_maps_to_generic() {
        let tick =
            TickTypes::RequestParameters(ibapi::market_data::realtime::TickRequestParameters {
                min_tick: 0.01,
                bbo_exchange: "SMART".into(),
                snapshot_permissions: 1,
            });
        let event = TickEvent::from(tick);
        match event {
            TickEvent::Generic { .. } => {} // expected
            _ => panic!("expected Generic variant for RequestParameters"),
        }
    }

    // ── TickStream tests ──

    #[tokio::test]
    async fn tick_stream_next_returns_none_when_stream_ended() {
        let (tx, rx) = tokio::sync::oneshot::channel::<()>();
        let stream = futures::stream::once(async {
            let _ = tx.send(());
            std::result::Result::<TickTypes, ibapi::Error>::Err(ibapi::Error::Shutdown)
        })
        .boxed();
        let mut ts = TickStream::new(stream);
        // First item should be an error (the Shutdown)
        let first = ts.next().await;
        assert!(first.is_some(), "expected error from stream before end");
        // Second item should be None (stream ended)
        // Actually, the stream will have ended after the first poll since
        // it yields exactly one item then ends.
        // The next() method should return None after the stream yields None.
        let second = ts.next().await;
        assert!(second.is_none(), "expected None after stream ended");
    }

    #[tokio::test]
    async fn tick_stream_forwards_stream_error() {
        let stream = futures::stream::once(
            async { std::result::Result::<TickTypes, ibapi::Error>::Err(ibapi::Error::Shutdown) },
        )
        .boxed();
        let mut ts = TickStream::new(stream);
        let item = ts.next().await;
        match item {
            Some(Err(IbError::ConnectionReset)) => {} // Shutdown maps to ConnectionReset
            other => panic!("expected Some(Err(ConnectionReset)), got {other:?}"),
        }
    }

    #[tokio::test]
    async fn tick_stream_forwards_tick_event() {
        let tick = TickTypes::Price(TickPrice {
            tick_type: TickType::Bid,
            price: 100.50,
            attributes: Default::default(),
        });
        let stream = futures::stream::once(
            async move { std::result::Result::<TickTypes, ibapi::Error>::Ok(tick) },
        )
        .boxed();
        let mut ts = TickStream::new(stream);
        let item = ts.next().await;
        match item {
            Some(Ok(TickEvent::Price { tick_type, price })) => {
                assert_eq!(tick_type, TickType::Bid);
                assert!((price - 100.50).abs() < 1e-10);
            }
            other => panic!("expected Some(Ok(Price)), got {other:?}"),
        }
    }

    #[tokio::test]
    async fn tick_stream_drop_is_safe() {
        let stream = futures::stream::pending::<std::result::Result<TickTypes, ibapi::Error>>()
            .boxed();
        let ts = TickStream::new(stream);
        drop(ts); // should not panic
    }
}