pub struct ContractBuilder { /* private fields */ }Expand description
Builder for creating and validating Contract instances
The ContractBuilder provides a fluent interface for constructing contracts with validation. It ensures that contracts are properly configured for their security type and prevents common errors through compile-time and runtime validation.
§Examples
§Creating a Stock Contract
use ibapi::contracts::{ContractBuilder, SecurityType};
// Using the builder pattern
let contract = ContractBuilder::new()
.symbol("AAPL")
.security_type(SecurityType::Stock)
.exchange("SMART")
.currency("USD")
.build()?;
// Using the convenience method
let contract = ContractBuilder::stock("AAPL", "SMART", "USD").build()?;§Creating an Option Contract
use ibapi::contracts::{ContractBuilder, OptionRight, SecurityType};
let contract = ContractBuilder::option("AAPL", "SMART", "USD")
.strike(150.0)
.right(OptionRight::Call)
.last_trade_date_or_contract_month("20241220")
.build()?;§Creating a Futures Contract
use ibapi::contracts::{ContractBuilder, SecurityType};
let contract = ContractBuilder::futures("ES", "CME", "USD")
.last_trade_date_or_contract_month("202412")
.build()?;§Creating a Crypto Contract
use ibapi::contracts::{ContractBuilder, SecurityType};
let contract = ContractBuilder::crypto("BTC", "PAXOS", "USD").build()?;§Validation
The builder performs validation when build is called:
- Symbol is always required
- Option contracts require strike, right, and expiration date
- Futures contracts require contract month
- Strike prices cannot be negative
Implementations§
Source§impl ContractBuilder
impl ContractBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new ContractBuilder
§Examples
use ibapi::contracts::{ContractBuilder, SecurityType};
let contract = ContractBuilder::new()
.symbol("MSFT")
.security_type(SecurityType::Stock)
.exchange("SMART")
.currency("USD")
.build()?;Sourcepub fn contract_id(self, contract_id: i32) -> Self
pub fn contract_id(self, contract_id: i32) -> Self
Sets the contract ID
The unique IB contract identifier. When specified, other contract details may be optional.
Sourcepub fn symbol<S: Into<String>>(self, symbol: S) -> Self
pub fn symbol<S: Into<String>>(self, symbol: S) -> Self
Sets the underlying asset symbol
Required field for all contracts.
§Examples
- Stocks: “AAPL”, “MSFT”, “TSLA”
- Futures: “ES”, “NQ”, “CL”
- Crypto: “BTC”, “ETH”
Sourcepub fn security_type(self, security_type: SecurityType) -> Self
pub fn security_type(self, security_type: SecurityType) -> Self
Sets the security type
Defines what type of instrument this contract represents. See SecurityType for available options.
Sourcepub fn last_trade_date_or_contract_month<S: Into<String>>(self, date: S) -> Self
pub fn last_trade_date_or_contract_month<S: Into<String>>(self, date: S) -> Self
Sets the last trade date or contract month
For futures and options, this field is required:
- Format YYYYMM for contract month (e.g., “202412”)
- Format YYYYMMDD for specific expiration date (e.g., “20241220”)
Sourcepub fn strike(self, strike: f64) -> Self
pub fn strike(self, strike: f64) -> Self
Sets the option’s strike price
Required for option contracts. Must be a positive value.
§Examples
use ibapi::contracts::{ContractBuilder, OptionRight};
let contract = ContractBuilder::option("AAPL", "SMART", "USD")
.strike(150.0)
.right(OptionRight::Call)
.last_trade_date_or_contract_month("20241220")
.build()?;Sourcepub fn right(self, right: OptionRight) -> Self
pub fn right(self, right: OptionRight) -> Self
Sets the option right (Call or Put).
Required for option contracts.
Sourcepub fn multiplier<S: Into<String>>(self, multiplier: S) -> Self
pub fn multiplier<S: Into<String>>(self, multiplier: S) -> Self
Sets the contract multiplier
For example, options on futures often have a multiplier that differs from the underlying future’s multiplier.
Sourcepub fn exchange<S: Into<String>>(self, exchange: S) -> Self
pub fn exchange<S: Into<String>>(self, exchange: S) -> Self
Sets the exchange for routing orders
Common values include:
- “SMART” for IB’s smart routing
- “NASDAQ”, “NYSE”, “AMEX” for US equities
- “CME”, “NYMEX” for futures
Sourcepub fn currency<S: Into<String>>(self, currency: S) -> Self
pub fn currency<S: Into<String>>(self, currency: S) -> Self
Sets the contract’s currency
Typically “USD” for US markets, “EUR” for European markets, etc.
Sourcepub fn local_symbol<S: Into<String>>(self, local_symbol: S) -> Self
pub fn local_symbol<S: Into<String>>(self, local_symbol: S) -> Self
Sets the local symbol
The symbol within the exchange. Often the same as symbol but can differ for futures and options contracts.
Sourcepub fn primary_exchange<S: Into<String>>(self, primary_exchange: S) -> Self
pub fn primary_exchange<S: Into<String>>(self, primary_exchange: S) -> Self
Sets the primary exchange
The primary listing exchange. Used with SMART routing to define the contract unambiguously.
Sourcepub fn trading_class<S: Into<String>>(self, trading_class: S) -> Self
pub fn trading_class<S: Into<String>>(self, trading_class: S) -> Self
Sets the trading class
For example, options traded on different exchanges may have different trading class names despite being otherwise identical.
Sourcepub fn include_expired(self, include_expired: bool) -> Self
pub fn include_expired(self, include_expired: bool) -> Self
Sets whether to include expired contracts
If true, contract details requests can return expired contracts.
Sourcepub fn security_id_type(self, security_id_type: SecurityIdType) -> Self
pub fn security_id_type(self, security_id_type: SecurityIdType) -> Self
Sets the security ID scheme paired with security_id.
Sourcepub fn security_id<S: Into<String>>(self, security_id: S) -> Self
pub fn security_id<S: Into<String>>(self, security_id: S) -> Self
Sets the security ID
The actual security identifier value corresponding to the security_id_type.
Sourcepub fn combo_legs_description<S: Into<String>>(
self,
combo_legs_description: S,
) -> Self
pub fn combo_legs_description<S: Into<String>>( self, combo_legs_description: S, ) -> Self
Sets the combo legs description
For combo orders, provides a human-readable description of the legs.
Sourcepub fn combo_legs(self, combo_legs: Vec<ComboLeg>) -> Self
pub fn combo_legs(self, combo_legs: Vec<ComboLeg>) -> Self
Sets the combo legs
Defines the individual legs for combo/spread contracts.
Sourcepub fn delta_neutral_contract(
self,
delta_neutral_contract: DeltaNeutralContract,
) -> Self
pub fn delta_neutral_contract( self, delta_neutral_contract: DeltaNeutralContract, ) -> Self
Sets the delta neutral contract
Used for delta-neutral combo orders.
Sourcepub fn last_trade_date(self, date: Date) -> Self
pub fn last_trade_date(self, date: Date) -> Self
Sets the last trade date.
Server responses overwrite this on contract-details round-trips, so most callers can leave it unset.
Sourcepub fn issuer_id<S: Into<String>>(self, issuer_id: S) -> Self
pub fn issuer_id<S: Into<String>>(self, issuer_id: S) -> Self
Sets the issuer ID
Primarily used for bond contracts.
Sourcepub fn description<S: Into<String>>(self, description: S) -> Self
pub fn description<S: Into<String>>(self, description: S) -> Self
Sets the contract description
Human-readable description of the contract.
Sourcepub fn stock<S: Into<String>>(symbol: S, exchange: S, currency: S) -> Self
pub fn stock<S: Into<String>>(symbol: S, exchange: S, currency: S) -> Self
Creates a stock contract builder with common defaults
§Examples
use ibapi::contracts::ContractBuilder;
let contract = ContractBuilder::stock("AAPL", "SMART", "USD")
.primary_exchange("NASDAQ")
.build()?;Sourcepub fn option<S: Into<String>>(symbol: S, exchange: S, currency: S) -> Self
pub fn option<S: Into<String>>(symbol: S, exchange: S, currency: S) -> Self
Creates an option contract builder with common defaults
§Examples
use ibapi::contracts::{ContractBuilder, OptionRight};
let contract = ContractBuilder::option("AAPL", "SMART", "USD")
.strike(150.0)
.right(OptionRight::Call)
.last_trade_date_or_contract_month("20241220")
.build()?;Sourcepub fn futures<S: Into<String>>(symbol: S, exchange: S, currency: S) -> Self
pub fn futures<S: Into<String>>(symbol: S, exchange: S, currency: S) -> Self
Creates a futures contract builder with common defaults
§Examples
use ibapi::contracts::ContractBuilder;
let contract = ContractBuilder::futures("ES", "CME", "USD")
.last_trade_date_or_contract_month("202412")
.multiplier("50")
.build()?;Sourcepub fn continuous_futures<S: Into<String>>(
symbol: S,
exchange: S,
currency: S,
) -> Self
pub fn continuous_futures<S: Into<String>>( symbol: S, exchange: S, currency: S, ) -> Self
Creates a continuous futures contract builder with common defaults
§Examples
use ibapi::contracts::ContractBuilder;
let contract = ContractBuilder::continuous_futures("ES", "CME", "USD")
.multiplier("50")
.build()?;Trait Implementations§
Source§impl Clone for ContractBuilder
impl Clone for ContractBuilder
Source§fn clone(&self) -> ContractBuilder
fn clone(&self) -> ContractBuilder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more