-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Wil Boayue
committed
Nov 7, 2024
1 parent
ce733a8
commit dbcfe57
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} |