-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathticker.rs
More file actions
70 lines (60 loc) · 2.26 KB
/
ticker.rs
File metadata and controls
70 lines (60 loc) · 2.26 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: Ticker plant - symbol discovery, reference data, and market data
//!
//! Run with: cargo run --example ticker
use std::env;
use tracing::info;
use rithmic_rs::{
ConnectStrategy, RithmicConfig, RithmicEnv, RithmicTickerPlant, rti::messages::RithmicMessage,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenvy::dotenv().ok();
tracing_subscriber::fmt().init();
let config = RithmicConfig::from_env(RithmicEnv::Demo)?;
let ticker_plant = RithmicTickerPlant::connect(&config, ConnectStrategy::Retry).await?;
let mut handle = ticker_plant.get_handle();
handle.login().await?;
let product = env::var("PRODUCT").unwrap_or_else(|_| "ES".to_string());
let exchange = env::var("EXCHANGE").unwrap_or_else(|_| "CME".to_string());
// Search for symbols
let symbols = handle
.search_symbols(&product, Some(&exchange), None, None, None)
.await?;
info!("Found {} symbols for {}", symbols.len(), product);
// Get the front month contract
let front_month = handle
.get_front_month_contract(&product, &exchange, false)
.await?;
info!("Front month: {:?}", front_month);
// Subscribe to market data
let symbol = env::var("SYMBOL").unwrap_or_else(|_| format!("{}H6", product));
handle.subscribe(&symbol, &exchange).await?;
let mut count = 0;
while count < 10 {
if let Ok(update) = handle.subscription_receiver.recv().await {
match &update.message {
RithmicMessage::LastTrade(t) => {
info!(
"Trade: {} @ {}",
t.trade_size.unwrap_or(0),
t.trade_price.unwrap_or(0.0)
);
count += 1;
}
RithmicMessage::BestBidOffer(b) => {
info!(
"BBO: {}x{} / {}x{}",
b.bid_size.unwrap_or(0),
b.bid_price.unwrap_or(0.0),
b.ask_price.unwrap_or(0.0),
b.ask_size.unwrap_or(0)
);
count += 1;
}
_ => {}
}
}
}
handle.disconnect().await?;
Ok(())
}