第24篇:聪明人只止必要的损:一文搞懂Freqtrade动态止损,减少动态亏损在自动交易系统中,止损策略是控制亏损、保护本金的核心机制之一。相比固定止损,Freqtrade提供的custom_stoploss方法支持更加灵活、动态的止损设计,帮助你在盈利中保护利润,在亏损中及时撤退。
在自动交易系统中,止损策略是控制亏损、保护本金的核心机制之一。
相比固定止损,Freqtrade 提供的 custom_stoploss 方法支持更加灵活、动态的止损设计,帮助你在盈利中保护利润,在亏损中及时撤退。
👉 点击访问:https://www.itrade.icu 这里有 Freqtrade 基础教程、策略源码、指标解析 等丰富内容,助你轻松掌握量化交易技巧!
custom_stoploss 允许你根据持仓状态、时间、市场行情等因素,动态计算并返回一个止损价格。
返回值类型:float,表示新的止损价;也可以返回 -1 表示继续使用默认止损机制。
注意:该函数主要用于止损,不支持止盈逻辑。如果你想控制止盈,应该使用
custom_exit。
随着价格上涨,我们动态上移止损位,始终保持 2% 下跌空间。
def custom_stoploss(self, trade, current_time, current_rate, current_profit, **kwargs) -> float:
    """
    动态止损逻辑:
    - 初始止损为 -2%
    - 每上涨 5%,止损上移至当前价下方 2%
    """
    entry_price = trade.open_rate
    if current_profit < 0.05:
        # 未到盈利区,使用初始止损(2%)
        return entry_price * 0.98
    # 一旦盈利超过 5%,止损跟随上涨
    return current_rate * 0.98
✅ 优点:止损位永远跟着当前价格上移,不回撤。
⚠️ 注意:止损只会“提高”,不会下调!
如果你想更精细化管理止损:
def custom_stoploss(self, trade, current_time, current_rate, current_profit, **kwargs) -> float:
    entry_price = trade.open_rate
    if current_profit < 0.05:
        return entry_price * 0.98  # 止损 -2%
    elif current_profit < 0.10:
        return entry_price  # 保本止损
    else:
        return entry_price * 1.05  # 锁定 5% 收益
一些币种容易“冲高回落”洗掉止损。
这时我们可以设置动态耐受浮亏:只有在亏损超过 -3%,才触发止损。
def custom_stoploss(self, trade, current_time, current_rate, current_profit, **kwargs) -> float:
    if current_profit < -0.03:
        return current_rate  # 当前价直接止损
    return -1  # 默认止损
| 项目 | 止盈(custom_exit) | 止损(custom_stoploss) | 
|---|---|---|
| 函数名 | custom_exit | 
custom_stoploss | 
| 是否触发平仓 | ✅ 直接卖出 | ✅ 设置新的止损价格 | 
| 调用频率 | 每个周期评估 | 每个周期评估 | 
| 使用目的 | 获利退出 | 控制亏损,保护本金 | 
| 返回值 | True / False / float | 止损价(float)或 -1 | 
| 搭配机制 | 可以和 custom_exit_price | 
可以和 trailing_stop 配合 | 
如果你已启用:
"trailing_stop": true,  //   启用追踪止损功能,当价格达到一定盈利后,止损价会自动上移,锁定利润。
"trailing_stop_positive": 0.02, //  追踪止损启动的盈利阈值(2%),价格盈利超过此值后,开始跟踪止损。
"trailing_stop_positive_offset": 0.04   //  追踪止损的偏移量(4%),表示止损价将会比当前最高价低4%,防止止损过早触发。
那么建议将 custom_stoploss 只用于前期控制(初始阶段),
后期由 trailing_stop 进行利润保护,两者配合更稳健。
在策略文件中定义函数后,无需额外启用。系统默认会调用。
但建议配合以下设置:
"stoploss": -0.99,  // 足够大,避免默认止损生效
"trailing_stop": false // 如果使用 trailing_stop,请协调两者逻辑
| <font style="color:rgb(102, 102, 102);">阶段</font> | <font style="color:rgb(102, 102, 102);">价格</font> | <font style="color:rgb(102, 102, 102);">止损价</font> | <font style="color:rgb(102, 102, 102);">说明</font> | 
|---|---|---|---|
| <font style="color:rgb(102, 102, 102);">开仓初始</font> | <font style="color:rgb(102, 102, 102);">100</font> | <font style="color:rgb(102, 102, 102);">100 × (1 - 2%) = 98</font> | <font style="color:rgb(102, 102, 102);">初始止损,防止立刻亏损</font> | 
| <font style="color:rgb(102, 102, 102);">第1次上涨</font> | <font style="color:rgb(102, 102, 102);">105</font> | <font style="color:rgb(102, 102, 102);">105 × (1 - 2%) = 102.9</font> | <font style="color:rgb(102, 102, 102);">止损价上移,锁定部分利润</font> | 
| <font style="color:rgb(102, 102, 102);">第2次上涨</font> | <font style="color:rgb(102, 102, 102);">110.25</font> | <font style="color:rgb(102, 102, 102);">110.25 × (1 - 2%) = 107.945</font> | <font style="color:rgb(102, 102, 102);">继续跟踪止损,保护利润更进一步</font> | 
| <font style="color:rgb(102, 102, 102);">第3次上涨</font> | <font style="color:rgb(102, 102, 102);">115.76</font> | <font style="color:rgb(102, 102, 102);">115.76 × (1 - 2%) = 113.45</font> | <font style="color:rgb(102, 102, 102);">持续追踪止损</font> | 
说明:随着价格上涨,止损价自动跟进,防止回落导致利润回吐。
-1 表示放弃修改,系统会用 stoploss 参数定义的默认值。custom_stoploss 是 Freqtrade 中最强大的风险控制工具之一。trailing_stop 和 custom_exit,能构建出完整的资金防御体系。如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!
