-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathload_historical_bars.rs
More file actions
70 lines (59 loc) · 2.03 KB
/
load_historical_bars.rs
File metadata and controls
70 lines (59 loc) · 2.03 KB
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
//! Example: Load historical time bars
//!
//! Run with: cargo run --example load_historical_bars
//!
//! Optional env vars: SYMBOL, EXCHANGE, START_TIME (unix seconds)
use std::{env, time::SystemTime};
use tracing::info;
use rithmic_rs::{
ConnectStrategy, RithmicConfig, RithmicEnv, RithmicHistoryPlant,
rti::{messages::RithmicMessage, request_time_bar_replay::BarType},
};
fn default_start_time() -> i32 {
// Note: Rithmic API uses i32 timestamps. This will overflow in 2038.
// We use try_into() to safely convert and fall back to a recent timestamp if needed.
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.ok()
.and_then(|d| i32::try_from(d.as_secs()).ok())
.map(|s| s - (24 * 60 * 60))
.unwrap_or(0)
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenvy::dotenv().ok();
tracing_subscriber::fmt().init();
let symbol = env::var("SYMBOL").unwrap_or_else(|_| "ESH6".to_string());
let exchange = env::var("EXCHANGE").unwrap_or_else(|_| "CME".to_string());
let start_time: i32 = env::var("START_TIME")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or_else(default_start_time);
let end_time = start_time + (23 * 60 * 60);
let config = RithmicConfig::from_env(RithmicEnv::Demo)?;
let history_plant = RithmicHistoryPlant::connect(&config, ConnectStrategy::Retry).await?;
let handle = history_plant.get_handle();
handle.login().await?;
info!(
"Loading 5-minute bars for {} from {} to {}",
symbol, start_time, end_time
);
let bars = handle
.load_time_bars(
symbol,
exchange,
BarType::MinuteBar,
5,
start_time,
end_time,
)
.await?;
info!("Received {} bars", bars.len());
for r in bars.iter().take(5) {
if let RithmicMessage::ResponseTimeBarReplay(bar) = &r.message {
info!("Bar: {:?}", bar);
}
}
handle.disconnect().await?;
Ok(())
}