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>
impl<'a, C> OptionChainBuilder<'a, C>
Sourcepub fn request_id(&self) -> i32
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.
Sourcepub fn exchange(self, exchange: &'a str) -> Self
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.
Sourcepub fn buffer_limit(self, limit: usize) -> Self
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>
impl<'a> OptionChainBuilder<'a, Client>
Sourcepub fn subscribe(self) -> Result<Subscription<OptionChain>, Error>
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>
impl<'a> OptionChainBuilder<'a, Client>
Sourcepub async fn subscribe(self) -> Result<Subscription<OptionChain>, Error>
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");
}