Skip to main content

nautilus_interactive_brokers/common/enums/
market_data.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::{fmt::Display, str::FromStr};
17
18/// Interactive Brokers historical tick request types.
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20#[cfg_attr(
21    feature = "python",
22    pyo3::pyclass(
23        module = "nautilus_trader.core.nautilus_pyo3.interactive_brokers",
24        from_py_object
25    )
26)]
27pub enum IbHistoricalTickType {
28    /// Historical trade ticks.
29    Trades,
30    /// Historical bid/ask ticks.
31    BidAsk,
32}
33
34impl IbHistoricalTickType {
35    /// Returns the IB wire string.
36    #[must_use]
37    pub const fn as_str(self) -> &'static str {
38        match self {
39            Self::Trades => "TRADES",
40            Self::BidAsk => "BID_ASK",
41        }
42    }
43}
44
45impl FromStr for IbHistoricalTickType {
46    type Err = anyhow::Error;
47
48    fn from_str(value: &str) -> Result<Self, Self::Err> {
49        match value.to_ascii_uppercase().as_str() {
50            "TRADES" => Ok(Self::Trades),
51            "BID_ASK" => Ok(Self::BidAsk),
52            _ => anyhow::bail!("Unknown IB historical tick type: {value}"),
53        }
54    }
55}
56
57impl Display for IbHistoricalTickType {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        f.write_str(self.as_str())
60    }
61}
62
63/// Interactive Brokers trading hours selector.
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65#[cfg_attr(
66    feature = "python",
67    pyo3::pyclass(
68        module = "nautilus_trader.core.nautilus_pyo3.interactive_brokers",
69        from_py_object
70    )
71)]
72pub enum IbTradingHours {
73    /// Regular trading hours only.
74    Regular,
75    /// Include extended trading hours.
76    Extended,
77}
78
79impl IbTradingHours {
80    /// Returns whether IB should use regular trading hours only.
81    #[must_use]
82    pub const fn use_rth(self) -> bool {
83        matches!(self, Self::Regular)
84    }
85
86    /// Converts to the rust-ibapi trading hours enum.
87    #[must_use]
88    pub const fn ibapi_trading_hours(self) -> ibapi::market_data::TradingHours {
89        match self {
90            Self::Regular => ibapi::market_data::TradingHours::Regular,
91            Self::Extended => ibapi::market_data::TradingHours::Extended,
92        }
93    }
94}
95
96impl From<bool> for IbTradingHours {
97    fn from(use_rth: bool) -> Self {
98        if use_rth {
99            Self::Regular
100        } else {
101            Self::Extended
102        }
103    }
104}
105
106/// Interactive Brokers historical bar-size values.
107#[derive(Debug, Clone, Copy, PartialEq, Eq)]
108#[cfg_attr(
109    feature = "python",
110    pyo3::pyclass(
111        module = "nautilus_trader.core.nautilus_pyo3.interactive_brokers",
112        from_py_object
113    )
114)]
115pub enum IbHistoricalBarSize {
116    Sec,
117    Sec5,
118    Sec10,
119    Sec15,
120    Sec30,
121    Min,
122    Min2,
123    Min3,
124    Min5,
125    Min10,
126    Min15,
127    Min20,
128    Min30,
129    Hour,
130    Hour2,
131    Hour3,
132    Hour4,
133    Hour8,
134    Day,
135    Week,
136    Month,
137}
138
139impl IbHistoricalBarSize {
140    /// Converts to the rust-ibapi historical bar-size enum.
141    #[must_use]
142    pub const fn ibapi_bar_size(self) -> ibapi::market_data::historical::BarSize {
143        match self {
144            Self::Sec => ibapi::market_data::historical::BarSize::Sec,
145            Self::Sec5 => ibapi::market_data::historical::BarSize::Sec5,
146            Self::Sec10 => ibapi::market_data::historical::BarSize::Sec10,
147            Self::Sec15 => ibapi::market_data::historical::BarSize::Sec15,
148            Self::Sec30 => ibapi::market_data::historical::BarSize::Sec30,
149            Self::Min => ibapi::market_data::historical::BarSize::Min,
150            Self::Min2 => ibapi::market_data::historical::BarSize::Min2,
151            Self::Min3 => ibapi::market_data::historical::BarSize::Min3,
152            Self::Min5 => ibapi::market_data::historical::BarSize::Min5,
153            Self::Min10 => ibapi::market_data::historical::BarSize::Min10,
154            Self::Min15 => ibapi::market_data::historical::BarSize::Min15,
155            Self::Min20 => ibapi::market_data::historical::BarSize::Min20,
156            Self::Min30 => ibapi::market_data::historical::BarSize::Min30,
157            Self::Hour => ibapi::market_data::historical::BarSize::Hour,
158            Self::Hour2 => ibapi::market_data::historical::BarSize::Hour2,
159            Self::Hour3 => ibapi::market_data::historical::BarSize::Hour3,
160            Self::Hour4 => ibapi::market_data::historical::BarSize::Hour4,
161            Self::Hour8 => ibapi::market_data::historical::BarSize::Hour8,
162            Self::Day => ibapi::market_data::historical::BarSize::Day,
163            Self::Week => ibapi::market_data::historical::BarSize::Week,
164            Self::Month => ibapi::market_data::historical::BarSize::Month,
165        }
166    }
167}
168
169impl Display for IbHistoricalBarSize {
170    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
171        write!(f, "{}", self.ibapi_bar_size())
172    }
173}
174
175/// Interactive Brokers historical data selectors.
176#[derive(Debug, Clone, Copy, PartialEq, Eq)]
177#[cfg_attr(
178    feature = "python",
179    pyo3::pyclass(
180        module = "nautilus_trader.core.nautilus_pyo3.interactive_brokers",
181        from_py_object
182    )
183)]
184pub enum IbHistoricalWhatToShow {
185    Trades,
186    Midpoint,
187    Bid,
188    Ask,
189    BidAsk,
190    HistoricalVolatility,
191    OptionImpliedVolatility,
192    FeeRate,
193    Schedule,
194    AdjustedLast,
195}
196
197impl IbHistoricalWhatToShow {
198    /// Returns the IB wire string.
199    #[must_use]
200    pub const fn as_str(self) -> &'static str {
201        match self {
202            Self::Trades => "TRADES",
203            Self::Midpoint => "MIDPOINT",
204            Self::Bid => "BID",
205            Self::Ask => "ASK",
206            Self::BidAsk => "BID_ASK",
207            Self::HistoricalVolatility => "HISTORICAL_VOLATILITY",
208            Self::OptionImpliedVolatility => "OPTION_IMPLIED_VOLATILITY",
209            Self::FeeRate => "FEE_RATE",
210            Self::Schedule => "SCHEDULE",
211            Self::AdjustedLast => "ADJUSTED_LAST",
212        }
213    }
214
215    /// Converts to the rust-ibapi historical data selector.
216    #[must_use]
217    pub const fn ibapi_what_to_show(self) -> ibapi::market_data::historical::WhatToShow {
218        match self {
219            Self::Trades => ibapi::market_data::historical::WhatToShow::Trades,
220            Self::Midpoint => ibapi::market_data::historical::WhatToShow::MidPoint,
221            Self::Bid => ibapi::market_data::historical::WhatToShow::Bid,
222            Self::Ask => ibapi::market_data::historical::WhatToShow::Ask,
223            Self::BidAsk => ibapi::market_data::historical::WhatToShow::BidAsk,
224            Self::HistoricalVolatility => {
225                ibapi::market_data::historical::WhatToShow::HistoricalVolatility
226            }
227            Self::OptionImpliedVolatility => {
228                ibapi::market_data::historical::WhatToShow::OptionImpliedVolatility
229            }
230            Self::FeeRate => ibapi::market_data::historical::WhatToShow::FeeRate,
231            Self::Schedule => ibapi::market_data::historical::WhatToShow::Schedule,
232            Self::AdjustedLast => ibapi::market_data::historical::WhatToShow::AdjustedLast,
233        }
234    }
235}
236
237impl Display for IbHistoricalWhatToShow {
238    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
239        f.write_str(self.as_str())
240    }
241}
242
243/// Interactive Brokers realtime bar-size values.
244#[derive(Debug, Clone, Copy, PartialEq, Eq)]
245#[cfg_attr(
246    feature = "python",
247    pyo3::pyclass(
248        module = "nautilus_trader.core.nautilus_pyo3.interactive_brokers",
249        from_py_object
250    )
251)]
252pub enum IbRealtimeBarSize {
253    Sec5,
254}
255
256impl IbRealtimeBarSize {
257    /// Converts to the rust-ibapi realtime bar-size enum.
258    #[must_use]
259    pub const fn ibapi_bar_size(self) -> ibapi::market_data::realtime::BarSize {
260        match self {
261            Self::Sec5 => ibapi::market_data::realtime::BarSize::Sec5,
262        }
263    }
264}
265
266impl Display for IbRealtimeBarSize {
267    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
268        match self {
269            Self::Sec5 => f.write_str("5 secs"),
270        }
271    }
272}
273
274/// Interactive Brokers realtime bar selectors.
275#[derive(Debug, Clone, Copy, PartialEq, Eq)]
276#[cfg_attr(
277    feature = "python",
278    pyo3::pyclass(
279        module = "nautilus_trader.core.nautilus_pyo3.interactive_brokers",
280        from_py_object
281    )
282)]
283pub enum IbRealtimeWhatToShow {
284    Trades,
285    Midpoint,
286    Bid,
287    Ask,
288}
289
290impl IbRealtimeWhatToShow {
291    /// Returns the IB wire string.
292    #[must_use]
293    pub const fn as_str(self) -> &'static str {
294        match self {
295            Self::Trades => "TRADES",
296            Self::Midpoint => "MIDPOINT",
297            Self::Bid => "BID",
298            Self::Ask => "ASK",
299        }
300    }
301
302    /// Converts to the rust-ibapi realtime data selector.
303    #[must_use]
304    pub const fn ibapi_what_to_show(self) -> ibapi::market_data::realtime::WhatToShow {
305        match self {
306            Self::Trades => ibapi::market_data::realtime::WhatToShow::Trades,
307            Self::Midpoint => ibapi::market_data::realtime::WhatToShow::MidPoint,
308            Self::Bid => ibapi::market_data::realtime::WhatToShow::Bid,
309            Self::Ask => ibapi::market_data::realtime::WhatToShow::Ask,
310        }
311    }
312}
313
314impl Display for IbRealtimeWhatToShow {
315    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
316        f.write_str(self.as_str())
317    }
318}
319
320/// Interactive Brokers market data tick types.
321#[derive(Debug, Clone, Copy, PartialEq, Eq)]
322#[cfg_attr(
323    feature = "python",
324    pyo3::pyclass(
325        module = "nautilus_trader.core.nautilus_pyo3.interactive_brokers",
326        from_py_object
327    )
328)]
329pub enum IbTickType {
330    Unknown,
331    BidSize,
332    Bid,
333    Ask,
334    AskSize,
335    Last,
336    LastSize,
337    High,
338    Low,
339    Volume,
340    Close,
341    BidOption,
342    AskOption,
343    LastOption,
344    ModelOption,
345    Open,
346    Low13Week,
347    High13Week,
348    Low26Week,
349    High26Week,
350    Low52Week,
351    High52Week,
352    AvgVolume,
353    OpenInterest,
354    OptionHistoricalVol,
355    OptionImpliedVol,
356    OptionBidExch,
357    OptionAskExch,
358    OptionCallOpenInterest,
359    OptionPutOpenInterest,
360    OptionCallVolume,
361    OptionPutVolume,
362    IndexFuturePremium,
363    BidExch,
364    AskExch,
365    AuctionVolume,
366    AuctionPrice,
367    AuctionImbalance,
368    MarkPrice,
369    BidEfpComputation,
370    AskEfpComputation,
371    LastEfpComputation,
372    OpenEfpComputation,
373    HighEfpComputation,
374    LowEfpComputation,
375    CloseEfpComputation,
376    LastTimestamp,
377    Shortable,
378    FundamentalRatios,
379    RtVolume,
380    Halted,
381    BidYield,
382    AskYield,
383    LastYield,
384    CustOptionComputation,
385    TradeCount,
386    TradeRate,
387    VolumeRate,
388    LastRthTrade,
389    RtHistoricalVol,
390    IbDividends,
391    BondFactorMultiplier,
392    RegulatoryImbalance,
393    NewsTick,
394    ShortTermVolume3Min,
395    ShortTermVolume5Min,
396    ShortTermVolume10Min,
397    DelayedBid,
398    DelayedAsk,
399    DelayedLast,
400    DelayedBidSize,
401    DelayedAskSize,
402    DelayedLastSize,
403    DelayedHigh,
404    DelayedLow,
405    DelayedVolume,
406    DelayedClose,
407    DelayedOpen,
408    RtTrdVolume,
409    CreditmanMarkPrice,
410    CreditmanSlowMarkPrice,
411    DelayedBidOption,
412    DelayedAskOption,
413    DelayedLastOption,
414    DelayedModelOption,
415    LastExch,
416    LastRegTime,
417    FuturesOpenInterest,
418    AvgOptVolume,
419    DelayedLastTimestamp,
420    ShortableShares,
421    DelayedHalted,
422    Reuters2MutualFunds,
423    EtfNavClose,
424    EtfNavPriorClose,
425    EtfNavBid,
426    EtfNavAsk,
427    EtfNavLast,
428    EtfFrozenNavLast,
429    EtfNavHigh,
430    EtfNavLow,
431    SocialMarketAnalytics,
432    EstimatedIpoMidpoint,
433    FinalIpoLast,
434    DelayedYieldBid,
435    DelayedYieldAsk,
436}
437
438impl IbTickType {
439    /// Returns the IB integer code.
440    #[must_use]
441    pub const fn as_i32(self) -> i32 {
442        match self {
443            Self::Unknown => -1,
444            Self::BidSize => 0,
445            Self::Bid => 1,
446            Self::Ask => 2,
447            Self::AskSize => 3,
448            Self::Last => 4,
449            Self::LastSize => 5,
450            Self::High => 6,
451            Self::Low => 7,
452            Self::Volume => 8,
453            Self::Close => 9,
454            Self::BidOption => 10,
455            Self::AskOption => 11,
456            Self::LastOption => 12,
457            Self::ModelOption => 13,
458            Self::Open => 14,
459            Self::Low13Week => 15,
460            Self::High13Week => 16,
461            Self::Low26Week => 17,
462            Self::High26Week => 18,
463            Self::Low52Week => 19,
464            Self::High52Week => 20,
465            Self::AvgVolume => 21,
466            Self::OpenInterest => 22,
467            Self::OptionHistoricalVol => 23,
468            Self::OptionImpliedVol => 24,
469            Self::OptionBidExch => 25,
470            Self::OptionAskExch => 26,
471            Self::OptionCallOpenInterest => 27,
472            Self::OptionPutOpenInterest => 28,
473            Self::OptionCallVolume => 29,
474            Self::OptionPutVolume => 30,
475            Self::IndexFuturePremium => 31,
476            Self::BidExch => 32,
477            Self::AskExch => 33,
478            Self::AuctionVolume => 34,
479            Self::AuctionPrice => 35,
480            Self::AuctionImbalance => 36,
481            Self::MarkPrice => 37,
482            Self::BidEfpComputation => 38,
483            Self::AskEfpComputation => 39,
484            Self::LastEfpComputation => 40,
485            Self::OpenEfpComputation => 41,
486            Self::HighEfpComputation => 42,
487            Self::LowEfpComputation => 43,
488            Self::CloseEfpComputation => 44,
489            Self::LastTimestamp => 45,
490            Self::Shortable => 46,
491            Self::FundamentalRatios => 47,
492            Self::RtVolume => 48,
493            Self::Halted => 49,
494            Self::BidYield => 50,
495            Self::AskYield => 51,
496            Self::LastYield => 52,
497            Self::CustOptionComputation => 53,
498            Self::TradeCount => 54,
499            Self::TradeRate => 55,
500            Self::VolumeRate => 56,
501            Self::LastRthTrade => 57,
502            Self::RtHistoricalVol => 58,
503            Self::IbDividends => 59,
504            Self::BondFactorMultiplier => 60,
505            Self::RegulatoryImbalance => 61,
506            Self::NewsTick => 62,
507            Self::ShortTermVolume3Min => 63,
508            Self::ShortTermVolume5Min => 64,
509            Self::ShortTermVolume10Min => 65,
510            Self::DelayedBid => 66,
511            Self::DelayedAsk => 67,
512            Self::DelayedLast => 68,
513            Self::DelayedBidSize => 69,
514            Self::DelayedAskSize => 70,
515            Self::DelayedLastSize => 71,
516            Self::DelayedHigh => 72,
517            Self::DelayedLow => 73,
518            Self::DelayedVolume => 74,
519            Self::DelayedClose => 75,
520            Self::DelayedOpen => 76,
521            Self::RtTrdVolume => 77,
522            Self::CreditmanMarkPrice => 78,
523            Self::CreditmanSlowMarkPrice => 79,
524            Self::DelayedBidOption => 80,
525            Self::DelayedAskOption => 81,
526            Self::DelayedLastOption => 82,
527            Self::DelayedModelOption => 83,
528            Self::LastExch => 84,
529            Self::LastRegTime => 85,
530            Self::FuturesOpenInterest => 86,
531            Self::AvgOptVolume => 87,
532            Self::DelayedLastTimestamp => 88,
533            Self::ShortableShares => 89,
534            Self::DelayedHalted => 90,
535            Self::Reuters2MutualFunds => 91,
536            Self::EtfNavClose => 92,
537            Self::EtfNavPriorClose => 93,
538            Self::EtfNavBid => 94,
539            Self::EtfNavAsk => 95,
540            Self::EtfNavLast => 96,
541            Self::EtfFrozenNavLast => 97,
542            Self::EtfNavHigh => 98,
543            Self::EtfNavLow => 99,
544            Self::SocialMarketAnalytics => 100,
545            Self::EstimatedIpoMidpoint => 101,
546            Self::FinalIpoLast => 102,
547            Self::DelayedYieldBid => 103,
548            Self::DelayedYieldAsk => 104,
549        }
550    }
551
552    /// Converts to the rust-ibapi tick type enum.
553    #[must_use]
554    pub fn ibapi_tick_type(self) -> ibapi::contracts::tick_types::TickType {
555        ibapi::contracts::tick_types::TickType::from(self.as_i32())
556    }
557}
558
559impl From<i32> for IbTickType {
560    fn from(value: i32) -> Self {
561        match value {
562            0 => Self::BidSize,
563            1 => Self::Bid,
564            2 => Self::Ask,
565            3 => Self::AskSize,
566            4 => Self::Last,
567            5 => Self::LastSize,
568            6 => Self::High,
569            7 => Self::Low,
570            8 => Self::Volume,
571            9 => Self::Close,
572            10 => Self::BidOption,
573            11 => Self::AskOption,
574            12 => Self::LastOption,
575            13 => Self::ModelOption,
576            14 => Self::Open,
577            15 => Self::Low13Week,
578            16 => Self::High13Week,
579            17 => Self::Low26Week,
580            18 => Self::High26Week,
581            19 => Self::Low52Week,
582            20 => Self::High52Week,
583            21 => Self::AvgVolume,
584            22 => Self::OpenInterest,
585            23 => Self::OptionHistoricalVol,
586            24 => Self::OptionImpliedVol,
587            25 => Self::OptionBidExch,
588            26 => Self::OptionAskExch,
589            27 => Self::OptionCallOpenInterest,
590            28 => Self::OptionPutOpenInterest,
591            29 => Self::OptionCallVolume,
592            30 => Self::OptionPutVolume,
593            31 => Self::IndexFuturePremium,
594            32 => Self::BidExch,
595            33 => Self::AskExch,
596            34 => Self::AuctionVolume,
597            35 => Self::AuctionPrice,
598            36 => Self::AuctionImbalance,
599            37 => Self::MarkPrice,
600            38 => Self::BidEfpComputation,
601            39 => Self::AskEfpComputation,
602            40 => Self::LastEfpComputation,
603            41 => Self::OpenEfpComputation,
604            42 => Self::HighEfpComputation,
605            43 => Self::LowEfpComputation,
606            44 => Self::CloseEfpComputation,
607            45 => Self::LastTimestamp,
608            46 => Self::Shortable,
609            47 => Self::FundamentalRatios,
610            48 => Self::RtVolume,
611            49 => Self::Halted,
612            50 => Self::BidYield,
613            51 => Self::AskYield,
614            52 => Self::LastYield,
615            53 => Self::CustOptionComputation,
616            54 => Self::TradeCount,
617            55 => Self::TradeRate,
618            56 => Self::VolumeRate,
619            57 => Self::LastRthTrade,
620            58 => Self::RtHistoricalVol,
621            59 => Self::IbDividends,
622            60 => Self::BondFactorMultiplier,
623            61 => Self::RegulatoryImbalance,
624            62 => Self::NewsTick,
625            63 => Self::ShortTermVolume3Min,
626            64 => Self::ShortTermVolume5Min,
627            65 => Self::ShortTermVolume10Min,
628            66 => Self::DelayedBid,
629            67 => Self::DelayedAsk,
630            68 => Self::DelayedLast,
631            69 => Self::DelayedBidSize,
632            70 => Self::DelayedAskSize,
633            71 => Self::DelayedLastSize,
634            72 => Self::DelayedHigh,
635            73 => Self::DelayedLow,
636            74 => Self::DelayedVolume,
637            75 => Self::DelayedClose,
638            76 => Self::DelayedOpen,
639            77 => Self::RtTrdVolume,
640            78 => Self::CreditmanMarkPrice,
641            79 => Self::CreditmanSlowMarkPrice,
642            80 => Self::DelayedBidOption,
643            81 => Self::DelayedAskOption,
644            82 => Self::DelayedLastOption,
645            83 => Self::DelayedModelOption,
646            84 => Self::LastExch,
647            85 => Self::LastRegTime,
648            86 => Self::FuturesOpenInterest,
649            87 => Self::AvgOptVolume,
650            88 => Self::DelayedLastTimestamp,
651            89 => Self::ShortableShares,
652            90 => Self::DelayedHalted,
653            91 => Self::Reuters2MutualFunds,
654            92 => Self::EtfNavClose,
655            93 => Self::EtfNavPriorClose,
656            94 => Self::EtfNavBid,
657            95 => Self::EtfNavAsk,
658            96 => Self::EtfNavLast,
659            97 => Self::EtfFrozenNavLast,
660            98 => Self::EtfNavHigh,
661            99 => Self::EtfNavLow,
662            100 => Self::SocialMarketAnalytics,
663            101 => Self::EstimatedIpoMidpoint,
664            102 => Self::FinalIpoLast,
665            103 => Self::DelayedYieldBid,
666            104 => Self::DelayedYieldAsk,
667            _ => Self::Unknown,
668        }
669    }
670}
671
672impl Display for IbTickType {
673    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
674        write!(f, "{}", self.as_i32())
675    }
676}