Files
2026-09-08 15:18:09 +08:00

58 lines
2.4 KiB
Python

"""使用 dcm 信号建立做 T 底仓。"""
from datetime import datetime
import logging as log
import math
from libs.calc import calc_buy_volume
from sdk import OP_BUY
from libs.runtime import Runtime
from libs.order import PlaceOrderRequest
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.code in run.account_cfg.excluded_codes:
continue
# 由委托簿检查活动委托,防止重复下单。
if (
run.orders.busy(item.code, "BUY")
or run.orders.busy(item.code, "SELL")
):
continue
# 行情无效或超过策略价格上限时跳过。
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
# 根据单笔买入金额计算整手数量,并预留少量价差和费用。
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
# 等待价格从观察低点反弹,防止直接接下跌中的“飞刀”。
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"
)
# 即使响应丢失,本轮也预留资金;状态簿只在取得实际成交后入账。
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