ibapi 3.0.0

A Rust implementation of the Interactive Brokers TWS API, providing a reliable and user friendly interface for TWS and IB Gateway. Designed with a focus on simplicity and performance.
Documentation
use super::common::{decoders, encoders, verify};
use super::*;
use crate::client::blocking::{ClientRequestBuilders, Subscription};
use crate::common::request_helpers;
use crate::messages::{IncomingMessages, OutgoingMessages};
use crate::protocol::{check_version, Features};
use crate::subscriptions::StreamDecoder;
use crate::{client::sync::Client, Error};
use log::{error, info};

impl Client {
    /// Requests contract information.
    ///
    /// Provides all the contracts matching the contract provided. It can also be used to retrieve complete options and futures chains. Though it is now (in API version > 9.72.12) advised to use [Client::option_chain] for that purpose.
    ///
    /// # Arguments
    /// * `contract` - The [Contract] used as sample to query the available contracts. Typically, it will contain the [Contract]'s symbol, currency, security_type, and exchange.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use ibapi::client::blocking::Client;
    /// use ibapi::contracts::Contract;
    ///
    /// let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
    ///
    /// let contract = Contract::stock("TSLA").build();
    /// let results = client.contract_details(&contract).expect("request failed");
    /// for contract_detail in results {
    ///     println!("contract: {contract_detail:?}");
    /// }
    /// ```
    pub fn contract_details(&self, contract: &Contract) -> Result<Vec<ContractDetails>, Error> {
        verify::verify_contract(self.server_version, contract)?;

        let builder = self.request();
        let request_id = builder.request_id();
        let packet = encoders::encode_request_contract_data(request_id, contract)?;

        let responses = builder.send_raw(packet)?;

        let mut contract_details: Vec<ContractDetails> = Vec::default();

        while let Some(response) = responses.next() {
            log::debug!("response: {response:#?}");
            match response {
                Ok(mut message) if message.message_type() == IncomingMessages::ContractData => {
                    let decoded = decoders::decode_contract_details(self.server_version, &mut message)?;
                    contract_details.push(decoded);
                }
                Ok(message) if message.message_type() == IncomingMessages::ContractDataEnd => return Ok(contract_details),
                Ok(message) if message.message_type() == IncomingMessages::Error => return Err(Error::from(message)),
                Ok(message) => return Err(Error::unexpected_response(&message)),
                Err(e) => return Err(e),
            }
        }

        Err(Error::UnexpectedEndOfStream)
    }

    /// Cancels an in-flight contract details request.
    ///
    /// # Arguments
    /// * `request_id` - The request ID returned by a prior `contract_details` call.
    pub fn cancel_contract_details(&self, request_id: i32) -> Result<(), Error> {
        check_version(self.server_version, Features::CANCEL_CONTRACT_DATA)?;

        let message = encoders::encode_cancel_contract_data(request_id)?;
        self.send_message(message)?;
        Ok(())
    }

    /// Requests details about a given market rule
    ///
    /// The market rule for an instrument on a particular exchange provides details about how the minimum price increment changes with price.
    /// A list of market rule ids can be obtained by invoking [Self::contract_details()] for a particular contract.
    /// The returned market rule ID list will provide the market rule ID for the instrument in the correspond valid exchange list in [`crate::contracts::ContractDetails`].
    pub fn market_rule(&self, market_rule_id: i32) -> Result<MarketRule, Error> {
        check_version(self.server_version, Features::MARKET_RULES)?;

        let request = encoders::encode_request_market_rule(market_rule_id)?;
        let subscription = self.shared_request(OutgoingMessages::RequestMarketRule).send_raw(request)?;

        match subscription.next() {
            Some(Ok(mut message)) => Ok(decoders::decode_market_rule(&mut message)?),
            Some(Err(e)) => Err(e),
            None => Err(Error::UnexpectedEndOfStream),
        }
    }

    /// Requests the underlying exchanges that contribute to a consolidated (BBO) feed.
    ///
    /// Given a BBO exchange code (an opaque per-session token, e.g. `"a6"`),
    /// returns the list of underlying exchanges with each entry's bit
    /// position, full exchange name, and single-letter abbreviation. Useful
    /// for decoding the `mdSize` / `mdMask` bitmaps on tick-by-tick and
    /// market-depth streams. The token is typically obtained from the
    /// `LAST_EXCHANGE` market-data tick (tick type 84).
    ///
    /// # Arguments
    /// * `bbo_exchange` - The BBO exchange token (e.g. `"a6"`).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use ibapi::client::blocking::Client;
    ///
    /// let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
    ///
    /// let components = client.smart_components("a6").expect("request failed");
    /// for component in &components {
    ///     println!("bit {}: {} ({})", component.bit_number, component.exchange, component.exchange_letter);
    /// }
    /// ```
    pub fn smart_components(&self, bbo_exchange: &str) -> Result<Vec<SmartComponent>, Error> {
        check_version(self.server_version, Features::SMART_COMPONENTS)?;

        request_helpers::blocking::one_shot_request_with_retry(
            self,
            |request_id| encoders::encode_request_smart_components(request_id, bbo_exchange),
            decoders::decode_smart_components_message,
            || Err(Error::UnexpectedEndOfStream),
        )
    }

