Files
big-qmt/py-client/strategy/trend/open.py

137 lines
5.1 KiB
Python
Raw Normal View History

2026-08-31 15:33:39 +08:00
"""趋势策略开仓逻辑。"""
2026-08-28 18:52:27 +08:00
from __future__ import annotations
from datetime import datetime
from libs import calc_buy_volume
from sdk import OP_BUY
2026-08-31 13:00:22 +08:00
from .runtime import Runtime
2026-08-28 18:52:27 +08:00
from .order import PlaceOrderRequest
2026-09-01 14:28:49 +08:00
import logging as log
2026-08-28 18:52:27 +08:00
2026-08-31 13:00:22 +08:00
def open_signal(run:Runtime, ticks, open_signals) -> None:
2026-08-28 18:52:27 +08:00
"""逐个验证开仓信号并提交买入委托。"""
for item in open_signals:
2026-09-05 11:46:31 +08:00
if item.code in run.account_cfg.excluded_codes:
log.info("[Open] %s 信号=%s,跳过:已配置为排除股票", item.code, item.signal_key)
continue
2026-08-28 18:52:27 +08:00
# 1. 验证信号配置允许开仓的时间区间。
signal_config = run.global_cfg.signals.get(item.signal_key)
2026-09-04 23:08:21 +08:00
if signal_config is None:
log.warning(
"[Open] %s 信号=%s,跳过:未找到信号配置",
item.code,
item.signal_key,
)
continue
if not check_timezone(signal_config.timezone):
log.info(
"[Open] %s 信号=%s,跳过:不在信号时间段(%s)",
item.code,
item.signal_key,
signal_config.timezone,
)
2026-08-28 18:52:27 +08:00
continue
# 2. 检查该证券是否已有买入委托锁,防止重复下单。
if run.orders.busy(item.code,"BUY"):
2026-09-01 15:49:09 +08:00
log.info("[Open] %s 信号=%s,跳过:买入委托处理中", item.code, item.signal_key)
2026-08-28 18:52:27 +08:00
continue
# 3. 验证行情和最新价格是否有效。
tick = ticks.get(item.code)
price = tick.last_price if tick is not None else 0
if price <= 0:
2026-09-01 15:49:09 +08:00
log.info("[Open] %s 信号=%s,跳过:价格无效", item.code, item.signal_key)
2026-08-28 18:52:27 +08:00
continue
# 5. 根据单笔买入金额计算整手开仓数量。
volume = calc_buy_volume(price, run.account_cfg.buy_value)
if volume <= 0:
2026-09-01 15:49:09 +08:00
log.info("[Open] %s 信号=%s,跳过:数量无效", item.code, item.signal_key)
2026-08-28 18:52:27 +08:00
continue
2026-08-31 13:00:22 +08:00
# 当前价高于昨收价可开仓
if signal_config.gt_last_price_is_open and item.last_close>0 and price>item.last_close:
try:
2026-09-03 15:27:11 +08:00
do_open(run, item.code, volume, item.signal_key, price)
2026-09-01 15:49:09 +08:00
log.info("[Open] %s 信号=%s,买入=%d股,原因=现价高于昨收", item.code, item.signal_key, volume)
2026-09-01 14:28:49 +08:00
except RuntimeError as exc:
2026-09-01 15:49:09 +08:00
log.info("[Open] %s 信号=%s,买入=%d股失败:%s", item.code, item.signal_key, volume, exc)
2026-09-01 14:28:49 +08:00
except Exception:
2026-09-01 15:49:09 +08:00
log.exception("[Open] %s 信号=%s,买入=%d股异常", item.code, item.signal_key, volume)
2026-08-31 13:00:22 +08:00
continue
# 4. 等待价格从观察低点反弹,防止直接接下跌中的“飞刀”。
if not run.open_watch.triggered("开仓", item.code, price):
2026-08-28 18:52:27 +08:00
continue
try:
2026-09-03 15:27:11 +08:00
do_open(run, item.code, volume, item.signal_key, price)
2026-09-01 15:49:09 +08:00
log.info("[Open] %s 信号=%s,买入=%d股,原因=反弹已确认", item.code, item.signal_key, volume)
2026-09-01 14:28:49 +08:00
except RuntimeError as exc:
2026-09-01 15:49:09 +08:00
log.warning("[Open] %s 信号=%s,买入=%d股失败:%s", item.code, item.signal_key, volume, exc)
2026-09-01 14:28:49 +08:00
except Exception:
2026-09-01 15:49:09 +08:00
log.exception("[Open] %s 信号=%s,买入=%d股异常", item.code, item.signal_key, volume)
2026-08-31 13:00:22 +08:00
2026-09-03 15:27:11 +08:00
def do_open(run: Runtime, code: str, volume: int, signal_key: str, price: float) -> None:
2026-08-31 13:00:22 +08:00
"""生成本地订单号并按最新价提交开仓委托。"""
order_id = run.orders.new_order_id()
2026-08-31 13:00:22 +08:00
request = PlaceOrderRequest(
OP_BUY,
code,
volume,
order_id,
signal_key,
2026-09-05 13:53:26 +08:00
kind="base",
2026-08-31 13:00:22 +08:00
)
2026-09-05 14:30:16 +08:00
if not run.orders.place(run.client,request):
2026-08-31 13:00:22 +08:00
raise RuntimeError("订单提交失败")
2026-08-28 18:52:27 +08:00
2026-09-03 15:27:11 +08:00
run.open_watch.forget(code)
2026-08-28 18:52:27 +08:00
def check_timezone(timezone: str, now: datetime | None = None) -> bool:
"""验证当前时间是否处于配置区间。
``*`` 表示全天允许多个区间用逗号分隔例如
``9:30-10:30,13:30-14:30``同时支持跨午夜区间
"""
timezone = str(timezone or "").strip()
if timezone == "*":
return True
current = now or datetime.now()
current_minutes = current.hour * 60 + current.minute
for section in timezone.split(","):
bounds = section.strip().split("-")
if len(bounds) != 2:
continue
start = _parse_minutes(bounds[0])
end = _parse_minutes(bounds[1])
if start is None or end is None:
continue
if start <= end and start <= current_minutes <= end:
return True
if start > end and (current_minutes >= start or current_minutes <= end):
return True
return False
def _parse_minutes(value: str) -> int | None:
"""把 ``时:分`` 转换为当天分钟数,无效值返回 None。"""
try:
hour_text, minute_text = value.strip().split(":")
hour, minute = int(hour_text), int(minute_text)
except (TypeError, ValueError):
return None
if not 0 <= hour <= 23 or not 0 <= minute <= 59:
return None
return hour * 60 + minute