Skip to main content

futu_qot/
order_book.rs

1use futu_core::error::{FutuError, Result};
2use futu_core::proto_id;
3use futu_net::client::FutuClient;
4
5use crate::types::{OrderBookData, OrderBookEntry, Security};
6
7/// 获取摆盘数据
8///
9/// 查询指定股票的买卖盘口数据。需要先订阅 SubType::OrderBook。
10///
11/// - `num`: 请求的摆盘档数。C++ 只拒 `< 1`,实际返回深度会按市场与行情权限截断;
12///   当前 Rust surface 建议使用 1..=40。
13pub async fn get_order_book(
14    client: &FutuClient,
15    security: &Security,
16    num: i32,
17) -> Result<OrderBookData> {
18    get_order_book_with_type(client, security, num, None).await
19}
20
21/// 获取摆盘数据,可指定 `Qot_Common.OrderBookType`。
22///
23/// C++ 10.7 对 SG/MY 支持 `OrderBookType_Odd = 1` 碎股摆盘;其他市场传入
24/// Odd 时 C++ 退回 normal 路径。调用者仍需先订阅对应的 SubType。
25pub async fn get_order_book_with_type(
26    client: &FutuClient,
27    security: &Security,
28    num: i32,
29    order_book_type: Option<i32>,
30) -> Result<OrderBookData> {
31    let req = futu_proto::qot_get_order_book::Request {
32        c2s: futu_proto::qot_get_order_book::C2s {
33            security: security.to_proto(),
34            num,
35            order_book_type,
36            header: None,
37        },
38    };
39
40    let body = prost::Message::encode_to_vec(&req);
41    let resp_frame = client.request(proto_id::QOT_GET_ORDER_BOOK, body).await?;
42
43    let resp: futu_proto::qot_get_order_book::Response =
44        prost::Message::decode(resp_frame.body.as_ref()).map_err(FutuError::Proto)?;
45
46    if resp.ret_type != 0 {
47        return Err(crate::server_err(resp.ret_type, resp.ret_msg));
48    }
49
50    let s2c = resp
51        .s2c
52        .ok_or(FutuError::Codec("missing s2c in GetOrderBook".into()))?;
53
54    Ok(OrderBookData {
55        security: Security::from_proto(&s2c.security),
56        ask_list: s2c
57            .order_book_ask_list
58            .iter()
59            .map(OrderBookEntry::from_proto)
60            .collect(),
61        bid_list: s2c
62            .order_book_bid_list
63            .iter()
64            .map(OrderBookEntry::from_proto)
65            .collect(),
66    })
67}