fix state.py

This commit is contained in:
2026-09-05 15:56:39 +08:00
parent ba1bd93afa
commit 1fc3e119d3
4 changed files with 78 additions and 119 deletions

View File

@@ -10,6 +10,7 @@ from sdk import OP_BUY, OP_SELL, PositionItem, Tick
from .order import PlaceOrderRequest
from .runtime import Runtime
from .state import PendingOrder
import logging as log
LOSS_TIERS = (-30.0, -50.0)
@@ -124,14 +125,13 @@ def handle_profit(
return TradeDecision(False, "无可用整手持仓")
order_id = runtime.orders.new_order_id()
request = PlaceOrderRequest(
client=runtime.client,
op=OP_SELL,
code=position.stock_code,
volume=volume,
order_id=order_id,
strategy_name=runtime.account_cfg.strategy,
)
if not runtime.orders.place(request):
if not runtime.orders.place(runtime.client, request):
return TradeDecision(False, "止盈委托失败")
return TradeDecision(True, f"卖出 {volume} 股,订单={order_id}")
@@ -167,7 +167,6 @@ def handle_loss(
order_id = runtime.orders.new_order_id()
request = PlaceOrderRequest(
client=runtime.client,
op=OP_BUY,
code=position.stock_code,
volume=volume,
@@ -175,7 +174,9 @@ def handle_loss(
strategy_name=runtime.account_cfg.strategy,
kind="add",
)
if not runtime.orders.place(request):
if not runtime.state.new_order(PendingOrder(order_id, position.stock_code, "added", volume)):
return TradeDecision(False, "已有待确认买单")
if not runtime.orders.place(runtime.client, request):
reserved = amount if runtime.state.busy(position.stock_code) else 0.0
return TradeDecision(False, "补仓委托失败或待确认", reserved)
runtime.add_watch.forget(position.stock_code)