综合3m,15m,1h的各个指标的入场出场条件

This commit is contained in:
Ubuntu 2025-08-31 18:13:19 +08:00
parent 434407dbd6
commit 84d47da911
2 changed files with 85 additions and 2 deletions

View File

@ -103,6 +103,17 @@ class FreqaiPrimer(IStrategy):
dataframe['bb_upper_3m'] = bb_3m[f'BBU_{self.bb_length}_{self.bb_std}']
dataframe['rsi_3m'] = ta.rsi(dataframe['close'], length=self.rsi_length)
# 新增 StochRSI 指标
stochrsi_3m = ta.stochrsi(dataframe['close'], length=14, rsi_length=14)
dataframe['stochrsi_k_3m'] = stochrsi_3m['STOCHRSIk_14_14_3_3']
dataframe['stochrsi_d_3m'] = stochrsi_3m['STOCHRSId_14_14_3_3']
# 新增 MACD 指标
macd_3m = ta.macd(dataframe['close'], fast=12, slow=26, signal=9)
dataframe['macd_3m'] = macd_3m['MACD_12_26_9']
dataframe['macd_signal_3m'] = macd_3m['MACDs_12_26_9']
dataframe['macd_hist_3m'] = macd_3m['MACDh_12_26_9']
# 计算3m时间框架的EMA50和EMA200
dataframe['ema_50_3m'] = ta.ema(dataframe['close'], length=50)
dataframe['ema_200_3m'] = ta.ema(dataframe['close'], length=200)
@ -120,6 +131,17 @@ class FreqaiPrimer(IStrategy):
df_15m['ema_50_15m'] = ta.ema(df_15m['close'], length=50)
df_15m['ema_200_15m'] = ta.ema(df_15m['close'], length=200)
# 新增 StochRSI 指标
stochrsi_15m = ta.stochrsi(df_15m['close'], length=14, rsi_length=14)
df_15m['stochrsi_k_15m'] = stochrsi_15m['STOCHRSIk_14_14_3_3']
df_15m['stochrsi_d_15m'] = stochrsi_15m['STOCHRSId_14_14_3_3']
# 新增 MACD 指标
macd_15m = ta.macd(df_15m['close'], fast=12, slow=26, signal=9)
df_15m['macd_15m'] = macd_15m['MACD_12_26_9']
df_15m['macd_signal_15m'] = macd_15m['MACDs_12_26_9']
df_15m['macd_hist_15m'] = macd_15m['MACDh_12_26_9']
# 手动合并 15m 数据
df_15m = df_15m[['date', 'rsi_15m', 'ema_50_15m', 'ema_200_15m']]
df_15m = df_15m.rename(columns={'date': 'date_15m'})
@ -133,6 +155,17 @@ class FreqaiPrimer(IStrategy):
df_1h['ema_200_1h'] = ta.ema(df_1h['close'], length=200) # 1h 200周期EMA
df_1h['trend_1h'] = df_1h['close'] > df_1h['ema_50_1h'] # 1h上涨趋势
# 新增 StochRSI 指标
stochrsi_1h = ta.stochrsi(df_1h['close'], length=14, rsi_length=14)
df_1h['stochrsi_k_1h'] = stochrsi_1h['STOCHRSIk_14_14_3_3']
df_1h['stochrsi_d_1h'] = stochrsi_1h['STOCHRSId_14_14_3_3']
# 新增 MACD 指标
macd_1h = ta.macd(df_1h['close'], fast=12, slow=26, signal=9)
df_1h['macd_1h'] = macd_1h['MACD_12_26_9']
df_1h['macd_signal_1h'] = macd_1h['MACDs_12_26_9']
df_1h['macd_hist_1h'] = macd_1h['MACDh_12_26_9']
# 手动合并 1h 数据
df_1h = df_1h[['date', 'rsi_1h', 'trend_1h', 'ema_50_1h', 'ema_200_1h']]
df_1h = df_1h.rename(columns={'date': 'date_1h'})
@ -195,6 +228,33 @@ class FreqaiPrimer(IStrategy):
return dataframe
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# 入场信号主要依据1小时周期
# 条件1: 价格接近布林带下轨
close_to_bb_lower_1h = (dataframe['close'] <= dataframe['bb_lower_1h'] * 1.02)
# 条件2: RSI 不高于阈值
rsi_condition_1h = dataframe['rsi_1h'] < self.rsi_oversold
# 条件3: StochRSI 处于超卖区域
stochrsi_condition_1h = (dataframe['stochrsi_k_1h'] < 20) & (dataframe['stochrsi_d_1h'] < 20)
# 条件4: MACD 上升趋势
macd_condition_1h = dataframe['macd_1h'] > dataframe['macd_signal_1h']
# 辅助条件: 3m 和 15m 趋势确认
trend_confirmation = (dataframe['trend_3m'] == 1) & (dataframe['trend_15m'] == 1)
# 合并所有条件
final_condition = close_to_bb_lower_1h & rsi_condition_1h & stochrsi_condition_1h & macd_condition_1h & trend_confirmation
# 设置入场信号
dataframe.loc[final_condition, 'enter_long'] = 1
# 日志记录
if dataframe['enter_long'].sum() > 0:
logger.info(f"[{metadata['pair']}] 发现入场信号数量: {dataframe['enter_long'].sum()}")
return dataframe
# 做多条件(放宽以增加入场信号)
# 1. 价格接近3m布林带下轨放宽到5%偏差)
close_to_bb_lower = (dataframe['close'] <= dataframe['bb_lower_3m'] * 1.05)
@ -245,6 +305,30 @@ class FreqaiPrimer(IStrategy):
return dataframe
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# 出场信号基于趋势和量价关系
# 条件1: 价格突破布林带上轨
breakout_condition = dataframe['close'] >= dataframe['bb_upper_1h']
# 条件2: 成交量显著放大
volume_spike = dataframe['volume'] > dataframe['volume_ma'] * 2
# 条件3: MACD 下降趋势
macd_downward = dataframe['macd_1h'] < dataframe['macd_signal_1h']
# 条件4: RSI 进入超买区域
rsi_overbought = dataframe['rsi_1h'] > self.rsi_overbought
# 合并所有条件
final_condition = breakout_condition | volume_spike | macd_downward | rsi_overbought
# 设置出场信号
dataframe.loc[final_condition, 'exit_long'] = 1
# 日志记录
if dataframe['exit_long'].sum() > 0:
logger.info(f"[{metadata['pair']}] 触发出场信号数量: {dataframe['exit_long'].sum()}")
return dataframe
# 缓存数据框以便在trailing_stop_positive属性中使用
self._dataframe_cache = dataframe

View File

@ -190,7 +190,6 @@ echo "docker-compose run --rm freqtrade backtesting $PAIRS_FLAG \
--freqaimodel LightGBMRegressorMultiTarget \
--config /freqtrade/config_examples/$CONFIG_FILE \
--config /freqtrade/templates/freqaiprimer.json \
--enable-protections \
--strategy-path /freqtrade/templates \
--strategy $STRATEGY_NAME \
--timerange $START_DATE-$END_DATE \
@ -203,8 +202,8 @@ docker-compose run --rm freqtrade backtesting $PAIRS_FLAG \
--freqaimodel LightGBMRegressorMultiTarget \
--config /freqtrade/config_examples/$CONFIG_FILE \
--config /freqtrade/templates/freqaiprimer.json \
--enable-protections \
--strategy-path /freqtrade/templates \
--enable-protections \
--strategy $STRATEGY_NAME \
--timerange $START_DATE-$END_DATE \
--fee 0.0008 \