大家好,我是策略老李。这2天好多读者在后台私信老李,“老李,啥时候更新下一篇啊”。惭愧,惭愧,老李目前还是一个上班族,作为一个芯片架构师虽然收入不菲但也只能在个人时间回复读者问题,写写文章。不过读者最大,今天老李为大家奉上《手把手编写miniQMT实盘量化执行程序》的第3篇-交易系统。废话不多说直接上代码:
livesystem.py
"""
交易系统
"""
import os
import pytz
import time
import subprocess
from datetime import datetime, timedelta, time as dt_time
from typing import Dict
from .liveengine import LiveEngine
from ..config.config import config
from ..core.exceptions import TradingSystemError
from ..utils.logger import sys_logger
logger = sys_logger.getChild('LiveSystem')
class LiveSystem:
"""交易系统主控类,负责协调各组件工作流程"""
MODE = 'live'
def __init__(self):
"""初始化交易系统"""
self._config = config
self._init_flag = False # 组件初始化标志
self.engine = None
# 记录当前日期
self._timezone = pytz.timezone('Asia/Shanghai')
#self._current_date = date(1900,1,1) #立即启动处理流程
self._current_date = datetime.now(self._timezone).date()
# 交易日状态管理
self._trading_status: Dict[str, bool] = {
'pre_market': False, # 开盘前准备完成
'on_open': False, # 开盘交易
'on_trade': False, # 盘中交易
'on_close': False, # 收盘交易
'post_market': False, # 收盘后处理完成
'is_trading_day': False, # 是否为交易日
'is_trading_time': False # 是否在交易时段
}
logger.debug("交易系统实例已创建")
def __enter__(self) -> None:
"""上下文管理器入口"""
try:
if not self._init_flag:
self._init()
return self
except Exception:
logger.critical("交易系统进入上下文失败", exc_info=True)
raise
def __exit__(self, exc_type, exc_val, exc_tb):
"""上下文管理器出口"""
try:
self.shutdown()
except Exception as e:
logger.error("系统关闭时发生异常: %s", str(e), exc_info=True)
finally:
self._init_flag = False
return False # 传播异常
def set_terminal_title(self, title: str = ""):
"""设置终端窗口标题(仅限Windows)"""
try:
full_title = f"{title} mode: {self.MODE} {os.getcwd()}"
command = f'$Host.UI.RawUI.WindowTitle = "{full_title}"'
subprocess.run(["powershell", "-Command", command], check=True)
logger.debug("终端标题已更新: %s", full_title)


1564

被折叠的 条评论
为什么被折叠?



