Skip to main content

InteractiveBrokersInstrumentProvider

Struct InteractiveBrokersInstrumentProvider 

Source
pub struct InteractiveBrokersInstrumentProvider { /* private fields */ }
Expand description

Interactive Brokers instrument provider.

This provider fetches contract details from Interactive Brokers using the rust-ibapi library and converts them to NautilusTrader instruments.

Implementations§

Source§

impl InteractiveBrokersInstrumentProvider

Source

pub fn new(config: InteractiveBrokersInstrumentProviderConfig) -> Self

Create a new InteractiveBrokersInstrumentProvider.

§Arguments
  • config - Configuration for the provider
Source

pub async fn initialize(&self) -> Result<()>

Initialize the provider by loading cache if configured.

This is equivalent to Python’s provider.initialize() method. It loads instruments from cache if cache_path is configured and cache is valid.

§Errors

Returns an error if cache loading fails.

Source

pub fn determine_venue( &self, contract: &Contract, contract_details: Option<&ContractDetails>, ) -> Venue

Determine venue from contract using provider configuration.

This is equivalent to Python’s determine_venue_from_contract method. It uses the config’s symbol-to-venue mapping and exchange-to-venue conversion settings.

§Arguments
  • contract - The IB contract
§Returns

The determined venue.

Source

pub fn symbology_method(&self) -> SymbologyMethod

Get the symbology method from the provider configuration.

Source

pub fn find(&self, instrument_id: &InstrumentId) -> Option<InstrumentAny>

Get an instrument by its ID.

§Arguments
  • instrument_id - The instrument ID to look up
§Returns

Returns the instrument if found, None otherwise.

Source

pub fn find_by_contract_id(&self, contract_id: i32) -> Option<InstrumentAny>

Get an instrument by contract ID.

§Arguments
  • contract_id - The IB contract ID to look up
§Returns

Returns the instrument if found, None otherwise.

Source

pub fn get_instrument_id_by_contract_id( &self, contract_id: i32, ) -> Option<InstrumentId>

Get an instrument ID by contract ID.

§Arguments
  • contract_id - The IB contract ID to look up
§Returns

Returns the instrument ID if found, None otherwise.

Source

pub fn is_filtered_sec_type(&self, sec_type: &str) -> bool

Check if a security type should be filtered.

§Arguments
  • sec_type - The security type to check
§Returns

Returns true if the security type should be filtered.

Source

pub fn get_all(&self) -> Vec<InstrumentAny>

Get all cached instruments.

§Returns

Returns a vector of all cached instruments.

Source

pub fn count(&self) -> usize

Get the number of cached instruments.

§Returns

Returns the number of cached instruments.

Source

pub fn get_price_magnifier(&self, instrument_id: &InstrumentId) -> i32

Get price magnifier for an instrument ID.

Price magnifier allows execution and strike prices to be reported consistently with market data and historical data.

This method first checks the dedicated price magnifier cache for fast lookup. If not found, it falls back to checking contract details. If still not found, it returns the default value of 1 and logs a warning if the instrument exists.

§Arguments
  • instrument_id - The instrument ID to look up
§Returns

Returns the price magnifier if found, otherwise 1.

Source

pub async fn get_instrument( &self, client: &Client, contract: &Contract, ) -> Result<Option<InstrumentAny>>

Get an instrument by IB Contract.

This is equivalent to Python’s get_instrument method. Supports BAG contracts by auto-loading legs.

§Arguments
  • client - The IB API client
  • contract - The IB contract to get instrument for
§Returns

Returns the instrument if found, None otherwise.

§Errors

Returns an error if fetching fails.

Source

pub fn instrument_id_to_ib_contract_details( &self, instrument_id: &InstrumentId, ) -> Option<ContractDetails>

Convert an instrument ID to IB contract details.

This is equivalent to Python’s instrument_id_to_ib_contract_details method.

§Arguments
  • instrument_id - The instrument ID to convert
§Returns

Returns the contract details if found, None otherwise.

Source

pub fn instrument_id_to_ib_contract( &self, instrument_id: &InstrumentId, ) -> Option<Contract>

Source

pub fn resolve_contract_for_instrument( &self, instrument_id: InstrumentId, ) -> Result<Contract>

Source

pub async fn load_async( &self, client: &Client, instrument_id: InstrumentId, filters: Option<HashMap<String, String>>, ) -> Result<()>

Load a single instrument (does not return loaded IDs).

