-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (67 loc) · 2.67 KB
/
Copy pathmain.py
File metadata and controls
87 lines (67 loc) · 2.67 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
#!/usr/bin/env python3
# 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.
"""
45b — Ticker Handler
TickerHandlerBase catches every single trade print -- the raw,
microscopic heartbeat of the market. Each ticker event tells you:
- price at which the trade happened
- volume (number of lots)
- direction (buy or sell vs the last trade)
- time down to the millisecond
When volume spikes or price moves sharply, ticker data is where
you see it first -- before it shows up in a K-line or quote update.
SDK: OpenQuoteContext.set_handler() + TickerHandlerBase
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
import time
import futu as ft
from connect import create_quote_context
class MyTickerHandler(ft.TickerHandlerBase):
def on_recv_rsp(self, rsp_pb):
ret_code, content = super().on_recv_rsp(rsp_pb)
if ret_code != ft.RET_OK:
return ft.RET_ERROR, content
# content is a DataFrame with columns:
# code, name, time, price, volume, direction, ticker_type, ...
for _, row in content.iterrows():
ts = row.get("time", "?")
code = row.get("code", "?")
price = row.get("price", "?")
vol = row.get("volume", "?")
direction = row.get("direction", "?")
tick_type = row.get("type", "?")
dir_str = "BUY" if str(direction) == "1" else "SELL"
print(f" {ts} | {code} | {dir_str:4s} | price={price} | vol={vol} | {tick_type}")
return ft.RET_OK, content
def main():
ctx = create_quote_context()
try:
ctx.set_handler(MyTickerHandler())
stock = "HK.00700"
print(f"Subscribing to {stock} TICKER stream...\n")
print("Format: timestamp | code | direction | price | volume | type\n")
ret, _ = ctx.subscribe(stock, ft.SubType.TICKER)
if ret != 0:
print(f"Subscribe failed: {ret}")
return
print("Waiting 15s for ticker prints (press Ctrl+C to exit)...\n")
time.sleep(15)
finally:
ctx.close()
print("\nDone.")
if __name__ == "__main__":
main()