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
66
67
68
69
70
71
72
73
74
75
#![allow(clippy::uninlined_format_args)]
//! Async real-time bars example
//!
//! This example demonstrates how to subscribe to 5-second real-time bars using the async API.
//!
//! # Usage
//!
//! Make sure IB Gateway or TWS is running with API connections enabled, then run:
//!
//! ```bash
//! cargo run --features async --example async_realtime_bars
//! ```
//!
//! # Configuration
//!
//! - Adjust the connection address if needed (default: 127.0.0.1:4002)
//! - Change the stock symbol if desired (default: AAPL)
//! - Modify WhatToShow to get different data (Trades, Bid, Ask, MidPoint)
use std::sync::Arc;
use ibapi::prelude::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
// Connect to IB Gateway
let client = Arc::new(Client::connect("127.0.0.1:4002", 100).await?);
println!("Connected to IB Gateway");
// Create a stock contract
let contract = Contract::stock("AAPL").build();
println!("Subscribing to real-time bars for {}", contract.symbol);
// Request real-time bars
// Note: Only 5-second bars are currently supported
let realtime_bars = client
.realtime_bars(
&contract,
RealtimeBarSize::Sec5, // 5-second bars
RealtimeWhatToShow::Trades, // Trade data
TradingHours::Regular, // Use regular trading hours
)
.await?;
println!("Real-time bars subscription created");
println!("Receiving 5-second bars...\n");
// Process real-time bars stream
let mut stream = realtime_bars;
let mut bar_count = 0;
while let Some(bar) = stream.next().await {
let bar = bar?;
bar_count += 1;
println!("Bar #{} at {}", bar_count, bar.date);
println!(" Open: ${:.2}", bar.open);
println!(" High: ${:.2}", bar.high);
println!(" Low: ${:.2}", bar.low);
println!(" Close: ${:.2}", bar.close);
println!(" Volume: {:.0}", bar.volume);
println!(" VWAP: ${:.2}", bar.wap);
println!(" Trades: {}", bar.count);
println!();
// Stop after 10 bars for demo
if bar_count >= 10 {
println!("Received {} bars. Stopping example.", bar_count);
break;
}
}
Ok(())
}