This is equivalent to Python’s load_async method.

§Arguments
  • client - The IB API client
  • instrument_id - The instrument ID to load
  • force_instrument_update - If true, force re-fetch even if already cached
§Errors

Returns an error if loading fails.

Source

pub async fn load_with_return_async( &self, client: &Client, instrument_id: InstrumentId, filters: Option<HashMap<String, String>>, ) -> Result<Option<InstrumentId>>

Load a single instrument and return the loaded instrument ID.

This is equivalent to Python’s load_with_return_async method.

§Arguments
  • client - The IB API client
  • instrument_id - The instrument ID to load
  • force_instrument_update - If true, force re-fetch even if already cached
§Returns

Returns the loaded instrument ID if successful, None otherwise.

§Errors

Returns an error if loading fails.

Source

pub async fn load_ids_async( &self, client: &Client, instrument_ids: Vec<InstrumentId>, filters: Option<HashMap<String, String>>, ) -> Result<()>

Load multiple instruments (does not return loaded IDs).

This is equivalent to Python’s load_ids_async method.

§Arguments
  • client - The IB API client
  • instrument_ids - Vector of instrument IDs to load
  • force_instrument_update - If true, force re-fetch even if already cached
§Errors

Returns an error if loading fails.

Source

pub async fn load_ids_with_return_async( &self, client: &Client, instrument_ids: Vec<InstrumentId>, filters: Option<HashMap<String, String>>, ) -> Result<Vec<InstrumentId>>

Load multiple instruments and return the loaded instrument IDs.

This is equivalent to Python’s load_ids_with_return_async method.

§Arguments
  • client - The IB API client
  • instrument_ids - Vector of instrument IDs to load
  • force_instrument_update - If true, force re-fetch even if already cached
§Returns

Returns a vector of successfully loaded instrument IDs.

§Errors

Returns an error if loading fails.

Source

pub async fn fetch_spread_instrument( &self, client: &Client, spread_instrument_id: InstrumentId, force_instrument_update: bool, filters: Option<HashMap<String, String>>, ) -> Result<bool>

Fetch a spread instrument by loading its individual legs.

This is equivalent to Python’s _fetch_spread_instrument method. It parses the spread instrument ID to extract leg tuples, loads each leg, and then creates the spread instrument.

§Arguments
  • client - The IB API client
  • spread_instrument_id - The spread instrument ID to fetch
  • force_instrument_update - If true, force re-fetch even if already cached
§Returns

Returns true if the spread instrument was successfully loaded, false otherwise.

§Errors

Returns an error if parsing or loading fails.

Source

pub async fn load_all_async( &self, client: &Client, instrument_ids: Option<Vec<InstrumentId>>, contracts: Option<Vec<Contract>>, force_instrument_update: bool, ) -> Result<Vec<InstrumentId>>

Load all instruments from provided IDs and contracts.

This is equivalent to Python’s load_all_async method. Python version loads from config’s _load_ids_on_start and _load_contracts_on_start. Rust version accepts these as parameters for flexibility.

§Arguments
  • client - The IB API client
  • instrument_ids - Optional vector of instrument IDs to load
  • contracts - Optional vector of IB contracts to load
  • force_instrument_update - If true, force re-fetch even if already cached
§Errors

Returns an error if loading fails.

Source§

impl InteractiveBrokersInstrumentProvider

Source

pub async fn fetch_contract_details( &self, client: &Client, instrument_id: InstrumentId, force_instrument_update: bool, filters: Option<HashMap<String, String>>, ) -> Result<()>

Fetch and cache contract details for an instrument ID using the provided IB client.

§Arguments
  • client - The IB API client
  • instrument_id - The instrument ID to fetch
§Errors

Returns an error if fetching fails.

Source

pub async fn batch_load( &self, client: &Client, instrument_ids: Vec<InstrumentId>, filters: Option<&[String]>, ) -> Result<Vec<InstrumentId>>

Batch load multiple instrument IDs.

This method fetches and caches contract details for multiple instrument IDs in parallel.

§Arguments
  • client - The IB API client
  • instrument_ids - Vector of instrument IDs to load
  • filters - Optional filters to apply (not yet implemented, reserved for future use)
§Returns

Returns a vector of successfully loaded instrument IDs.

§Errors

Returns an error if fetching fails.

Source

pub async fn fetch_option_chain_by_range( &self, client: &Client, underlying: &Contract, expiry_min: Option<&str>, expiry_max: Option<&str>, ) -> Result<usize>

