Freqtrade Strategy Development
Build profitable trading strategies with disciplined iteration, tight risk management, and data-driven entry/exit rules. Assumes Freqtrade is running via Docker (docker-compose).
Strategy Anatomy
Every Freqtrade strategy requires three methods:
- -
populate_indicators(dataframe, metadata) — Add technical indicators (RSI, MACD, Bollinger Bands, etc.) to the dataframe populate_entry_trend(dataframe, metadata) — Define buy signal logic; set enter_long = 1 when conditions metpopulate_exit_trend(dataframe, metadata) — Define sell signal logic; set exit_long = 1 when conditions met (optional if using ROI/stop-loss)
Key Config Parameters
CODEBLOCK0
Proven Entry Pattern
CODEBLOCK1
Key Lessons Learned
- 1. Tight stops save accounts — 3% max loss beats 5%, 7%, or 8% every time
- Quality over quantity — 25 selective trades outperform 308 mediocre ones
- Win rate alone is meaningless — 63% win rate unprofitable if avg loss is 5x avg gain
- Selectivity is survival — RSI(30) + CCI(-100) dual filters dramatically reduce noise
- Test in bear markets — If strategy survives a crash, it works everywhere
- Volume confirms conviction — Entries without above-average volume fail more often
Useful Indicators
- - RSI (14) — Momentum; < 30 = oversold, > 70 = overbought
- CCI — Commodity Channel Index; momentum confirmation; < -100 = deep oversold
- MACD — Trend following; watch for crossovers
- Bollinger Bands — Volatility; price near lower band = potential reversal
- EMA — Trend filter; price above EMA = uptrend
- MFI — Money Flow Index; volume-weighted momentum
Iteration Workflow
- 1. Write baseline strategy with core entry/exit logic
- Backtest on 90–120 days of historical data
- Analyze exit reasons: are you exiting winners or losers too fast?
- Tighten ONE parameter at a time (e.g., RSI threshold)
- Backtest same period, compare vs. baseline
- If better → keep; if worse → revert
- Test different market conditions (Bull, bear, sideways)
- Dry-run on live feeds before deploying to live trading
Version Control
Keep all versions: name files MyStrategy_v1.py, MyStrategy_v2.py, etc. Add comments above each change explaining what improved and why. This preserves your iteration history and makes reverting safe.
References
- -
references/indicators-guide.md — Technical indicator formulas and interpretation references/iteration-workflow.md — Step-by-step walkthrough of strategy optimization
Freqtrade 策略开发
通过纪律严明的迭代、严格的风险管理和数据驱动的入场/出场规则,构建盈利的交易策略。假设 Freqtrade 通过 Docker(docker-compose)运行。
策略结构
每个 Freqtrade 策略都需要三个方法:
- - populateindicators(dataframe, metadata) — 向数据框中添加技术指标(RSI、MACD、布林带等)
- populateentrytrend(dataframe, metadata) — 定义买入信号逻辑;当条件满足时设置 enterlong = 1
- populateexittrend(dataframe, metadata) — 定义卖出信号逻辑;当条件满足时设置 exit_long = 1(如果使用 ROI/止损,则为可选)
关键配置参数
python
stoploss = -0.03 # 每笔交易最大亏损 3%
trailing_stop = True
trailingstoppositive = 0.01
trailingstoppositive_offset = 0.02
minimal_roi = {
0: 0.04, # 立即获利 4%
30: 0.02, # 30 根 K 线后获利 2%
60: 0.01, # 60 根 K 线后获利 1%
}
timeframe = 5m # 或 15m、1h 等
stake_currency = USDT
dry_run = True # 始终先进行回测/模拟运行
经过验证的入场模式
python
stoploss = -0.03
trailing_stop = True
trailingstoppositive = 0.01
trailingstoppositive_offset = 0.02
minimal_roi = {0: 0.04, 30: 0.02, 60: 0.01}
在 populate_indicators 中:计算 RSI、CCI、布林带、EMA、成交量 SMA
在 populateentrytrend 中:仅当所有条件满足时买入
conditions = [
(dataframe[rsi] < 30), # 超卖
(dataframe[cci] < -100), # 动量确认
(dataframe[close] < dataframe[bb_lowerband]), # 价格接近下轨
(dataframe[volume] > dataframe[volume_sma]), # 成交量确认
(dataframe[bullish_candle]), # 形态确认
]
dataframe.loc[reduce(lambda x, y: x & y, conditions), enter_long] = 1
关键经验教训
- 1. 严格止损保护账户 — 3% 的最大亏损始终优于 5%、7% 或 8%
- 质量胜于数量 — 25 次精选交易优于 308 次平庸交易
- 胜率本身毫无意义 — 如果平均亏损是平均盈利的 5 倍,63% 的胜率也无法盈利
- 选择性是生存之道 — RSI(30) + CCI(-100) 双重过滤器大幅减少噪音
- 在熊市中测试 — 如果策略能经受住崩盘,它在任何地方都有效
- 成交量确认信念 — 没有高于平均成交量的入场更容易失败
实用指标
- - RSI(14) — 动量指标;< 30 = 超卖,> 70 = 超买
- CCI — 商品通道指数;动量确认;< -100 = 深度超卖
- MACD — 趋势跟踪;关注交叉信号
- 布林带 — 波动率指标;价格接近下轨 = 潜在反转
- EMA — 趋势过滤器;价格高于 EMA = 上升趋势
- MFI — 资金流量指数;成交量加权动量
迭代工作流程
- 1. 编写包含核心入场/出场逻辑的基线策略
- 对 90-120 天的历史数据进行回测
- 分析出场原因:你是过早卖出盈利单还是亏损单?
- 每次收紧一个参数(例如,RSI 阈值)
- 对同一时期进行回测,与基线对比
- 如果更好 → 保留;如果更差 → 恢复
- 测试不同的市场条件(牛市、熊市、震荡市)
- 在部署到实盘交易前,先在实时行情上进行模拟运行
版本控制
保留所有版本:将文件命名为 MyStrategyv1.py、MyStrategyv2.py 等。在每个更改上方添加注释,说明改进了什么以及原因。这样可以保留你的迭代历史,并使回滚变得安全。
参考资料
- - references/indicators-guide.md — 技术指标公式和解读
- references/iteration-workflow.md — 策略优化的逐步指南