fix zt&state.py

This commit is contained in:
2026-09-12 13:42:23 +08:00
parent bcb03e2ed9
commit 7a7049ce44
9 changed files with 677 additions and 363 deletions

View File

@@ -1,6 +1,7 @@
"""趋势策略持仓止盈与分级补仓。"""
from dataclasses import dataclass
import math
from libs.calc import calc_buy_volume, calculate_min_profit_rate
from libs.grid_take_profit import GridState
@@ -45,9 +46,9 @@ def manage_positions(
continue
if (
not code
or position.open_price <= 0
or position.volume <= 0
or tick is None
or not math.isfinite(tick.last_price)
or tick.last_price <= 0
):
log.warning(
@@ -57,16 +58,24 @@ def manage_positions(
)
continue
if code in state.blocked_codes:
log.warning('[Position] %s 状态待核对,暂停该证券交易', code)
continue
posState = state.get_by_code(position.stock_code)
if not posState:
continue
volume = position.can_use_volume
target_qty = posState.get('base_qty', 0)
cost_price = position.open_price
if posState.get('added_qty',0) >=100:
volume = posState.get('added_qty',0)
if posState.get('added_qty', 0) > 0:
target_qty = posState['added_qty']
cost_price = posState.get('added_price',0)
# 在途股份不影响已有可卖库存;补仓、底仓均受柜台可卖上限约束。
volume = max(0, min(target_qty, position.can_use_volume, position.volume))
if not math.isfinite(cost_price) or cost_price <= 0:
log.warning('[Position] %s 成本无效,暂停该证券交易', code)
continue
pnl_rate = round(
(tick.last_price - cost_price) / cost_price * 100,
@@ -86,7 +95,8 @@ def manage_positions(
if runtime.account_cfg.enable_loss_add_position and market_ok:
loss_decision = handle_loss(
runtime=runtime,
position=position,
stock_code=position.stock_code,
volume=volume,
tick=tick,
pnl_rate=pnl_rate,
available=available,
@@ -99,7 +109,7 @@ def manage_positions(
strTag = "-"
if pnl_rate >= minimum_profit:
strTag = ""
elif pnl_rate< LOSS_TIERS[0]:
elif pnl_rate< LOSS_TIERS:
strTag = ""
if strTag != "-":
@@ -148,9 +158,8 @@ def handle_profit(
if runtime.orders.busy(stock_code, "SELL"):
return TradeDecision(False, "卖出委托处理中")
volume = volume % 100
if volume <= 0:
return TradeDecision(False, "无可用整手持仓")
return TradeDecision(False, "无可用持仓")
order_id = runtime.orders.new_order_id("zt","SELL")
request = PlaceOrderRequest(
op=OP_SELL,
@@ -199,7 +208,7 @@ def handle_loss(
if not runtime.orders.place(runtime.client, request):
return TradeDecision(False, "补仓订单委托失败")
runtime.add_watch.forget(position.stock_code)
runtime.add_watch.forget(stock_code)
return TradeDecision(True, f"[补仓买入] {volume} 股,订单={order_id}", amount)