-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
90 lines (76 loc) · 3.9 KB
/
Copy pathmain.py
File metadata and controls
90 lines (76 loc) · 3.9 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
# -*- coding: utf-8 -*-
# Copyright (c) 2026 shing1211
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""经纪队列 (get_broker_queue)
Demonstrates:
- subscribe: subscribe to broker queue subtype
- get_broker_queue: fetch current broker bid/ask queue
- Broker data: top N brokers with their bid/ask volumes
- All returned fields logged
Broker queue shows which brokerage firms are at the bid/ask.
This is useful for understanding institutional order flow.
"""
import logging
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
import futu as ft
from connect import create_quote_context
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
logger = logging.getLogger(__name__)
if __name__ == "__main__":
logger.info("=== Broker Queue Demo ===")
ctx = create_quote_context()
try:
code = "HK.00700"
ret, _ = ctx.subscribe(code, ft.SubType.BROKER)
logger.info("subscribe ret=%d code=%s", ret, code)
# ── get_broker_queue ─────────────────────────────────────────────
logger.info("\n=== get_broker_queue: %s ===", code)
ret, (bid_data, ask_data) = ctx.get_broker_queue(code)
if ret != 0:
logger.error("get_broker_queue failed: bid_data=%s", bid_data)
else:
logger.info("\nBID Brokers (%d entries):", len(bid_data) if bid_data else 0)
if bid_data:
logger.info("Columns: %s", list(bid_data.columns) if hasattr(bid_data, 'columns') else "N/A")
for _, row in bid_data.iterrows():
logger.info(" broker_id=%s name=%s bid_price=%.2f bid_vol=%d",
row.get("broker_id", "?"), row.get("name", "?"),
row.get("bid_price", 0), row.get("bid_vol", 0))
logger.info("\n%s", bid_data.to_string())
logger.info("\nASK Brokers (%d entries):", len(ask_data) if ask_data else 0)
if ask_data:
logger.info("Columns: %s", list(ask_data.columns) if hasattr(ask_data, 'columns') else "N/A")
for _, row in ask_data.iterrows():
logger.info(" broker_id=%s name=%s ask_price=%.2f ask_vol=%d",
row.get("broker_id", "?"), row.get("name", "?"),
row.get("ask_price", 0), row.get("ask_vol", 0))
logger.info("\n%s", ask_data.to_string())
# ── Also try with HK.HSImain (index) ────────────────────────────
code2 = "HK.HSImain"
logger.info("\n=== get_broker_queue: %s ===", code2)
ret2, _ = ctx.subscribe(code2, ft.SubType.BROKER)
logger.info("subscribe ret=%d", ret2)
ret2, data2 = ctx.get_broker_queue(code2)
if ret2 != 0:
logger.error("get_broker_queue (%s) failed: %s", code2, data2)
else:
bid2, ask2 = data2 if isinstance(data2, tuple) and len(data2) == 2 else (data2, None)
bid_len = len(bid2) if bid2 is not None and not bid2.empty else 0
ask_len = len(ask2) if ask2 is not None and not ask2.empty else 0
logger.info("%s BID brokers: %d | ASK brokers: %d", code2, bid_len, ask_len)
finally:
ctx.close()
logger.info("Done.")