Freqtrade Backtester
Run backtests on your trading strategies, interpret results, and iterate safely before going live.
What Backtesting Does
Backtesting runs your strategy against historical market data to see how it would have performed. It shows you win rates, losses, drawdowns, and exit reasons — everything you need to decide if a strategy is worth trading with real money.
Download Data
Historical data is cached, so download once and reuse it for many backtests.
⚠️ Download one pair at a time — passing multiple pairs to --pairs can cause errors. Run the command separately for each pair:
CODEBLOCK0
Replace the pair, timeframe, and timerange as needed. The --timerange format is YYYYMMDD-YYYYMMDD; omit the end date to download through today.
Run a Backtest
CODEBLOCK1
Replace YourStrategy with your actual strategy class name. The --export trades flag creates a JSON file with detailed trade logs.
Key Metrics
- - Win rate — Percentage of winning trades. >55% is decent; >60% is strong.
- Max drawdown — Largest peak-to-trough decline. Keep it <20%; above 25% indicates excessive risk.
- Sharpe ratio — Risk-adjusted return. >1.0 is acceptable; >2.0 is excellent.
- Avg profit per trade — Average win/loss size. Wins should be larger than losses.
- Exit reasons — Breakdown of why trades closed (ROI, stop-loss, trailing stop, etc.).
The Key Insight
High win rate ≠ profitability. A 70% win rate with 5% average loss per trade loses money. A 40% win rate with 2% average wins and 1% average loss is profitable. Control your losses; the wins take care of themselves.
Example: A strategy with tight stops (-3%) outperforms one with loose stops (-7% or -8%), even if the loose version has more winners.
Iteration Pattern
- 1. Run a backtest on a fixed time period (e.g., 6 months of data).
- Change one parameter (e.g., RSI threshold, stop-loss percentage).
- Backtest the same period again.
- Compare results — did it improve?
- If yes, keep it; if no, revert.
- Repeat until satisfied.
Always test across multiple market conditions (bull runs, bear markets, sideways consolidation) before going live. A strategy that works in one environment often fails in another.
Environment Variables
Freqtrade reads secrets from environment variables at runtime. Use the double-underscore format:
CODEBLOCK2
In your config.json, set these fields to empty strings:
CODEBLOCK3
Freqtrade will populate them from the environment at startup.
References
- - Reading Results — INLINECODE6
- Iteration Guide — INLINECODE7
Freqtrade 回测工具
在实盘交易前,对交易策略进行回测、解读结果并安全迭代。
回测的作用
回测利用历史市场数据运行你的策略,观察其历史表现。它能展示胜率、亏损、回撤幅度和退出原因——所有判断策略是否值得用真金白银交易所需的信息。
下载数据
历史数据会被缓存,因此只需下载一次即可重复用于多次回测。
⚠️ 每次只下载一个交易对——向 --pairs 参数传递多个交易对可能导致错误。请为每个交易对单独运行命令:
bash
docker-compose run --rm freqtrade download-data \
--exchange kraken \
--pairs BTC/USDT \
--timeframe 5m \
--timerange 20240101-
根据需要替换交易对、时间周期和时间范围。--timerange 的格式为 YYYYMMDD-YYYYMMDD;省略结束日期将下载至今的数据。
运行回测
bash
docker-compose run --rm freqtrade backtesting \
--strategy YourStrategy \
--timerange 20240101-20260101 \
--export trades \
--export-filename userdata/backtestresults/my_backtest.json
将 YourStrategy 替换为你的实际策略类名。--export trades 标志会创建一个包含详细交易日志的 JSON 文件。
关键指标
- - 胜率——盈利交易占比。>55% 算不错;>60% 为强劲。
- 最大回撤——从峰值到谷值的最大跌幅。保持在 <20%;超过 25% 表示风险过高。
- 夏普比率——风险调整后收益。>1.0 可接受;>2.0 为优秀。
- 每笔交易平均利润——平均盈亏规模。盈利应大于亏损。
- 退出原因——交易平仓原因分析(ROI、止损、移动止损等)。
核心洞察
高胜率 ≠ 盈利能力。 70% 胜率但每笔交易平均亏损 5% 的策略会亏钱。40% 胜率但平均盈利 2%、平均亏损 1% 的策略却能盈利。控制亏损,盈利自然而来。
示例:采用紧止损(-3%)的策略优于松止损(-7% 或 -8%)的策略,即使后者有更多盈利交易。
迭代模式
- 1. 在固定时间段(例如 6 个月数据)上运行回测。
- 更改一个参数(例如 RSI 阈值、止损百分比)。
- 在同一时间段再次回测。
- 比较结果——是否有改善?
- 如果有,保留;如果没有,恢复原状。
- 重复直到满意为止。
在实盘前,务必在多种市场环境(牛市、熊市、横盘整理)下进行测试。在一种环境中有效的策略,在另一种环境中往往失效。
环境变量
Freqtrade 在运行时从环境变量读取密钥。使用双下划线格式:
bash
export FREQTRADEEXCHANGEKEY=your-api-key
export FREQTRADEEXCHANGESECRET=your-api-secret
在你的 config.json 中,将这些字段设置为空字符串:
json
{
exchange: {
key: ,
secret:
}
}
Freqtrade 会在启动时从环境中填充这些值。
参考文档
- - 解读结果——references/reading-results.md
- 迭代指南——references/iteration-guide.md