use crate::client::ids::RequestId;
use crate::contracts::{OptionChain, SecurityType};
use crate::Error;
#[must_use = "OptionChainBuilder does nothing until you call .subscribe()"]
pub struct OptionChainBuilder<'a, C> {
client: &'a C,
symbol: &'a str,
security_type: SecurityType,
contract_id: i32,
exchange: Option<&'a str>,
request_id: RequestId,
buffer_limit: Option<usize>,
}
impl<'a, C> OptionChainBuilder<'a, C> {
pub(crate) fn new(client: &'a C, symbol: &'a str, security_type: SecurityType, contract_id: i32, request_id: RequestId) -> Self {
Self {
client,
symbol,
security_type,
contract_id,
exchange: None,
request_id,
buffer_limit: None,
}
}
pub fn request_id(&self) -> i32 {
self.request_id.raw()
}
pub fn exchange(mut self, exchange: &'a str) -> Self {
self.exchange = Some(exchange);
self
}
#[cfg_attr(
feature = "sync",
doc = r#"
```no_run
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}"),
}
}
```
"#
)]
#[cfg_attr(
feature = "async",
doc = r#"
```no_run
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}"),
}
}
}
```
"#
)]
pub fn buffer_limit(mut self, limit: usize) -> Self {
self.buffer_limit = Some(limit);
self
}
}
#[cfg(feature = "sync")]
impl<'a> OptionChainBuilder<'a, crate::client::sync::Client> {
pub fn subscribe(self) -> Result<crate::subscriptions::sync::Subscription<OptionChain>, Error> {
crate::contracts::sync::option_chain(
self.client,
self.symbol,
self.exchange,
self.security_type,
self.contract_id,
self.request_id,
self.buffer_limit,
)
}
}
#[cfg(feature = "async")]
impl<'a> OptionChainBuilder<'a, crate::client::r#async::Client> {
pub async fn subscribe(self) -> Result<crate::subscriptions::r#async::Subscription<OptionChain>, Error> {
crate::contracts::r#async::option_chain(
self.client,
self.symbol,
self.exchange,
self.security_type,
self.contract_id,
self.request_id,
self.buffer_limit,
)
.await
}
}