Skip to main content

ContractBuilder

Struct ContractBuilder 

Source
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

Source

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()?;
Source

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.

Source

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”
Source

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.

Source

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”)
Source

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()?;
Source

pub fn right(self, right: OptionRight) -> Self

Sets the option right (Call or Put).

Required for option contracts.

Source

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.

Source

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
Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn include_expired(self, include_expired: bool) -> Self

Sets whether to include expired contracts

If true, contract details requests can return expired contracts.

Source

pub fn security_id_type(self, security_id_type: SecurityIdType) -> Self

Sets the security ID scheme paired with security_id.

Source

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.

Source

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.

Source

pub fn combo_legs(self, combo_legs: Vec<ComboLeg>) -> Self

Sets the combo legs

Defines the individual legs for combo/spread contracts.

Source

pub fn delta_neutral_contract( self, delta_neutral_contract: DeltaNeutralContract, ) -> Self

Sets the delta neutral contract

Used for delta-neutral combo orders.

Source

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.

Source

pub fn issuer_id<S: Into<String>>(self, issuer_id: S) -> Self

Sets the issuer ID

Primarily used for bond contracts.

Source

pub fn description<S: Into<String>>(self, description: S) -> Self

Sets the contract description

Human-readable description of the contract.

Source

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()?;
Source

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()?;
Source

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()?;
Source

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()?;
Source

pub fn crypto<S: Into<String>>(symbol: S, exchange: S, currency: S) -> Self

Creates a crypto contract builder with common defaults

§Examples
use ibapi::contracts::ContractBuilder;

let contract = ContractBuilder::crypto("BTC", "PAXOS", "USD").build()?;
Source

pub fn build(self) -> Result<Contract, Error>

Builds the final Contract instance with validation

§Errors

Returns an error if:

  • Symbol is not provided
  • Option contracts are missing required fields (strike, right, expiration)
  • Futures contracts are missing contract month
  • Strike price is negative

Trait Implementations§

Source§

impl Clone for ContractBuilder

Source§

fn clone(&self) -> ContractBuilder

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ContractBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ContractBuilder

Source§

fn default() -> ContractBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.