Skip to main content

futucli/cmd/
broker.rs

1//! `futucli broker` — 获取经纪队列
2
3use std::time::Duration;
4
5use anyhow::Result;
6use serde::Serialize;
7use tabled::Tabled;
8
9use crate::common::{connect_gateway, parse_symbol};
10use crate::output::OutputFormat;
11use futu_qot::types::SubType;
12
13#[derive(Tabled)]
14struct BrokerRow {
15    #[tabled(rename = "Side")]
16    side: String,
17    #[tabled(rename = "Pos")]
18    pos: i32,
19    #[tabled(rename = "Broker ID")]
20    id: String,
21    #[tabled(rename = "Broker Name")]
22    name: String,
23}
24
25#[derive(Serialize)]
26struct BrokerJson {
27    side: &'static str,
28    pos: i32,
29    id: i64,
30    name: String,
31}
32
33pub async fn run(gateway: &str, symbol: &str, format: OutputFormat) -> Result<()> {
34    let sec = parse_symbol(symbol)?;
35    let (client, _push_rx) = connect_gateway(gateway, "futucli-broker").await?;
36
37    futu_qot::sub::subscribe(
38        &client,
39        std::slice::from_ref(&sec),
40        &[SubType::Broker],
41        true,
42        true,
43    )
44    .await?;
45    tokio::time::sleep(Duration::from_millis(300)).await;
46
47    let data = futu_qot::broker::get_broker(&client, &sec).await?;
48
49    let mut rows = Vec::new();
50    for a in &data.ask_list {
51        rows.push(BrokerRow {
52            side: "ASK".to_string(),
53            pos: a.pos,
54            id: a.id.to_string(),
55            name: a.name.clone(),
56        });
57    }
58    for b in &data.bid_list {
59        rows.push(BrokerRow {
60            side: "BID".to_string(),
61            pos: b.pos,
62            id: b.id.to_string(),
63            name: b.name.clone(),
64        });
65    }
66
67    let mut jsons = Vec::new();
68    for a in &data.ask_list {
69        jsons.push(BrokerJson {
70            side: "ask",
71            pos: a.pos,
72            id: a.id,
73            name: a.name.clone(),
74        });
75    }
76    for b in &data.bid_list {
77        jsons.push(BrokerJson {
78            side: "bid",
79            pos: b.pos,
80            id: b.id,
81            name: b.name.clone(),
82        });
83    }
84
85    format.print_rows(&rows, &jsons)?;
86    Ok(())
87}