动态步长-fix

This commit is contained in:
zhangkun9038@dingtalk.com 2025-11-27 16:26:57 +08:00
parent 6efcf3ea9d
commit a0fb7e949c
2 changed files with 19 additions and 2 deletions

View File

@ -306,8 +306,14 @@ class GridManager:
def _round_to_grid(self, price: float) -> float: def _round_to_grid(self, price: float) -> float:
""" """
将价格舍入到最接近的网格点向下舍入 将价格舍入到最接近的网格点向下舍入
基于 lower_price 的偏移量进行计算
""" """
return int(price / self.step) * self.step # 计算相对于下沿的偏移量
offset = price - self.lower_price
# 舍入到最接近的网格间距
grid_count = int(offset / self.step)
# 返回对应的网格价格
return self.lower_price + grid_count * self.step
def get_summary(self) -> Dict[str, Any]: def get_summary(self) -> Dict[str, Any]:
"""获取当前持仓的完整摘要""" """获取当前持仓的完整摘要"""

View File

@ -4,7 +4,7 @@ from pandas import DataFrame
from typing import Optional, Dict, Any from typing import Optional, Dict, Any
import logging import logging
import sys import sys
from grid_manager import GridManager, OrderFill, AdjustmentType from grid_manager import GridManager, OrderFill, AdjustmentType, GridLevel
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -75,6 +75,17 @@ class StaticGrid(IStrategy):
grid_manager.grid_prices = [new_lower + i * step for i in range(grid_manager.total_grid_levels)] grid_manager.grid_prices = [new_lower + i * step for i in range(grid_manager.total_grid_levels)]
grid_manager.step = step # 更新步长 grid_manager.step = step # 更新步长
# 重新初始化 grid_states应对范围和步长的变化
grid_manager.grid_states.clear()
for price in grid_manager.grid_prices:
grid_manager.grid_states[price] = GridLevel(
price=price,
status="empty",
quantity=0.0,
entry_price=0.0,
entry_time=0
)
print(f"[StaticGrid] {pair} 更新网格范围 - 从 1000-5000 更新为 {new_lower:.2f}-{new_upper:.2f}", print(f"[StaticGrid] {pair} 更新网格范围 - 从 1000-5000 更新为 {new_lower:.2f}-{new_upper:.2f}",
file=sys.stderr, flush=True) file=sys.stderr, flush=True)
print(f"[StaticGrid] {pair} 新网格数: {grid_manager.total_grid_levels}, 步长: {step:.4f}", print(f"[StaticGrid] {pair} 新网格数: {grid_manager.total_grid_levels}, 步长: {step:.4f}",