-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
111 lines (94 loc) · 4.64 KB
/
Copy pathmain.py
File metadata and controls
111 lines (94 loc) · 4.64 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
# -*- 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_stock_filter)
Demonstrates:
- SimpleFilter: filter by simple numeric fields (price, volume, etc.)
- FinancialFilter: filter by financial metrics (ratios, earnings, etc.)
- get_stock_filter: combined stock screener
- StockField: all available filter fields
- FinancialQuarter: annual vs quarterly financial data
- All returned fields logged
"""
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__)
def main():
logger.info("=== Stock Filter (Screener) Demo ===")
ctx = create_quote_context()
try:
# ── Simple Filter: stocks with price between 2-1000 HKD ─────────
logger.info("\n--- SimpleFilter: CUR_PRICE 2-1000 HKD ---")
simple_filter = ft.SimpleFilter()
simple_filter.filter_min = 2
simple_filter.filter_max = 1000
simple_filter.stock_field = ft.StockField.CUR_PRICE
simple_filter.is_no_filter = False
# ── Financial Filter: current ratio 0.5-50 ───────────────────────
logger.info("--- FinancialFilter: CURRENT_RATIO 0.5-50 (ANNUAL) ---")
financial_filter = ft.FinancialFilter()
financial_filter.filter_min = 0.5
financial_filter.filter_max = 50
financial_filter.stock_field = ft.StockField.CURRENT_RATIO
financial_filter.is_no_filter = False
financial_filter.sort = ft.SortDir.ASCEND # Only one sort direction allowed
financial_filter.quarter = ft.FinancialQuarter.ANNUAL
# ── Run screener for HK market ───────────────────────────────────
logger.info("\n=== get_stock_filter: HK market (Simple + Financial) ===")
ret, ls = ctx.get_stock_filter(
market=ft.Market.HK,
filter_list=[simple_filter, financial_filter],
)
if ret != ft.RET_OK:
logger.error("get_stock_filter failed: %s", ls)
else:
last_page, all_count, ret_list = ls
logger.info("Result: last_page=%s all_count=%d ret_list=%d",
last_page, all_count, len(ret_list))
if not ret_list:
logger.info("No stocks matched the filter criteria")
else:
logger.info("First few matches:")
for item in ret_list[:5]:
logger.info(" code=%s name=%s cur_price=%s",
item.stock_code, item.stock_name, item.cur_price)
logger.info(" item type: %s", type(item).__name__)
logger.info("\nTotal returned: %d stocks | Total count (all pages): %d",
len(ret_list), all_count)
# ── Try different markets ───────────────────────────────────────
for market, label in [(ft.Market.US, "US"), (ft.Market.SH, "SH"), (ft.Market.SZ, "SZ")]:
logger.info("\n=== get_stock_filter: %s market (price only) ===", label)
sf = ft.SimpleFilter()
sf.filter_min = 1
sf.filter_max = 10000
sf.stock_field = ft.StockField.CUR_PRICE
sf.is_no_filter = False
ret, ls = ctx.get_stock_filter(market=market, filter_list=[sf])
if ret != ft.RET_OK:
logger.error("get_stock_filter (%s) failed: %s", label, ls)
else:
last_page, all_count, ret_list = ls
logger.info("%s: %d matches (showing first 3)", label, len(ret_list))
for item in ret_list[:3]:
logger.info(" code=%s name=%s cur_price=%s", item.stock_code, item.stock_name, item.cur_price)
finally:
ctx.close()
logger.info("Done.")
if __name__ == "__main__":
main()