This commit is contained in:
2026-09-05 11:46:31 +08:00
parent 3689a7ad86
commit 63ffc1329b
13 changed files with 229 additions and 731 deletions

View File

@@ -42,58 +42,61 @@ def manage_positions(
remaining_cash = max(0.0, available)
for position in positions:
code = position.stock_code
tick = ticks.get(code)
if code in runtime.account_cfg.excluded_codes:
log.info("[Position - ] 代码=%s,名称=%s,止盈=跳过,补仓=跳过,原因=已配置为排除股票", code, position.stock_name)
continue
if (
not code
or position.open_price <= 0
or position.volume <= 0
or tick is None
or tick.last_price <= 0
):
log.warning("[Position - ] 代码=%s,名称=%s,止盈=跳过,补仓=跳过,原因=持仓或行情数据无效", code or "未知", position.stock_name)
continue
try:
code = position.stock_code
tick = ticks.get(code)
if code in runtime.account_cfg.excluded_codes:
log.info("[Position - ] 代码=%s,名称=%s,止盈=跳过,补仓=跳过,原因=已配置为排除股票", code, position.stock_name)
continue
if (
not code
or position.open_price <= 0
or position.volume <= 0
or tick is None
or tick.last_price <= 0
):
log.warning("[Position - ] 代码=%s,名称=%s,止盈=跳过,补仓=跳过,原因=持仓或行情数据无效", code or "未知", position.stock_name)
continue
pnl_rate = round(
(tick.last_price - position.open_price) / position.open_price * 100,
2,
)
minimum_profit = calculate_min_profit_rate(position.open_price, 1)
profit_decision = handle_profit(
runtime=runtime,
position=position,
tick=tick,
pnl_rate=pnl_rate,
minimum_profit=minimum_profit,
)
profit_action = profit_decision.message or "未触发"
loss_add_action = "未启用"
if runtime.account_cfg.enable_loss_add_position and market_ok:
loss_decision = handle_loss(
pnl_rate = round(
(tick.last_price - position.open_price) / position.open_price * 100,
2,
)
minimum_profit = calculate_min_profit_rate(position.open_price, 1)
profit_decision = handle_profit(
runtime=runtime,
position=position,
tick=tick,
pnl_rate=pnl_rate,
available=remaining_cash,
minimum_profit=minimum_profit,
)
remaining_cash -= loss_decision.reserved_cash
loss_add_action = loss_decision.message or "触发"
elif runtime.account_cfg.enable_loss_add_position:
loss_add_action = "大盘信号不允许"
profit_action = profit_decision.message or "未触发"
loss_add_action = "启用"
if runtime.account_cfg.enable_loss_add_position and market_ok:
loss_decision = handle_loss(
runtime=runtime,
position=position,
tick=tick,
pnl_rate=pnl_rate,
available=remaining_cash,
)
remaining_cash -= loss_decision.reserved_cash
loss_add_action = loss_decision.message or "未触发"
elif runtime.account_cfg.enable_loss_add_position:
loss_add_action = "大盘信号不允许"
if pnl_rate>=0:
log.info(
"[Position ↑ ] 代码=%s,名称=%s,盈亏=%.2f%%,止盈=%s,补仓=%s",
code, position.stock_name, pnl_rate, profit_action, loss_add_action,
)
else:
log.info(
"[Position ↓ ] 代码=%s,名称=%s,盈亏=%.2f%%,止盈=%s,补仓=%s",
code, position.stock_name, pnl_rate, profit_action, loss_add_action,
)
if pnl_rate>=0:
log.info(
"[Position ↑ ] 代码=%s,名称=%s,盈亏=%.2f%%,止盈=%s,补仓=%s",
code, position.stock_name, pnl_rate, profit_action, loss_add_action,
)
else:
log.info(
"[Position ↓ ] 代码=%s,名称=%s,盈亏=%.2f%%,止盈=%s,补仓=%s",
code, position.stock_name, pnl_rate, profit_action, loss_add_action,
)
except Exception:
log.exception("[Position] 持仓处理异常,代码=%s,继续处理后续持仓", position.stock_code)
def handle_profit(