65 lines
3.7 KiB
Diff
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

--- original_strategy.py
+++ modified_strategy.py
@@ -297,6 +297,9 @@ class FreqaiPrimer(IStrategy):
dataframe["bb_lowerband"] = lowerband
dataframe["rsi"] = ta.RSI(dataframe, timeperiod=14)
dataframe["volume_mean_20"] = dataframe["volume"].rolling(20).mean()
dataframe["volume_std_20"] = dataframe["volume"].rolling(20).std()
dataframe["volume_z_score"] = (dataframe["volume"] - dataframe["volume_mean_20"]) / dataframe["volume_std_20"]
+ # 计算15分钟ATR和3分钟EMA10、EMA20
+ dataframe_15m = self.dp.get_pair_dataframe(pair=pair, timeframe="15m")
+ cuyuan
+ dataframe_15m['atr15m'] = ta.ATR(dataframe_15m, timeperiod=14)
+ dataframe['atr15m'] = dataframe_15m['atr15m'].reindex(dataframe.index, method="ffill")
+ dataframe['ema10'] = ta.EMA(dataframe, timeperiod=10)
+ dataframe['ema20'] = ta.EMA(dataframe, timeperiod=20)
@@ -390,8 +390,11 @@ class FreqaiPrimer(IStrategy):
conditions = []
if "&-price_value_divergence" in dataframe.columns:
cond1 = (dataframe["&-price_value_divergence"] > self.sell_threshold * 1.025)
- cond2 = (dataframe["rsi"] > 75)
+ rsi_threshold = 70 if trend_score > 50 else 65 # 动态RSI阈值
+ cond2 = (dataframe["rsi"] > rsi_threshold)
+ cond3 = (dataframe['ema10'] < dataframe['ema20']) # 短期均线交叉
sell_condition = cond1 | cond2
conditions.append(sell_condition)
@@ -442,6 +442,13 @@ class FreqaiPrimer(IStrategy):
initial_stake_amount = trade.stake_amount / 3
logger.debug(f"{pair} 首次入场金额: {initial_stake_amount:.2f}, 当前持仓金额: {trade.stake_amount:.2f}, "
f"加仓次数: {trade.nr_of_successful_entries - 1}")
+ # 分批出场:短期涨幅触发
+ short_term_return = (current_rate - dataframe['close'].shift(3).iloc[-1]) / dataframe['close'].shift(3).iloc[-1]
+ if short_term_return >= 0.03: # 3%涨幅
+ reduce_amount = -0.5 * trade.stake_amount # 卖出50%仓位
+ logger.info(f"{pair} 短期涨幅 {short_term_return*100:.2f}%卖出50%仓位 {abs(reduce_amount):.2f}")
+ return (reduce_amount, f"Short-term gain {short_term_return*100:.2f}%")
+
# 加仓逻辑
max_entry_adjustments = self.MAX_ENTRY_POSITION_ADJUSTMENT.value
@@ -484,12 +484,11 @@ class FreqaiPrimer(IStrategy):
# 追踪止损逻辑
trailing_stop_start = self.TRAILING_STOP_START.value
trailing_stop_distance = self.TRAILING_STOP_DISTANCE.value
- # 使用 Sigmoid 映射调整追踪止损参数
- sigmoid = 1 / (1 + np.exp(-0.1 * (trend_score - 50)))
- trailing_factor = 0.8 + (1.2 - 0.8) * sigmoid # 牛市(100) -> 1.2, 熊市(0) -> 0.8
- distance_factor = 0.7 + (1.5 - 0.7) * sigmoid # 牛市(100) -> 1.5, 熊市(0) -> 0.7
- trailing_stop_start *= trailing_factor
- trailing_stop_distance *= distance_factor
+ # 使用15分钟ATR动态调整止损距离
+ atr15m = dataframe['atr15m'].iloc[-1]
+ trailing_stop_distance = min(max(2 * atr15m / current_rate, 0.01), 0.05) # 2倍ATR限制在0.01-0.05
+ if trend_score > 50: # 强趋势时放宽止损
+ trailing_stop_distance *= 1.2
+ trailing_stop_price = max_rate * (1 - trailing_stop_distance)
if profit_ratio >= trailing_stop_start and not self.trailing_stop_enabled:
self.trailing_stop_enabled = True
trade.adjust_min_max_rates(current_rate, current_rate)
logger.info(f"{pair} 价格上涨超过 {trailing_stop_start*100:.1f}%,启动追踪止损")
return None