ibapi/orders/mod.rs
1//! # Order Management
2//!
3//! This module provides comprehensive order management capabilities including
4//! order creation, modification, cancellation, and execution tracking.
5//! It supports various order types and advanced trading features.
6//!
7//! ## Features
8//!
9//! - **Order Placement**: Submit market, limit, stop, and other order types
10//! - **Order Modification**: Modify existing orders with new parameters
11//! - **Order Cancellation**: Cancel individual orders or all open orders
12//! - **Execution Tracking**: Monitor order fills and execution details
13//! - **Commission Reports**: Track commission charges for executed trades
14//! - **Order Status Updates**: Real-time updates on order state changes
15//!
16//! ## Order Types
17//!
18//! The module supports various order types including:
19//! - Market orders
20//! - Limit orders
21//! - Stop orders
22//! - Stop-limit orders
23//! - Trailing stop orders
24//! - VWAP/TWAP algorithmic orders
25//! - And many more specialized order types
26//!
27//! ## Usage
28//!
29//! Orders are created using the `Order` struct and can be customized with various
30//! parameters. The `order_builder` module provides a fluent API for constructing
31//! complex orders.
32
33// Common implementation modules
34pub(crate) mod common;
35
36/// Fluent builder APIs for constructing orders.
37pub mod builder;
38
39/// Order condition types for conditional orders.
40pub mod conditions;
41
42/// Convenience re-export for low-level order builder helpers.
43pub use common::order_builder;
44
45// Re-export builder types
46pub use builder::{BracketOrderBuilder, BracketOrderIds, OrderBuilder, OrderId};
47
48// Re-export condition types and builders
49pub use conditions::{
50 ExecutionCondition, ExecutionConditionBuilder, MarginCondition, MarginConditionBuilder, PercentChangeCondition, PercentChangeConditionBuilder,
51 PriceCondition, PriceConditionBuilder, TimeCondition, TimeConditionBuilder, VolumeCondition, VolumeConditionBuilder,
52};
53
54use std::convert::From;
55use std::fmt::Debug;
56
57use serde::{Deserialize, Serialize};
58
59use crate::contracts::Contract;
60use crate::{encode_option_field, ToField};
61
62// Public types - always available regardless of feature flags
63
64/// Make sure to test using only your paper trading account when applicable. A good way of finding out if an order type/exchange combination
65/// is possible is by trying to place such order manually using the TWS.
66/// Before contacting our API support team please refer to the available documentation.
67pub use crate::contracts::TagValue;
68
69pub(crate) const COMPETE_AGAINST_BEST_OFFSET_UP_TO_MID: Option<f64> = Some(f64::INFINITY);
70
71#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
72#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
73/// Order describes the order.
74pub struct Order {
75 /// The API client's order id.
76 pub order_id: i32,
77 /// The Solicited field should be used for orders initiated or recommended by the broker or adviser that were approved by the client (by phone, email, chat, verbally, etc.) prior to entry. Please note that orders that the adviser or broker placed without specifically discussing with the client are discretionary orders, not solicited.
78 pub solicited: bool,
79 /// The API client id which placed the order.
80 pub client_id: i32,
81 /// The Host order identifier.
82 pub perm_id: i32,
83 /// Identifies the side.
84 /// Generally available values are BUY and SELL.
85 /// Additionally, SSHORT and SLONG are available in some institutional-accounts only.
86 /// For general account types, a SELL order will be able to enter a short position automatically if the order quantity is larger than your current long position.
87 /// SSHORT is only supported for institutional account configured with Long/Short account segments or clearing with a separate account.
88 /// SLONG is available in specially-configured institutional accounts to indicate that long position not yet delivered is being sold.
89 pub action: Action,
90 /// The number of positions being bought/sold.
91 pub total_quantity: f64,
92 /// The order's type.
93 pub order_type: String,
94 /// The LIMIT price.
95 /// Used for limit, stop-limit and relative orders. In all other cases specify zero. For relative orders with no limit price, also specify zero.
96 pub limit_price: Option<f64>,
97 /// Generic field to contain the stop price for STP LMT orders, trailing amount, etc.
98 pub aux_price: Option<f64>,
99 /// The time in force - specifies how long the order remains active.
100 ///
101 /// See [`TimeInForce`] for available options and their behavior.
102 pub tif: TimeInForce,
103 /// One-Cancels-All group identifier.
104 pub oca_group: String,
105 /// Tells how to handle remaining orders in an OCA group when one order or part of an order executes.
106 ///
107 /// See [`OcaType`] for available options. "With block" provides overfill protection by ensuring
108 /// only one order in the group is routed at a time.
109 pub oca_type: OcaType,
110 /// The order reference.
111 /// Intended for institutional customers only, although all customers may use it to identify the API client that sent the order when multiple API clients are running.
112 pub order_ref: String,
113 /// Specifies whether the order will be transmitted by TWS. If set to false, the order will be created at TWS but will not be sent.
114 pub transmit: bool,
115 /// The order ID of the parent order, used for bracket and auto trailing stop orders.
116 pub parent_id: i32,
117 /// If set to true, specifies that the order is an ISE Block order.
118 pub block_order: bool,
119 /// If set to true, specifies that the order is a Sweep-to-Fill order.
120 pub sweep_to_fill: bool,
121 /// The publicly disclosed order size, used when placing Iceberg orders.
122 pub display_size: Option<i32>,
123 /// Specifies how Simulated Stop, Stop-Limit and Trailing Stop orders are triggered.
124 ///
125 /// See [`conditions::TriggerMethod`] for available options.
126 pub trigger_method: conditions::TriggerMethod,
127 /// If set to true, allows orders to also trigger or fill outside of regular trading hours.
128 pub outside_rth: bool,
129 /// If set to true, the order will not be visible when viewing the market depth. This option only applies to orders routed to the NASDAQ exchange.
130 pub hidden: bool,
131 /// Specifies the date and time after which the order will be active.
132 /// Format: yyyymmdd hh:mm:ss {optional Timezone}.
133 pub good_after_time: String,
134 /// The date and time until the order will be active.
135 /// You must enter GTD as the time in force to use this string. The trade's "Good Till Date," format "yyyyMMdd HH:mm:ss (optional time zone)" or UTC "yyyyMMdd-HH:mm:ss".
136 pub good_till_date: String,
137 /// Overrides TWS constraints.
138 /// Precautionary constraints are defined on the TWS Presets page, and help ensure that your price and size order values are reasonable. Orders sent from the API are also validated against these safety constraints, and may be rejected if any constraint is violated. To override validation, set this parameter's value to True.
139 pub override_percentage_constraints: bool,
140 /// NYSE Rule 80A designation values:
141 /// - Individual = `I`
142 /// - Agency = `A`
143 /// - AgentOtherMember = `W`
144 /// - IndividualPTIA = `J`
145 /// - AgencyPTIA = `U`
146 /// - AgentOtherMemberPTIA = `M`
147 /// - IndividualPT = `K`
148 /// - AgencyPT = `Y`
149 /// - AgentOtherMemberPT = `N`
150 pub rule_80_a: Option<Rule80A>,
151 /// Indicates whether or not all the order has to be filled on a single execution.
152 pub all_or_none: bool,
153 /// Identifies a minimum quantity order type.
154 pub min_qty: Option<i32>,
155 /// The percent offset amount for relative orders.
156 pub percent_offset: Option<f64>,
157 /// Trail stop price for TRAIL LIMIT orders.
158 pub trail_stop_price: Option<f64>,
159 /// Specifies the trailing amount of a trailing stop order as a percentage.
160 ///
161 /// Observe the following guidelines when using the trailingPercent field:
162 /// - This field is mutually exclusive with the existing trailing amount. That is, the API client can send one
163 /// or the other but not both.
164 /// - This field is read AFTER the stop price (barrier price) as follows: deltaNeutralAuxPrice stopPrice,
165 /// trailingPercent, scale order attributes.
166 /// - The field will also be sent to the API in the openOrder message if the API client version is >= 56.
167 /// It is sent after the stopPrice field as follows: stopPrice, trailingPct, basisPoint.
168 pub trailing_percent: Option<f64>,
169 /// The Financial Advisor group the trade will be allocated to. Use an empty string if not applicable.
170 pub fa_group: String,
171 /// The Financial Advisor allocation profile the trade will be allocated to. Use an empty string if not applicable.
172 pub fa_profile: String,
173 /// The Financial Advisor allocation method the trade will be allocated to. Use an empty string if not applicable.
174 pub fa_method: String,
175 /// The Financial Advisor percentage concerning the trade's allocation. Use an empty string if not applicable.
176 pub fa_percentage: String,
177 /// For institutional customers only. Valid values are O (open) and C (close).
178 ///
179 /// Available for institutional clients to determine if this order is to open or close a position.
180 /// - When Action = "BUY" and OpenClose = "O" this will open a new position.
181 /// - When Action = "BUY" and OpenClose = "C" this will close an existing short position.
182 pub open_close: Option<OrderOpenClose>,
183 /// The order's origin. Same as TWS "Origin" column. Identifies the type of customer from which the order originated.
184 ///
185 /// See [`OrderOrigin`] for available options.
186 pub origin: OrderOrigin,
187 /// For institutions only. Specifies the short sale slot.
188 ///
189 /// See [`ShortSaleSlot`] for available options.
190 pub short_sale_slot: ShortSaleSlot,
191 /// For institutions only. Indicates the location where the shares to short come from.
192 /// Used only when short sale slot is set to `ThirdParty`.
193 pub designated_location: String,
194 /// Only available with IB Execution-Only accounts with applicable securities.
195 /// Mark order as exempt from short sale uptick rule.
196 pub exempt_code: i32,
197 /// The amount off the limit price allowed for discretionary orders.
198 pub discretionary_amt: f64,
199 /// Use to opt out of default SmartRouting for orders routed directly to ASX.
200 /// This attribute defaults to false unless explicitly set to true.
201 /// When set to false, orders routed directly to ASX will NOT use SmartRouting.
202 /// When set to true, orders routed directly to ASX orders WILL use SmartRouting.
203 pub opt_out_smart_routing: bool,
204 /// For BOX orders only.
205 ///
206 /// See [`AuctionStrategy`] for available options.
207 pub auction_strategy: Option<AuctionStrategy>,
208 /// The auction's starting price. For BOX orders only.
209 pub starting_price: Option<f64>,
210 /// The stock's reference price.
211 /// The reference price is used for VOL orders to compute the limit price sent to an exchange (whether or not Continuous Update is selected), and for price range monitoring.
212 pub stock_ref_price: Option<f64>,
213 /// The stock's Delta. For orders on BOX only.
214 pub delta: Option<f64>,
215 /// The lower value for the acceptable underlying stock price range.
216 /// For price improvement option orders on BOX and VOL orders with dynamic management.
217 pub stock_range_lower: Option<f64>,
218 /// The upper value for the acceptable underlying stock price range.
219 /// For price improvement option orders on BOX and VOL orders with dynamic management.
220 pub stock_range_upper: Option<f64>,
221 /// The option price in volatility, as calculated by TWS' Option Analytics.
222 /// This value is expressed as a percent and is used to calculate the limit price sent to the exchange.
223 pub volatility: Option<f64>,
224 /// VOL orders only. See [`VolatilityType`] for available options.
225 pub volatility_type: Option<VolatilityType>,
226 /// Specifies whether TWS will automatically update the limit price of the order as the underlying price moves. VOL orders only.
227 pub continuous_update: bool,
228 /// Specifies how you want TWS to calculate the limit price for options, and for stock range price monitoring.
229 /// VOL orders only.
230 ///
231 /// See [`ReferencePriceType`] for available options.
232 pub reference_price_type: Option<ReferencePriceType>,
233 /// Enter an order type to instruct TWS to submit a delta neutral trade on full or partial execution of the VOL order. VOL orders only. For no hedge delta order to be sent, specify NONE.
234 pub delta_neutral_order_type: String,
235 /// Use this field to enter a value if the value in the deltaNeutralOrderType field is an order type that requires an Aux price, such as a REL order. VOL orders only.
236 pub delta_neutral_aux_price: Option<f64>,
237 /// The unique contract identifier specifying the security in Delta Neutral order.
238 pub delta_neutral_con_id: i32,
239 /// Indicates the firm which will settle the Delta Neutral trade. Institutions only.
240 pub delta_neutral_settling_firm: String,
241 /// Specifies the beneficiary of the Delta Neutral order.
242 pub delta_neutral_clearing_account: String,
243 /// Specifies where the clients want their shares to be cleared at. Must be specified by execution-only clients.
244 ///
245 /// Valid values are:
246 /// - `IB`
247 /// - `Away`
248 /// - `PTA` (post trade allocation)
249 pub delta_neutral_clearing_intent: String,
250 /// Specifies whether the order is an Open or a Close order and is used when the hedge involves a CFD and and the order is clearing away.
251 pub delta_neutral_open_close: String,
252 /// Used when the hedge involves a stock and indicates whether or not it is sold short.
253 pub delta_neutral_short_sale: bool,
254 /// Indicates a short sale Delta Neutral order. Has a value of 1 (the clearing broker holds shares) or 2 (delivered from a third party). If you use 2, then you must specify a deltaNeutralDesignatedLocation.
255 pub delta_neutral_short_sale_slot: i32,
256 /// Identifies third party order origin. Used only when deltaNeutralShortSaleSlot = 2.
257 pub delta_neutral_designated_location: String,
258 /// Specifies Basis Points for EFP order. The values increment in 0.01% = 1 basis point. For EFP orders only.
259 pub basis_points: Option<f64>,
260 /// Specifies the increment of the Basis Points. For EFP orders only.
261 pub basis_points_type: Option<i32>,
262 /// Defines the size of the first, or initial, order component. For Scale orders only.
263 pub scale_init_level_size: Option<i32>,
264 /// Defines the order size of the subsequent scale order components. For Scale orders only. Used in conjunction with scaleInitLevelSize().
265 pub scale_subs_level_size: Option<i32>,
266 /// Defines the price increment between scale components. For Scale orders only. This value is compulsory.
267 pub scale_price_increment: Option<f64>,
268 /// Modifies the value of the Scale order. For extended Scale orders.
269 pub scale_price_adjust_value: Option<f64>,
270 /// Specifies the interval when the price is adjusted. For extended Scale orders.
271 pub scale_price_adjust_interval: Option<i32>,
272 /// Specifies the offset when to adjust profit. For extended scale orders.
273 pub scale_profit_offset: Option<f64>,
274 /// Restarts the Scale series if the order is cancelled. For extended scale orders.
275 pub scale_auto_reset: bool,
276 /// The initial position of the Scale order. For extended scale orders.
277 pub scale_init_position: Option<i32>,
278 /// Specifies the initial quantity to be filled. For extended scale orders.
279 pub scale_init_fill_qty: Option<i32>,
280 /// Defines the random percent by which to adjust the position. For extended scale orders.
281 pub scale_random_percent: bool,
282 /// For hedge orders.
283 ///
284 /// Possible values include:
285 /// - `D` - Delta
286 /// - `B` - Beta
287 /// - `F` - FX
288 /// - `P` - Pair
289 pub hedge_type: String,
290 /// For hedge orders.
291 /// Beta = x for Beta hedge orders, ratio = y for Pair hedge order
292 pub hedge_param: String,
293 /// The account the trade will be allocated to.
294 pub account: String,
295 /// Indicates the firm which will settle the trade. Institutions only.
296 pub settling_firm: String,
297 /// Specifies the true beneficiary of the order.
298 /// For IBExecution customers. This value is required for FUT/FOP orders for reporting to the exchange.
299 pub clearing_account: String,
300 /// For execution-only clients to know where do they want their shares to be cleared at.
301 ///
302 /// Valid values are:
303 /// - `IB`
304 /// - `Away`
305 /// - `PTA` (post trade allocation)
306 pub clearing_intent: String,
307 /// The algorithm strategy.
308 ///
309 /// As of API version 9.6, the following algorithms are supported:
310 /// - `ArrivalPx` - Arrival Price
311 /// - `DarkIce` - Dark Ice
312 /// - `PctVol` - Percentage of Volume
313 /// - `Twap` - TWAP (Time Weighted Average Price)
314 /// - `Vwap` - VWAP (Volume Weighted Average Price)
315 ///
316 /// For more information about IB's API algorithms, refer to [https://www.interactivebrokers.com/en/software/api/apiguide/tables/ibalgo_parameters.htm](https://www.interactivebrokers.com/en/software/api/apiguide/tables/ibalgo_parameters.htm)
317 pub algo_strategy: String,
318 /// The list of parameters for the IB algorithm.
319 /// For more information about IB's API algorithms, refer to [https://www.interactivebrokers.com/en/software/api/apiguide/tables/ibalgo_parameters.htm](https://www.interactivebrokers.com/en/software/api/apiguide/tables/ibalgo_parameters.htm)
320 pub algo_params: Vec<TagValue>,
321 /// Allows to retrieve the commissions and margin information.
322 /// When placing an order with this attribute set to true, the order will not be placed as such. Instead it will used to request the commissions and margin information that would result from this order.
323 pub what_if: bool,
324 /// Identifies orders generated by algorithmic trading.
325 pub algo_id: String,
326 /// Orders routed to IBDARK are tagged as "post only" and are held in IB's order book, where incoming SmartRouted orders from other IB customers are eligible to trade against them.
327 /// For IBDARK orders only.
328 pub not_held: bool,
329 /// Advanced parameters for Smart combo routing.
330 /// These features are for both guaranteed and non-guaranteed combination orders routed to Smart, and are available based on combo type and order type. SmartComboRoutingParams is similar to AlgoParams in that it makes use of tag/value pairs to add parameters to combo orders.
331 /// Make sure that you fully understand how Advanced Combo Routing works in TWS itself first: <https://guides.interactivebrokers.com/tws/twsguide.htm#usersguidebook/specializedorderentry/advanced_combo_routing.htm>
332 /// The parameters cover the following capabilities:
333 ///
334 /// * Non-Guaranteed - Determine if the combo order is Guaranteed or Non-Guaranteed.
335 /// <br/>Tag = NonGuaranteed
336 /// <br/>Value = 0: The order is guaranteed
337 /// <br/>Value = 1: The order is non-guaranteed
338 ///
339 /// * Select Leg to Fill First - User can specify which leg to be executed first.
340 /// <br/>Tag = LeginPrio
341 /// <br/>Value = -1: No priority is assigned to either combo leg
342 /// <br/>Value = 0: Priority is assigned to the first leg being added to the comboLeg
343 /// <br/>Value = 1: Priority is assigned to the second leg being added to the comboLeg
344 /// <br/>Note: The LeginPrio parameter can only be applied to two-legged combo.
345 ///
346 /// * Maximum Leg-In Combo Size - Specify the maximum allowed leg-in size per segment
347 /// <br/>Tag = MaxSegSize
348 /// <br/>Value = Unit of combo size
349 ///
350 /// * Do Not Start Next Leg-In if Previous Leg-In Did Not Finish - Specify whether or not the system should attempt to fill the next segment before the current segment fills.
351 /// <br/>Tag = DontLeginNext
352 /// <br/>Value = 0: Start next leg-in even if previous leg-in did not finish
353 /// <br/>Value = 1: Do not start next leg-in if previous leg-in did not finish
354 ///
355 /// * Price Condition - Combo order will be rejected or cancelled if the leg market price is outside of the specified price range [CondPriceMin, CondPriceMax]
356 /// <br/>Tag = PriceCondConid: The ContractID of the combo leg to specify price condition on
357 /// <br/>Value = The ContractID
358 /// <br/>Tag = CondPriceMin: The lower price range of the price condition
359 /// <br/>Value = The lower price
360 /// <br/>Tag = CondPriceMax: The upper price range of the price condition
361 /// <br/>Value = The upper price
362 pub smart_combo_routing_params: Vec<TagValue>,
363 /// List of Per-leg price following the same sequence combo legs are added. The combo price must be left unspecified when using per-leg prices.
364 pub order_combo_legs: Vec<OrderComboLeg>,
365 /// For internal use only. Use the default value XYZ.
366 pub order_misc_options: Vec<TagValue>,
367 /// Defines the start time of GTC orders.
368 pub active_start_time: String,
369 /// Defines the stop time of GTC orders.
370 pub active_stop_time: String,
371 /// The list of scale orders. Used for scale orders.
372 pub scale_table: String,
373 /// Is used to place an order to a model. For example, "Technology" model can be used for tech stocks first created in TWS.
374 pub model_code: String,
375 /// This is a regulatory attribute that applies to all US Commodity (Futures) Exchanges, provided to allow client to comply with CFTC Tag 50 Rules.
376 pub ext_operator: String,
377 /// The native cash quantity.
378 pub cash_qty: Option<f64>,
379 /// Identifies a person as the responsible party for investment decisions within the firm. Orders covered by MiFID 2 (Markets in Financial Instruments Directive 2) must include either Mifid2DecisionMaker or Mifid2DecisionAlgo field (but not both). Requires TWS 969+.
380 pub mifid2_decision_maker: String,
381 /// Identifies the algorithm responsible for investment decisions within the firm. Orders covered under MiFID 2 must include either Mifid2DecisionMaker or Mifid2DecisionAlgo, but cannot have both. Requires TWS 969+.
382 pub mifid2_decision_algo: String,
383 /// For MiFID 2 reporting; identifies a person as the responsible party for the execution of a transaction within the firm. Requires TWS 969+.
384 pub mifid2_execution_trader: String,
385 /// For MiFID 2 reporting; identifies the algorithm responsible for the execution of a transaction within the firm. Requires TWS 969+.
386 pub mifid2_execution_algo: String,
387 /// Don't use auto price for hedge.
388 pub dont_use_auto_price_for_hedge: bool,
389 /// Specifies the date to auto cancel the order.
390 pub auto_cancel_date: String, // TODO date object
391 /// Specifies the initial order quantity to be filled.
392 pub filled_quantity: f64,
393 /// Identifies the reference future conId.
394 pub ref_futures_con_id: Option<i32>,
395 /// Cancels the parent order if child order was cancelled.
396 pub auto_cancel_parent: bool,
397 /// Identifies the Shareholder.
398 pub shareholder: String,
399 /// Used to specify "imbalance only open orders" or "imbalance only closing orders".
400 pub imbalance_only: bool,
401 /// Routes market order to Best Bid Offer.
402 pub route_marketable_to_bbo: bool,
403 /// Parent order Id.
404 pub parent_perm_id: Option<i64>,
405 /// Accepts a list with parameters obtained from advancedOrderRejectJson.
406 pub advanced_error_override: String,
407 /// Used by brokers and advisors when manually entering, modifying or cancelling orders at the direction of a client. Only used when allocating orders to specific groups or accounts. Excluding "All" group.
408 pub manual_order_time: String,
409 /// Defines the minimum trade quantity to fill. For IBKRATS orders.
410 pub min_trade_qty: Option<i32>,
411 /// Defines the minimum size to compete. For IBKRATS orders.
412 pub min_compete_size: Option<i32>,
413 /// Specifies the offset off the midpoint that will be applied to the order. For IBKRATS orders.
414 pub compete_against_best_offset: Option<f64>,
415 /// his offset is applied when the spread is an even number of cents wide. This offset must be in whole-penny increments or zero. For IBKRATS orders.
416 pub mid_offset_at_whole: Option<f64>,
417 /// This offset is applied when the spread is an odd number of cents wide. This offset must be in half-penny increments. For IBKRATS orders.
418 pub mid_offset_at_half: Option<f64>,
419 /// Randomizes the order's size. Only for Volatility and Pegged to Volatility orders.
420 pub randomize_size: bool,
421 /// Randomizes the order's price. Only for Volatility and Pegged to Volatility orders.
422 pub randomize_price: bool,
423 /// Pegged-to-benchmark orders: this attribute will contain the conId of the contract against which the order will be pegged.
424 pub reference_contract_id: i32,
425 /// Pegged-to-benchmark orders: indicates whether the order's pegged price should increase or decreases.
426 pub is_pegged_change_amount_decrease: bool,
427 /// Pegged-to-benchmark orders: amount by which the order's pegged price should move.
428 pub pegged_change_amount: Option<f64>,
429 /// Pegged-to-benchmark orders: the amount the reference contract needs to move to adjust the pegged order.
430 pub reference_change_amount: Option<f64>,
431 /// Pegged-to-benchmark orders: the exchange against which we want to observe the reference contract.
432 pub reference_exchange: String,
433 /// Adjusted Stop orders: the parent order will be adjusted to the given type when the adjusted trigger price is penetrated.
434 pub adjusted_order_type: String,
435 /// Adjusted Stop orders: specifies the trigger price to execute.
436 pub trigger_price: Option<f64>,
437 /// Adjusted Stop orders: specifies the price offset for the stop to move in increments.
438 pub limit_price_offset: Option<f64>,
439 /// Adjusted Stop orders: specifies the stop price of the adjusted (STP) parent.
440 pub adjusted_stop_price: Option<f64>,
441 /// Adjusted Stop orders: specifies the stop limit price of the adjusted (STPL LMT) parent.
442 pub adjusted_stop_limit_price: Option<f64>,
443 /// Adjusted Stop orders: specifies the trailing amount of the adjusted (TRAIL) parent.
444 pub adjusted_trailing_amount: Option<f64>,
445 /// Adjusted Stop orders: specifies where the trailing unit is an amount (set to 0) or a percentage (set to 1)
446 pub adjustable_trailing_unit: i32,
447 /// Conditions determining when the order will be activated or canceled.
448 pub conditions: Vec<OrderCondition>,
449 /// Indicates whether or not conditions will also be valid outside Regular Trading Hours.
450 pub conditions_ignore_rth: bool,
451 /// Conditions can determine if an order should become active or canceled.
452 pub conditions_cancel_order: bool,
453 /// Define the Soft Dollar Tier used for the order. Only provided for registered professional advisors and hedge and mutual funds.
454 pub soft_dollar_tier: SoftDollarTier,
455 /// Set to true to create tickets from API orders when TWS is used as an OMS.
456 pub is_oms_container: bool,
457 /// Set to true to convert order of type 'Primary Peg' to 'D-Peg'.
458 pub discretionary_up_to_limit_price: bool,
459 /// Specifies wether to use Price Management Algo. CTCI users only.
460 pub use_price_mgmt_algo: bool,
461 /// Specifies the duration of the order. Format: yyyymmdd hh:mm:ss TZ. For GTD orders.
462 pub duration: Option<i32>, // TODO date object?
463 /// Value must be positive, and it is number of seconds that SMART order would be parked for at IBKRATS before being routed to exchange.
464 pub post_to_ats: Option<i32>,
465 /// Customer account information for completed orders.
466 pub customer_account: String,
467 /// Indicates if this is a professional customer order.
468 pub professional_customer: bool,
469 /// Accrued interest for bond orders.
470 pub bond_accrued_interest: String,
471 /// Include overnight trading.
472 pub include_overnight: bool,
473 /// Manual order indicator.
474 pub manual_order_indicator: Option<i32>,
475 /// Identifies the submitter of the order.
476 pub submitter: String,
477}
478
479impl Default for Order {
480 fn default() -> Self {
481 Self {
482 order_id: 0,
483 solicited: false,
484 client_id: 0,
485 perm_id: 0,
486 action: Action::Buy,
487 total_quantity: 0.0,
488 order_type: "".to_owned(),
489 limit_price: None,
490 aux_price: None,
491 tif: TimeInForce::Day,
492 oca_group: "".to_owned(),
493 oca_type: OcaType::None,
494 order_ref: "".to_owned(),
495 transmit: true,
496 parent_id: 0,
497 block_order: false,
498 sweep_to_fill: false,
499 display_size: Some(0), // TODO - default to None?
500 trigger_method: conditions::TriggerMethod::Default,
501 outside_rth: false,
502 hidden: false,
503 good_after_time: "".to_owned(),
504 good_till_date: "".to_owned(),
505 override_percentage_constraints: false,
506 rule_80_a: None,
507 all_or_none: false,
508 min_qty: None,
509 percent_offset: None,
510 trail_stop_price: None,
511 trailing_percent: None,
512 fa_group: "".to_owned(),
513 fa_profile: "".to_owned(),
514 fa_method: "".to_owned(),
515 fa_percentage: "".to_owned(),
516 open_close: None,
517 origin: OrderOrigin::Customer,
518 short_sale_slot: ShortSaleSlot::None,
519 designated_location: "".to_owned(),
520 exempt_code: -1,
521 discretionary_amt: 0.0,
522 opt_out_smart_routing: false,
523 auction_strategy: None,
524 starting_price: None,
525 stock_ref_price: None,
526 delta: None,
527 stock_range_lower: None,
528 stock_range_upper: None,
529 volatility: None,
530 volatility_type: None,
531 continuous_update: false,
532 reference_price_type: None,
533 delta_neutral_order_type: "".to_owned(),
534 delta_neutral_aux_price: None,
535 delta_neutral_con_id: 0,
536 delta_neutral_settling_firm: "".to_owned(),
537 delta_neutral_clearing_account: "".to_owned(),
538 delta_neutral_clearing_intent: "".to_owned(),
539 delta_neutral_open_close: "".to_owned(),
540 delta_neutral_short_sale: false,
541 delta_neutral_short_sale_slot: 0,
542 delta_neutral_designated_location: "".to_owned(),
543 basis_points: Some(0.0),
544 basis_points_type: Some(0),
545 scale_init_level_size: None,
546 scale_subs_level_size: None,
547 scale_price_increment: None,
548 scale_price_adjust_value: None,
549 scale_price_adjust_interval: None,
550 scale_profit_offset: None,
551 scale_auto_reset: false,
552 scale_init_position: None,
553 scale_init_fill_qty: None,
554 scale_random_percent: false,
555 hedge_type: "".to_owned(),
556 hedge_param: "".to_owned(),
557 account: "".to_owned(),
558 settling_firm: "".to_owned(),
559 clearing_account: "".to_owned(),
560 clearing_intent: "".to_owned(),
561 algo_strategy: "".to_owned(),
562 algo_params: vec![],
563 what_if: false,
564 algo_id: "".to_owned(),
565 not_held: false,
566 smart_combo_routing_params: vec![],
567 order_combo_legs: vec![],
568 order_misc_options: vec![],
569 active_start_time: "".to_owned(),
570 active_stop_time: "".to_owned(),
571 scale_table: "".to_owned(),
572 model_code: "".to_owned(),
573 ext_operator: "".to_owned(),
574 cash_qty: None,
575 mifid2_decision_maker: "".to_owned(),
576 mifid2_decision_algo: "".to_owned(),
577 mifid2_execution_trader: "".to_owned(),
578 mifid2_execution_algo: "".to_owned(),
579 dont_use_auto_price_for_hedge: false,
580 auto_cancel_date: "".to_owned(),
581 filled_quantity: 0.0,
582 ref_futures_con_id: Some(0),
583 auto_cancel_parent: false,
584 shareholder: "".to_owned(),
585 imbalance_only: false,
586 route_marketable_to_bbo: false,
587 parent_perm_id: None,
588 advanced_error_override: "".to_owned(),
589 manual_order_time: "".to_owned(),
590 min_trade_qty: None,
591 min_compete_size: None,
592 compete_against_best_offset: None,
593 mid_offset_at_whole: None,
594 mid_offset_at_half: None,
595 randomize_size: false,
596 randomize_price: false,
597 reference_contract_id: 0,
598 is_pegged_change_amount_decrease: false,
599 pegged_change_amount: Some(0.0),
600 reference_change_amount: Some(0.0),
601 reference_exchange: "".to_owned(),
602 adjusted_order_type: "".to_owned(),
603 trigger_price: None,
604 limit_price_offset: None,
605 adjusted_stop_price: None,
606 adjusted_stop_limit_price: None,
607 adjusted_trailing_amount: None,
608 adjustable_trailing_unit: 0,
609 conditions: vec![],
610 conditions_ignore_rth: false,
611 conditions_cancel_order: false,
612 soft_dollar_tier: SoftDollarTier::default(),
613 is_oms_container: false,
614 discretionary_up_to_limit_price: false,
615 use_price_mgmt_algo: false,
616 duration: None,
617 post_to_ats: None,
618 customer_account: String::new(),
619 professional_customer: false,
620 bond_accrued_interest: String::new(),
621 include_overnight: false,
622 manual_order_indicator: None,
623 submitter: String::new(),
624 }
625 }
626}
627
628impl Order {
629 /// Returns `true` if delta-neutral parameters are configured.
630 pub fn is_delta_neutral(&self) -> bool {
631 !self.delta_neutral_order_type.is_empty()
632 }
633
634 /// Returns `true` if scale order parameters are configured.
635 pub fn is_scale_order(&self) -> bool {
636 match self.scale_price_increment {
637 Some(price_increment) => price_increment > 0.0,
638 _ => false,
639 }
640 }
641}
642
643/// Identifies the side.
644/// Generally available values are BUY and SELL.
645/// Additionally, SSHORT and SLONG are available in some institutional-accounts only.
646/// For general account types, a SELL order will be able to enter a short position automatically if the order quantity is larger than your current long position.
647/// SSHORT is only supported for institutional account configured with Long/Short account segments or clearing with a separate account.
648/// SLONG is available in specially-configured institutional accounts to indicate that long position not yet delivered is being sold.
649#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
650#[derive(Clone, Debug, Default, PartialEq, Eq, Copy, Serialize, Deserialize)]
651pub enum Action {
652 #[default]
653 /// Buy-side order.
654 Buy,
655 /// Sell-side order.
656 Sell,
657 /// SSHORT is only supported for institutional account configured with Long/Short account segments or clearing with a separate account.
658 SellShort,
659 /// SLONG is available in specially-configured institutional accounts to indicate that long position not yet delivered is being sold.
660 SellLong,
661}
662
663impl ToField for Action {
664 fn to_field(&self) -> String {
665 self.to_string()
666 }
667}
668
669impl std::fmt::Display for Action {
670 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
671 let text = match self {
672 Action::Buy => "BUY",
673 Action::Sell => "SELL",
674 Action::SellShort => "SSHORT",
675 Action::SellLong => "SLONG",
676 };
677
678 write!(f, "{text}")
679 }
680}
681
682impl Action {
683 /// Return the logical opposite action (buy ↔ sell).
684 pub fn reverse(self) -> Action {
685 match self {
686 Action::Buy => Action::Sell,
687 Action::Sell => Action::Buy,
688 Action::SellShort => Action::SellLong,
689 Action::SellLong => Action::SellShort,
690 }
691 }
692
693 /// Parse an action from the TWS string identifier.
694 pub fn from(name: &str) -> Self {
695 match name {
696 "BUY" => Self::Buy,
697 "SELL" => Self::Sell,
698 "SSHORT" => Self::SellShort,
699 "SLONG" => Self::SellLong,
700 &_ => todo!(),
701 }
702 }
703}
704
705/// Time in force specifies how long an order remains active.
706#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
707#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
708pub enum TimeInForce {
709 /// Valid for the day only.
710 #[default]
711 Day,
712 /// Good until canceled. The order will continue to work within the system and in the marketplace
713 /// until it executes or is canceled. GTC orders will be automatically cancelled under certain conditions.
714 GoodTilCanceled,
715 /// Immediate or Cancel. Any portion that is not filled as soon as it becomes available in the
716 /// market is canceled.
717 ImmediateOrCancel,
718 /// Good until Date. It will remain working within the system and in the marketplace until it
719 /// executes or until the close of the market on the date specified.
720 GoodTilDate,
721 /// Market-on-open (MOO) or limit-on-open (LOO) order.
722 OnOpen,
723 /// Fill-or-Kill. If the entire order does not execute as soon as it becomes available, the entire
724 /// order is canceled.
725 FillOrKill,
726 /// Day until Canceled.
727 DayTilCanceled,
728 /// Auction - for auction orders.
729 Auction,
730}
731
732impl ToField for TimeInForce {
733 fn to_field(&self) -> String {
734 self.to_string()
735 }
736}
737
738impl std::fmt::Display for TimeInForce {
739 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
740 let text = match self {
741 TimeInForce::Day => "DAY",
742 TimeInForce::GoodTilCanceled => "GTC",
743 TimeInForce::ImmediateOrCancel => "IOC",
744 TimeInForce::GoodTilDate => "GTD",
745 TimeInForce::OnOpen => "OPG",
746 TimeInForce::FillOrKill => "FOK",
747 TimeInForce::DayTilCanceled => "DTC",
748 TimeInForce::Auction => "AUC",
749 };
750 write!(f, "{text}")
751 }
752}
753
754impl From<String> for TimeInForce {
755 fn from(value: String) -> Self {
756 Self::from(value.as_str())
757 }
758}
759
760impl From<&str> for TimeInForce {
761 fn from(value: &str) -> Self {
762 match value {
763 "DAY" => TimeInForce::Day,
764 "GTC" => TimeInForce::GoodTilCanceled,
765 "IOC" => TimeInForce::ImmediateOrCancel,
766 "GTD" => TimeInForce::GoodTilDate,
767 "OPG" => TimeInForce::OnOpen,
768 "FOK" => TimeInForce::FillOrKill,
769 "DTC" => TimeInForce::DayTilCanceled,
770 "AUC" => TimeInForce::Auction,
771 _ => TimeInForce::Day, // Default fallback
772 }
773 }
774}
775
776/// Tells how to handle remaining orders in an OCA group when one order or part of an order executes.
777#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
778#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
779pub enum OcaType {
780 /// Not part of OCA group.
781 #[default]
782 None = 0,
783 /// Cancel all remaining orders with block (overfill protection - only one order routed at a time).
784 CancelWithBlock = 1,
785 /// Proportionally reduce remaining orders with block.
786 ReduceWithBlock = 2,
787 /// Proportionally reduce remaining orders without block.
788 ReduceWithoutBlock = 3,
789}
790
791impl ToField for OcaType {
792 fn to_field(&self) -> String {
793 i32::from(*self).to_string()
794 }
795}
796
797impl From<OcaType> for i32 {
798 fn from(value: OcaType) -> i32 {
799 value as i32
800 }
801}
802
803impl From<i32> for OcaType {
804 fn from(value: i32) -> Self {
805 match value {
806 0 => OcaType::None,
807 1 => OcaType::CancelWithBlock,
808 2 => OcaType::ReduceWithBlock,
809 3 => OcaType::ReduceWithoutBlock,
810 _ => OcaType::None,
811 }
812 }
813}
814
815/// The order's origin. Identifies the type of customer from which the order originated.
816#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
817#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
818pub enum OrderOrigin {
819 /// Customer order.
820 #[default]
821 Customer = 0,
822 /// Firm order (institutional customers only).
823 Firm = 1,
824}
825
826impl ToField for OrderOrigin {
827 fn to_field(&self) -> String {
828 i32::from(*self).to_string()
829 }
830}
831
832impl From<OrderOrigin> for i32 {
833 fn from(value: OrderOrigin) -> i32 {
834 value as i32
835 }
836}
837
838impl From<i32> for OrderOrigin {
839 fn from(value: i32) -> Self {
840 match value {
841 0 => OrderOrigin::Customer,
842 1 => OrderOrigin::Firm,
843 _ => OrderOrigin::Customer,
844 }
845 }
846}
847
848/// Specifies the short sale slot (for institutional short sales).
849#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
850#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
851pub enum ShortSaleSlot {
852 /// Not a short sale.
853 #[default]
854 None = 0,
855 /// Broker holds shares.
856 Broker = 1,
857 /// Shares come from elsewhere (third party). Use with `designated_location` field.
858 ThirdParty = 2,
859}
860
861impl ToField for ShortSaleSlot {
862 fn to_field(&self) -> String {
863 i32::from(*self).to_string()
864 }
865}
866
867impl From<ShortSaleSlot> for i32 {
868 fn from(value: ShortSaleSlot) -> i32 {
869 value as i32
870 }
871}
872
873impl From<i32> for ShortSaleSlot {
874 fn from(value: i32) -> Self {
875 match value {
876 0 => ShortSaleSlot::None,
877 1 => ShortSaleSlot::Broker,
878 2 => ShortSaleSlot::ThirdParty,
879 _ => ShortSaleSlot::None,
880 }
881 }
882}
883
884/// Volatility type for VOL orders.
885#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
886#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
887pub enum VolatilityType {
888 /// Daily volatility.
889 Daily = 1,
890 /// Annual volatility.
891 Annual = 2,
892}
893
894impl ToField for VolatilityType {
895 fn to_field(&self) -> String {
896 i32::from(*self).to_string()
897 }
898}
899
900impl ToField for Option<VolatilityType> {
901 fn to_field(&self) -> String {
902 encode_option_field(self)
903 }
904}
905
906impl From<VolatilityType> for i32 {
907 fn from(value: VolatilityType) -> i32 {
908 value as i32
909 }
910}
911
912impl From<i32> for VolatilityType {
913 fn from(value: i32) -> Self {
914 match value {
915 1 => VolatilityType::Daily,
916 2 => VolatilityType::Annual,
917 _ => VolatilityType::Daily,
918 }
919 }
920}
921
922/// Reference price type for VOL orders.
923#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
924#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
925pub enum ReferencePriceType {
926 /// Average of National Best Bid/Offer.
927 AverageOfNBBO = 1,
928 /// NBB or NBO depending on action and right.
929 NBBO = 2,
930}
931
932impl ToField for ReferencePriceType {
933 fn to_field(&self) -> String {
934 i32::from(*self).to_string()
935 }
936}
937
938impl ToField for Option<ReferencePriceType> {
939 fn to_field(&self) -> String {
940 encode_option_field(self)
941 }
942}
943
944impl From<ReferencePriceType> for i32 {
945 fn from(value: ReferencePriceType) -> i32 {
946 value as i32
947 }
948}
949
950impl From<i32> for ReferencePriceType {
951 fn from(value: i32) -> Self {
952 match value {
953 1 => ReferencePriceType::AverageOfNBBO,
954 2 => ReferencePriceType::NBBO,
955 _ => ReferencePriceType::AverageOfNBBO,
956 }
957 }
958}
959
960/// NYSE Rule 80A designations for institutional trading.
961#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
962#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
963pub enum Rule80A {
964 /// Individual customer.
965 Individual,
966 /// Agency transaction.
967 Agency,
968 /// Agent for other member.
969 AgentOtherMember,
970 /// Individual principal transaction in agency cross.
971 IndividualPTIA,
972 /// Agency principal transaction in agency cross.
973 AgencyPTIA,
974 /// Agent for other member principal transaction in agency cross.
975 AgentOtherMemberPTIA,
976 /// Individual principal transaction.
977 IndividualPT,
978 /// Agency principal transaction.
979 AgencyPT,
980 /// Agent for other member principal transaction.
981 AgentOtherMemberPT,
982}
983
984impl ToField for Rule80A {
985 fn to_field(&self) -> String {
986 self.to_string()
987 }
988}
989
990impl ToField for Option<Rule80A> {
991 fn to_field(&self) -> String {
992 encode_option_field(self)
993 }
994}
995
996impl std::fmt::Display for Rule80A {
997 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
998 let text = match self {
999 Rule80A::Individual => "I",
1000 Rule80A::Agency => "A",
1001 Rule80A::AgentOtherMember => "W",
1002 Rule80A::IndividualPTIA => "J",
1003 Rule80A::AgencyPTIA => "U",
1004 Rule80A::AgentOtherMemberPTIA => "M",
1005 Rule80A::IndividualPT => "K",
1006 Rule80A::AgencyPT => "Y",
1007 Rule80A::AgentOtherMemberPT => "N",
1008 };
1009
1010 write!(f, "{text}")
1011 }
1012}
1013
1014impl Rule80A {
1015 /// Parse a rule 80A code from its string representation.
1016 pub fn from(source: &str) -> Option<Self> {
1017 match source {
1018 "I" => Some(Rule80A::Individual),
1019 "A" => Some(Rule80A::Agency),
1020 "W" => Some(Rule80A::AgentOtherMember),
1021 "J" => Some(Rule80A::IndividualPTIA),
1022 "U" => Some(Rule80A::AgencyPTIA),
1023 "M" => Some(Rule80A::AgentOtherMemberPTIA),
1024 "K" => Some(Rule80A::IndividualPT),
1025 "Y" => Some(Rule80A::AgencyPT),
1026 "N" => Some(Rule80A::AgentOtherMemberPT),
1027 _ => None,
1028 }
1029 }
1030}
1031
1032/// Auction strategy for BOX orders.
1033#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1034#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
1035pub enum AuctionStrategy {
1036 /// Match strategy.
1037 Match = 1,
1038 /// Improvement strategy.
1039 Improvement = 2,
1040 /// Transparent strategy.
1041 Transparent = 3,
1042}
1043
1044impl ToField for AuctionStrategy {
1045 fn to_field(&self) -> String {
1046 i32::from(*self).to_string()
1047 }
1048}
1049
1050impl ToField for Option<AuctionStrategy> {
1051 fn to_field(&self) -> String {
1052 encode_option_field(self)
1053 }
1054}
1055
1056impl From<AuctionStrategy> for i32 {
1057 fn from(value: AuctionStrategy) -> i32 {
1058 value as i32
1059 }
1060}
1061
1062impl From<i32> for AuctionStrategy {
1063 fn from(value: i32) -> Self {
1064 match value {
1065 1 => AuctionStrategy::Match,
1066 2 => AuctionStrategy::Improvement,
1067 3 => AuctionStrategy::Transparent,
1068 _ => AuctionStrategy::Match,
1069 }
1070 }
1071}
1072
1073/// Represents the price component of a combo leg order.
1074#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1075#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
1076pub struct OrderComboLeg {
1077 /// The price for this combo leg.
1078 pub price: Option<f64>,
1079}
1080
1081/// Order condition types for conditional orders.
1082///
1083/// Each variant wraps a specific condition type that defines when the order
1084/// should be activated or canceled based on market conditions.
1085#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1086#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
1087pub enum OrderCondition {
1088 /// Price-based condition that triggers when a contract reaches a specified price.
1089 Price(PriceCondition),
1090 /// Time-based condition that triggers at a specified time.
1091 Time(TimeCondition),
1092 /// Margin-based condition that triggers when margin cushion percentage changes.
1093 Margin(MarginCondition),
1094 /// Execution-based condition that triggers when a specific contract is executed.
1095 Execution(ExecutionCondition),
1096 /// Volume-based condition that triggers when a contract reaches a specified volume.
1097 Volume(VolumeCondition),
1098 /// Percent change condition that triggers when a contract's price changes by a specified percentage.
1099 PercentChange(PercentChangeCondition),
1100}
1101
1102impl OrderCondition {
1103 /// Returns the condition type discriminator as used by the TWS API.
1104 pub fn condition_type(&self) -> i32 {
1105 match self {
1106 Self::Price(_) => 1,
1107 Self::Time(_) => 3,
1108 Self::Margin(_) => 4,
1109 Self::Execution(_) => 5,
1110 Self::Volume(_) => 6,
1111 Self::PercentChange(_) => 7,
1112 }
1113 }
1114
1115 /// Returns whether this is a conjunction (AND) condition.
1116 pub fn is_conjunction(&self) -> bool {
1117 match self {
1118 Self::Price(c) => c.is_conjunction,
1119 Self::Time(c) => c.is_conjunction,
1120 Self::Margin(c) => c.is_conjunction,
1121 Self::Execution(c) => c.is_conjunction,
1122 Self::Volume(c) => c.is_conjunction,
1123 Self::PercentChange(c) => c.is_conjunction,
1124 }
1125 }
1126}
1127
1128impl ToField for OrderCondition {
1129 fn to_field(&self) -> String {
1130 self.condition_type().to_string()
1131 }
1132}
1133
1134impl ToField for Option<OrderCondition> {
1135 fn to_field(&self) -> String {
1136 encode_option_field(self)
1137 }
1138}
1139
1140impl From<i32> for OrderCondition {
1141 /// Creates an OrderCondition variant with default values from a type discriminator.
1142 fn from(val: i32) -> Self {
1143 match val {
1144 1 => OrderCondition::Price(PriceCondition::default()),
1145 3 => OrderCondition::Time(TimeCondition::default()),
1146 4 => OrderCondition::Margin(MarginCondition::default()),
1147 5 => OrderCondition::Execution(ExecutionCondition::default()),
1148 6 => OrderCondition::Volume(VolumeCondition::default()),
1149 7 => OrderCondition::PercentChange(PercentChangeCondition::default()),
1150 _ => panic!("OrderCondition({val}) is unsupported"),
1151 }
1152 }
1153}
1154
1155/// Stores Soft Dollar Tier information.
1156#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1157#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
1158pub struct SoftDollarTier {
1159 /// Soft dollar tier name.
1160 pub name: String,
1161 /// Tier identifier value.
1162 pub value: String,
1163 /// User-friendly display name.
1164 pub display_name: String,
1165}
1166
1167/// Contains order information including the order, contract, and order state.
1168#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1169#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
1170pub struct OrderData {
1171 /// The order's unique id
1172 pub order_id: i32,
1173 /// The order's Contract.
1174 pub contract: Contract,
1175 /// The currently active order
1176 pub order: Order,
1177 /// The order's OrderState
1178 pub order_state: OrderState,
1179}
1180
1181/// Provides an active order's current state.
1182#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1183#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
1184pub struct OrderState {
1185 /// The order's current status
1186 pub status: String,
1187 /// The account's current initial margin.
1188 pub initial_margin_before: Option<f64>,
1189 /// The account's current maintenance margin
1190 pub maintenance_margin_before: Option<f64>,
1191 /// The account's current equity with loan
1192 pub equity_with_loan_before: Option<f64>,
1193 /// The change of the account's initial margin.
1194 pub initial_margin_change: Option<f64>,
1195 /// The change of the account's maintenance margin
1196 pub maintenance_margin_change: Option<f64>,
1197 /// The change of the account's equity with loan
1198 pub equity_with_loan_change: Option<f64>,
1199 /// The order's impact on the account's initial margin.
1200 pub initial_margin_after: Option<f64>,
1201 /// The order's impact on the account's maintenance margin
1202 pub maintenance_margin_after: Option<f64>,
1203 /// Shows the impact the order would have on the account's equity with loan
1204 pub equity_with_loan_after: Option<f64>,
1205 /// The order's generated commission.
1206 pub commission: Option<f64>,
1207 /// The execution's minimum commission.
1208 pub minimum_commission: Option<f64>,
1209 /// The executions maximum commission.
1210 pub maximum_commission: Option<f64>,
1211 /// The generated commission currency
1212 pub commission_currency: String,
1213 /// Margin currency
1214 pub margin_currency: String,
1215 /// The account's current initial margin outside RTH
1216 pub initial_margin_before_outside_rth: Option<f64>,
1217 /// The account's current maintenance margin outside RTH
1218 pub maintenance_margin_before_outside_rth: Option<f64>,
1219 /// The account's current equity with loan outside RTH
1220 pub equity_with_loan_before_outside_rth: Option<f64>,
1221 /// The change of the account's initial margin outside RTH
1222 pub initial_margin_change_outside_rth: Option<f64>,
1223 /// The change of the account's maintenance margin outside RTH
1224 pub maintenance_margin_change_outside_rth: Option<f64>,
1225 /// The change of the account's equity with loan outside RTH
1226 pub equity_with_loan_change_outside_rth: Option<f64>,
1227 /// The order's impact on the account's initial margin outside RTH
1228 pub initial_margin_after_outside_rth: Option<f64>,
1229 /// The order's impact on the account's maintenance margin outside RTH
1230 pub maintenance_margin_after_outside_rth: Option<f64>,
1231 /// Shows the impact the order would have on the account's equity with loan outside RTH
1232 pub equity_with_loan_after_outside_rth: Option<f64>,
1233 /// Suggested order size
1234 pub suggested_size: Option<f64>,
1235 /// Reject reason
1236 pub reject_reason: String,
1237 /// Order allocations
1238 pub order_allocations: Vec<OrderAllocation>,
1239 /// If the order is warranted, a descriptive message will be provided.
1240 pub warning_text: String,
1241 /// Timestamp when the order completed execution.
1242 pub completed_time: String,
1243 /// Status value after completion (e.g. `Filled`).
1244 pub completed_status: String,
1245}
1246
1247/// Allocation of an order across accounts.
1248#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1249#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
1250pub struct OrderAllocation {
1251 /// Allocation account
1252 pub account: String,
1253 /// Position
1254 pub position: Option<f64>,
1255 /// Desired position
1256 pub position_desired: Option<f64>,
1257 /// Position after
1258 pub position_after: Option<f64>,
1259 /// Desired allocation quantity
1260 pub desired_alloc_qty: Option<f64>,
1261 /// Allowed allocation quantity
1262 pub allowed_alloc_qty: Option<f64>,
1263 /// Is monetary
1264 pub is_monetary: bool,
1265}
1266
1267/// For institutional customers only. Valid values are O (open) and C (close).
1268/// Available for institutional clients to determine if this order is to open or close a position.
1269/// When Action = "BUY" and OpenClose = "O" this will open a new position.
1270/// When Action = "BUY" and OpenClose = "C" this will close and existing short position.
1271#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1272#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
1273pub enum OrderOpenClose {
1274 /// Open a new position.
1275 Open,
1276 /// Close an existing position.
1277 Close,
1278}
1279
1280impl ToField for OrderOpenClose {
1281 fn to_field(&self) -> String {
1282 self.to_string()
1283 }
1284}
1285
1286impl ToField for Option<OrderOpenClose> {
1287 fn to_field(&self) -> String {
1288 encode_option_field(self)
1289 }
1290}
1291
1292impl std::fmt::Display for OrderOpenClose {
1293 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1294 let text = match self {
1295 OrderOpenClose::Open => "O",
1296 OrderOpenClose::Close => "C",
1297 };
1298
1299 write!(f, "{text}")
1300 }
1301}
1302
1303impl OrderOpenClose {
1304 /// Parse an `OrderOpenClose` from the wire-format string.
1305 pub fn from(source: &str) -> Option<Self> {
1306 match source {
1307 "O" => Some(OrderOpenClose::Open),
1308 "C" => Some(OrderOpenClose::Close),
1309 _ => None,
1310 }
1311 }
1312}
1313
1314/// Represents the commission generated by an execution.
1315#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1316#[derive(Clone, Debug, Default)]
1317pub struct CommissionReport {
1318 /// the execution's id this commission belongs to.
1319 pub execution_id: String,
1320 /// the commissions cost.
1321 pub commission: f64,
1322 /// the reporting currency.
1323 pub currency: String,
1324 /// the realized profit and loss
1325 pub realized_pnl: Option<f64>,
1326 /// The income return.
1327 pub yields: Option<f64>,
1328 /// date expressed in yyyymmdd format.
1329 pub yield_redemption_date: String,
1330}
1331
1332/// Liquidity types for executions.
1333#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1334#[derive(Clone, Debug, Default, PartialEq)]
1335pub enum Liquidity {
1336 /// No liquidity information.
1337 #[default]
1338 None = 0,
1339 /// Added liquidity to the market.
1340 AddedLiquidity = 1,
1341 /// Removed liquidity from the market.
1342 RemovedLiquidity = 2,
1343 /// Liquidity was routed out.
1344 LiquidityRoutedOut = 3,
1345}
1346
1347impl From<i32> for Liquidity {
1348 fn from(val: i32) -> Self {
1349 match val {
1350 1 => Liquidity::AddedLiquidity,
1351 2 => Liquidity::RemovedLiquidity,
1352 3 => Liquidity::LiquidityRoutedOut,
1353 _ => Liquidity::None,
1354 }
1355 }
1356}
1357
1358/// Describes an order's execution.
1359#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1360#[derive(Clone, Debug, Default)]
1361pub struct Execution {
1362 /// The API client's order Id. May not be unique to an account.
1363 pub order_id: i32,
1364 /// The API client identifier which placed the order which originated this execution.
1365 pub client_id: i32,
1366 /// The execution's identifier. Each partial fill has a separate ExecId.
1367 /// A correction is indicated by an ExecId which differs from a previous ExecId in only the digits after the final period,
1368 /// e.g. an ExecId ending in ".02" would be a correction of a previous execution with an ExecId ending in ".01"
1369 pub execution_id: String,
1370 /// The execution's server time.
1371 pub time: String,
1372 /// The account to which the order was allocated.
1373 pub account_number: String,
1374 /// The exchange where the execution took place.
1375 pub exchange: String,
1376 /// Specifies if the transaction was buy or sale
1377 /// BOT for bought, SLD for sold
1378 pub side: String,
1379 /// The number of shares filled.
1380 pub shares: f64,
1381 /// The order's execution price excluding commissions.
1382 pub price: f64,
1383 /// The TWS order identifier. The PermId can be 0 for trades originating outside IB.
1384 pub perm_id: i32,
1385 /// Identifies whether an execution occurred because of an IB-initiated liquidation.
1386 pub liquidation: i32,
1387 /// Cumulative quantity.
1388 // Used in regular trades, combo trades and legs of the combo.
1389 pub cumulative_quantity: f64,
1390 /// Average price.
1391 /// Used in regular trades, combo trades and legs of the combo. Does not include commissions.
1392 pub average_price: f64,
1393 /// The OrderRef is a user-customizable string that can be set from the API or TWS and will be associated with an order for its lifetime.
1394 pub order_reference: String,
1395 /// The Economic Value Rule name and the respective optional argument.
1396 /// The two values should be separated by a colon. For example, aussieBond:YearsToExpiration=3. When the optional argument is not present, the first value will be followed by a colon.
1397 pub ev_rule: String,
1398 /// Tells you approximately how much the market value of a contract would change if the price were to change by 1.
1399 /// It cannot be used to get market value by multiplying the price by the approximate multiplier.
1400 pub ev_multiplier: Option<f64>,
1401 /// model code
1402 pub model_code: String,
1403 /// Liquidity type of the execution (requires TWS 968+ / API v973.05+).
1404 pub last_liquidity: Liquidity,
1405 /// Indicates whether a price revision is pending.
1406 pub pending_price_revision: bool,
1407 /// Identifies the submitter of the execution.
1408 pub submitter: String,
1409}
1410
1411/// Contains execution information including the request ID, contract, and execution details.
1412#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1413#[derive(Clone, Debug, Default)]
1414pub struct ExecutionData {
1415 /// The request ID associated with this execution.
1416 pub request_id: i32,
1417 /// The contract that was executed.
1418 pub contract: Contract,
1419 /// The execution details.
1420 pub execution: Execution,
1421}
1422
1423/// Responses from placing an order.
1424#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1425#[derive(Clone, Debug)]
1426#[allow(clippy::large_enum_variant)]
1427pub enum PlaceOrder {
1428 /// Order status update.
1429 OrderStatus(OrderStatus),
1430 /// Open order information.
1431 OpenOrder(OrderData),
1432 /// Execution data.
1433 ExecutionData(ExecutionData),
1434 /// Commission report.
1435 CommissionReport(CommissionReport),
1436 /// Notice or error message.
1437 Message(crate::messages::Notice),
1438}
1439
1440/// Updates received when monitoring order activity.
1441/// This enum is used by `order_update_stream` to deliver real-time order updates.
1442#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1443#[derive(Clone, Debug)]
1444#[allow(clippy::large_enum_variant)]
1445pub enum OrderUpdate {
1446 /// Order status update.
1447 OrderStatus(OrderStatus),
1448 /// Open order information.
1449 OpenOrder(OrderData),
1450 /// Execution data.
1451 ExecutionData(ExecutionData),
1452 /// Commission report.
1453 CommissionReport(CommissionReport),
1454 /// Notice or error message.
1455 Message(crate::messages::Notice),
1456}
1457
1458/// Contains all relevant information on the current status of the order execution-wise (i.e. amount filled and pending, filling price, etc.).
1459#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1460#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
1461pub struct OrderStatus {
1462 /// The order's client id.
1463 pub order_id: i32,
1464 /// The current status of the order. Possible values:
1465 /// * ApiPending - indicates order has not yet been sent to IB server, for instance if there is a delay in receiving the security definition. Uncommonly received.
1466 /// * PendingSubmit - indicates that you have transmitted the order, but have not yet received confirmation that it has been accepted by the order destination.
1467 /// * PendingCancel - indicates that you have sent a request to cancel the order but have not yet received cancel confirmation from the order destination. At this point, your order is not confirmed canceled. It is not guaranteed that the cancellation will be successful.
1468 /// * PreSubmitted - indicates that a simulated order type has been accepted by the IB system and that this order has yet to be elected. The order is held in the IB system until the election criteria are met. At that time the order is transmitted to the order destination as specified .
1469 /// * Submitted - indicates that your order has been accepted by the system.
1470 /// * ApiCancelled - after an order has been submitted and before it has been acknowledged, an API client client can request its cancelation, producing this state.
1471 /// * Cancelled - indicates that the balance of your order has been confirmed canceled by the IB system. This could occur unexpectedly when IB or the destination has rejected your order.
1472 /// * Filled - indicates that the order has been completely filled. Market orders executions will not always trigger a Filled status.
1473 /// * Inactive - indicates that the order was received by the system but is no longer active because it was rejected or canceled.
1474 pub status: String,
1475 /// Number of filled positions.
1476 pub filled: f64,
1477 /// The remnant positions.
1478 pub remaining: f64,
1479 /// Average filling price.
1480 pub average_fill_price: f64,
1481 /// The order's permId used by the TWS to identify orders.
1482 pub perm_id: i32,
1483 /// Parent's id. Used for bracket and auto trailing stop orders.
1484 pub parent_id: i32,
1485 /// Price at which the last positions were filled.
1486 pub last_fill_price: f64,
1487 /// API client which submitted the order.
1488 pub client_id: i32,
1489 /// This field is used to identify an order held when TWS is trying to locate shares for a short sell. The value used to indicate this is 'locate'.
1490 pub why_held: String,
1491 /// If an order has been capped, this indicates the current capped price. Requires TWS 967+ and API v973.04+. Python API specifically requires API v973.06+.
1492 pub market_cap_price: f64,
1493}
1494
1495/// Enumerates possible results from cancelling an order.
1496#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1497#[derive(Debug)]
1498pub enum CancelOrder {
1499 /// Order status information.
1500 OrderStatus(OrderStatus),
1501 /// Informational notice.
1502 Notice(crate::messages::Notice),
1503}
1504
1505/// Enumerates possible results from querying an [Order].
1506#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1507#[derive(Debug)]
1508#[allow(clippy::large_enum_variant)]
1509pub enum Orders {
1510 /// Detailed order data.
1511 OrderData(OrderData),
1512 /// Order status update.
1513 OrderStatus(OrderStatus),
1514 /// Informational notice.
1515 Notice(crate::messages::Notice),
1516}
1517
1518#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1519#[derive(Debug, Default)]
1520/// Filter criteria used to determine which execution reports are returned.
1521pub struct ExecutionFilter {
1522 /// The API client which placed the order.
1523 pub client_id: Option<i32>,
1524 /// The account to which the order was allocated to
1525 pub account_code: String,
1526 /// Time from which the executions will be returned yyyymmdd hh:mm:ss
1527 /// Only those executions reported after the specified time will be returned.
1528 pub time: String,
1529 /// The instrument's symbol
1530 pub symbol: String,
1531 /// The Contract's security's type (i.e. STK, OPT...)
1532 pub security_type: String,
1533 /// The exchange at which the execution was produced
1534 pub exchange: String,
1535 /// The Contract's side (BUY or SELL)
1536 pub side: String,
1537 /// Filter executions from the last N days (0 = no filter).
1538 pub last_n_days: i32,
1539 /// Filter executions for specific dates (format: yyyymmdd, e.g., "20260130").
1540 pub specific_dates: Vec<String>,
1541}
1542
1543/// Enumerates possible results from querying an [Execution].
1544#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1545#[derive(Debug)]
1546#[allow(clippy::large_enum_variant)]
1547pub enum Executions {
1548 /// Execution data payload.
1549 ExecutionData(ExecutionData),
1550 /// Commission report payload.
1551 CommissionReport(CommissionReport),
1552 /// Informational notice.
1553 Notice(crate::messages::Notice),
1554}
1555
1556/// Exercise action for options.
1557#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1558#[derive(Debug, Clone, Copy)]
1559pub enum ExerciseAction {
1560 /// Exercise the option.
1561 Exercise = 1,
1562 /// Let the option lapse.
1563 Lapse = 2,
1564}
1565
1566/// Responses from exercising options.
1567#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
1568#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1569#[allow(clippy::large_enum_variant)]
1570pub enum ExerciseOptions {
1571 /// Open order information.
1572 OpenOrder(OrderData),
1573 /// Order status update.
1574 OrderStatus(OrderStatus),
1575 /// Notice or error message.
1576 Notice(crate::messages::Notice),
1577}
1578
1579// Feature-specific implementations
1580#[cfg(feature = "sync")]
1581mod sync;
1582
1583#[cfg(feature = "async")]
1584mod r#async;
1585
1586// Re-export API functions based on active feature
1587#[cfg(feature = "sync")]
1588/// Blocking order management API wrappers on top of the synchronous client.
1589pub mod blocking {
1590 pub(crate) use super::sync::{
1591 all_open_orders, auto_open_orders, cancel_order, completed_orders, executions, exercise_options, global_cancel, next_valid_order_id,
1592 open_orders, order_update_stream, place_order, submit_order,
1593 };
1594}
1595
1596#[cfg(feature = "async")]
1597pub(crate) use r#async::{
1598 all_open_orders, auto_open_orders, cancel_order, completed_orders, executions, exercise_options, global_cancel, next_valid_order_id, open_orders,
1599 order_update_stream, place_order, submit_order,
1600};