Skip to content

Commit

Permalink
bracket order example
Browse files Browse the repository at this point in the history
  • Loading branch information
Wil Boayue committed Nov 7, 2024
1 parent ce733a8 commit dbcfe57
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions examples/bracket_order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use ibapi::contracts::Contract;
use ibapi::orders::Action;
use ibapi::orders::{order_builder, PlaceOrder};
use ibapi::Client;
use std::thread;

fn place_bracket_order(client: &Client, contract: &Contract, parent_id: i32) -> Result<(), Box<dyn std::error::Error>> {
let orders = order_builder::bracket_order(parent_id, Action::Buy, 100.0, 220.00, 230.0, 210.0);
let mut subscriptions = Vec::new();

for order in &orders {
let subscription = client.place_order(order.order_id, contract, order)?;
subscriptions.push(subscription);
}

let mut num_submitted = 0;
while num_submitted < orders.len() {
subscriptions
.iter()
.filter_map(|subscription| subscription.try_next())
.for_each(|event| match event {
PlaceOrder::OrderStatus(event) if event.status == "Submitted" => {
println!("{event:?}");
num_submitted += 1;
}
_ => println!("Received other event: {event:?}"),
});
thread::sleep(std::time::Duration::from_millis(100));
}

println!("Bracket order placed successfully");
Ok(())
}

fn main() {
env_logger::init();
let client = Client::connect("127.0.0.1:4002", 100).expect("connection failed");

let parent_id = client.next_valid_order_id().expect("error getting next order id");
let contract = Contract::stock("AAPL");

if let Err(e) = place_bracket_order(&client, &contract, parent_id) {
eprintln!("Failed to place bracket order: {e}");
}
}

0 comments on commit dbcfe57

Please sign in to comment.