• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wboayue / rust-ibapi / 21299514581

23 Jan 2026 08:01PM UTC coverage: 86.685% (+0.006%) from 86.679%
21299514581

push

github

web-flow
fix: prevent bracket/OCA order ID collisions (#375)

Reserve all order IDs upfront by calling next_order_id() for each
order instead of using base_id + offset calculation.

Fixes #374

0 of 18 new or added lines in 2 files covered. (0.0%)

2 existing lines in 1 file now uncovered.

8698 of 10034 relevant lines covered (86.69%)

117.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/orders/builder/sync_impl.rs
1
use super::{BracketOrderBuilder, BracketOrderIds, OrderBuilder, OrderId};
2
use crate::client::sync::Client;
3
use crate::contracts::Contract;
4
use crate::errors::Error;
5
use crate::orders;
6

7
#[cfg(test)]
8
mod tests;
9

10
impl<'a> OrderBuilder<'a, Client> {
11
    /// Submit the order synchronously
12
    /// Returns the order ID assigned to the submitted order
13
    pub fn submit(self) -> Result<OrderId, Error> {
×
14
        let client = self.client;
×
15
        let contract = self.contract;
×
16
        let order_id = client.next_order_id();
×
17
        let order = self.build()?;
×
18
        orders::blocking::submit_order(client, order_id, contract, &order)?;
×
19
        Ok(OrderId::new(order_id))
×
20
    }
21

22
    /// Build the order and return it without submitting
23
    /// Useful for batch operations or custom submission logic
24
    pub fn build_order(self) -> Result<crate::orders::Order, Error> {
×
25
        self.build().map_err(Into::into)
×
26
    }
27

28
    /// Analyze order for margin/commission (what-if)
29
    pub fn analyze(mut self) -> Result<crate::orders::OrderState, Error> {
×
30
        self.what_if = true;
×
31
        let client = self.client;
×
32
        let contract = self.contract;
×
33
        let order_id = client.next_order_id();
×
34
        let order = self.build()?;
×
35

36
        // Submit what-if order and get the response
37
        let responses = orders::blocking::place_order(client, order_id, contract, &order)?;
×
38

39
        // Look for the order state in the responses
40
        for response in responses {
×
41
            if let crate::orders::PlaceOrder::OpenOrder(order_data) = response {
×
42
                if order_data.order_id == order_id {
×
43
                    return Ok(order_data.order_state);
×
44
                }
45
            }
46
        }
47

48
        Err(Error::Simple("What-if analysis did not return order state".to_string()))
×
49
    }
50
}
51

52
impl<'a> BracketOrderBuilder<'a, Client> {
53
    /// Submit bracket orders synchronously
54
    /// Returns BracketOrderIds containing all three order IDs
55
    pub fn submit_all(self) -> Result<BracketOrderIds, Error> {
×
56
        let client = self.parent_builder.client;
×
57
        let contract = self.parent_builder.contract;
×
58
        let orders = self.build()?;
×
59

60
        // Reserve all order IDs upfront to prevent collisions
NEW
61
        let parent_id = client.next_order_id();
×
NEW
62
        let tp_id = client.next_order_id();
×
NEW
63
        let sl_id = client.next_order_id();
×
NEW
64
        let reserved_ids = [parent_id, tp_id, sl_id];
×
65

66
        for (i, mut order) in orders.into_iter().enumerate() {
×
NEW
67
            let order_id = reserved_ids[i];
×
68
            order.order_id = order_id;
×
69

70
            // Update parent_id for child orders
71
            if i > 0 {
×
NEW
72
                order.parent_id = parent_id;
×
73
            }
74

75
            // Only transmit the last order
76
            if i == 2 {
×
77
                order.transmit = true;
×
78
            }
79

80
            orders::blocking::submit_order(client, order_id, contract, &order)?;
×
81
        }
82

NEW
83
        Ok(BracketOrderIds::new(parent_id, tp_id, sl_id))
×
84
    }
85
}
86

87
/// Extension trait for submitting multiple OCA orders
88
impl Client {
89
    /// Submit multiple OCA (One-Cancels-All) orders
90
    ///
91
    /// When one order in the group is filled, all others are automatically cancelled.
92
    ///
93
    /// # Example
94
    /// ```no_run
95
    /// use ibapi::client::blocking::Client;
96
    /// use ibapi::contracts::Contract;
97
    ///
98
    /// let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");
99
    ///
100
    /// let contract1 = Contract::stock("AAPL").build();
101
    /// let contract2 = Contract::stock("MSFT").build();
102
    ///
103
    /// let order1 = client.order(&contract1)
104
    ///     .buy(100)
105
    ///     .limit(50.0)
106
    ///     .oca_group("MyOCA", 1)
107
    ///     .build_order().expect("order build failed");
108
    ///     
109
    /// let order2 = client.order(&contract2)
110
    ///     .buy(100)
111
    ///     .limit(45.0)
112
    ///     .oca_group("MyOCA", 1)
113
    ///     .build_order().expect("order build failed");
114
    ///
115
    /// let order_ids = client.submit_oca_orders(
116
    ///     vec![(contract1, order1), (contract2, order2)]
117
    /// ).expect("OCA submission failed");
118
    /// ```
119
    pub fn submit_oca_orders(&self, orders: Vec<(Contract, crate::orders::Order)>) -> Result<Vec<OrderId>, Error> {
×
120
        let mut order_ids = Vec::new();
×
121

NEW
122
        for (contract, mut order) in orders.into_iter() {
×
NEW
123
            let order_id = self.next_order_id();
×
124
            order.order_id = order_id;
×
125
            order_ids.push(OrderId::new(order_id));
×
126
            orders::blocking::submit_order(self, order_id, &contract, &order)?;
×
127
        }
128

129
        Ok(order_ids)
×
130
    }
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc