fix trend,zt
This commit is contained in:
@@ -8,12 +8,12 @@ import math
|
||||
|
||||
from libs.calc import calc_buy_volume
|
||||
from sdk import OP_BUY
|
||||
from .runtime import Runtime
|
||||
from .order import PlaceOrderRequest
|
||||
from .state import PendingOrder
|
||||
from libs.runtime import Runtime
|
||||
from libs.order import PlaceOrderRequest
|
||||
from .state import PendingOrder, TState
|
||||
|
||||
|
||||
def open_signal(run: Runtime, ticks, signals, available: float) -> float:
|
||||
def open_signal(run: Runtime, state: TState, ticks, signals, available: float) -> float:
|
||||
"""逐个验证开仓信号并提交买入委托,返回本轮剩余资金。"""
|
||||
for item in signals:
|
||||
try:
|
||||
@@ -22,20 +22,28 @@ def open_signal(run: Runtime, ticks, signals, available: float) -> float:
|
||||
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:
|
||||
item_state = state.items.get(item.code)
|
||||
if item_state is not None and item_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"):
|
||||
if (
|
||||
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:
|
||||
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)
|
||||
@@ -47,8 +55,18 @@ def open_signal(run: Runtime, ticks, signals, available: float) -> float:
|
||||
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()))
|
||||
request = PlaceOrderRequest(
|
||||
OP_BUY, item.code, volume, order_id, "zt", kind="base"
|
||||
)
|
||||
state.new_order(
|
||||
PendingOrder(
|
||||
order_id,
|
||||
item.code,
|
||||
"base",
|
||||
volume,
|
||||
datetime.now().date().isoformat(),
|
||||
)
|
||||
)
|
||||
# 即使响应丢失,也保留资金预算和 pending,不能继续使用这笔钱。
|
||||
available -= amount
|
||||
if run.orders.place(run.client, request):
|
||||
|
||||
Reference in New Issue
Block a user