ibapi/
lib.rs

1//! [![github]](https://github.com/wboayue/rust-ibapi) [![crates-io]](https://crates.io/crates/ibapi) [![license]](https://opensource.org/licenses/MIT)
2//!
3//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
4//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
5//! [license]: https://img.shields.io/badge/License-MIT-blue.svg?style=for-the-badge&labelColor=555555
6//!
7//! <br>
8//!
9//! A comprehensive Rust implementation of the Interactive Brokers TWS API, providing a robust and
10//! user-friendly interface for TWS and IB Gateway. Designed with simplicity in mind, it integrates smoothly into trading systems.
11//!
12//! **API Documentation:**
13//! * [TWS API Reference](https://interactivebrokers.github.io/tws-api/introduction.html) - Detailed technical documentation
14//! * [IBKR Campus](https://ibkrcampus.com/ibkr-api-page/trader-workstation-api/) - IB's official learning platform
15//!
16//! This fully featured API enables the retrieval of account information, access to real-time and historical market data, order management,
17//! market scanning, and access to news and Wall Street Horizons (WSH) event data. Future updates will focus on bug fixes,
18//! maintaining parity with the official API, and enhancing usability.
19//!
20//! For an overview of API usage, refer to the [README](https://github.com/wboayue/rust-ibapi/blob/main/README.md).
21
22#![warn(missing_docs)]
23// Allow octal-looking escapes in string literals (used in test data)
24#![allow(clippy::octal_escapes)]
25#![allow(clippy::bool_assert_comparison)]
26#![allow(clippy::useless_format)]
27#![allow(clippy::uninlined_format_args)]
28#![allow(clippy::assertions_on_constants)]
29
30// Feature guards
31#[cfg(not(any(feature = "sync", feature = "async")))]
32compile_error!(
33    "You must enable at least one of the 'sync' or 'async' features to use this crate.\n\
34     The 'async' feature is enabled by default; if you disabled default features, be sure to\n\
35     opt back into either API:\n\
36         ibapi = { version = \"2.0\", default-features = false, features = [\"sync\"] }\n\
37         ibapi = { version = \"2.0\", default-features = false, features = [\"async\"] }\n\
38     You may also enable both to access the synchronous API under `client::blocking`."
39);
40
41/// Describes items present in an account.
42pub mod accounts;
43
44/// TWS API Client.
45///
46/// The Client establishes the connection to TWS or the Gateway.
47/// It manages the routing of messages between TWS and the application.
48pub mod client;
49
50pub(crate) mod transport;
51
52/// Connection management
53pub(crate) mod connection;
54
55/// Common utilities shared across modules
56pub(crate) mod common;
57
58/// Subscription types for streaming data
59pub mod subscriptions;
60
61/// A [Contract](crate::contracts::Contract) object represents trading instruments such as a stocks, futures or options.
62///
63/// 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.
64pub mod contracts;
65// Describes primary data structures used by the model.
66pub mod errors;
67/// APIs for retrieving market data
68pub mod market_data;
69pub mod messages;
70/// APIs for retrieving news data including articles, bulletins, and providers
71pub mod news;
72/// Data types for building and placing orders.
73pub mod orders;
74/// APIs for working with the market scanner.
75pub mod scanner;
76/// APIs for working with Wall Street Horizon: Earnings Calendar & Event Data.
77pub mod wsh;
78
79/// Server interaction tracing for debugging and monitoring
80pub mod trace;
81
82/// A prelude module for convenient importing of commonly used types.
83pub mod prelude;
84
85/// Protocol version checking and constants for TWS API features.
86pub mod protocol;
87
88mod server_versions;
89
90#[doc(inline)]
91pub use errors::Error;
92
93#[doc(inline)]
94pub use client::Client;
95use std::sync::LazyLock;
96use time::{
97    format_description::{self, BorrowedFormatItem},
98    Date,
99};
100
101#[cfg(test)]
102pub(crate) mod stubs;
103
104#[cfg(test)]
105pub(crate) mod tests;
106
107#[cfg(test)]
108pub(crate) mod testdata;
109
110// ToField
111
112pub(crate) trait ToField {
113    fn to_field(&self) -> String;
114}
115
116impl ToField for bool {
117    fn to_field(&self) -> String {
118        if *self {
119            String::from("1")
120        } else {
121            String::from("0")
122        }
123    }
124}
125
126impl ToField for String {
127    fn to_field(&self) -> String {
128        self.clone()
129    }
130}
131
132impl ToField for Option<String> {
133    fn to_field(&self) -> String {
134        encode_option_field(self)
135    }
136}
137
138impl ToField for &str {
139    fn to_field(&self) -> String {
140        <&str>::clone(self).to_string()
141    }
142}
143
144impl ToField for Option<&str> {
145    fn to_field(&self) -> String {
146        encode_option_field(self)
147    }
148}
149
150impl ToField for usize {
151    fn to_field(&self) -> String {
152        self.to_string()
153    }
154}
155
156impl ToField for i32 {
157    fn to_field(&self) -> String {
158        self.to_string()
159    }
160}
161
162impl ToField for Option<i32> {
163    fn to_field(&self) -> String {
164        encode_option_field(self)
165    }
166}
167
168impl ToField for f64 {
169    fn to_field(&self) -> String {
170        self.to_string()
171    }
172}
173
174impl ToField for Option<f64> {
175    fn to_field(&self) -> String {
176        encode_option_field(self)
177    }
178}
179
180fn date_format() -> Vec<BorrowedFormatItem<'static>> {
181    format_description::parse("[year][month][day]").unwrap()
182}
183
184static DATE_FORMAT: LazyLock<Vec<BorrowedFormatItem<'static>>> = LazyLock::new(date_format);
185
186impl ToField for Date {
187    fn to_field(&self) -> String {
188        self.format(&DATE_FORMAT).unwrap()
189    }
190}
191
192impl ToField for Option<Date> {
193    fn to_field(&self) -> String {
194        encode_option_field(self)
195    }
196}
197
198fn encode_option_field<T: ToField>(val: &Option<T>) -> String {
199    match val {
200        Some(val) => val.to_field(),
201        None => String::from(""),
202    }
203}
204
205// max attempts to retry failed tws requests
206const MAX_RETRIES: i32 = 5;