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
//! Example of establishing a sync connection to TWS/Gateway
//!
//! To run this example:
//! ```bash
//! cargo run --features sync --example connect
//! ```
//!
//! Make sure TWS or IB Gateway is running with API connections enabled:
//! - For Gateway: Configure -> Settings -> API -> Settings
//! - Enable "Enable ActiveX and Socket Clients"
//! - Add "127.0.0.1" to "Trusted IPs"
//! - Default ports: 4001 (live), 4002 (paper)
use ibapi::client::blocking::Client;
fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
println!("Connecting to IB Gateway...");
// Connect to Gateway at the default paper trading port
let client = Client::connect("127.0.0.1:4002", 100)?;
println!("Connected successfully!");
println!("Server version: {}", client.server_version());
println!("Connection time: {:?}", client.connection_time());
println!("Next order ID: {}", client.next_order_id());
// Get server time to verify connection is working
match client.server_time() {
Ok(time) => println!("Server time: {time:?}"),
Err(e) => eprintln!("Failed to get server time: {e:?}"),
}
// Keep the connection alive for a moment
std::thread::sleep(std::time::Duration::from_secs(1));
println!("Disconnecting...");
Ok(())
}