adjust_trade_position 优化

This commit is contained in:
zhangkun9038@dingtalk.com 2025-10-17 05:49:34 +00:00
parent 66c156bbd5
commit fb720324ac

View File

@ -572,13 +572,17 @@ class FreqaiPrimer(IStrategy):
# -------------------------- 减仓逻辑 --------------------------
if current_profit > 0:
# 盈利超8.5%退出50%仓位(仅触发一次)
if current_profit >= 0.085 and trade.nr_of_successful_exits == 0:
if current_profit >= 0.095 and trade.nr_of_successful_exits == 0:
reduce_amount = - (trade.stake_amount * 0.5)
reduce_amount = max(-trade.stake_amount, min(reduce_amount, -min_stake))
logger.info(f"[{pair}] 触发减仓: 盈利{current_profit:.2%}≥8.5%退出50%仓位,减仓金额{abs(reduce_amount):.2f}")
return reduce_amount
# 盈利超8.5%退出50%仓位(仅触发一次)
elif current_profit >= 0.085 and trade.nr_of_successful_exits == 0:
reduce_amount = - (trade.stake_amount * 0.33)
reduce_amount = max(-trade.stake_amount, min(reduce_amount, -min_stake))
logger.info(f"[{pair}] 触发减仓: 盈利{current_profit:.2%}≥8.5%退出50%仓位,减仓金额{abs(reduce_amount):.2f}")
return reduce_amount
# 盈利超7.5%但未达8.5%退出25%仓位(仅触发一次)
elif 0.075 <= current_profit < 0.085 and trade.nr_of_successful_exits == 0:
reduce_amount = - (trade.stake_amount * 0.25)
@ -586,6 +590,7 @@ class FreqaiPrimer(IStrategy):
logger.info(f"[{pair}] 触发减仓: 盈利{current_profit:.2%}≥7.5%退出25%仓位,减仓金额{abs(reduce_amount):.2f}")
return reduce_amount
# -------------------------- 加仓逻辑 --------------------------
# 获取当前交易的加仓次数
entry_count = len(trade.orders) # 获取所有入场订单数量
@ -594,34 +599,23 @@ class FreqaiPrimer(IStrategy):
if entry_count - 1 >= self.max_entry_adjustments.value:
return 0.0
# 获取初始入场价格和当前价格的差值百分比
initial_price = trade.open_rate
if initial_price == 0:
return 0.0
if current_profit > -self.add_position_callback.value:
return 0.0
# 获取初始入场价格和当前价格的差值百分比: 当前阶段跌幅
price_diff_pct = (current_rate - initial_price) / initial_price
# 检查价格回调是否达到加仓间隔
dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
#current_state = dataframe['market_state'].iloc[-1] if 'market_state' in dataframe.columns else 'neutral'
# 相比上次加仓/入场价格, 当前币对 价格降幅 低于 阈值
# 检查价格回调是否达到加仓间隔, 相比上次加仓/入场价格, 当前币对 价格降幅 低于 阈值
if price_diff_pct <= -self.add_position_callback.value:
# 计算初始入场金额
initial_stake = trade.orders[0].cost # 第一笔订单的成本
# 计算加仓金额: (initial_stake / stake_divisor) ^ (adjustment_count + 1)
# 计算加仓金额: ( 步进系数 * 初始入场金额 / 加仓份额 ) ^ 已经加仓次数
additional_stake = (self.step_coefficient.value * initial_stake / self.stake_divisor.value) ** (entry_count)
# 确保加仓金额在允许的范围内
additional_stake = max(min_stake, min(additional_stake, max_stake - trade.stake_amount))
#logger.info(f"[{pair}] 触发加仓: 第{adjustment_count + 1}次加仓, 初始金额{initial_stake:.2f}, \
# 加仓金额{additional_stake:.2f}, 价格差{price_diff_pct:.2%}, 当前利润{current_profit:.2%}")
return additional_stake
# 不符合加仓条件返回0
return 0.0