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](https://interactivebrokers.github.io/tws-api/introduction.html), 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//! This fully featured API enables the retrieval of account information, access to real-time and historical market data, order management,
13//! market scanning, and access to news and Wall Street Horizons (WSH) event data. Future updates will focus on bug fixes,
14//! maintaining parity with the official API, and enhancing usability.
15//!
16//! For an overview of API usage, refer to the [README](https://github.com/wboayue/rust-ibapi/blob/main/README.md).
17
18/// Describes items present in an account.
19pub mod accounts;
20
21/// TWS API Client.
22///
23/// The Client establishes the connection to TWS or the Gateway.
24/// It manages the routing of messages between TWS and the application.
25pub mod client;
26
27pub(crate) mod transport;
28
29/// A [Contract](crate::contracts::Contract) object represents trading instruments such as a stocks, futures or options.
30///
31/// 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.
32pub mod contracts;
33// Describes primary data structures used by the model.
34pub mod errors;
35/// APIs for retrieving market data
36pub mod market_data;
37mod messages;
38pub mod news;
39/// Data types for building and placing orders.
40pub mod orders;
41/// APIs for working with the market scanner.
42pub mod scanner;
43/// APIs for working with Wall Street Horizon: Earnings Calendar & Event Data.
44pub mod wsh;
45
46/// A prelude module for convenient importing of commonly used types.
47pub mod prelude;
48
49mod server_versions;
50
51#[doc(inline)]
52pub use errors::Error;
53
54#[doc(inline)]
55pub use client::Client;
56use std::sync::LazyLock;
57use time::{
58    format_description::{self, BorrowedFormatItem},
59    Date,
60};
61
62#[cfg(test)]
63pub(crate) mod stubs;
64
65#[cfg(test)]
66pub(crate) mod tests;
67
68#[cfg(test)]
69pub(crate) mod testdata;
70
71// ToField
72
73pub(crate) trait ToField {
74    fn to_field(&self) -> String;
75}
76
77impl ToField for bool {
78    fn to_field(&self) -> String {
79        if *self {
80            String::from("1")
81        } else {
82            String::from("0")
83        }
84    }
85}
86
87impl ToField for String {
88    fn to_field(&self) -> String {
89        self.clone()
90    }
91}
92
93impl ToField for Option<String> {
94    fn to_field(&self) -> String {
95        encode_option_field(self)
96    }
97}
98
99impl ToField for &str {
100    fn to_field(&self) -> String {
101        <&str>::clone(self).to_string()
102    }
103}
104
105impl ToField for Option<&str> {
106    fn to_field(&self) -> String {
107        encode_option_field(self)
108    }
109}
110
111impl ToField for usize {
112    fn to_field(&self) -> String {
113        self.to_string()
114    }
115}
116
117impl ToField for i32 {
118    fn to_field(&self) -> String {
119        self.to_string()
120    }
121}
122
123impl ToField for Option<i32> {
124    fn to_field(&self) -> String {
125        encode_option_field(self)
126    }
127}
128
129impl ToField for f64 {
130    fn to_field(&self) -> String {
131        self.to_string()
132    }
133}
134
135impl ToField for Option<f64> {
136    fn to_field(&self) -> String {
137        encode_option_field(self)
138    }
139}
140
141fn date_format() -> Vec<BorrowedFormatItem<'static>> {
142    format_description::parse("[year][month][day]").unwrap()
143}
144
145static DATE_FORMAT: LazyLock<Vec<BorrowedFormatItem<'static>>> = LazyLock::new(date_format);
146
147impl ToField for Date {
148    fn to_field(&self) -> String {
149        self.format(&DATE_FORMAT).unwrap()
150    }
151}
152
153impl ToField for Option<Date> {
154    fn to_field(&self) -> String {
155        encode_option_field(self)
156    }
157}
158
159fn encode_option_field<T: ToField>(val: &Option<T>) -> String {
160    match val {
161        Some(val) => val.to_field(),
162        None => String::from(""),
163    }
164}
165
166// max attempts to retry failed tws requests
167const MAX_RETRIES: i32 = 5;