-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathorder_book_top.rs
More file actions
72 lines (63 loc) · 2.26 KB
/
Copy pathorder_book_top.rs
File metadata and controls
72 lines (63 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
71
72
//! Subscribe to one orderbook and print current best bid/ask from snapshots.
//!
//! Run:
//! cargo run --example order_book_top --release -- "<key_base64>" [host:port] [market] [watch_seconds]
use std::env;
use std::time::{Duration, Instant};
use moonproto::state::OrderBookEvent;
use moonproto::Event;
mod common;
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
eprintln!("Usage: order_book_top <key_base64> [host:port] [market] [watch_seconds]");
std::process::exit(1);
}
let market = args
.get(3)
.cloned()
.unwrap_or_else(|| "BTCUSDT".to_string());
let watch_secs: u64 = args.get(4).and_then(|s| s.parse().ok()).unwrap_or(30);
let mut init = common::init_config();
init.subscribe_orderbooks.push(market.clone());
let client = match common::connect(&args[1], args.get(2), init) {
Ok(client) => client,
Err(err) => {
eprintln!("[connect/init] failed: {err}");
std::process::exit(2);
}
};
println!("[subscribe] orderbook market={market}");
let deadline = Instant::now() + Duration::from_secs(watch_secs);
let mut updates = 0u64;
while Instant::now() < deadline {
for event in client.drain_events() {
if let Event::OrderBook(OrderBookEvent::Apply {
market_name,
kind,
top,
..
}) = event
{
let Some(name) = market_name.as_deref() else {
continue;
};
if name != market {
continue;
}
updates += 1;
let bid = top
.bid
.map(|level| format!("{} @ {}", level.quantity, level.rate))
.unwrap_or_else(|| "none".to_string());
let ask = top
.ask
.map(|level| format!("{} @ {}", level.quantity, level.rate))
.unwrap_or_else(|| "none".to_string());
println!("[top] {name} {} bid={} ask={}", kind.as_str(), bid, ask);
}
}
std::thread::sleep(Duration::from_millis(50));
}
println!("[done] top-updates={updates}");
}