Skip to main content

MarketDataBuilder

Struct MarketDataBuilder 

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

Source

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
§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

Source

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.

Source

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.

Source

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

pub fn streaming(self) -> Self

Enable real-time streaming data (default)

This is the default behavior - data will stream continuously until the subscription is cancelled.

Source§

impl<'a> MarketDataBuilder<'a, Client>

Source

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:?}");
}
Source

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>

Source

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:?}");
    }
}
Source

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());
}

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<'a, C> UnwindSafe for MarketDataBuilder<'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.