修复网格加仓无限循环问题 - decide_adjustment返回网格价格而非当前价格

This commit is contained in:
zhangkun9038@dingtalk.com 2025-11-27 18:05:10 +08:00
parent 30085590a1
commit 2cb0c7349e
2 changed files with 8 additions and 6 deletions

View File

@ -179,12 +179,12 @@ class GridManager:
Args:
adjustment: PositionRecord 对象包含加减仓的所有信息
"""
price = adjustment.price
price = adjustment.price # ✅ 现在 price 已经是网格价格,不需要舍入
quantity = adjustment.quantity
adj_type = adjustment.type
# 找到该操作所属的网格点
grid_price = self._round_to_grid(price)
# ✅ 直接使用 price 作为网格价格
grid_price = price
grid_state = self.grid_states.get(grid_price)
if adj_type == AdjustmentType.ENTRY or adj_type == AdjustmentType.ADD:
@ -303,10 +303,10 @@ class GridManager:
f"下方网格 {grid_price:.2f} 为空需填满",
file=sys.stderr, flush=True)
# 加仓该网格
# 加仓该网格 - 关键修复price 应该是网格价格,而不是当前价
return PositionRecord(
level_index=self._price_to_level_index(grid_price),
price=self.current_price,
price=grid_price, # ✅ 修复:使用网格价格而不是当前价格
quantity=1.0,
type=AdjustmentType.ADD if self.total_quantity > 0 else AdjustmentType.ENTRY,
timestamp=self.candle_index

View File

@ -55,7 +55,9 @@ class StaticGrid(IStrategy):
# 动态更新网格范围(仅在第一次初始化时)
# 检查是否需要从占位符更新为实际价格
has_placeholder = grid_manager.lower_price <= 1000 and grid_manager.upper_price <= 5000
# ✅ 修复只有在范围恰好是占位符1000-5000时才更新
has_placeholder = (abs(grid_manager.lower_price - 1000) < 0.1 and
abs(grid_manager.upper_price - 5000) < 0.1)
if has_placeholder:
# 第一次初始化,从占位符更新为实际价格