修复sync_from_trade_object覆盖真实加仓记录问题
This commit is contained in:
parent
2cb0c7349e
commit
35e9786752
@ -430,10 +430,13 @@ class GridManager:
|
|||||||
self.total_invested = trade.stake_amount
|
self.total_invested = trade.stake_amount
|
||||||
self.avg_entry_price = trade.open_rate
|
self.avg_entry_price = trade.open_rate
|
||||||
|
|
||||||
# 仅在第一次同步时,根据均价、更新所有网格的状态
|
# 仅在第一次同步且没有任何 FILLED 网格时,根据均价更新所有网格的状态
|
||||||
# 注意:使用 _grid_initialized_from_trade 而不是 _synced_from_trade_once
|
# 注意:使用 _grid_initialized_from_trade 而不是 _synced_from_trade_once
|
||||||
# 因为后者可能被重置但我们不希望此时重新初始化网格
|
# 因为后者可能被重置但我们不希望此时重新初始化网格
|
||||||
if not self._grid_initialized_from_trade and self.total_quantity > 0:
|
filled_count = sum(1 for gs in self.grid_states.values() if gs.status == "filled")
|
||||||
|
|
||||||
|
# ✅ 只有在没有任何 FILLED 网格时才从 Trade 初始化
|
||||||
|
if not self._grid_initialized_from_trade and self.total_quantity > 0 and filled_count == 0:
|
||||||
self._grid_initialized_from_trade = True
|
self._grid_initialized_from_trade = True
|
||||||
|
|
||||||
# 根据 Trade 对象的平均价,更新所有网格的状态
|
# 根据 Trade 对象的平均价,更新所有网格的状态
|
||||||
|
|||||||
@ -194,38 +194,7 @@ class StaticGrid(IStrategy):
|
|||||||
grid_manager.restore_from_redis_state(recovered_state)
|
grid_manager.restore_from_redis_state(recovered_state)
|
||||||
print(f"[StaticGrid] {pair} 已从 Redis 恢复", file=sys.stderr, flush=True)
|
print(f"[StaticGrid] {pair} 已从 Redis 恢复", file=sys.stderr, flush=True)
|
||||||
else:
|
else:
|
||||||
print(f"[StaticGrid] {pair} 新建持仓", file=sys.stderr, flush=True)
|
print(f"[StaticGrid] {pair} 新建持仓 - 网格初始为空,等待加仓逐步填充", file=sys.stderr, flush=True)
|
||||||
|
|
||||||
# ✅ 新建时,自动填充下沿到当前价格之间的一半网格(50%)
|
|
||||||
if current_price > 0:
|
|
||||||
rounded_price = grid_manager._round_to_grid(current_price)
|
|
||||||
filled_count = 0
|
|
||||||
|
|
||||||
# 从下沿开始,填充到当前价格(及以上)的网格
|
|
||||||
for grid_price in grid_manager.grid_prices:
|
|
||||||
if grid_price <= rounded_price:
|
|
||||||
grid_manager.grid_states[grid_price].status = "filled"
|
|
||||||
grid_manager.grid_states[grid_price].quantity = 1.0
|
|
||||||
grid_manager.grid_states[grid_price].entry_price = current_price
|
|
||||||
grid_manager.grid_states[grid_price].entry_time = 0
|
|
||||||
filled_count += 1
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
|
|
||||||
# 更新持仓统计(假设每个网格买 1 个,价格为 current_price)
|
|
||||||
grid_manager.total_quantity = float(filled_count)
|
|
||||||
grid_manager.total_invested = grid_manager.total_quantity * current_price
|
|
||||||
grid_manager.avg_entry_price = current_price
|
|
||||||
|
|
||||||
print(f"[StaticGrid] {pair} 新建网格 - 自动填充下沿到当前价格 - "
|
|
||||||
f"当前价: {current_price:.2f}, 舍入价: {rounded_price:.2f}, "
|
|
||||||
f"填充网格数: {filled_count}/{grid_manager.total_grid_levels}, "
|
|
||||||
f"持仓: {grid_manager.total_quantity:.6f}",
|
|
||||||
file=sys.stderr, flush=True)
|
|
||||||
|
|
||||||
# 同步到 Redis
|
|
||||||
if self.redis_available:
|
|
||||||
grid_manager.sync_grid_state_to_redis()
|
|
||||||
|
|
||||||
# 初始化 Redis 连接用于后续同步
|
# 初始化 Redis 连接用于后续同步
|
||||||
if self.redis_available:
|
if self.redis_available:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user