Skip to main content

OptionChainBuilder

Struct OptionChainBuilder 

Source
pub struct OptionChainBuilder<'a, C> { /* private fields */ }
Expand description

Builder for an underlying’s option chain: one OptionChain per exchange the options trade on.

The request id is allocated when the builder is made, so a caller can record it before anything is written; subscribe sends once and never retries. TWS has no cancel for this request: dropping the subscription early only discards the rest of the chain.

Implementations§

Source§

impl<'a, C> OptionChainBuilder<'a, C>

Source

pub fn request_id(&self) -> i32

The request id subscribe will send. Allocated when the builder was made; nothing has been written yet. A dropped builder skips the id.

Source

pub fn exchange(self, exchange: &'a str) -> Self

Restrict the chain to options trading on one exchange.

This is TWS’s futFopExchange: it selects the exchange for futures options. For a stock underlying leave it unset — TWS then returns one OptionChain per listing exchange (twenty for AAPL), whereas naming any of them, SMART included, returns an empty chain.

Source

pub fn buffer_limit(self, limit: usize) -> Self

Fail the stream instead of queueing more than limit unread chains.

When limit items are unread and another arrives, the subscription yields every queued item, then Error::BufferLimitExceeded, then ends. The end marker and errors always get through. Same semantics and caveats as ContractDetailsBuilder::buffer_limit; limit must be 1..=MAX_BUFFER_LIMIT.

§Examples
use ibapi::client::blocking::Client;
use ibapi::contracts::SecurityType;
use ibapi::Error;

let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");

let request = client.option_chain("AAPL", SecurityType::Stock, 265598).buffer_limit(8);
println!("request id: {}", request.request_id());

let subscription = request.subscribe().expect("request option chain failed");
for chain in subscription.iter_data() {
    match chain {
        Ok(chain) => println!("{}: {} strikes", chain.exchange, chain.strikes.len()),
        Err(Error::BufferLimitExceeded { limit }) => eprintln!("fell {limit} chains behind; stopping"),
        Err(e) => eprintln!("error: {e}"),
    }
}
use ibapi::prelude::*;
use ibapi::Error;

#[tokio::main]
async fn main() {
    let client = Client::connect("127.0.0.1:4002", 100).await.expect("connection failed");

    let request = client.option_chain("AAPL", SecurityType::Stock, 265598).buffer_limit(8);
    println!("request id: {}", request.request_id());

    let subscription = request.subscribe().await.expect("request option chain failed");
    let mut chains = subscription.filter_data();
    while let Some(chain) = chains.next().await {
        match chain {
            Ok(chain) => println!("{}: {} strikes", chain.exchange, chain.strikes.len()),
            Err(Error::BufferLimitExceeded { limit }) => eprintln!("fell {limit} chains behind; stopping"),
            Err(e) => eprintln!("error: {e}"),
        }
    }
}
Source§

impl<'a> OptionChainBuilder<'a, Client>

Source

pub fn subscribe(self) -> Result<Subscription<OptionChain>, Error>

Submit the request once and return a subscription yielding one OptionChain per exchange. The subscription ends when TWS has sent every exchange. There is no retry: after a connection reset, build a new request (new id).

§Examples
use ibapi::client::blocking::Client;
use ibapi::contracts::SecurityType;

let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");

// Every exchange listing AAPL options (contract id 265598):
let subscription = client
    .option_chain("AAPL", SecurityType::Stock, 265598)
    .subscribe()
    .expect("request option chain failed");

for chain in subscription.iter_data() {
    let chain = chain.expect("decode error");
    println!("{}: {} expirations, {} strikes", chain.exchange, chain.expirations.len(), chain.strikes.len());
}

// Futures options on one exchange:
let subscription = client
    .option_chain("ES", SecurityType::Future, 495512563)
    .exchange("CME")
    .subscribe()
    .expect("request option chain failed");
Source§

impl<'a> OptionChainBuilder<'a, Client>

Source

pub async fn subscribe(self) -> Result<Subscription<OptionChain>, Error>

Submit the request once and return a subscription yielding one OptionChain per exchange. The subscription ends when TWS has sent every exchange. There is no retry: after a connection reset, build a new request (new id).

§Examples
use ibapi::prelude::*;

#[tokio::main]
async fn main() {
    let client = Client::connect("127.0.0.1:4002", 100).await.expect("connection failed");

    // Every exchange listing AAPL options (contract id 265598):
    let subscription = client
        .option_chain("AAPL", SecurityType::Stock, 265598)
        .subscribe()
        .await
        .expect("request option chain failed");

    let mut chains = subscription.filter_data();
    while let Some(chain) = chains.next().await {
        let chain = chain.expect("decode error");
        println!("{}: {} expirations, {} strikes", chain.exchange, chain.expirations.len(), chain.strikes.len());
    }

    // Futures options on one exchange:
    let subscription = client
        .option_chain("ES", SecurityType::Future, 495512563)
        .exchange("CME")
        .subscribe()
        .await
        .expect("request option chain failed");
}

Auto Trait Implementations§

§

impl<'a, C> Freeze for OptionChainBuilder<'a, C>
where &'a C: Freeze,

§

impl<'a, C> RefUnwindSafe for OptionChainBuilder<'a, C>

§

impl<'a, C> Send for OptionChainBuilder<'a, C>
where &'a C: Send,

§

impl<'a, C> Sync for OptionChainBuilder<'a, C>
where &'a C: Sync,

§

impl<'a, C> Unpin for OptionChainBuilder<'a, C>
where &'a C: Unpin,

§

impl<'a, C> UnsafeUnpin for OptionChainBuilder<'a, C>

§

impl<'a, C> UnwindSafe for OptionChainBuilder<'a, C>

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.