dev zt
This commit is contained in:
@@ -2,30 +2,100 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
import logging as log
|
||||
import math
|
||||
|
||||
from libs.calc import calc_buy_volume
|
||||
from sdk import OP_BUY
|
||||
from strategy.trend.order import PlaceOrderRequest
|
||||
from .runtime import Runtime
|
||||
from .order import PlaceOrderRequest
|
||||
from .state import PendingOrder
|
||||
|
||||
|
||||
def open_signal(run, ticks, signals) -> None:
|
||||
"""仅处理 dcm 信号,使用趋势策略同款反弹确认建立底仓。"""
|
||||
for signal in signals:
|
||||
if signal.signal_key != "dcm" or run.orders.busy(signal.code, "BUY"):
|
||||
def open_signal(run: Runtime, ticks, signals, available: float) -> float:
|
||||
"""逐个验证开仓信号并提交买入委托,返回本轮剩余资金。"""
|
||||
for item in signals:
|
||||
try:
|
||||
now = datetime.now()
|
||||
if (now.hour, now.minute) >= (14, 50):
|
||||
break
|
||||
if item.signal_key != "dcm" or item.code in run.account_cfg.excluded_codes:
|
||||
continue
|
||||
state = run.state.items.get(item.code)
|
||||
if state is not None and state.base_qty > 0:
|
||||
continue
|
||||
# 1. 验证信号配置允许开仓的时间区间。
|
||||
signal_config = run.global_cfg.signals.get("dcm")
|
||||
if signal_config is None or not check_timezone(signal_config.timezone):
|
||||
continue
|
||||
# 2. 检查该证券是否已有买入委托锁,防止重复下单。
|
||||
if run.state.busy(item.code) or run.orders.busy(item.code, "BUY") or run.orders.busy(item.code, "SELL"):
|
||||
continue
|
||||
# 3. 验证行情和最新价格是否有效。
|
||||
tick = ticks.get(item.code)
|
||||
price = tick.last_price if tick else 0.0
|
||||
if not math.isfinite(price) or price <= 0 or price > run.account_cfg.zt_max_price:
|
||||
continue
|
||||
# 4. 根据单笔买入金额计算整手开仓数量,预留少量价差和费用。
|
||||
budget = min(run.account_cfg.buy_value, available)
|
||||
volume = calc_buy_volume(price, budget)
|
||||
amount = price * volume * 1.01
|
||||
if volume <= 0 or price * volume > budget or amount > available:
|
||||
continue
|
||||
# 5. 等待价格从观察低点反弹,防止直接接下跌中的“飞刀”。
|
||||
if not run.open_watch.triggered("ZT 建仓", item.code, price):
|
||||
continue
|
||||
order_id = run.orders.new_order_id("base")
|
||||
request = PlaceOrderRequest(OP_BUY, item.code, volume, order_id, "zt", kind="base")
|
||||
run.state.new_order(PendingOrder(order_id, item.code, "base", volume, datetime.now().date().isoformat()))
|
||||
# 即使响应丢失,也保留资金预算和 pending,不能继续使用这笔钱。
|
||||
available -= amount
|
||||
if run.orders.place(run.client, request):
|
||||
run.open_watch.forget(item.code)
|
||||
log.info("[ZT 建仓] %s 买入 %d 股,等待实际成交", item.code, volume)
|
||||
except Exception:
|
||||
log.exception("[ZT 建仓] %s 处理异常,继续后续信号", item.code)
|
||||
return available
|
||||
|
||||
|
||||
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
|
||||
tick = ticks.get(signal.code)
|
||||
price = tick.last_price if tick else 0.0
|
||||
if price <= 0 or price > run.account_cfg.zt_max_price:
|
||||
start = _parse_minutes(bounds[0])
|
||||
end = _parse_minutes(bounds[1])
|
||||
if start is None or end is None:
|
||||
continue
|
||||
volume = calc_buy_volume(price, run.account_cfg.buy_value)
|
||||
if volume <= 0 or not run.buy_watch.triggered("ZT 建仓", signal.code, price):
|
||||
continue
|
||||
request = PlaceOrderRequest(run.client, OP_BUY, signal.code, volume, run.orders.new_order_id("base"), run.account_cfg.strategy)
|
||||
if run.orders.place(request):
|
||||
run.buy_watch.forget(signal.code)
|
||||
logging.info("[ZT 建仓] %s 买入 %d 股", signal.code, volume)
|
||||
|
||||
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
|
||||
|
||||
|
||||
# 与 trend 策略的开仓函数命名保持一致。
|
||||
open_base = open_signal
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user