📘 爆赚利器!三步搞定 Freqtrade 核心买卖信号,手把手教你写自动交易策略!
在 Freqtrade 策略框架中,populate_indicators
、populate_entry_trend
和 populate_exit_trend
是三个最基础、最常用的函数。它们分别负责:
- ✍️ 计算技术指标(如 RSI、MACD、均线等)
- 📈 判断买入时机
- 📉 判断卖出时机
本篇文章将为你详细讲解这三个函数的作用、使用方式、代码示例以及注意事项,帮助你从零搭建自己的量化交易策略。
1️⃣ populate_indicators
: 计算技术指标的核心函数
✅ 功能介绍
这个函数的作用是:对数据帧(DataFrame)计算并填充技术指标,用于后续判断买卖信号。
它的输入是历史K线数据,输出是新增了一些指标列的数据帧。通常你会在这里引入如 RSI、MACD、SMA、EMA 等。
🧠 回测 vs 实盘
- 回测模式:
populate_indicators
会对整个历史数据调用一次。 - 实盘模式:每生成一个新K线(如每分钟、每小时)就会调用一次。
因此在实盘时,你可以在该函数中加入实时判断与处理逻辑,而在回测中需要用 for row in dataframe.iterrows()
遍历历史数据。
💡 示例代码
python
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# 计算14日 RSI 指标
dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
# 如果你想在回测时处理每根K线
for index, row in dataframe.iterrows():
if row['rsi'] < 30:
# 记录日志、打标签等操作
pass
return dataframe
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
2️⃣ populate_entry_trend
: 买入信号逻辑
✅ 功能介绍
populate_entry_trend
是定义何时买入(开仓)的地方。它会在数据帧中生成一个 buy
列,Freqtrade 会检查这一列的值:
buy = 1
表示当前K线应买入buy = 0
(默认)表示不操作
Freqtrade 会在满足该条件的时刻,用市价单或限价单发起交易。
🛠 技术用法
你可以使用 .loc
对 dataframe 做布尔筛选并设置买入信号:
python
dataframe.loc[条件表达式, 'buy'] = 1
1
💡 示例代码
python
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe['buy'] = 0 # 初始化所有K线为不买入
# RSI 小于 30 时,设置为买入信号
dataframe.loc[dataframe['rsi'] < 30, 'buy'] = 1
return dataframe
1
2
3
4
5
6
7
2
3
4
5
6
7
📌 实用建议
- 多重条件组合:可使用
(cond1) & (cond2)
等方式组合多个技术指标。 - 也可以使用布林带、MA 等其他技术指标来增强买点判断。
3️⃣ populate_exit_trend
: 卖出信号逻辑
✅ 功能介绍
populate_exit_trend
是你定义何时平仓(卖出)的函数,操作逻辑与买入类似,只是设置的是 sell
列:
sell = 1
表示该K线触发平仓sell = 0
表示不卖出
Freqtrade 会在检测到 sell=1
时,按设置发出卖出指令。
💡 示例代码
python
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe['sell'] = 0 # 初始化为不卖出
# RSI 高于 70 时设置为卖出信号
dataframe.loc[dataframe['rsi'] > 70, 'sell'] = 1
return dataframe
1
2
3
4
5
6
7
2
3
4
5
6
7
🧠 小技巧
- 你也可以加入成交量、价格形态等逻辑来提高卖点准确性。
- 配合策略中的 ROI 或 trailing stop 可形成更完整的出场机制。
🧩 实战案例:最小可运行策略 - RSI 策略
以下是一个完整的策略类,结合上述三个函数。策略逻辑如下:
- 当 RSI < 30:认为市场超卖,发出买入信号
- 当 RSI > 70:认为市场超买,发出卖出信号
python
from freqtrade.strategy import IStrategy
from pandas import DataFrame
import talib.abstract as ta
class RsiExampleStrategy(IStrategy):
# 使用的K线周期
timeframe = '5m'
# 每次下单金额(可覆盖配置文件设置)
stake_amount = 100
# 最小盈利目标和止损
minimal_roi = {"0": 0.1}
stoploss = -0.2
# 使用自定义的买入/卖出信号
use_exit_signal = True
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# 计算14日 RSI 指标
dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
return dataframe
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe['buy'] = 0
# RSI 小于30,表示市场超卖,买入信号
dataframe.loc[
(dataframe['rsi'] < 30),
'buy'
] = 1
return dataframe
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe['sell'] = 0
# RSI 大于70,表示市场超买,卖出信号
dataframe.loc[
(dataframe['rsi'] > 70),
'sell'
] = 1
return dataframe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
🧾 总结
函数名 | 作用 | 返回值 |
---|---|---|
populate_indicators | 计算技术指标 | 含有新指标的数据帧 |
populate_entry_trend | 设置买入信号 | 含有 buy=1 的数据帧 |
populate_exit_trend | 设置卖出信号 | 含有 sell=1 的数据帧 |
这些函数是策略运行的“三驾马车”,你可以在此基础上不断优化与叠加逻辑,构建自己的交易系统。
下一篇,我们将进入进阶主题,讲解 custom_exit
和 custom_exit_price
的区别与配合使用,欢迎关注系列更新。
如果你喜欢这类内容,欢迎点赞、转发、收藏!⚡️