optz
This commit is contained in:
@@ -1,67 +1,191 @@
|
||||
"""ETF 策略入口:每 30 秒运行,日线指标当天缓存,失败标的单独重试。"""
|
||||
"""趋势策略启动器。
|
||||
|
||||
该模块负责组合 SDK、配置、状态存储和趋势策略组件,供 main.py 调用。
|
||||
"""
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
import hashlib
|
||||
import logging as log
|
||||
from pathlib import Path
|
||||
import time
|
||||
import httpx
|
||||
import logging as log
|
||||
from concurrent.futures import Future, ThreadPoolExecutor
|
||||
from datetime import datetime
|
||||
|
||||
import config
|
||||
from libs.calc import trading_time
|
||||
from libs.snapshot import cache_portfolio
|
||||
from libs.market import market_allow_open
|
||||
from libs.overview import Overview
|
||||
from libs.signal import init_signals, SignalItem
|
||||
from sdk import Client
|
||||
|
||||
from .config import load
|
||||
from .data import daily_bars
|
||||
from .engine import Engine
|
||||
from .indicators import calculate
|
||||
from .state import Store
|
||||
|
||||
from libs.snapshot import cache_portfolio
|
||||
from libs.grid_take_profit import GridTrailingTracker
|
||||
from libs.order import OrderBook
|
||||
from libs.watch import DipWatch
|
||||
from libs.runtime import Runtime
|
||||
from .open import open_signal
|
||||
from .positions import manage_positions
|
||||
from .signal import gen_signals
|
||||
|
||||
def StartETF() -> None:
|
||||
cfg = load(config.account_config.etf_config_path or None)
|
||||
account = str(config.account_config.account_id).strip()
|
||||
if not account:
|
||||
raise ValueError('ETF 策略缺少账户编号')
|
||||
key = hashlib.sha256(account.encode('utf-8')).hexdigest()
|
||||
store = Store(Path(config.global_config.qmt_data_dir) / 'etf' / key / 'state.json', account)
|
||||
# 独立 HTTP 连接池读取外部日线,不向外部接口发送 QMT 认证信息。
|
||||
with Client(config.global_config.qmt_base_url, config.global_config.qmt_token, config.HTTP_TIMEOUT) as client, \
|
||||
httpx.Client(timeout=config.HTTP_TIMEOUT) as history_client:
|
||||
engine = Engine(client, cfg, store, config.account_config.min_cash_ratio,
|
||||
config.account_config.excluded_codes)
|
||||
log.info('[ETF启动] 标的=%s 每次=%d手 每只上限=%d手 状态=%s',
|
||||
cfg.codes, cfg.buy_hands, cfg.max_hands, store.path)
|
||||
log.info('[ETF启动] 管理配置白名单内已有持仓,卖出以券商可用份额为限')
|
||||
indicators, retry_at = {}, {}
|
||||
cached_day = None
|
||||
"""初始化趋势策略,并以 30 秒间隔持续执行。"""
|
||||
client = Client(
|
||||
config.global_config.qmt_base_url,
|
||||
config.global_config.qmt_token,
|
||||
config.HTTP_TIMEOUT,
|
||||
)
|
||||
executor = None
|
||||
try:
|
||||
portfolio = client.portfolio()
|
||||
assets = portfolio.assets
|
||||
positions = list(portfolio.positions.values())
|
||||
cache_portfolio(config.account_config.account_id, assets, positions, client.deals())
|
||||
order_book = OrderBook()
|
||||
order_book.refresh(client, portfolio.orders)
|
||||
|
||||
executor = ThreadPoolExecutor(max_workers=2, thread_name_prefix="ETF")
|
||||
run = Runtime(
|
||||
client=client,
|
||||
etf_cfg=config.etf_config,
|
||||
orders=order_book,
|
||||
executor=executor,
|
||||
)
|
||||
|
||||
# 获取本策略的信号开仓数据
|
||||
signals = gen_signals(run)
|
||||
log.info(
|
||||
"[启动] ETF策略已启动,账户=%s,信号=%d,持仓=%d",
|
||||
config.account_config.account_id,
|
||||
len(signals),
|
||||
len(positions),
|
||||
)
|
||||
|
||||
Overview(assets, positions, config.account_config)
|
||||
|
||||
DEFAULT_TICK_INTERVAL = 30
|
||||
while True:
|
||||
now = datetime.now()
|
||||
if now.hour >= 15:
|
||||
log.info('[ETF结束] 已到 15:00')
|
||||
lt = time.localtime()
|
||||
if (lt.tm_hour, lt.tm_min, lt.tm_sec) >= (15, 0, 0):
|
||||
log.info("[ETF] 已到 15:00,结束趋势策略")
|
||||
return
|
||||
if trading_time(now):
|
||||
try:
|
||||
if cached_day != now.date():
|
||||
indicators, retry_at, cached_day = {}, {}, now.date()
|
||||
for code in cfg.codes:
|
||||
if code in indicators or now < retry_at.get(code, datetime.min):
|
||||
continue
|
||||
try:
|
||||
rows = daily_bars(history_client, code, now.date())
|
||||
indicators[code] = calculate(rows, now.date(), cfg)
|
||||
log.info('[ETF指标] %s %s', code, indicators[code])
|
||||
except Exception:
|
||||
retry_at[code] = now + timedelta(minutes=5)
|
||||
log.exception('[ETF日线] %s 获取或计算失败,5分钟后重试', code)
|
||||
portfolio = client.portfolio()
|
||||
ticks = client.full_tick(list(cfg.codes))
|
||||
engine.run(portfolio, ticks, indicators, datetime.now())
|
||||
try:
|
||||
cache_portfolio(account, portfolio.assets, list(portfolio.positions.values()), client.deals())
|
||||
except Exception:
|
||||
log.exception('[ETF采集] 成交快照读取失败')
|
||||
except Exception:
|
||||
log.exception('[ETF异常] 本轮失败,下一轮继续')
|
||||
time.sleep(30 - datetime.now().second % 30)
|
||||
current_sec = lt.tm_sec
|
||||
|
||||
# 计算距离下一个目标时间点(0秒或30秒)的等待时间
|
||||
if current_sec < DEFAULT_TICK_INTERVAL:
|
||||
wait_seconds = DEFAULT_TICK_INTERVAL - current_sec
|
||||
elif current_sec < 60:
|
||||
wait_seconds = 60 - current_sec
|
||||
else:
|
||||
wait_seconds = DEFAULT_TICK_INTERVAL
|
||||
|
||||
# 等待到目标时间点
|
||||
time.sleep(wait_seconds)
|
||||
|
||||
# 单轮失败不能杀死唯一的交易定时线程。
|
||||
try:
|
||||
RunOnce(run, signals)
|
||||
except Exception as e:
|
||||
log.error(
|
||||
f"[ETF] 本 tick 执行失败,下一 tick 继续: {e}", exc_info=True
|
||||
)
|
||||
finally:
|
||||
try:
|
||||
if executor is not None:
|
||||
executor.shutdown(wait=True)
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
|
||||
def RunOnce(run: Runtime, signals: list[SignalItem]) -> None:
|
||||
"""按固定步骤执行一轮趋势策略, ``RunOnce``。"""
|
||||
if not trading_time(datetime.now()):
|
||||
return
|
||||
|
||||
print(
|
||||
"=" * 40 + f" Ticker {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} " + "=" * 40
|
||||
)
|
||||
|
||||
started_at = time.monotonic()
|
||||
|
||||
# 1. 一次获取资产、持仓和订单,并清理过期订单。
|
||||
try:
|
||||
portfolio = run.client.portfolio()
|
||||
assets = portfolio.assets
|
||||
position_codes = list(portfolio.positions)
|
||||
positions = list(portfolio.positions.values())
|
||||
cache_portfolio(run.account_cfg.account_id, assets, positions, run.client.deals())
|
||||
run.orders.refresh(run.client, portfolio.orders)
|
||||
except Exception:
|
||||
log.exception("[Portfolio] 刷新账户快照失败")
|
||||
return
|
||||
|
||||
futures: list[tuple[str, Future]] = []
|
||||
|
||||
# 2. 验证可用资金;低于资金安全线时禁止开新仓。
|
||||
allow_open_by_cash = (
|
||||
assets.available >= assets.total * run.account_cfg.min_cash_ratio
|
||||
)
|
||||
if not allow_open_by_cash:
|
||||
log.info(
|
||||
"[Status] 禁止开仓:可用资金不足,可用=%.2f,总资产=%.2f",
|
||||
assets.available,
|
||||
assets.total,
|
||||
)
|
||||
|
||||
# 3. 获取大盘状态,只有大盘信号允许时才执行开仓。
|
||||
market_ok = market_allow_open()
|
||||
|
||||
# 4. 验证有效开仓信号:排除已有持仓。
|
||||
allow_open: list[SignalItem] = []
|
||||
allow_codes: list[str] = []
|
||||
for signal in signals:
|
||||
if signal.code not in portfolio.positions:
|
||||
allow_open.append(signal)
|
||||
allow_codes.append(signal.code)
|
||||
|
||||
if allow_open and not market_ok:
|
||||
log.info("[开仓] 禁止开仓:大盘信号不允许,候选=%d", len(allow_open))
|
||||
|
||||
# 5. 获取持仓和待开仓证券的实时行情 tick。
|
||||
all_codes = list(dict.fromkeys(position_codes + allow_codes))
|
||||
try:
|
||||
ticks = run.client.full_tick(all_codes)
|
||||
except Exception:
|
||||
log.exception("[行情] 获取行情失败,代码数量=%d", len(all_codes))
|
||||
return
|
||||
|
||||
log.info(
|
||||
"[RunOnce] 本轮就绪,持仓=%d,候选=%d,大盘允许=%s,资金允许=%s",
|
||||
len(positions),
|
||||
len(allow_open),
|
||||
market_ok,
|
||||
allow_open_by_cash,
|
||||
)
|
||||
|
||||
# 启动线程,开始计算
|
||||
# 7. 持仓计算。
|
||||
futures.append(
|
||||
(
|
||||
"持仓计算",
|
||||
run.executor.submit(
|
||||
manage_positions, run, ticks, positions, market_ok, assets.available
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
# 8. 开仓计算:必须同时存在有效信号且大盘允许开仓。
|
||||
if allow_open and market_ok and allow_open_by_cash:
|
||||
futures.append(
|
||||
("开仓计算", run.executor.submit(open_signal, run, ticks, allow_open))
|
||||
)
|
||||
|
||||
# 9. 开始执行
|
||||
for name, future in futures:
|
||||
_wait_worker(name, future)
|
||||
log.info(
|
||||
"[RunOnce] 本轮完成,耗时=%d毫秒", int((time.monotonic() - started_at) * 1000)
|
||||
)
|
||||
|
||||
|
||||
def _wait_worker(name: str, future: Future) -> None:
|
||||
"""保留单轮继续运行的语义,分别记录工作线程异常。"""
|
||||
try:
|
||||
future.result()
|
||||
except Exception:
|
||||
log.exception("[运行] %s线程失败", name)
|
||||
|
||||
Reference in New Issue
Block a user