grid管理器更新
This commit is contained in:
parent
e14905b271
commit
30085590a1
@ -65,9 +65,27 @@ class StaticGrid(IStrategy):
|
||||
new_lower = current_price * (1 - percent)
|
||||
new_upper = current_price * (1 + percent)
|
||||
|
||||
# 动态计算步长:范围 / 50
|
||||
# ✅ 处理极端情况:如果上下沿舍入后相同,需要调整
|
||||
# 这可能发生在低价币上(如 0.01 * 1.33 = 0.01330... 舍入到 0.01)
|
||||
range_size = new_upper - new_lower
|
||||
step = range_size / 50
|
||||
if range_size <= 0 or abs(range_size) < 1e-8:
|
||||
# 范围太小,动态扩大:使用固定的小数位精度
|
||||
# 计算 current_price 的小数位数
|
||||
price_str = f"{current_price:.10f}".rstrip('0')
|
||||
decimal_places = len(price_str.split('.')[1]) if '.' in price_str else 0
|
||||
|
||||
# 基于小数位数动态设置步长
|
||||
min_step = 10 ** (-decimal_places - 1) # 比最小单位少一位
|
||||
# 设置范围:上下各 25 步(总共 50 个网格)
|
||||
new_lower = max(min_step, current_price - min_step * 25) # 下方 25 步(不低于最小单位)
|
||||
new_upper = current_price + min_step * 25 # 上方 25 步
|
||||
step = min_step
|
||||
|
||||
print(f"[StaticGrid] {pair} 检测到价格范围过小,动态调整 - 小数位: {decimal_places}, 步长: {step:.10f}, 范围: {new_lower:.10f}-{new_upper:.10f}",
|
||||
file=sys.stderr, flush=True)
|
||||
else:
|
||||
# 动态计算步长:范围 / 50
|
||||
step = range_size / 50
|
||||
|
||||
# 更新 GridManager 的范围
|
||||
grid_manager.lower_price = new_lower
|
||||
@ -175,6 +193,37 @@ class StaticGrid(IStrategy):
|
||||
print(f"[StaticGrid] {pair} 已从 Redis 恢复", file=sys.stderr, flush=True)
|
||||
else:
|
||||
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 连接用于后续同步
|
||||
if self.redis_available:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user