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
76
77
78
79
80
81
82
83
84
85
86
//! # binance-stream-handler
//!
//! Produce live Binance order books as Tokio `watch::Receiver<OrderBook>` streams.
//!
//! ## Quick start
//!
//! ```no_run
//! use binance_stream_handler::generate_orderbooks;
//! use chrono::NaiveTime;
//!
//! // Currency pairs must be defined as a 'static slice.
//! pub static CURRENCY_PAIRS: &[&str] = &["ADAUSDT", "DOGEUSDT"];
//!
//! #[tokio::main(flavor = "multi_thread")]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let cutoffs = (NaiveTime::from_hms_opt(2,0,0).unwrap(),
//! NaiveTime::from_hms_opt(18,42,0).unwrap());
//!
//! let streams = generate_orderbooks(CURRENCY_PAIRS, 1024, 512, cutoffs);
//!
//! let mut ada = streams["ADAUSDT"].clone();
//! tokio::spawn(async move {
//! while ada.changed().await.is_ok() {
//! let ob = ada.borrow().clone();
//! println!("{} best bid={:?} ask={:?}",
//! ob.symbol, ob.bids.keys().last(), ob.asks.keys().next());
//! }
//! });
//!
//! futures_util::future::pending::<()>().await;
//! Ok(())
//! }
//! ```
//! ## The `OrderBook` type
//!
//! Each stream yields an [`OrderBook`], which contains the current snapshot
//! of bids and asks for a symbol.
//!
//! ```text
//! struct OrderBook {
//! symbol: String, // e.g. "ADAUSDT"
//! bids: BTreeMap<Price, Qty>, // sorted descending
//! asks: BTreeMap<Price, Qty>, // sorted ascending
//! last_u: Option<u64>, // last update ID applied
//! snapshot_id: Option<u64>, // REST snapshot ID
//! depth: u16 // snapshot depth (default 1000)
//! }
//! ```
//!
//! - **`bids`**: map from price → quantity, sorted by price descending
//! - **`asks`**: map from price → quantity, sorted by price ascending
//! - **`last_u`**: last WebSocket update sequence number applied
//! - **`snapshot_id`**: ID of the REST snapshot used to initialize the book
//! - **`depth`**: the configured maximum depth (default: 1000)
//!
//! You normally just clone the latest `OrderBook` from a `watch::Receiver` and
//! inspect the maps to get the best bid/ask or traverse the book.
use NaiveTime;
use HashMap;
use ;
pub use crateinit_order_books;
pub use crateOrderBook;
use crateDualRouter;
pub async