pub struct MarketDataBuilder<'a, C> { /* private fields */ }Expand description
Builder for creating market data subscriptions with a fluent interface.
Defaults: no generic ticks, streaming (not snapshot), no regulatory
snapshot. Terminals: .subscribe(), or .snapshot_once(timeout) for a
collected one-shot snapshot.
Implementations§
Source§impl<'a, C> MarketDataBuilder<'a, C>
impl<'a, C> MarketDataBuilder<'a, C>
Sourcepub fn generic_ticks(self, ticks: &[&str]) -> Self
pub fn generic_ticks(self, ticks: &[&str]) -> Self
Replace the generic tick list to subscribe to
Each value is a numeric IB generic tick request ID (the
genericTickList parameter on reqMktData). To add ticks one at a
time, use Self::add_generic_tick instead.
§Arguments
ticks- Slice of generic tick request IDs. Prefer the named constants incrate::market_data::realtime::generic_tickover raw numeric strings.
§Examples
use ibapi::client::blocking::Client;
use ibapi::contracts::Contract;
use ibapi::market_data::realtime::generic_tick;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("AAPL").build();
let subscription = client
.market_data(&contract)
.generic_ticks(&[generic_tick::RT_VOLUME, generic_tick::SHORTABLE])
.subscribe()
.expect("subscription failed");See: https://interactivebrokers.github.io/tws-api/tick_types.html
Sourcepub fn add_generic_tick(self, tick: impl AsRef<str>) -> Self
pub fn add_generic_tick(self, tick: impl AsRef<str>) -> Self
Append a single generic tick ID to the subscription
Multiple calls accumulate; use Self::generic_ticks to replace the
list in one shot. Pairs naturally with conditional composition (e.g.
only add generic_tick::SHORTABLE for stocks). Prefer the named
constants over raw numeric strings.
See Self::subscribe for a runnable end-to-end example.
Sourcepub fn snapshot(self) -> Self
pub fn snapshot(self) -> Self
Request a one-time snapshot of market data
When enabled, the subscription will receive current market data once and then automatically end with a SnapshotEnd tick type.
Sourcepub fn regulatory_snapshot(self) -> Self
pub fn regulatory_snapshot(self) -> Self
Request regulatory snapshot
For U.S. stocks, a regulatory snapshot request requires the subscription of Market Data for US Securities and Futures Snapshot Bundle.
Source§impl<'a> MarketDataBuilder<'a, Client>
impl<'a> MarketDataBuilder<'a, Client>
Sourcepub fn subscribe(self) -> Result<Subscription<TickTypes>, Error>
pub fn subscribe(self) -> Result<Subscription<TickTypes>, Error>
Subscribe to market data
Returns a subscription that yields TickTypes as market data arrives.
§Examples
use ibapi::client::blocking::Client;
use ibapi::contracts::Contract;
use ibapi::market_data::realtime::{generic_tick, TickTypes};
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("AAPL").build();
let subscription = client.market_data(&contract)
.add_generic_tick(generic_tick::RT_VOLUME)
.add_generic_tick(generic_tick::SHORTABLE)
.subscribe()
.expect("subscription failed");
for tick in &subscription {
println!("{tick:?}");
}Sourcepub fn snapshot_once(self, timeout: Duration) -> Result<Vec<TickTypes>, Error>
pub fn snapshot_once(self, timeout: Duration) -> Result<Vec<TickTypes>, Error>
Request a one-shot snapshot and collect the resulting ticks.
Forces snapshot mode, subscribes, and collects every
tick until the snapshot completes (or timeout elapses), returning the
accumulated TickTypes for the caller to map into a domain struct.
This replaces the hand-written collect-with-timeout loop. See
Subscription::collect_for
for the underlying terminal and its stop conditions.
§Examples
use ibapi::client::blocking::Client;
use ibapi::contracts::Contract;
use std::time::Duration;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("AAPL").build();
let ticks = client.market_data(&contract).snapshot_once(Duration::from_secs(5)).expect("snapshot failed");
println!("collected {} ticks", ticks.len());Source§impl<'a> MarketDataBuilder<'a, Client>
impl<'a> MarketDataBuilder<'a, Client>
Sourcepub async fn subscribe(self) -> Result<Subscription<TickTypes>, Error>
pub async fn subscribe(self) -> Result<Subscription<TickTypes>, Error>
Subscribe to market data
Returns a subscription that yields TickTypes as market data arrives.
§Examples
use ibapi::market_data::realtime::generic_tick;
use ibapi::prelude::*;
#[tokio::main]
async fn main() {
let client = Client::connect("127.0.0.1:4002", 100).await.expect("connection failed");
let contract = Contract::stock("AAPL").build();
let mut subscription = client.market_data(&contract)
.add_generic_tick(generic_tick::RT_VOLUME)
.add_generic_tick(generic_tick::SHORTABLE)
.subscribe()
.await
.expect("subscription failed");
while let Some(tick) = subscription.next().await {
println!("{tick:?}");
}
}Sourcepub async fn snapshot_once(
self,
timeout: Duration,
) -> Result<Vec<TickTypes>, Error>
pub async fn snapshot_once( self, timeout: Duration, ) -> Result<Vec<TickTypes>, Error>
Request a one-shot snapshot and collect the resulting ticks.
Forces snapshot mode, subscribes, and collects every
tick until the snapshot completes (or timeout elapses), returning the
accumulated TickTypes for the caller to map into a domain struct.
This replaces the hand-written collect-with-timeout loop. See
Subscription::collect_for
for the underlying terminal and its stop conditions.
§Examples
use ibapi::prelude::*;
use std::time::Duration;
#[tokio::main]
async fn main() {
let client = Client::connect("127.0.0.1:4002", 100).await.expect("connection failed");
let contract = Contract::stock("AAPL").build();
let ticks = client.market_data(&contract).snapshot_once(Duration::from_secs(5)).await.expect("snapshot failed");
println!("collected {} ticks", ticks.len());
}