#![allow(clippy::uninlined_format_args)]
use std::sync::Arc;
use ibapi::{
contracts::{Contract, ContractMonth},
market_data::historical::ToDuration,
Client,
};
use time::macros::datetime;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let client = Arc::new(Client::connect("127.0.0.1:4002", 102).await?);
println!("Connected to IB Gateway");
let contracts = vec![
("AAPL", Contract::stock("AAPL").build(), "NASDAQ"),
("SPY", Contract::stock("SPY").build(), "NYSE"),
(
"GC",
Contract::futures("GC")
.expires_in(ContractMonth::new(2025, 2))
.on_exchange("COMEX")
.build(),
"COMEX",
),
];
for (name, contract, exchange) in contracts {
println!("\n{name} Trading Schedule ({exchange}):");
let duration = 30.days();
match client.historical_schedules(&contract, duration).fetch().await {
Ok(schedule) => {
println!(" Schedule from {} to {}", schedule.start, schedule.end);
println!(" Timezone: {}", schedule.time_zone);
let session_count = schedule.sessions.len();
let sessions_to_show = session_count.min(5);
println!("\n Last {sessions_to_show} trading sessions:");
for session in schedule.sessions.iter().rev().take(sessions_to_show).rev() {
println!(" {} - Trading: {} to {}", session.reference, session.start, session.end);
}
println!(" Total sessions in period: {session_count}");
}
Err(e) => {
println!(" Error: {e:?}");
}
}
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
}
println!("\n\nSpecific date range example (Thanksgiving week 2023):");
let contract = Contract::stock("AAPL").build();
let end_date = datetime!(2023-11-26 00:00 UTC);
let duration = 7.days();
match client.historical_schedules(&contract, duration).ending(end_date).fetch().await {
Ok(schedule) => {
println!("Schedule for Thanksgiving week:");
for session in &schedule.sessions {
println!(" {} - Trading: {} to {}", session.reference, session.start, session.end);
}
}
Err(e) => {
println!("Error: {e:?}");
}
}
println!("\nExample completed!");
Ok(())
}