pub struct Client { /* private fields */ }
Expand description
TWS API Client. Manages the connection to TWS or Gateway. Tracks some global information such as server version and server time. Supports generation of order ids.
Implementations§
Source§impl Client
impl Client
Sourcepub fn connect(address: &str, client_id: i32) -> Result<Client, Error>
pub fn connect(address: &str, client_id: i32) -> Result<Client, Error>
Establishes connection to TWS or Gateway
Connects to server using the given connection string
§Arguments
address
- address of server. e.g. 127.0.0.1:4002client_id
- id of client. e.g. 100
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
println!("server_version: {}", client.server_version());
println!("connection_time: {:?}", client.connection_time());
println!("next_order_id: {}", client.next_order_id());
Sourcepub fn next_request_id(&self) -> i32
pub fn next_request_id(&self) -> i32
Returns the next request ID.
Sourcepub fn next_order_id(&self) -> i32
pub fn next_order_id(&self) -> i32
Returns and increments the order ID.
The client maintains a sequence of order IDs. This function returns the next order ID in the sequence.
Sourcepub fn next_valid_order_id(&self) -> Result<i32, Error>
pub fn next_valid_order_id(&self) -> Result<i32, Error>
Gets the next valid order ID from the TWS server.
Unlike Self::next_order_id, this function requests the next valid order ID from the TWS server and updates the client’s internal order ID sequence. This can be for ensuring that order IDs are unique across multiple clients.
Use this method when coordinating order IDs across multiple client instances or when you need to synchronize with the server’s order ID sequence at the start of a session.
§Examples
use ibapi::Client;
// Connect to the TWS server at the given address with client ID.
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
// Request the next valid order ID from the server.
let next_valid_order_id = client.next_valid_order_id().expect("request failed");
println!("next_valid_order_id: {next_valid_order_id}");
Sourcepub fn server_version(&self) -> i32
pub fn server_version(&self) -> i32
Returns the version of the TWS API server to which the client is connected. This version is determined during the initial connection handshake.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let server_version = client.server_version();
println!("Connected to TWS server version: {}", server_version);
Sourcepub fn connection_time(&self) -> Option<OffsetDateTime>
pub fn connection_time(&self) -> Option<OffsetDateTime>
The time of the server when the client connected
Sourcepub fn server_time(&self) -> Result<OffsetDateTime, Error>
pub fn server_time(&self) -> Result<OffsetDateTime, Error>
TWS’s current time. TWS is synchronized with the server (not local computer) using NTP and this function will receive the current time in TWS.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let server_time = client.server_time().expect("error requesting server time");
println!("server time: {server_time:?}");
Sourcepub fn positions(&self) -> Result<Subscription<'_, PositionUpdate>, Error>
pub fn positions(&self) -> Result<Subscription<'_, PositionUpdate>, Error>
Subscribes to PositionUpdates for all accessible accounts. All positions sent initially, and then only updates as positions change.
§Examples
use ibapi::Client;
use ibapi::accounts::PositionUpdate;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let subscription = client.positions().expect("error requesting positions");
for position_response in subscription.iter() {
match position_response {
PositionUpdate::Position(position) => println!("{position:?}"),
PositionUpdate::PositionEnd => println!("initial set of positions received"),
}
}
Sourcepub fn positions_multi(
&self,
account: Option<&str>,
model_code: Option<&str>,
) -> Result<Subscription<'_, PositionUpdateMulti>, Error>
pub fn positions_multi( &self, account: Option<&str>, model_code: Option<&str>, ) -> Result<Subscription<'_, PositionUpdateMulti>, Error>
Subscribes to PositionUpdateMulti updates for account and/or model. Initially all positions are returned, and then updates are returned for any position changes in real time.
§Arguments
account
- If an account Id is provided, only the account’s positions belonging to the specified model will be delivered.model_code
- The code of the model’s positions we are interested in.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let account = "U1234567";
let subscription = client.positions_multi(Some(account), None).expect("error requesting positions by model");
for position in subscription.iter() {
println!("{position:?}")
}
Sourcepub fn pnl(
&self,
account: &str,
model_code: Option<&str>,
) -> Result<Subscription<'_, PnL>, Error>
pub fn pnl( &self, account: &str, model_code: Option<&str>, ) -> Result<Subscription<'_, PnL>, Error>
Creates subscription for real time daily PnL and unrealized PnL updates.
§Arguments
account
- account for which to receive PnL updatesmodel_code
- specify to request PnL updates for a specific model
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let account = "account id";
let subscription = client.pnl(account, None).expect("error requesting pnl");
for pnl in subscription.iter() {
println!("{pnl:?}")
}
Sourcepub fn pnl_single<'a>(
&'a self,
account: &str,
contract_id: i32,
model_code: Option<&str>,
) -> Result<Subscription<'a, PnLSingle>, Error>
pub fn pnl_single<'a>( &'a self, account: &str, contract_id: i32, model_code: Option<&str>, ) -> Result<Subscription<'a, PnLSingle>, Error>
Requests real time updates for daily PnL of individual positions.
§Arguments
account
- Account in which position existscontract_id
- Contract ID of contract to receive daily PnL updates for. Note: does not return response if invalid conId is entered.model_code
- Model in which position exists
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let account = "<account id>";
let contract_id = 1001;
let subscription = client.pnl_single(account, contract_id, None).expect("error requesting pnl");
for pnl in &subscription {
println!("{pnl:?}")
}
Sourcepub fn account_summary<'a>(
&'a self,
group: &str,
tags: &[&str],
) -> Result<Subscription<'a, AccountSummaries>, Error>
pub fn account_summary<'a>( &'a self, group: &str, tags: &[&str], ) -> Result<Subscription<'a, AccountSummaries>, Error>
Requests a specific account’s summary. Subscribes to the account summary as presented in the TWS’ Account Summary tab. Data received is specified by using a specific tags value.
§Arguments
group
- Set to “All” to return account summary data for all accounts, or set to a specific Advisor Account Group name that has already been created in TWS Global Configuration.tags
- List of the desired tags.
§Examples
use ibapi::Client;
use ibapi::accounts::AccountSummaryTags;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let group = "All";
let subscription = client.account_summary(group, AccountSummaryTags::ALL).expect("error requesting account summary");
for summary in &subscription {
println!("{summary:?}")
}
Sourcepub fn account_updates<'a>(
&'a self,
account: &str,
) -> Result<Subscription<'a, AccountUpdate>, Error>
pub fn account_updates<'a>( &'a self, account: &str, ) -> Result<Subscription<'a, AccountUpdate>, Error>
Subscribes to a specific account’s information and portfolio.
All account values and positions will be returned initially, and then there will only be updates when there is a change in a position, or to an account value every 3 minutes if it has changed. Only one account can be subscribed at a time.
§Arguments
account
- The account id (i.e. U1234567) for which the information is requested.
§Examples
use ibapi::Client;
use ibapi::accounts::AccountUpdate;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let account = "U1234567";
let subscription = client.account_updates(account).expect("error requesting account updates");
for update in &subscription {
println!("{update:?}");
// stop after full initial update
if let AccountUpdate::End = update {
subscription.cancel();
}
}
Sourcepub fn account_updates_multi<'a>(
&'a self,
account: Option<&str>,
model_code: Option<&str>,
) -> Result<Subscription<'a, AccountUpdateMulti>, Error>
pub fn account_updates_multi<'a>( &'a self, account: Option<&str>, model_code: Option<&str>, ) -> Result<Subscription<'a, AccountUpdateMulti>, Error>
Requests account updates for account and/or model.
All account values and positions will be returned initially, and then there will only be updates when there is a change in a position, or to an account value every 3 minutes if it has changed. Only one account can be subscribed at a time.
§Arguments
account
- Account values can be requested for a particular account.model_code
- Account values can also be requested for a model.
§Examples
use ibapi::Client;
use ibapi::accounts::AccountUpdateMulti;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let account = Some("U1234567");
let subscription = client.account_updates_multi(account, None).expect("error requesting account updates multi");
for update in &subscription {
println!("{update:?}");
// stop after full initial update
if let AccountUpdateMulti::End = update {
subscription.cancel();
}
}
Sourcepub fn managed_accounts(&self) -> Result<Vec<String>, Error>
pub fn managed_accounts(&self) -> Result<Vec<String>, Error>
Requests the accounts to which the logged user has access to.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let accounts = client.managed_accounts().expect("error requesting managed accounts");
println!("managed accounts: {accounts:?}")
Sourcepub fn contract_details(
&self,
contract: &Contract,
) -> Result<Vec<ContractDetails>, Error>
pub fn contract_details( &self, contract: &Contract, ) -> Result<Vec<ContractDetails>, Error>
Requests contract information.
Provides all the contracts matching the contract provided. It can also be used to retrieve complete options and futures chains. Though it is now (in API version > 9.72.12) advised to use Client::option_chain for that purpose.
§Arguments
contract
- The Contract used as sample to query the available contracts. Typically, it will contain the Contract’s symbol, currency, security_type, and exchange.
§Examples
use ibapi::Client;
use ibapi::contracts::Contract;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("TSLA");
let results = client.contract_details(&contract).expect("request failed");
for contract_detail in results {
println!("contract: {:?}", contract_detail);
}
Sourcepub fn family_codes(&self) -> Result<Vec<FamilyCode>, Error>
pub fn family_codes(&self) -> Result<Vec<FamilyCode>, Error>
Get current FamilyCodes for all accessible accounts.
Sourcepub fn market_rule(&self, market_rule_id: i32) -> Result<MarketRule, Error>
pub fn market_rule(&self, market_rule_id: i32) -> Result<MarketRule, Error>
Requests details about a given market rule
The market rule for an instrument on a particular exchange provides details about how the minimum price increment changes with price. A list of market rule ids can be obtained by invoking Self::contract_details() for a particular contract. The returned market rule ID list will provide the market rule ID for the instrument in the correspond valid exchange list in contracts::ContractDetails.
Sourcepub fn matching_symbols(
&self,
pattern: &str,
) -> Result<impl Iterator<Item = ContractDescription>, Error>
pub fn matching_symbols( &self, pattern: &str, ) -> Result<impl Iterator<Item = ContractDescription>, Error>
Requests matching stock symbols.
§Arguments
pattern
- Either start of ticker symbol or (for larger strings) company name.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contracts = client.matching_symbols("IB").expect("request failed");
for contract in contracts {
println!("contract: {:?}", contract);
}
Sourcepub fn calculate_option_price(
&self,
contract: &Contract,
volatility: f64,
underlying_price: f64,
) -> Result<OptionComputation, Error>
pub fn calculate_option_price( &self, contract: &Contract, volatility: f64, underlying_price: f64, ) -> Result<OptionComputation, Error>
Calculates an option’s price based on the provided volatility and its underlying’s price.
§Arguments
contract
- The Contract object representing the option for which the calculation is being requested.volatility
- Hypothetical volatility as a percentage (e.g., 20.0 for 20%).underlying_price
- Hypothetical price of the underlying asset.
§Examples
use ibapi::Client;
use ibapi::contracts::Contract;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::option("AAPL", "20251219", 150.0, "C");
let calculation = client.calculate_option_price(&contract, 100.0, 235.0).expect("request failed");
println!("calculation: {:?}", calculation);
Sourcepub fn calculate_implied_volatility(
&self,
contract: &Contract,
option_price: f64,
underlying_price: f64,
) -> Result<OptionComputation, Error>
pub fn calculate_implied_volatility( &self, contract: &Contract, option_price: f64, underlying_price: f64, ) -> Result<OptionComputation, Error>
Calculates the implied volatility based on the hypothetical option price and underlying price.
§Arguments
contract
- The Contract object representing the option for which the calculation is being requested.option_price
- Hypothetical option price.underlying_price
- Hypothetical price of the underlying asset.
§Examples
use ibapi::Client;
use ibapi::contracts::Contract;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::option("AAPL", "20230519", 150.0, "C");
let calculation = client.calculate_implied_volatility(&contract, 25.0, 235.0).expect("request failed");
println!("calculation: {:?}", calculation);
Sourcepub fn option_chain(
&self,
symbol: &str,
exchange: &str,
security_type: SecurityType,
contract_id: i32,
) -> Result<Subscription<'_, OptionChain>, Error>
pub fn option_chain( &self, symbol: &str, exchange: &str, security_type: SecurityType, contract_id: i32, ) -> Result<Subscription<'_, OptionChain>, Error>
Requests security definition option parameters for viewing a contract’s option chain.
§Arguments
symbol
- Contract symbol of the underlying.
exchange
- The exchange on which the returned options are trading. Can be set to the empty string for all exchanges.
security_type
- The type of the underlying security, i.e. STK
contract_id
- The contract ID of the underlying security.
§Examples
use ibapi::{contracts::SecurityType, Client};
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let symbol = "AAPL";
let exchange = ""; // all exchanges
let security_type = SecurityType::Stock;
let contract_id = 265598;
let subscription = client
.option_chain(symbol, exchange, security_type, contract_id)
.expect("request option chain failed!");
for option_chain in &subscription {
println!("{option_chain:?}")
}
Sourcepub fn all_open_orders(&self) -> Result<Subscription<'_, Orders>, Error>
pub fn all_open_orders(&self) -> Result<Subscription<'_, Orders>, Error>
Requests all current open orders in associated accounts at the current moment. Open orders are returned once; this function does not initiate a subscription.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let subscription = client.all_open_orders().expect("request failed");
for order_data in &subscription {
println!("{order_data:?}")
}
Sourcepub fn auto_open_orders(
&self,
auto_bind: bool,
) -> Result<Subscription<'_, Orders>, Error>
pub fn auto_open_orders( &self, auto_bind: bool, ) -> Result<Subscription<'_, Orders>, Error>
Requests status updates about future orders placed from TWS. Can only be used with client ID 0.
§Arguments
auto_bind
- if set to true, the newly created orders will be assigned an API order ID and implicitly associated with this client. If set to false, future orders will not be.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 0).expect("connection failed");
let subscription = client.auto_open_orders(false).expect("request failed");
for order_data in &subscription {
println!("{order_data:?}")
}
Sourcepub fn cancel_order(
&self,
order_id: i32,
manual_order_cancel_time: &str,
) -> Result<Subscription<'_, CancelOrder>, Error>
pub fn cancel_order( &self, order_id: i32, manual_order_cancel_time: &str, ) -> Result<Subscription<'_, CancelOrder>, Error>
Cancels an active Order placed by the same API client ID.
§Arguments
order_id
- ID of the Order to cancel.manual_order_cancel_time
- Optional timestamp to specify the cancellation time. Use an empty string to use the current time.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let order_id = 15;
let subscription = client.cancel_order(order_id, "").expect("request failed");
for result in subscription {
println!("{result:?}");
}
Sourcepub fn completed_orders(
&self,
api_only: bool,
) -> Result<Subscription<'_, Orders>, Error>
pub fn completed_orders( &self, api_only: bool, ) -> Result<Subscription<'_, Orders>, Error>
Requests completed Orders.
§Arguments
api_only
- request only orders placed by the API.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let subscription = client.completed_orders(false).expect("request failed");
for order_data in &subscription {
println!("{order_data:?}")
}
Sourcepub fn executions(
&self,
filter: ExecutionFilter,
) -> Result<Subscription<'_, Executions>, Error>
pub fn executions( &self, filter: ExecutionFilter, ) -> Result<Subscription<'_, Executions>, Error>
Requests current day’s (since midnight) executions matching the filter.
Only the current day’s executions can be retrieved. Along with the orders::ExecutionData, the orders::CommissionReport will also be returned. When requesting executions, a filter can be specified to receive only a subset of them
§Arguments
filter
- filter criteria used to determine which execution reports are returned
§Examples
use ibapi::Client;
use ibapi::orders::ExecutionFilter;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let filter = ExecutionFilter{
side: "BUY".to_owned(),
..ExecutionFilter::default()
};
let subscription = client.executions(filter).expect("request failed");
for execution_data in &subscription {
println!("{execution_data:?}")
}
Sourcepub fn global_cancel(&self) -> Result<(), Error>
pub fn global_cancel(&self) -> Result<(), Error>
Sourcepub fn open_orders(&self) -> Result<Subscription<'_, Orders>, Error>
pub fn open_orders(&self) -> Result<Subscription<'_, Orders>, Error>
Requests all open orders places by this specific API client (identified by the API client id). For client ID 0, this will bind previous manual TWS orders.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let subscription = client.open_orders().expect("request failed");
for order_data in &subscription {
println!("{order_data:?}")
}
Sourcepub fn place_order(
&self,
order_id: i32,
contract: &Contract,
order: &Order,
) -> Result<Subscription<'_, PlaceOrder>, Error>
pub fn place_order( &self, order_id: i32, contract: &Contract, order: &Order, ) -> Result<Subscription<'_, PlaceOrder>, Error>
Places or modifies an Order.
Submits an Order using Client for the given Contract. Upon successful submission, the client will start receiving events related to the order’s activity via the subscription, including order status updates and execution reports.
§Arguments
order_id
- ID for Order. Get next valid ID using Client::next_order_id.contract
- Contract to submit order for.order
- Order to submit.
§Examples
use ibapi::Client;
use ibapi::contracts::Contract;
use ibapi::orders::{order_builder, Action, PlaceOrder};
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("MSFT");
let order = order_builder::market_order(Action::Buy, 100.0);
let order_id = client.next_order_id();
let events = client.place_order(order_id, &contract, &order).expect("request failed");
for event in &events {
match event {
PlaceOrder::OrderStatus(order_status) => {
println!("order status: {order_status:?}")
}
PlaceOrder::OpenOrder(open_order) => println!("open order: {open_order:?}"),
PlaceOrder::ExecutionData(execution) => println!("execution: {execution:?}"),
PlaceOrder::CommissionReport(report) => println!("commission report: {report:?}"),
PlaceOrder::Message(message) => println!("message: {message:?}"),
}
}
Sourcepub fn submit_order(
&self,
order_id: i32,
contract: &Contract,
order: &Order,
) -> Result<(), Error>
pub fn submit_order( &self, order_id: i32, contract: &Contract, order: &Order, ) -> Result<(), Error>
Submits or modifies an Order without returning a subscription.
This is a fire-and-forget method that submits an Order for the given Contract
but does not return a subscription for order updates. To receive order status updates,
fills, and commission reports, use the order_update_stream
method
or use place_order
instead which returns a subscription.
§Arguments
order_id
- ID for Order. Get next valid ID using Client::next_order_id.contract
- Contract to submit order for.order
- Order to submit.
§Returns
Ok(())
if the order was successfully sentErr(Error)
if validation failed or sending failed
§Examples
use ibapi::Client;
use ibapi::contracts::Contract;
use ibapi::orders::{order_builder, Action};
let client = Client::connect("127.0.0.1:4002", 100)?;
let contract = Contract::stock("MSFT");
let order = order_builder::market_order(Action::Buy, 100.0);
let order_id = client.next_order_id();
// Submit order without waiting for confirmation
client.submit_order(order_id, &contract, &order)?;
// Monitor all order updates via the order update stream
// This will receive updates for ALL orders, not just this one
use ibapi::orders::OrderUpdate;
for event in client.order_update_stream()? {
match event {
OrderUpdate::OrderStatus(status) => println!("Order Status: {status:?}"),
OrderUpdate::ExecutionData(exec) => println!("Execution: {exec:?}"),
OrderUpdate::CommissionReport(report) => println!("Commission: {report:?}"),
_ => {}
}
}
Sourcepub fn order_update_stream(
&self,
) -> Result<Subscription<'_, OrderUpdate>, Error>
pub fn order_update_stream( &self, ) -> Result<Subscription<'_, OrderUpdate>, Error>
Creates a subscription stream for receiving real-time order updates.
This method establishes a stream that receives all order-related events including:
- Order status updates (e.g., submitted, filled, cancelled)
- Open order information
- Execution data for trades
- Commission reports
- Order-related messages and notices
The stream will receive updates for all orders placed through this client connection, including both new orders submitted after creating the stream and existing orders.
§Returns
Returns a Subscription<OrderUpdate>
that yields OrderUpdate
enum variants containing:
OrderStatus
: Current status of an order (filled amount, average price, etc.)OpenOrder
: Complete order details including contract and order parametersExecutionData
: Details about individual trade executionsCommissionReport
: Commission information for executed tradesMessage
: Notices or error messages related to orders
§Errors
Returns an error if the subscription cannot be created, typically due to connection issues or internal errors.
§Examples
use ibapi::Client;
use ibapi::orders::OrderUpdate;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
// Create order update stream
let updates = client.order_update_stream().expect("failed to create stream");
// Process order updates
for update in updates {
match update {
OrderUpdate::OrderStatus(status) => {
println!("Order {} status: {} - filled: {}/{}",
status.order_id, status.status, status.filled, status.remaining);
},
OrderUpdate::OpenOrder(order_data) => {
println!("Open order {}: {} {} @ {}",
order_data.order.order_id,
order_data.order.action,
order_data.order.total_quantity,
order_data.order.limit_price.unwrap_or(0.0));
},
OrderUpdate::ExecutionData(exec) => {
println!("Execution: {} {} @ {} on {}",
exec.execution.side,
exec.execution.shares,
exec.execution.price,
exec.execution.exchange);
},
OrderUpdate::CommissionReport(report) => {
println!("Commission: ${} for execution {}",
report.commission, report.execution_id);
},
OrderUpdate::Message(notice) => {
println!("Order message: {}", notice.message);
}
}
}
§Note
This stream provides updates for all orders, not just a specific order. To track a specific order, filter the updates by order ID.
Sourcepub fn exercise_options<'a>(
&'a self,
contract: &Contract,
exercise_action: ExerciseAction,
exercise_quantity: i32,
account: &str,
ovrd: bool,
manual_order_time: Option<OffsetDateTime>,
) -> Result<Subscription<'a, ExerciseOptions>, Error>
pub fn exercise_options<'a>( &'a self, contract: &Contract, exercise_action: ExerciseAction, exercise_quantity: i32, account: &str, ovrd: bool, manual_order_time: Option<OffsetDateTime>, ) -> Result<Subscription<'a, ExerciseOptions>, Error>
Exercises an options contract.
Note: this function is affected by a TWS setting which specifies if an exercise request must be finalized.
§Arguments
contract
- The option Contract to be exercised.exercise_action
- Exercise option. ExerciseAction::Exercise or ExerciseAction::Lapse.exercise_quantity
- Number of contracts to be exercised.account
- Destination account.ovrd
- Specifies whether your setting will override the system’s natural action. For example, if your action is “exercise” and the option is not in-the-money, by natural action the option would not exercise. If you have override set to true the natural action would be overridden and the out-of-the money option would be exercised.manual_order_time
- Specify the time at which the options should be exercised. IfNone
, the current time will be used. Requires TWS API 10.26 or higher.
Sourcepub fn head_timestamp(
&self,
contract: &Contract,
what_to_show: WhatToShow,
use_rth: bool,
) -> Result<OffsetDateTime, Error>
pub fn head_timestamp( &self, contract: &Contract, what_to_show: WhatToShow, use_rth: bool, ) -> Result<OffsetDateTime, Error>
Returns the timestamp of earliest available historical data for a contract and data type.
use ibapi::Client;
use ibapi::contracts::Contract;
use ibapi::market_data::historical::{self, WhatToShow};
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("MSFT");
let what_to_show = WhatToShow::Trades;
let use_rth = true;
let result = client.head_timestamp(&contract, what_to_show, use_rth).expect("head timestamp failed");
print!("head_timestamp: {result:?}");
Sourcepub fn historical_data(
&self,
contract: &Contract,
interval_end: Option<OffsetDateTime>,
duration: Duration,
bar_size: BarSize,
what_to_show: WhatToShow,
use_rth: bool,
) -> Result<HistoricalData, Error>
pub fn historical_data( &self, contract: &Contract, interval_end: Option<OffsetDateTime>, duration: Duration, bar_size: BarSize, what_to_show: WhatToShow, use_rth: bool, ) -> Result<HistoricalData, Error>
Requests interval of historical data ending at specified time for Contract.
§Arguments
contract
- Contract to retrieve historical::HistoricalData for.interval_end
- optional end date of interval to retrieve historical::HistoricalData for. IfNone
current time or last trading of contract is implied.duration
- duration of interval to retrieve historical::HistoricalData for.bar_size
- historical::BarSize to return.what_to_show
- requested bar type: historical::WhatToShow.use_rth
- use regular trading hours.
§Examples
use time::macros::datetime;
use ibapi::contracts::Contract;
use ibapi::Client;
use ibapi::market_data::historical::{BarSize, ToDuration, WhatToShow};
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("TSLA");
let historical_data = client
.historical_data(&contract, Some(datetime!(2023-04-15 0:00 UTC)), 7.days(), BarSize::Day, WhatToShow::Trades, true)
.expect("historical data request failed");
println!("start_date: {}, end_date: {}", historical_data.start, historical_data.end);
for bar in &historical_data.bars {
println!("{bar:?}");
}
Sourcepub fn historical_data_ending_now(
&self,
contract: &Contract,
duration: Duration,
bar_size: BarSize,
what_to_show: WhatToShow,
use_rth: bool,
) -> Result<HistoricalData, Error>
👎Deprecated since 1.1.0: use historical_data
instead
pub fn historical_data_ending_now( &self, contract: &Contract, duration: Duration, bar_size: BarSize, what_to_show: WhatToShow, use_rth: bool, ) -> Result<HistoricalData, Error>
historical_data
insteadRequests interval of historical data ending now for Contract.
§Arguments
contract
- Contract to retrieve historical::HistoricalData for.duration
- duration of interval to retrieve historical::HistoricalData for.bar_size
- historical::BarSize to return.what_to_show
- requested bar type: historical::WhatToShow.use_rth
- use regular trading hours.
§Examples
use ibapi::contracts::Contract;
use ibapi::Client;
use ibapi::market_data::historical::{BarSize, ToDuration, WhatToShow};
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("TSLA");
let historical_data = client
.historical_data_ending_now(&contract, 7.days(), BarSize::Day, WhatToShow::Trades, true)
.expect("historical data request failed");
println!("start_date: {}, end_date: {}", historical_data.start, historical_data.end);
for bar in &historical_data.bars {
println!("{bar:?}");
}
Sourcepub fn historical_schedules(
&self,
contract: &Contract,
interval_end: OffsetDateTime,
duration: Duration,
) -> Result<Schedule, Error>
pub fn historical_schedules( &self, contract: &Contract, interval_end: OffsetDateTime, duration: Duration, ) -> Result<Schedule, Error>
Requests Schedule for an interval of given duration ending at specified date.
§Arguments
contract
- Contract to retrieve Schedule for.interval_end
- end date of interval to retrieve Schedule for.duration
- duration of interval to retrieve Schedule for.
§Examples
use time::macros::datetime;
use ibapi::contracts::Contract;
use ibapi::Client;
use ibapi::market_data::historical::ToDuration;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("GM");
let historical_data = client
.historical_schedules(&contract, datetime!(2023-04-15 0:00 UTC), 30.days())
.expect("historical schedule request failed");
println!("start: {:?}, end: {:?}", historical_data.start, historical_data.end);
for session in &historical_data.sessions {
println!("{session:?}");
}
Sourcepub fn historical_schedules_ending_now(
&self,
contract: &Contract,
duration: Duration,
) -> Result<Schedule, Error>
pub fn historical_schedules_ending_now( &self, contract: &Contract, duration: Duration, ) -> Result<Schedule, Error>
Requests historical::Schedule for interval ending at current time.
§Arguments
contract
- Contract to retrieve historical::Schedule for.duration
- historical::Duration for interval to retrieve historical::Schedule for.
§Examples
use ibapi::contracts::Contract;
use ibapi::Client;
use ibapi::market_data::historical::ToDuration;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("GM");
let historical_data = client
.historical_schedules_ending_now(&contract, 30.days())
.expect("historical schedule request failed");
println!("start: {:?}, end: {:?}", historical_data.start, historical_data.end);
for session in &historical_data.sessions {
println!("{session:?}");
}
Sourcepub fn historical_ticks_bid_ask(
&self,
contract: &Contract,
start: Option<OffsetDateTime>,
end: Option<OffsetDateTime>,
number_of_ticks: i32,
use_rth: bool,
ignore_size: bool,
) -> Result<TickSubscription<TickBidAsk>, Error>
pub fn historical_ticks_bid_ask( &self, contract: &Contract, start: Option<OffsetDateTime>, end: Option<OffsetDateTime>, number_of_ticks: i32, use_rth: bool, ignore_size: bool, ) -> Result<TickSubscription<TickBidAsk>, Error>
Requests historical time & sales data (Bid/Ask) for an instrument.
§Arguments
contract
- Contract object that is subject of querystart
- Start time. Either start time or end time is specified.end
- End time. Either start time or end time is specified.number_of_ticks
- Number of distinct data points. Max currently 1000 per request.use_rth
- Data from regular trading hours (true), or all available hours (false)ignore_size
- A filter only used when the source price is Bid_Ask
§Examples
use time::macros::datetime;
use ibapi::contracts::Contract;
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("TSLA");
let ticks = client
.historical_ticks_bid_ask(&contract, Some(datetime!(2023-04-15 0:00 UTC)), None, 100, true, false)
.expect("historical ticks request failed");
for tick in ticks {
println!("{tick:?}");
}
Sourcepub fn historical_ticks_mid_point(
&self,
contract: &Contract,
start: Option<OffsetDateTime>,
end: Option<OffsetDateTime>,
number_of_ticks: i32,
use_rth: bool,
) -> Result<TickSubscription<TickMidpoint>, Error>
pub fn historical_ticks_mid_point( &self, contract: &Contract, start: Option<OffsetDateTime>, end: Option<OffsetDateTime>, number_of_ticks: i32, use_rth: bool, ) -> Result<TickSubscription<TickMidpoint>, Error>
Requests historical time & sales data (Midpoint) for an instrument.
§Arguments
contract
- Contract object that is subject of querystart
- Start time. Either start time or end time is specified.end
- End time. Either start time or end time is specified.number_of_ticks
- Number of distinct data points. Max currently 1000 per request.use_rth
- Data from regular trading hours (true), or all available hours (false)
§Examples
use time::macros::datetime;
use ibapi::contracts::Contract;
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("TSLA");
let ticks = client
.historical_ticks_mid_point(&contract, Some(datetime!(2023-04-15 0:00 UTC)), None, 100, true)
.expect("historical ticks request failed");
for tick in ticks {
println!("{tick:?}");
}
Sourcepub fn historical_ticks_trade(
&self,
contract: &Contract,
start: Option<OffsetDateTime>,
end: Option<OffsetDateTime>,
number_of_ticks: i32,
use_rth: bool,
) -> Result<TickSubscription<TickLast>, Error>
pub fn historical_ticks_trade( &self, contract: &Contract, start: Option<OffsetDateTime>, end: Option<OffsetDateTime>, number_of_ticks: i32, use_rth: bool, ) -> Result<TickSubscription<TickLast>, Error>
Requests historical time & sales data (Trades) for an instrument.
§Arguments
contract
- Contract object that is subject of querystart
- Start time. Either start time or end time is specified.end
- End time. Either start time or end time is specified.number_of_ticks
- Number of distinct data points. Max currently 1000 per request.use_rth
- Data from regular trading hours (true), or all available hours (false)
§Examples
use time::macros::datetime;
use ibapi::contracts::Contract;
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("TSLA");
let ticks = client
.historical_ticks_trade(&contract, Some(datetime!(2023-04-15 0:00 UTC)), None, 100, true)
.expect("historical ticks request failed");
for tick in ticks {
println!("{tick:?}");
}
Sourcepub fn histogram_data(
&self,
contract: &Contract,
use_rth: bool,
period: BarSize,
) -> Result<Vec<HistogramEntry>, Error>
pub fn histogram_data( &self, contract: &Contract, use_rth: bool, period: BarSize, ) -> Result<Vec<HistogramEntry>, Error>
Requests data histogram of specified contract.
§Arguments
contract
- Contract to retrieve Histogram Entries for.use_rth
- Data from regular trading hours (true), or all available hours (false).period
- The time period of each histogram bar (e.g.,BarSize::Day
,BarSize::Week
,BarSize::Month
).
§Examples
use time::macros::datetime;
use ibapi::contracts::Contract;
use ibapi::Client;
use ibapi::market_data::historical::BarSize;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("GM");
let histogram = client
.histogram_data(&contract, true, BarSize::Week)
.expect("histogram request failed");
for item in &histogram {
println!("{item:?}");
}
Sourcepub fn realtime_bars<'a>(
&'a self,
contract: &Contract,
bar_size: BarSize,
what_to_show: WhatToShow,
use_rth: bool,
) -> Result<Subscription<'a, Bar>, Error>
pub fn realtime_bars<'a>( &'a self, contract: &Contract, bar_size: BarSize, what_to_show: WhatToShow, use_rth: bool, ) -> Result<Subscription<'a, Bar>, Error>
Requests realtime bars.
§Arguments
contract
- The Contract used as sample to query the available contracts. Typically, it will contain the Contract’s symbol, currency, security_type, and exchange.
§Examples
use ibapi::Client;
use ibapi::contracts::Contract;
use ibapi::market_data::realtime::{BarSize, WhatToShow};
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("TSLA");
let subscription = client.realtime_bars(&contract, BarSize::Sec5, WhatToShow::Trades, false).expect("request failed");
for (i, bar) in subscription.iter().enumerate().take(60) {
println!("bar[{i}]: {bar:?}");
}
Sourcepub fn tick_by_tick_all_last<'a>(
&'a self,
contract: &Contract,
number_of_ticks: i32,
ignore_size: bool,
) -> Result<Subscription<'a, Trade>, Error>
pub fn tick_by_tick_all_last<'a>( &'a self, contract: &Contract, number_of_ticks: i32, ignore_size: bool, ) -> Result<Subscription<'a, Trade>, Error>
Requests tick by tick AllLast ticks.
§Arguments
contract
- The Contract for which to request tick-by-tick data.number_of_ticks
- The number of ticks to retrieve. TWS usually limits this to 1000.ignore_size
- Specifies if tick sizes should be ignored.
§Examples
use ibapi::Client;
use ibapi::contracts::Contract;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("AAPL");
let number_of_ticks = 10; // Request a small number of ticks for the example
let ignore_size = false;
let subscription = client.tick_by_tick_all_last(&contract, number_of_ticks, ignore_size)
.expect("tick-by-tick all last data request failed");
for tick in subscription.iter().take(number_of_ticks as usize) { // Take to limit example output
println!("All Last Tick: {:?}", tick);
}
Sourcepub fn tick_by_tick_bid_ask<'a>(
&'a self,
contract: &Contract,
number_of_ticks: i32,
ignore_size: bool,
) -> Result<Subscription<'a, BidAsk>, Error>
pub fn tick_by_tick_bid_ask<'a>( &'a self, contract: &Contract, number_of_ticks: i32, ignore_size: bool, ) -> Result<Subscription<'a, BidAsk>, Error>
Requests tick by tick BidAsk ticks.
§Arguments
contract
- The Contract for which to request tick-by-tick data.number_of_ticks
- The number of ticks to retrieve. TWS usually limits this to 1000.ignore_size
- Specifies if tick sizes should be ignored. (typically true for BidAsk ticks to get changes based on price).
§Examples
use ibapi::Client;
use ibapi::contracts::Contract;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("AAPL");
let number_of_ticks = 10; // Request a small number of ticks for the example
let ignore_size = false;
let subscription = client.tick_by_tick_bid_ask(&contract, number_of_ticks, ignore_size)
.expect("tick-by-tick bid/ask data request failed");
for tick in subscription.iter().take(number_of_ticks as usize) { // Take to limit example output
println!("BidAsk Tick: {:?}", tick);
}
Sourcepub fn tick_by_tick_last<'a>(
&'a self,
contract: &Contract,
number_of_ticks: i32,
ignore_size: bool,
) -> Result<Subscription<'a, Trade>, Error>
pub fn tick_by_tick_last<'a>( &'a self, contract: &Contract, number_of_ticks: i32, ignore_size: bool, ) -> Result<Subscription<'a, Trade>, Error>
Requests tick by tick Last ticks.
§Arguments
contract
- The Contract for which to request tick-by-tick data.number_of_ticks
- The number of ticks to retrieve. TWS usually limits this to 1000.ignore_size
- Specifies if tick sizes should be ignored (typically false for Last ticks).
§Examples
use ibapi::Client;
use ibapi::contracts::Contract;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("AAPL");
let number_of_ticks = 10; // Request a small number of ticks for the example
let ignore_size = false;
let subscription = client.tick_by_tick_last(&contract, number_of_ticks, ignore_size)
.expect("tick-by-tick last data request failed");
for tick in subscription.iter().take(number_of_ticks as usize) { // Take to limit example output
println!("Last Tick: {:?}", tick);
}
Sourcepub fn tick_by_tick_midpoint<'a>(
&'a self,
contract: &Contract,
number_of_ticks: i32,
ignore_size: bool,
) -> Result<Subscription<'a, MidPoint>, Error>
pub fn tick_by_tick_midpoint<'a>( &'a self, contract: &Contract, number_of_ticks: i32, ignore_size: bool, ) -> Result<Subscription<'a, MidPoint>, Error>
Requests tick by tick MidPoint ticks.
§Arguments
contract
- The Contract for which to request tick-by-tick data.number_of_ticks
- The number of ticks to retrieve. TWS usually limits this to 1000.ignore_size
- Specifies if tick sizes should be ignored.
§Examples
use ibapi::Client;
use ibapi::contracts::Contract;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("AAPL");
let number_of_ticks = 10; // Request a small number of ticks for the example
let ignore_size = false;
let subscription = client.tick_by_tick_bid_ask(&contract, number_of_ticks, ignore_size)
.expect("tick-by-tick mid-point data request failed");
for tick in subscription.iter().take(number_of_ticks as usize) { // Take to limit example output
println!("MidPoint Tick: {:?}", tick);
}
Sourcepub fn switch_market_data_type(
&self,
market_data_type: MarketDataType,
) -> Result<(), Error>
pub fn switch_market_data_type( &self, market_data_type: MarketDataType, ) -> Result<(), Error>
Switches market data type returned from request_market_data requests to Live, Frozen, Delayed, or FrozenDelayed.
§Arguments
market_data_type
- Type of market data to retrieve.
§Examples
use ibapi::Client;
use ibapi::market_data::{MarketDataType};
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let market_data_type = MarketDataType::Live;
client.switch_market_data_type(market_data_type).expect("request failed");
println!("market data switched: {:?}", market_data_type);
Sourcepub fn market_depth<'a>(
&'a self,
contract: &Contract,
number_of_rows: i32,
is_smart_depth: bool,
) -> Result<Subscription<'a, MarketDepths>, Error>
pub fn market_depth<'a>( &'a self, contract: &Contract, number_of_rows: i32, is_smart_depth: bool, ) -> Result<Subscription<'a, MarketDepths>, Error>
Requests the contract’s market depth (order book).
§Arguments
contract
- The Contract for which the depth is being requested.number_of_rows
- The number of rows on each side of the order book.is_smart_depth
- Flag indicates that this is smart depth request.
§Examples
use ibapi::Client;
use ibapi::contracts::Contract;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("AAPL");
let subscription = client.market_depth(&contract, 5, true).expect("error requesting market depth");
for row in &subscription {
println!("row: {row:?}");
}
if let Some(error) = subscription.error() {
println!("error: {:?}", error);
}
Sourcepub fn market_depth_exchanges(
&self,
) -> Result<Vec<DepthMarketDataDescription>, Error>
pub fn market_depth_exchanges( &self, ) -> Result<Vec<DepthMarketDataDescription>, Error>
Requests venues for which market data is returned to market_depth (those with market makers)
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let exchanges = client.market_depth_exchanges().expect("error requesting market depth exchanges");
for exchange in &exchanges {
println!("{exchange:?}");
}
Sourcepub fn market_data(
&self,
contract: &Contract,
generic_ticks: &[&str],
snapshot: bool,
regulatory_snapshot: bool,
) -> Result<Subscription<'_, TickTypes>, Error>
pub fn market_data( &self, contract: &Contract, generic_ticks: &[&str], snapshot: bool, regulatory_snapshot: bool, ) -> Result<Subscription<'_, TickTypes>, Error>
Requests real time market data.
Returns market data for an instrument either in real time or 10-15 minutes delayed data.
§Arguments
contract
- Contract for which the data is being requested.generic_ticks
- IDs of the available generic ticks:- 100 Option Volume (currently for stocks)
- 101 Option Open Interest (currently for stocks)
- 104 Historical Volatility (currently for stocks)
- 105 Average Option Volume (currently for stocks)
- 106 Option Implied Volatility (currently for stocks)
- 162 Index Future Premium
- 165 Miscellaneous Stats
- 221 Mark Price (used in TWS P&L computations)
- 225 Auction values (volume, price and imbalance)
- 233 RTVolume - contains the last trade price, last trade size, last trade time, total volume, VWAP, and single trade flag.
- 236 Shortable
- 256 Inventory
- 258 Fundamental Ratios
- 411 Realtime Historical Volatility
- 456 IBDividends
snapshot
- for users with corresponding real time market data subscriptions. A true value will return a one-time snapshot, while a false value will provide streaming data.regulatory_snapshot
- snapshot for US stocks requests NBBO snapshots for users which have “US Securities Snapshot Bundle” subscription but not corresponding Network A, B, or C subscription necessary for streaming market data. One-time snapshot of current market price that will incur a fee of 1 cent to the account per snapshot.
§Examples
use ibapi::{contracts::Contract, market_data::realtime::TickTypes, Client};
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("AAPL");
// https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#available-tick-types
let generic_ticks = &["233", "293"];
let snapshot = false;
let regulatory_snapshot = false;
let subscription = client
.market_data(&contract, generic_ticks, snapshot, regulatory_snapshot)
.expect("error requesting market data");
for tick in &subscription {
match tick {
TickTypes::Price(tick_price) => println!("{:?}", tick_price),
TickTypes::Size(tick_size) => println!("{:?}", tick_size),
TickTypes::PriceSize(tick_price_size) => println!("{:?}", tick_price_size),
TickTypes::Generic(tick_generic) => println!("{:?}", tick_generic),
TickTypes::String(tick_string) => println!("{:?}", tick_string),
TickTypes::EFP(tick_efp) => println!("{:?}", tick_efp),
TickTypes::OptionComputation(option_computation) => println!("{:?}", option_computation),
TickTypes::RequestParameters(tick_request_parameters) => println!("{:?}", tick_request_parameters),
TickTypes::Notice(notice) => println!("{:?}", notice),
TickTypes::SnapshotEnd => subscription.cancel(),
}
}
Sourcepub fn news_providers(&self) -> Result<Vec<NewsProvider>, Error>
pub fn news_providers(&self) -> Result<Vec<NewsProvider>, Error>
Requests news providers which the user has subscribed to.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let news_providers = client.news_providers().expect("request news providers failed");
for news_provider in &news_providers {
println!("news provider {:?}", news_provider);
}
Sourcepub fn news_bulletins(
&self,
all_messages: bool,
) -> Result<Subscription<'_, NewsBulletin>, Error>
pub fn news_bulletins( &self, all_messages: bool, ) -> Result<Subscription<'_, NewsBulletin>, Error>
Subscribes to IB’s News Bulletins.
§Arguments
all_messages
- If set to true, will return all the existing bulletins for the current day, set to false to receive only the new bulletins.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let news_bulletins = client.news_bulletins(true).expect("request news providers failed");
for news_bulletin in &news_bulletins {
println!("news bulletin {:?}", news_bulletin);
}
Sourcepub fn historical_news(
&self,
contract_id: i32,
provider_codes: &[&str],
start_time: OffsetDateTime,
end_time: OffsetDateTime,
total_results: u8,
) -> Result<Subscription<'_, NewsArticle>, Error>
pub fn historical_news( &self, contract_id: i32, provider_codes: &[&str], start_time: OffsetDateTime, end_time: OffsetDateTime, total_results: u8, ) -> Result<Subscription<'_, NewsArticle>, Error>
Requests historical news headlines.
§Arguments
contract_id
- Contract ID of ticker. See contract_details for how to retrieve contract ID.provider_codes
- A list of provider codes.start_time
- Marks the (exclusive) start of the date range.end_time
- Marks the (inclusive) end of the date range.total_results
- The maximum number of headlines to fetch (1 – 300)
§Examples
use ibapi::Client;
use ibapi::contracts::Contract; // Or remove if conId is always known
use time::macros::datetime;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
// Example: Fetch historical news for a known contract ID (e.g., AAPL's conId)
let contract_id = 265598;
let provider_codes = &["DJNL", "BRFG"]; // Example provider codes
// Define a past date range for the news query
let start_time = datetime!(2023-01-01 0:00 UTC);
let end_time = datetime!(2023-01-02 0:00 UTC);
let total_results = 5u8; // Request a small number of articles for the example
let articles_subscription = client.historical_news(
contract_id,
provider_codes,
start_time,
end_time,
total_results,
).expect("request historical news failed");
println!("Requested historical news articles:");
for article in articles_subscription.iter().take(total_results as usize) {
println!("- Headline: {}, ID: {}, Provider: {}, Time: {}",
article.headline, article.article_id, article.provider_code, article.time);
}
Sourcepub fn news_article(
&self,
provider_code: &str,
article_id: &str,
) -> Result<NewsArticleBody, Error>
pub fn news_article( &self, provider_code: &str, article_id: &str, ) -> Result<NewsArticleBody, Error>
Requests news article body given articleId.
§Arguments
provider_code
- Short code indicating news provider, e.g. FLY.article_id
- ID of the specific article.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
// can get these using the historical_news method
let provider_code = "DJ-N";
let article_id = "DJ-N$1915168d";
let article = client.news_article(provider_code, article_id).expect("request news article failed");
println!("{:?}", article);
Sourcepub fn contract_news(
&self,
contract: &Contract,
provider_codes: &[&str],
) -> Result<Subscription<'_, NewsArticle>, Error>
pub fn contract_news( &self, contract: &Contract, provider_codes: &[&str], ) -> Result<Subscription<'_, NewsArticle>, Error>
Requests realtime contract specific news
§Arguments
contract
- Contract for which news is being requested.provider_codes
- Short codes indicating news providers, e.g. DJ-N.
§Examples
use ibapi::Client;
use ibapi::contracts::Contract;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract = Contract::stock("AAPL");
let provider_codes = ["DJ-N"];
let subscription = client.contract_news(&contract, &provider_codes).expect("request contract news failed");
for article in &subscription {
println!("{:?}", article);
}
Sourcepub fn broad_tape_news(
&self,
provider_code: &str,
) -> Result<Subscription<'_, NewsArticle>, Error>
pub fn broad_tape_news( &self, provider_code: &str, ) -> Result<Subscription<'_, NewsArticle>, Error>
Requests realtime BroadTape News
§Arguments
provider_code
- Short codes indicating news provider, e.g. DJ-N.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let provider_code = "BRFG";
let subscription = client.broad_tape_news(provider_code).expect("request broad tape news failed");
for article in &subscription {
println!("{:?}", article);
}
Sourcepub fn scanner_parameters(&self) -> Result<String, Error>
pub fn scanner_parameters(&self) -> Result<String, Error>
Requests an XML list of scanner parameters valid in TWS.
§Examples
use ibapi::Client;
use ibapi::scanner::ScannerSubscription;
use ibapi::orders::TagValue; // Or ensure common::TagValue is the correct path
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let mut sub = ScannerSubscription::default();
sub.instrument = Some("STK".to_string());
sub.location_code = Some("STK.US.MAJOR".to_string());
sub.scan_code = Some("TOP_PERC_GAIN".to_string());
// Further customize the subscription object as needed, for example:
// sub.above_price = Some(1.0);
// sub.below_price = Some(100.0);
// sub.number_of_rows = Some(20);
// Filter options are advanced and not always needed. Pass an empty Vec if not used.
let filter_options: Vec<TagValue> = Vec::new();
// Example of adding a filter:
// filter_options.push(TagValue { tag: "marketCapAbove".to_string(), value: "1000000000".to_string() });
match client.scanner_subscription(&sub, &filter_options) {
Ok(subscription) => {
// Iterate over received scanner data.
// Note: Scanner subscriptions can be continuous or return a snapshot.
// This example just takes the first batch if available.
if let Some(scanner_results_vec) = subscription.iter().next() {
println!("Scanner Results (first batch):");
for data in scanner_results_vec {
println!(" Rank: {}, Symbol: {}",
data.rank,
data.contract_details.contract.symbol);
}
} else {
println!("No scanner results received in the first check.");
}
// In a real application, you might continuously iterate or handle updates.
// Remember to cancel the subscription when no longer needed if it's continuous.
// subscription.cancel();
}
Err(e) => {
eprintln!("Failed to start scanner subscription: {}", e);
}
};
Sourcepub fn scanner_subscription(
&self,
subscription: &ScannerSubscription,
filter: &Vec<TagValue>,
) -> Result<Subscription<'_, Vec<ScannerData>>, Error>
pub fn scanner_subscription( &self, subscription: &ScannerSubscription, filter: &Vec<TagValue>, ) -> Result<Subscription<'_, Vec<ScannerData>>, Error>
Starts a subscription to market scan results based on the provided parameters.
§Examples
use ibapi::Client;
use ibapi::scanner::ScannerSubscription;
use ibapi::orders::TagValue;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let mut sub = ScannerSubscription::default();
sub.instrument = Some("STK".to_string());
sub.location_code = Some("STK.US.MAJOR".to_string());
sub.scan_code = Some("TOP_PERC_GAIN".to_string());
// Further customize the subscription object as needed, for example:
// sub.above_price = Some(1.0);
// sub.below_price = Some(100.0);
// sub.number_of_rows = Some(20);
// Filter options are advanced and not always needed. Pass an empty Vec if not used.
let mut filter_options: Vec<TagValue> = Vec::new();
// Example of adding a filter:
// filter_options.push(TagValue { tag: "marketCapAbove".to_string(), value: "1000000000".to_string() });
match client.scanner_subscription(&sub, &filter_options) {
Ok(subscription) => {
// Iterate over received scanner data.
// Note: Scanner subscriptions can be continuous or return a snapshot.
// This example just takes the first batch if available.
if let Some(scanner_results_vec) = subscription.iter().next() {
println!("Scanner Results (first batch):");
for data in scanner_results_vec {
println!(" Rank: {}, Symbol: {}",
data.rank,
data.contract_details.contract.symbol);
}
} else {
println!("No scanner results received in the first check.");
}
// In a real application, you might continuously iterate or handle updates.
// Remember to cancel the subscription when no longer needed if it's continuous.
// subscription.cancel();
}
Err(e) => {
eprintln!("Failed to start scanner subscription: {}", e);
}
};
Sourcepub fn wsh_metadata(&self) -> Result<WshMetadata, Error>
pub fn wsh_metadata(&self) -> Result<WshMetadata, Error>
Requests metadata from the WSH calendar.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let metadata = client.wsh_metadata().expect("request wsh metadata failed");
println!("{:?}", metadata);
Sourcepub fn wsh_event_data_by_contract(
&self,
contract_id: i32,
start_date: Option<Date>,
end_date: Option<Date>,
limit: Option<i32>,
auto_fill: Option<AutoFill>,
) -> Result<WshEventData, Error>
pub fn wsh_event_data_by_contract( &self, contract_id: i32, start_date: Option<Date>, end_date: Option<Date>, limit: Option<i32>, auto_fill: Option<AutoFill>, ) -> Result<WshEventData, Error>
Requests event data for a specified contract from the Wall Street Horizons (WSH) calendar.
§Arguments
contract_id
- Contract identifier for the event request.start_date
- Start date of the event request.end_date
- End date of the event request.limit
- Maximum number of events to return. Maximum of 100.auto_fill
- Fields to automatically fill in. See AutoFill for more information.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let contract_id = 76792991; // TSLA
let event_data = client.wsh_event_data_by_contract(contract_id, None, None, None, None).expect("request wsh event data failed");
println!("{:?}", event_data);
Sourcepub fn wsh_event_data_by_filter(
&self,
filter: &str,
limit: Option<i32>,
auto_fill: Option<AutoFill>,
) -> Result<Subscription<'_, WshEventData>, Error>
pub fn wsh_event_data_by_filter( &self, filter: &str, limit: Option<i32>, auto_fill: Option<AutoFill>, ) -> Result<Subscription<'_, WshEventData>, Error>
Requests event data from the Wall Street Horizons (WSH) calendar using a JSON filter.
§Arguments
filter
- Json-formatted string containing all filter values.limit
- Maximum number of events to return. Maximum of 100.auto_fill
- Fields to automatically fill in. See AutoFill for more information.
§Examples
use ibapi::Client;
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
let filter = ""; // see https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#wsheventdata-object
let event_data = client.wsh_event_data_by_filter(filter, None, None).expect("request wsh event data failed");
println!("{:?}", event_data);