    /// Requests matching stock symbols.
    ///
    /// # Arguments
    /// * `pattern` - Either start of ticker symbol or (for larger strings) company name.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use ibapi::client::blocking::Client;
    ///
    /// let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
    ///
    /// let contracts = client.matching_symbols("IB").expect("request failed");
    /// for contract in contracts {
    ///     println!("contract: {contract:?}");
    /// }
    /// ```
    pub fn matching_symbols(&self, pattern: &str) -> Result<Vec<ContractDescription>, Error> {
        check_version(self.server_version, Features::REQ_MATCHING_SYMBOLS)?;

        let builder = self.request();
        let request_id = builder.request_id();
        let request = encoders::encode_request_matching_symbols(request_id, pattern)?;
        let subscription = builder.send_raw(request)?;

        if let Some(Ok(mut message)) = subscription.next() {
            match message.message_type() {
                IncomingMessages::SymbolSamples => {
                    return decoders::decode_contract_descriptions(self.server_version, &mut message);
                }
                IncomingMessages::Error => {
                    error!("unexpected error: {message:?}");
                    return Err(Error::unexpected_response(&message));
                }
                _ => {
                    info!("unexpected message: {message:?}");
                    return Err(Error::unexpected_response(&message));
                }
            }
        }

        Ok(Vec::new())
    }

    /// Calculates an option's price based on the provided volatility and its underlying's price.
    ///
    /// # Arguments
    /// * `contract`        - The [Contract] object representing the option for which the calculation is being requested.
    /// * `volatility`      - Hypothetical volatility as a percentage (e.g., 20.0 for 20%).
    /// * `underlying_price` - Hypothetical price of the underlying asset.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use ibapi::client::blocking::Client;
    /// use ibapi::contracts::{Contract, OptionRight};
    ///
    /// let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
    ///
    /// let contract = Contract::option("AAPL", "20251219", 150.0, OptionRight::Call);
    /// let calculation = client.calculate_option_price(&contract, 100.0, 235.0).expect("request failed");
    /// println!("calculation: {calculation:?}");
    /// ```
    pub fn calculate_option_price(&self, contract: &Contract, volatility: f64, underlying_price: f64) -> Result<OptionComputation, Error> {
        check_version(self.server_version, Features::REQ_CALC_OPTION_PRICE)?;

        let builder = self.request();
        let request_id = builder.request_id();
        let message = encoders::encode_calculate_option_price(request_id, contract, volatility, underlying_price)?;
        let subscription = builder.send_raw(message)?;

        match subscription.next() {
            Some(Ok(mut message)) => OptionComputation::decode(&self.decoder_context(), &mut message),
            Some(Err(e)) => Err(e),
            None => Err(Error::UnexpectedEndOfStream),
        }
    }

    /// Calculates the implied volatility based on the hypothetical option price and underlying price.
    ///
    /// # Arguments
    /// * `contract`        - The [Contract] object representing the option for which the calculation is being requested.
    /// * `option_price`    - Hypothetical option price.
    /// * `underlying_price` - Hypothetical price of the underlying asset.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use ibapi::client::blocking::Client;
    /// use ibapi::contracts::{Contract, OptionRight};
    ///
    /// let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
    ///
    /// let contract = Contract::option("AAPL", "20230519", 150.0, OptionRight::Call);
    /// let calculation = client.calculate_implied_volatility(&contract, 25.0, 235.0).expect("request failed");
    /// println!("calculation: {calculation:?}");
    /// ```
    pub fn calculate_implied_volatility(&self, contract: &Contract, option_price: f64, underlying_price: f64) -> Result<OptionComputation, Error> {
        check_version(self.server_version, Features::REQ_CALC_IMPLIED_VOLAT)?;

        let builder = self.request();
        let request_id = builder.request_id();
        let message = encoders::encode_calculate_implied_volatility(request_id, contract, option_price, underlying_price)?;
        let subscription = builder.send_raw(message)?;

        match subscription.next() {
            Some(Ok(mut message)) => OptionComputation::decode(&self.decoder_context(), &mut message),
            Some(Err(e)) => Err(e),
            None => Err(Error::UnexpectedEndOfStream),
        }
    }

    /// Requests security definition option parameters for viewing a contract's option chain.
    ///
    /// # Arguments
    /// `symbol`   - Contract symbol of the underlying.
    /// `exchange` - The exchange on which the returned options are trading. Can be set to the empty string for all exchanges.
    /// `security_type` - The type of the underlying security, i.e. STK
    /// `contract_id`   - The contract ID of the underlying security.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use ibapi::client::blocking::Client;
    /// use ibapi::contracts::SecurityType;
    ///
    /// let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
    ///
    /// let symbol = "AAPL";
    /// let exchange = ""; // all exchanges
    /// let security_type = SecurityType::Stock;
    /// let contract_id = 265598;
    ///
    /// let subscription = client
    ///     .option_chain(symbol, exchange, security_type, contract_id)
    ///     .expect("request option chain failed!");
    ///
    /// for option_chain in &subscription {
    ///     println!("{option_chain:?}")
    /// }
    /// ```
    pub fn option_chain(
        &self,
        symbol: &str,
        exchange: &str,
        security_type: SecurityType,
        contract_id: i32,
    ) -> Result<Subscription<OptionChain>, Error> {
        request_helpers::blocking::request_with_id(self, Features::SEC_DEF_OPT_PARAMS_REQ, |request_id| {
            encoders::encode_request_option_chain(request_id, symbol, exchange, security_type, contract_id)
        })
    }
}

#[cfg(test)]
#[path = "sync_tests.rs"]
mod tests;