Repository navigation
Expand file tree
/
Copy pathbinance_ws_example.rs
More file actions
232 lines (196 loc) · 7.6 KB
/
Copy pathbinance_ws_example.rs
File metadata and controls
232 lines (196 loc) · 7.6 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//! Binance WebSocket example
//!
//! Demonstrates how to use Binance WebSocket features for real-time market data monitoring
// Allow clippy warnings for example code - examples prioritize readability over strict linting
#![allow(clippy::disallowed_methods)]
use anyhow::{Context, Result};
use ccxt_core::logging::{LogConfig, init_logging};
use ccxt_core::prelude::{Amount, ExchangeConfig, Price};
use ccxt_exchanges::binance::Binance;
use rust_decimal::Decimal;
use serde_json::json;
use std::collections::HashMap;
use tokio::time::{Duration, sleep};
#[tokio::main]
async fn main() -> Result<()> {
println!("=== Binance WebSocket Example ===\n");
let mut config = LogConfig::development();
config.show_span_events = false;
init_logging(&config);
let exchange =
Binance::new(ExchangeConfig::default()).context("Failed to initialize Binance exchange")?;
// Example 1: Monitor single ticker
println!("1. Monitor single ticker (BTC/USDT)");
println!(" Connecting to WebSocket...");
match exchange.watch_ticker("BTC/USDT", None).await {
Ok(ticker) => {
println!(" ✓ Connected successfully!");
println!(" Symbol: {}", ticker.symbol);
println!(
" Last Price: {}",
ticker.last.unwrap_or(Price(Decimal::ZERO))
);
println!(" Bid: {}", ticker.bid.unwrap_or(Price(Decimal::ZERO)));
println!(" Ask: {}", ticker.ask.unwrap_or(Price(Decimal::ZERO)));
println!(
" 24h Volume: {}",
ticker.base_volume.unwrap_or(Amount(Decimal::ZERO))
);
println!(
" 24h Change: {}%",
ticker.percentage.unwrap_or(Decimal::ZERO)
);
}
Err(e) => {
eprintln!(" ✗ Error: {}", e);
}
}
sleep(Duration::from_secs(2)).await;
// Example 2: Use miniTicker (more lightweight)
println!("\n2. Monitor using miniTicker (ETH/USDT)");
let mut params = HashMap::new();
params.insert("name".to_string(), json!("miniTicker"));
match exchange.watch_ticker("ETH/USDT", Some(params)).await {
Ok(ticker) => {
println!(" ✓ MiniTicker connected successfully!");
println!(" Symbol: {}", ticker.symbol);
println!(
" Last Price: {}",
ticker.last.unwrap_or(Price(Decimal::ZERO))
);
println!(
" 24h Volume: {}",
ticker.base_volume.unwrap_or(Amount(Decimal::ZERO))
);
}
Err(e) => {
eprintln!(" ✗ Error: {}", e);
}
}
sleep(Duration::from_secs(2)).await;
// Example 3: Monitor multiple tickers
println!("\n3. Monitor multiple tickers");
let symbols = vec![
"BTC/USDT".to_string(),
"ETH/USDT".to_string(),
"BNB/USDT".to_string(),
];
match exchange.watch_tickers(Some(symbols.clone()), None).await {
Ok(tickers) => {
println!(" ✓ Received {} ticker updates", tickers.len());
for symbol in &symbols {
if let Some(ticker) = tickers.get(symbol) {
println!(
" {} - ${}",
ticker.symbol,
ticker.last.unwrap_or(Price(Decimal::ZERO))
);
}
}
}
Err(e) => {
eprintln!(" ✗ Error: {}", e);
}
}
sleep(Duration::from_secs(2)).await;
// Example 4: Monitor all tickers (may take longer)
println!("\n4. Monitor all tickers (first 10)");
match exchange.watch_tickers(None, None).await {
Ok(tickers) => {
println!(" ✓ Received {} tickers", tickers.len());
for (i, (symbol, ticker)) in tickers.iter().enumerate() {
if i >= 10 {
break;
}
println!(
" {} - ${:.2}",
symbol,
ticker.last.unwrap_or(Price(Decimal::ZERO))
);
}
println!(" ... {} trading pairs total", tickers.len());
}
Err(e) => {
eprintln!(" ✗ Error: {}", e);
}
}
sleep(Duration::from_secs(2)).await;
// Example 5: Monitor futures mark price (1-second updates)
println!("\n5. Monitor futures mark price (BTC/USDT:USDT, 1-second updates)");
// Initialize futures exchange for monitoring futures markets
let futures_exchange = Binance::new_swap(ExchangeConfig::default())
.context("Failed to initialize Binance futures exchange")?;
let mut params = HashMap::new();
params.insert("use1sFreq".to_string(), json!(true));
match futures_exchange
.watch_mark_price("BTC/USDT:USDT", Some(params))
.await
{
Ok(mark_price) => {
println!(" ✓ Mark price connected successfully!");
println!(" Symbol: {}", mark_price.symbol);
println!(" Mark Price: {}", mark_price.mark_price);
if let Some(index_price) = mark_price.index_price {
println!(" Index Price: {}", index_price);
}
if let Some(funding_rate) = mark_price.last_funding_rate {
println!(" Funding Rate: {}", funding_rate);
}
}
Err(e) => {
eprintln!(" ✗ Error: {}", e);
}
}
sleep(Duration::from_secs(2)).await;
// Example 6: Monitor multiple futures mark prices
println!("\n6. Monitor multiple futures mark prices");
let futures_symbols = vec!["BTC/USDT:USDT".to_string(), "ETH/USDT:USDT".to_string()];
match futures_exchange
.watch_mark_prices(Some(futures_symbols.clone()), None)
.await
{
Ok(mark_prices) => {
println!(" ✓ Received {} mark prices", mark_prices.len());
for (symbol, mark_price) in &mark_prices {
println!(" {} - Mark: ${}", symbol, mark_price.mark_price);
}
}
Err(e) => {
eprintln!(" ✗ Error: {}", e);
}
}
// Example 7: Continuous ticker monitoring
println!("\n7. Continuous ticker monitoring (Running for 5 seconds)");
println!(" Tip: Continuous monitoring requires the watch_ticker() method");
println!(" This method maintains WebSocket connection and continuously pushes updates");
println!(" Starting monitoring loop...");
let start = std::time::Instant::now();
let duration = Duration::from_secs(5);
while start.elapsed() < duration {
match exchange.watch_ticker("BTC/USDT", None).await {
Ok(ticker) => {
println!(
" [{}] {} - ${}",
chrono::Local::now().format("%H:%M:%S"),
ticker.symbol,
ticker.last.unwrap_or(Price(Decimal::ZERO))
);
}
Err(e) => {
eprintln!(" Error: {}", e);
break;
}
}
}
println!(" Stopped.");
sleep(Duration::from_secs(2)).await;
println!("\n=== Example Complete ===");
println!("\nTips:");
println!(" - ticker: Complete 24-hour statistics");
println!(" - miniTicker: Lightweight version with fewer fields");
println!(" - markPrice: Futures mark price with funding rate");
println!(" - bookTicker: Best bid/ask prices, use watch_bids_asks()");
println!(" - 1s frequency: Faster updates but more bandwidth");
println!(" - 3s frequency: Default frequency, balanced performance");
Ok(())
}