只改3处配置, 可自动适配3吗,5m,15m,1h: 改 timeframe 字段(5m → 15m → 1h), 同步 additional_timeframes

This commit is contained in:
phyer 2025-12-30 22:02:20 +08:00
parent d47431803f
commit 3fc7e1ebb0
2 changed files with 39 additions and 13 deletions

View File

@ -78,7 +78,12 @@
"price_side": "other",
"use_order_book": false
},
"fee": 0.0008,
"fee": 0.001,
"internals": {
"process_throttle_secs": 5,
"weight_factor": 20,
"loglevel": "DEBUG"
},
"api_server": {
"enabled": true,
"listen_ip_address": "0.0.0.0",

View File

@ -7,6 +7,7 @@ from pandas import DataFrame
import pandas as pd
import pandas_ta as ta
from freqtrade.persistence import Trade
from freqtrade.exchange import timeframe_to_minutes
import numpy as np
import datetime
import math
@ -27,6 +28,9 @@ class FreqaiPrimer(IStrategy):
stoploss = -0.15 # 固定止损 -15% (大幅放宽止损以承受更大波动)
trailing_stop = True
trailing_stop_positive_offset = 0.005 # 追踪止损偏移量 0.5% (更容易触发跟踪止盈)
ignore_roi_i_entry_signal = True
use_sell_signal = True
# 用于跟踪市场状态的数据框缓存
_dataframe_cache = None
@ -37,6 +41,12 @@ class FreqaiPrimer(IStrategy):
# 存储从配置文件加载的默认值
self._trailing_stop_positive_default = 0.004 # 降低默认值以更容易触发跟踪止盈
# ====== 根据 timeframe 自动计算保护机制参数 ======
# 这样只需要改配置文件中的 timeframe其他参数都会自动适配
tf_minutes = timeframe_to_minutes(self.timeframe)
# 将 5m 作为标准基准,计算相对系数
self._timeframe_divisor = tf_minutes / 5 # 5m时=1, 15m时=3, 1h时=12
# 初始化历史波动系数缓存字典,用于存储每个币对的波动系数历史值
# 格式: {pair: [volatility_coefficient1, volatility_coefficient2, ...]}
self._volatility_history = {}
@ -66,7 +76,7 @@ class FreqaiPrimer(IStrategy):
2. 维护最近200个波动系数的历史序列
3. 计算该序列的EMA20值作为最终波动系数
波动系数表示某币对与BTC/USDT相比的波动幅度倍数
波动系 数表示某币对与BTC/USDT相比的波动幅度倍数
- 山寨币的波动系数可能大于3
- 稳定性较高的币对如DOT/USDT波动系数可能小于1
@ -180,27 +190,38 @@ class FreqaiPrimer(IStrategy):
@property
def protections(self):
"""
保护机制配置
基于最新Freqtrade规范保护机制应定义在策略文件中而非配置文件
保护机制配置 - 自动根据 timeframe 计算
说明:
- 保护时间窗口总是相同的如300分钟但K线数量会根据 timeframe 自动调整
- 只需改配置文件中的 timeframe这里会自动计算对应的 candle 数量
- 基准是 5m其他 timeframe 会成比例调整
"""
# 自动计算各参数,保持时间窗口不变
stoploss_lookback = max(1, round(60 / self._timeframe_divisor)) # 300分钟回看
stoploss_stop = max(1, round(60 / self._timeframe_divisor)) # 300分钟暂停
cooldown_stop = max(1, round(2 / self._timeframe_divisor)) # 10分钟冷却
max_drawdown_lookback = max(1, round(48 / self._timeframe_divisor)) # 240分钟回看
max_drawdown_stop = max(1, round(24 / self._timeframe_divisor)) # 120分钟暂停
return [
{
"method": "StoplossGuard",
"lookback_period_candles": 60, # 3小时回看期60根3分钟K线
"trade_limit": 2, # 最多2笔止损交易
"stop_duration_candles": 60, # 暂停180分钟60根3分钟K线
"only_per_pair": False # 仅针对单个币对
"lookback_period_candles": stoploss_lookback, # 自动调整的回看期
"trade_limit": 2, # 最多2笔止损交易
"stop_duration_candles": stoploss_stop, # 自动调整的暂停时长
"only_per_pair": False
},
{
"method": "CooldownPeriod",
"stop_duration_candles": 2 # 6分钟冷却期2根3分钟K线
"stop_duration_candles": max(1, cooldown_stop) # 自动调整的冷却期
},
{
"method": "MaxDrawdown",
"lookback_period_candles": 48, # 2.4小时回看期
"trade_limit": 4, # 4笔交易限制
"stop_duration_candles": 24, # 72分钟暂停24根3分钟K线
"max_allowed_drawdown": 0.20 # 20%最大回撤容忍度
"lookback_period_candles": max_drawdown_lookback, # 自动调整的回看期
"trade_limit": 4, # 4笔交易限制
"stop_duration_candles": max_drawdown_stop, # 自动调整的暂停时长
"max_allowed_drawdown": 0.20
}
]