Fetch option chain for a given underlying contract with expiry filtering.

This is equivalent to Python’s get_option_chain_details_by_range. It uses contract_details to fetch options with precise expiry filtering, which is more flexible than the basic option_chain API.

§Arguments
  • client - The IB API client
  • underlying - The underlying contract
  • expiry_min - Minimum expiry date string (YYYYMMDD format, can be None for no min)
  • expiry_max - Maximum expiry date string (YYYYMMDD format, can be None for no max)
§Returns

Returns the number of option instruments loaded.

§Errors

Returns an error if fetching fails.

Source

pub async fn fetch_futures_chain( &self, client: &Client, symbol: &str, exchange: &str, currency: &str, min_expiry_days: Option<u32>, max_expiry_days: Option<u32>, ) -> Result<usize>

Fetch and cache futures chain (all futures contracts for a symbol).

This method fetches all futures contracts for a given underlying symbol and populates the cache with all individual futures instruments.

§Arguments
  • client - The IB API client
  • symbol - The underlying symbol
  • exchange - The exchange (use “” for all exchanges)
  • currency - The currency (use USD as default)
§Returns

Returns the number of futures instruments loaded.

§Errors

Returns an error if fetching fails.

Source

pub async fn fetch_bag_contract( &self, client: &Client, bag_contract: &Contract, ) -> Result<usize>

Fetch and cache a BAG (spread) contract.

This method fetches contract details for a spread contract by requesting contract details with a BAG contract. The BAG contract should have its combo_legs populated with the individual leg contract IDs.

§Arguments
  • client - The IB API client
  • bag_contract - The BAG contract with populated combo_legs
§Returns

Returns the number of spread instruments loaded (0 or 1).

§Errors

Returns an error if fetching fails.

§Notes

This method now auto-loads all leg instruments from combo_legs and creates a proper spread instrument, matching Python’s _load_bag_contract behavior.

Source

pub async fn save_cache(&self, cache_path: &str) -> Result<()>

Save the current instrument cache to disk.

§Arguments
  • cache_path - Path to the cache file
§Errors

Returns an error if serialization or file I/O fails.

Source

pub async fn load_cache(&self, cache_path: &str) -> Result<bool>

Load instrument cache from disk if valid.

§Arguments
  • cache_path - Path to the cache file
§Returns

Returns true if cache was loaded successfully and is valid, false otherwise.

§Errors

Returns an error if deserialization or file I/O fails (but treats missing file as non-error).

Trait Implementations§

Source§

impl Clone for InteractiveBrokersInstrumentProvider

Source§

fn clone(&self) -> InteractiveBrokersInstrumentProvider

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for InteractiveBrokersInstrumentProvider

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, 'py> FromPyObject<'a, 'py> for InteractiveBrokersInstrumentProvider
where Self: Clone,

Source§

type Error = PyClassGuardError<'a, 'py>

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

fn extract( obj: Borrowed<'a, 'py, PyAny>, ) -> Result<Self, <Self as FromPyObject<'a, 'py>>::Error>

Extracts Self from the bound smart pointer obj. Read more
Source§

impl<'py> IntoPyObject<'py> for InteractiveBrokersInstrumentProvider

Source§

type Target = InteractiveBrokersInstrumentProvider

The Python output type
Source§

type Output = Bound<'py, <InteractiveBrokersInstrumentProvider as IntoPyObject<'py>>::Target>

The smart pointer type to use. Read more
Source§

type Error = PyErr

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

