-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathplace_order.rs
65 lines (51 loc) · 2.46 KB
/
place_order.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use clap::{arg, ArgMatches, Command};
use log::{debug, info};
use ibapi::contracts::Contract;
use ibapi::orders::{self, order_builder, PlaceOrder};
use ibapi::Client;
fn main() {
env_logger::init();
let matches = Command::new("place_order")
.version("1.0")
.author("Wil Boayue <[email protected]>")
.about("Submits order to broker")
.arg(arg!(--connection_string <VALUE>).default_value("127.0.0.1:4002"))
.arg(arg!(--stock <SYMBOL>).required(true))
.arg(arg!(--buy <QUANTITY>).value_parser(clap::value_parser!(i32)))
.arg(arg!(--sell <QUANTITY>).value_parser(clap::value_parser!(i32)))
.get_matches();
let connection_string = matches.get_one::<String>("connection_string").expect("connection_string is required");
let stock_symbol = matches.get_one::<String>("stock").expect("stock symbol is required");
if let Some((action, quantity)) = get_order(&matches) {
println!("action: {action}, quantity: {quantity}");
}
println!("connection_string: {connection_string}, stock_symbol: {stock_symbol}");
let client = Client::connect(&connection_string, 100).expect("connection failed");
info!("Connected {client:?}");
let mut contract = Contract::stock(stock_symbol);
contract.currency = "USD".to_string();
debug!("contract template {contract:?}");
let order_id = client.next_order_id();
println!("order_id: {order_id}");
let order = order_builder::market_order(orders::Action::Buy, 100.0);
println!("contract: {contract:?}, order: {order:?}");
let subscription = client.place_order(order_id, &contract, &order).expect("could not place order");
for status in subscription {
match status {
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!("notice: {message}"),
}
}
}
fn get_order(matches: &ArgMatches) -> Option<(String, i32)> {
if let Some(quantity) = matches.get_one::<i32>("buy") {
Some(("BUY".to_string(), *quantity))
} else {
matches.get_one::<i32>("sell").map(|quantity| ("SELL".to_string(), *quantity))
}
}