2026-08-31 13:00:22 +08:00
|
|
|
"""使用 dcm 信号建立做 T 底仓。"""
|
|
|
|
|
|
2026-09-06 11:53:28 +08:00
|
|
|
from datetime import datetime
|
|
|
|
|
import logging as log
|
|
|
|
|
import math
|
2026-08-31 13:00:22 +08:00
|
|
|
|
|
|
|
|
from libs.calc import calc_buy_volume
|
|
|
|
|
from sdk import OP_BUY
|
2026-09-06 13:12:48 +08:00
|
|
|
from libs.runtime import Runtime
|
|
|
|
|
from libs.order import PlaceOrderRequest
|
2026-08-31 13:00:22 +08:00
|
|
|
|
|
|
|
|
|
2026-09-08 15:18:09 +08:00
|
|
|
def open_signal(run: Runtime, ticks, signals, available: float) -> float:
|
2026-09-06 11:53:28 +08:00
|
|
|
"""逐个验证开仓信号并提交买入委托,返回本轮剩余资金。"""
|
|
|
|
|
for item in signals:
|
|
|
|
|
try:
|
|
|
|
|
now = datetime.now()
|
|
|
|
|
if (now.hour, now.minute) >= (14, 50):
|
|
|
|
|
break
|
2026-09-07 00:27:33 +08:00
|
|
|
if item.code in run.account_cfg.excluded_codes:
|
2026-09-06 11:53:28 +08:00
|
|
|
continue
|
2026-09-07 00:27:33 +08:00
|
|
|
# 由委托簿检查活动委托,防止重复下单。
|
2026-09-06 13:12:48 +08:00
|
|
|
if (
|
2026-09-07 00:27:33 +08:00
|
|
|
run.orders.busy(item.code, "BUY")
|
2026-09-06 13:12:48 +08:00
|
|
|
or run.orders.busy(item.code, "SELL")
|
|
|
|
|
):
|
2026-09-06 11:53:28 +08:00
|
|
|
continue
|
2026-09-07 00:27:33 +08:00
|
|
|
# 行情无效或超过策略价格上限时跳过。
|
2026-09-06 11:53:28 +08:00
|
|
|
tick = ticks.get(item.code)
|
|
|
|
|
price = tick.last_price if tick else 0.0
|
2026-09-06 13:12:48 +08:00
|
|
|
if (
|
|
|
|
|
not math.isfinite(price)
|
|
|
|
|
or price <= 0
|
|
|
|
|
or price > run.account_cfg.zt_max_price
|
|
|
|
|
):
|
2026-09-06 11:53:28 +08:00
|
|
|
continue
|
2026-09-07 00:27:33 +08:00
|
|
|
# 根据单笔买入金额计算整手数量,并预留少量价差和费用。
|
2026-09-06 11:53:28 +08:00
|
|
|
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
|
2026-09-07 00:27:33 +08:00
|
|
|
# 等待价格从观察低点反弹,防止直接接下跌中的“飞刀”。
|
2026-09-06 11:53:28 +08:00
|
|
|
if not run.open_watch.triggered("ZT 建仓", item.code, price):
|
|
|
|
|
continue
|
|
|
|
|
order_id = run.orders.new_order_id("base")
|
2026-09-06 13:12:48 +08:00
|
|
|
request = PlaceOrderRequest(
|
|
|
|
|
OP_BUY, item.code, volume, order_id, "zt", kind="base"
|
|
|
|
|
)
|
2026-09-07 00:27:33 +08:00
|
|
|
# 即使响应丢失,本轮也预留资金;状态簿只在取得实际成交后入账。
|
2026-09-06 11:53:28 +08:00
|
|
|
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
|