fn into_pyobject( self, py: Python<'py>, ) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>

Performs the conversion.
Source§

impl PyClass for InteractiveBrokersInstrumentProvider

Source§

const NAME: &str = "InteractiveBrokersInstrumentProvider"

Name of the class. Read more
Source§

type Frozen = False

Whether the pyclass is frozen. Read more
Source§

impl PyClassImpl for InteractiveBrokersInstrumentProvider

Source§

const MODULE: Option<&str>

Module which the class will be associated with. Read more
Source§

const IS_BASETYPE: bool = false

#[pyclass(subclass)]
Source§

const IS_SUBCLASS: bool = false

#[pyclass(extends=…)]
Source§

const IS_MAPPING: bool = false

#[pyclass(mapping)]
Source§

const IS_SEQUENCE: bool = false

#[pyclass(sequence)]
Source§

const IS_IMMUTABLE_TYPE: bool = false

#[pyclass(immutable_type)]
Source§

const RAW_DOC: &'static CStr = /// Interactive Brokers instrument provider. /// /// This provider fetches contract details from Interactive Brokers using the `rust-ibapi` library /// and converts them to NautilusTrader instruments.

Docstring for the class provided on the struct or enum. Read more
Source§

const DOC: &'static CStr

Fully rendered class doc, including the text_signature if a constructor is defined. Read more
Source§

type Layout = <<InteractiveBrokersInstrumentProvider as PyClassImpl>::BaseNativeType as PyClassBaseType>::Layout<InteractiveBrokersInstrumentProvider>

Description of how this class is laid out in memory
Source§

type BaseType = PyAny

Base class
Source§

type ThreadChecker = ThreadCheckerImpl

This handles following two situations: Read more
Source§

type Inventory = Pyo3MethodsInventoryForInteractiveBrokersInstrumentProvider

Source§

type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild

Immutable or mutable
Source§

type Dict = PyClassDummySlot

Specify this class has #[pyclass(dict)] or not.
Source§

type WeakRef = PyClassDummySlot

Specify this class has #[pyclass(weakref)] or not.
Source§

type BaseNativeType = PyAny

The closest native ancestor. This is PyAny by default, and when you declare #[pyclass(extends=PyDict)], it’s PyDict.
Source§

fn items_iter() -> PyClassItemsIter

Source§

fn lazy_type_object() -> &'static LazyTypeObject<Self>

§

fn dict_offset() -> Option<PyObjectOffset>

Used to provide the dictoffset slot (equivalent to tp_dictoffset)
§

fn weaklist_offset() -> Option<PyObjectOffset>

Used to provide the weaklistoffset slot (equivalent to tp_weaklistoffset
Source§

impl PyClassNewTextSignature for InteractiveBrokersInstrumentProvider

Source§

const TEXT_SIGNATURE: &'static str = "(config)"

Source§

impl PyTypeInfo for InteractiveBrokersInstrumentProvider

Source§

const NAME: &str = <Self as ::pyo3::PyClass>::NAME

👎Deprecated since 0.28.0:

prefer using ::type_object(py).name() to get the correct runtime value

Class name.
Source§

const MODULE: Option<&str> = <Self as ::pyo3::impl_::pyclass::PyClassImpl>::MODULE

👎Deprecated since 0.28.0:

prefer using ::type_object(py).module() to get the correct runtime value

Module name, if any.
Source§

fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject

Returns the PyTypeObject instance for this type.
§

fn type_object(py: Python<'_>) -> Bound<'_, PyType>

Returns the safe abstraction over the type object.
§

fn is_type_of(object: &Bound<'_, PyAny>) -> bool

Checks if object is an instance of this type or a subclass of this type.
§

fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool

Checks if object is an instance of this type.
Source§

impl DerefToPyAny for InteractiveBrokersInstrumentProvider

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<'py, T> IntoPyObjectExt<'py> for T
where T: IntoPyObject<'py>,

§

fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>

Converts self into an owned Python object, dropping type information.
§

fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>

Converts self into an owned Python object, dropping type information and unbinding it from the 'py lifetime.
§

fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>

Converts self into a Python object. Read more
§

impl<'py, T> IntoPyObjectNautilusExt<'py> for T
where T: IntoPyObjectExt<'py>,

§

fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny>

Convert self into a [Py<PyAny>] while panicking if the conversion fails. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
§

impl<T> PyErrArguments for T
where T: for<'py> IntoPyObject<'py> + Send + Sync,

§

fn arguments(self, py: Python<'_>) -> Py<PyAny>

Arguments for exception
§

impl<T> PyTypeCheck for T
where T: PyTypeInfo,

§

const NAME: &'static str = T::NAME

👎Deprecated since 0.27.0:

Use ::classinfo_object() instead and format the type name at runtime. Note that using built-in cast features is often better than manual PyTypeCheck usage.

Name of self. This is used in error messages, for example.
§

fn type_check(object: &Bound<'_, PyAny>) -> bool

Checks if object is an instance of Self, which may include a subtype. Read more
§

fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>

Returns the expected type as a possible argument for the isinstance and issubclass function. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<'py, T> FromPyObjectOwned<'py> for T
where T: for<'a> FromPyObject<'a, 'py>,

§

impl<T> Ungil for T
where T: Send,