From 4787011177f34f08d2a685148b2686dce43730c5 Mon Sep 17 00:00:00 2001 From: "GDP\\solonot" Date: Tue, 25 Nov 2025 17:13:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=97=A5=E5=BF=97=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 3 ++- core/logger.py | 16 ++++++++++------ core/main_ui.py | 21 +++++++++++++++++---- core/qmt.py | 11 ++++++----- starter.py | 2 +- 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/config.py b/config.py index 25291b9..d9ce65a 100644 --- a/config.py +++ b/config.py @@ -40,7 +40,7 @@ def exist_config() -> bool: return config_path.exists() def initConfig() -> bool: - global miniQMTPath, account_no + global miniQMTPath, account_no, log_level # 获取配置文件路径 config_path = get_config_path() @@ -49,6 +49,7 @@ def initConfig() -> bool: config.read(config_path, encoding='utf-8') miniQMTPath = config.get('config','miniQMTPath') account_no = config.get('config','account_no') + log_level = config.get('config','log_level') # 判断miniQMTPath是否为空,并且目录是否存在 if not miniQMTPath or not Path(miniQMTPath).exists(): diff --git a/core/logger.py b/core/logger.py index 896e46a..019bc09 100644 --- a/core/logger.py +++ b/core/logger.py @@ -3,12 +3,16 @@ from enum import Enum from core.eventbus import EventPrintLog, event_bus import config + class LogLevel(Enum): - DEBUG = "DEBUG" - INFO = "INFO" - WARNING = "WARNING" - ERROR = "ERROR" - CRITICAL = "CRITICAL" + DEBUG = 0 + INFO = 1 + WARNING = 2 + ERROR = 3 + CRITICAL = 4 + + def __le__(self, other): + return self.value <= other.value class LogData: def __init__(self, level:LogLevel, message:str): @@ -19,4 +23,4 @@ def PrintLog(level:LogLevel, message:str): data = LogData(level, message) event_bus.publish(EventPrintLog, data) if config.console_log: - print(f'{level.value} {message}') + print(f'{level.name} {message}') diff --git a/core/main_ui.py b/core/main_ui.py index f200fc1..076d21b 100644 --- a/core/main_ui.py +++ b/core/main_ui.py @@ -1,13 +1,16 @@ import tkinter as tk from tkinter import ttk -from core.logger import LogLevel +from core.logger import LogLevel, LogData, PrintLog from core.qmt import qmtv from core.sfgrid.sfgrid_ui import TradeTargetUI from tkinter import ttk, messagebox +from core.eventbus import EventPrintLog +from core.eventbus import event_bus as eBus + class MainWindow: - def __init__(self): + def __init__(self, configLogLevel:str): self.root = tk.Tk() self.root.title("神之一手 - 交易系统") self.root.geometry("1400x700") @@ -19,6 +22,11 @@ class MainWindow: # 日志面板可见性标志 self.log_visible = False self.create_ui() + + self.logLevel = LogLevel[configLogLevel] + PrintLog(LogLevel.DEBUG, f"系统启动成功 {self.logLevel.name}") + + eBus.subscribe(EventPrintLog, self.on_log_event) def create_ui(self): @@ -40,7 +48,7 @@ class MainWindow: # 创建Tab按钮(垂直排列,文字垂直显示) self.tab_buttons = [] - strategy_names = ["蒙派", "复盘"] + strategy_names = ["网格", "复盘"] for idx, name in enumerate(strategy_names): btn = ttk.Button( @@ -167,12 +175,17 @@ class MainWindow: self.log_table.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) + + def on_log_event(self, event:LogData): + if self.logLevel.value <= event.level.value: + self.add_log(event.level, event.message) + def add_log(self, level:LogLevel, message): """添加日志记录 - 全局方法""" from datetime import datetime timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - self.log_table.insert('', 0, values=(timestamp, level.value, message)) + self.log_table.insert('', 0, values=(timestamp, level.name, message)) def clear_logs(self): """清空日志记录""" diff --git a/core/qmt.py b/core/qmt.py index bd49599..98fdd3d 100644 --- a/core/qmt.py +++ b/core/qmt.py @@ -94,7 +94,7 @@ class QmtV(XtQuantTraderCallback): def dailyUpStop(self, stock_code:str): cacheStock = self.cacheStockDetail(stock_code) - PrintLog(LogLevel.INFO, f'- [成功]获取股票详情: {stock_code} {cacheStock["InstrumentName"]} {cacheStock['UpStopPrice']}') + PrintLog(LogLevel.INFO, f'- [成功]获取股票详情: {stock_code} {cacheStock["InstrumentName"]} {cacheStock["UpStopPrice"]}') return cacheStock['UpStopPrice'] def dailyDownStop(self, stock_code:str): @@ -134,10 +134,11 @@ class QmtV(XtQuantTraderCallback): tmpMarketStatus = self.isMarketActive PrintLog(LogLevel.INFO, f'- [市场状态变更] {self.isMarketActive}') eBus.event_bus.publish(eBus.EventMarketActiveSwitch, self.isMarketActive) - if tmpMarketStatus and self.isMarketActive: - if tmpTime - self.lastMarketDataUpdateTimestamp > 10: # 上次更新市场状态已经超过10秒 - self.isMarketActive = False - PrintLog(LogLevel.INFO, f'- [市场状态] {self.isMarketActive}') # 市场已 inactive + if tmpMarketStatus and self.isMarketActive and tmpTime - self.lastMarketDataUpdateTimestamp > 10: # 上次更新市场状态已经超过10秒 + self.isMarketActive = False + PrintLog(LogLevel.INFO, f'- [市场状态变更] {self.isMarketActive}') + + PrintLog(LogLevel.DEBUG, f'- [市场状态] {self.isMarketActive}') # 市场已 inactive # ====== 市场回调方法 -- 以下方法由XtQuantTrader调用 ====== diff --git a/starter.py b/starter.py index 0249097..1bd2280 100644 --- a/starter.py +++ b/starter.py @@ -131,7 +131,7 @@ def initialize_system(): connected = qmtv.connect() if connected: # 连接成功,启动主窗口 - window = MainWindow() + window = MainWindow(sdConstants.log_level) window.run() break else: