自定义加仓逻辑

This commit is contained in:
zhangkun9038@dingtalk.com 2025-08-30 14:07:33 +08:00
parent eb426096e3
commit bcf65f9f9d

View File

@ -12,17 +12,17 @@ import datetime
logger = logging.getLogger(__name__)
class FreqaiPrimer(IStrategy):
# 策略参数
# 策略参数 - 调整以提高单笔盈利潜力
minimal_roi = {
"0": 0.03, # 3% ROI (10 分钟内)
"60": 0.015, # 1.5% ROI (1 小时)
"180": 0.005, # 0.5% ROI (3 小时)
"360": 0.0 # 0% ROI (6 小时)
"0": 0.05, # 5% ROI (10 分钟内)
"60": 0.03, # 3% ROI (1 小时)
"180": 0.01, # 1% ROI (3 小时)
"360": 0.005 # 0.5% ROI (6 小时)
}
stoploss = -0.01 # 固定止损 -1%
stoploss = -0.15 # 固定止损 -15% (大幅放宽止损以承受更大波动)
trailing_stop = True
trailing_stop_positive_offset = 0.008 # 追踪止损偏移量 0.8%
trailing_stop_positive_offset = 0.005 # 追踪止损偏移量 0.5% (更容易触发跟踪止盈)
# 用于跟踪市场状态的数据框缓存
_dataframe_cache = None
@ -31,7 +31,7 @@ class FreqaiPrimer(IStrategy):
"""初始化策略参数调用父类初始化方法并接受config参数"""
super().__init__(config) # 调用父类的初始化方法并传递config
# 存储从配置文件加载的默认值
self._trailing_stop_positive_default = 0.005
self._trailing_stop_positive_default = 0.004 # 降低默认值以更容易触发跟踪止盈
@property
def trailing_stop_positive(self):
@ -40,9 +40,9 @@ class FreqaiPrimer(IStrategy):
if self._dataframe_cache is not None and len(self._dataframe_cache) > 0:
current_state = self._dataframe_cache['market_state'].iloc[-1]
if current_state == 'strong_bull':
return 0.01 # 强劲牛市中放宽跟踪止盈
return 0.007 # 强劲牛市中降低跟踪止盈,让利润奔跑
elif current_state == 'weak_bull':
return 0.007
return 0.005 # 弱势牛市中保持较低的跟踪止盈
return self._trailing_stop_positive_default # 返回默认值
@trailing_stop_positive.setter
@ -279,15 +279,15 @@ class FreqaiPrimer(IStrategy):
# 趋势反转信号15m RSI超买
trend_reversal = merged_data['rsi_15m'] > 75
# 动态调整退出条件
# 强劲趋势中:只有获利达到3倍ATR才退出或出现明确的趋势反转信号
dataframe.loc[strong_trend & ((dataframe['close'] > dataframe['open'] + dataframe['atr'] * 3) | (basic_exit & trend_reversal)), 'exit_long'] = 1
# 动态调整退出条件 - 提高ATR倍数以增加单笔盈利潜力
# 强劲趋势中:提高到4.5倍ATR才退出给利润更多增长空间
dataframe.loc[strong_trend & ((dataframe['close'] > dataframe['open'] + dataframe['atr'] * 4.5) | (basic_exit & trend_reversal)), 'exit_long'] = 1
# 一般趋势中:保持原有2倍ATR退出
dataframe.loc[normal_trend & ((dataframe['close'] > dataframe['open'] + dataframe['atr'] * 2) | basic_exit), 'exit_long'] = 1
# 一般趋势中:提高到3倍ATR退出
dataframe.loc[normal_trend & ((dataframe['close'] > dataframe['open'] + dataframe['atr'] * 3) | basic_exit), 'exit_long'] = 1
# 非趋势或弱势:使用基础条件+1.5倍ATR
dataframe.loc[~strong_trend & ~normal_trend & (basic_exit | (dataframe['close'] > dataframe['open'] + dataframe['atr'] * 1.5)), 'exit_long'] = 1
# 非趋势或弱势:提高到2倍ATR
dataframe.loc[~strong_trend & ~normal_trend & (basic_exit | (dataframe['close'] > dataframe['open'] + dataframe['atr'] * 2)), 'exit_long'] = 1
# 记录退出决策日志
if len(dataframe[dataframe['exit_long'] == 1]) > 0:
@ -413,3 +413,57 @@ class FreqaiPrimer(IStrategy):
# 返回应退出的比例0.0表示不退出1.0表示全部退出)
return exit_ratio
def adjust_trade_position(self, trade: 'Trade', current_time, current_rate: float,
current_profit: float, min_stake: float, max_stake: float, **kwargs) -> float:
"""
根据用户要求实现加仓逻辑
- 加仓间隔设置为0.0474.7%回调
- 加仓额度为: (stake_amount / 2) ^ (加仓次数 - 1)
"""
# 检查是否已启用加仓
if not hasattr(self, 'max_entry_adjustments'):
self.max_entry_adjustments = 3 # 设置最大加仓次数
# 获取当前交易对
pair = trade.pair
# 获取当前交易的加仓次数
# 初始交易算第1次加仓次数=total_entry_position - 1
entry_count = len(trade.orders) # 获取所有入场订单数量
# 如果已经达到最大加仓次数,则不再加仓
if entry_count - 1 >= self.max_entry_adjustments:
return 0.0
# 获取初始入场价格和当前价格的差值百分比
initial_price = trade.open_rate
price_diff_pct = (current_rate - initial_price) / initial_price
# 检查价格回调是否达到加仓间隔0.047
# 价格回调表示价格比初始价格低4.7%或更多
if price_diff_pct <= -0.047:
# 计算初始入场金额
initial_stake = trade.orders[0].cost # 第一笔订单的成本
# 计算加仓次数从1开始计数
adjustment_count = entry_count - 1 # 已加仓次数
# 计算加仓额度: (stake_amount / 2) ^ (加仓次数)
# 对于第一次加仓,公式为 (initial_stake / 2) ^ 1 = initial_stake / 2
# 第二次加仓,公式为 (initial_stake / 2) ^ 2
# 第三次加仓,公式为 (initial_stake / 2) ^ 3
# 计算加仓金额
additional_stake = (initial_stake / 2) ** (adjustment_count + 1)
# 确保加仓金额在允许的范围内
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