数据下载¶
获取用于回测和超参数优化的数据¶
要下载回测和超参数优化所需的数据(蜡烛图 / OHLCV),请使用 freqtrade download-data 命令。
如果不指定额外参数,freqtrade 将为最近 30 天下载 "1m" 和 "5m" 时间周期的数据。交易所和交易对将来自 config.json(如果通过 -c/--config 指定)。如果没有提供配置文件,则必须使用 --exchange 参数。
你可以使用相对时间范围(--days 20)或绝对起始点(--timerange 20200101-)。对于增量式下载,建议使用相对方式。
提示:更新现有数据
如果你的数据目录中已有回测数据,并希望将其更新至今天,freqtrade 将自动计算已有交易对缺失的时间段,并从最新数据点下载至当前时间,此时无需指定 --days 或 --timerange 参数。freqtrade 会保留已有数据,仅下载缺失部分。
如果你在添加了尚无数据的新交易对后更新数据,请使用 --new-pairs-days xx 参数。该参数指定的天数将用于下载新交易对的数据,而旧交易对仅补充缺失数据。
使用方法¶
usage: freqtrade download-data [-h] [-v] [--no-color] [--logfile FILE] [-V]
[-c PATH] [-d PATH] [--userdir PATH]
[-p PAIRS [PAIRS ...]] [--pairs-file FILE]
[--days INT] [--new-pairs-days INT]
[--include-inactive-pairs]
[--no-parallel-download]
[--timerange TIMERANGE] [--dl-trades]
[--convert] [--exchange EXCHANGE]
[-t TIMEFRAMES [TIMEFRAMES ...]] [--erase]
[--data-format-ohlcv {json,jsongz,feather,parquet}]
[--data-format-trades {json,jsongz,feather,parquet}]
[--trading-mode {spot,margin,futures}]
[--prepend]
options:
-h, --help show this help message and exit
-p PAIRS [PAIRS ...], --pairs PAIRS [PAIRS ...]
Limit command to these pairs. Pairs are space-
separated.
--pairs-file FILE File containing a list of pairs. Takes precedence over
--pairs or pairs configured in the configuration.
--days INT Download data for given number of days.
--new-pairs-days INT Download data of new pairs for given number of days.
Default: `None`.
--include-inactive-pairs
Also download data from inactive pairs.
--no-parallel-download
Disable parallel startup download. Only use this if
you experience issues.
--timerange TIMERANGE
Specify what timerange of data to use.
--dl-trades Download trades instead of OHLCV data.
--convert Convert downloaded trades to OHLCV data. Only
applicable in combination with `--dl-trades`. Will be
automatic for exchanges which don't have historic
OHLCV (e.g. Kraken). If not provided, use `trades-to-
ohlcv` to convert trades data to OHLCV data.
--exchange EXCHANGE Exchange name. Only valid if no config is provided.
-t TIMEFRAMES [TIMEFRAMES ...], --timeframes TIMEFRAMES [TIMEFRAMES ...]
Specify which tickers to download. Space-separated
list. Default: `1m 5m`.
--erase Clean all existing data for the selected
exchange/pairs/timeframes.
--data-format-ohlcv {json,jsongz,feather,parquet}
Storage format for downloaded candle (OHLCV) data.
(default: `feather`).
--data-format-trades {json,jsongz,feather,parquet}
Storage format for downloaded trades data. (default:
`feather`).
--trading-mode {spot,margin,futures}, --tradingmode {spot,margin,futures}
Select Trading mode
--prepend Allow data prepending. (Data-appending is disabled)
Common arguments:
-v, --verbose Verbose mode (-vv for more, -vvv to get all messages).
--no-color Disable colorization of hyperopt results. May be
useful if you are redirecting output to a file.
--logfile FILE, --log-file FILE
Log to the file specified. Special values are:
'syslog', 'journald'. See the documentation for more
details.
-V, --version show program's version number and exit
-c PATH, --config PATH
Specify configuration file (default:
`userdir/config.json` or `config.json` whichever
exists). Multiple --config options may be used. Can be
set to `-` to read config from stdin.
-d PATH, --datadir PATH, --data-dir PATH
Path to the base directory of the exchange with
historical backtesting data. To see futures data, use
trading-mode additionally.
--userdir PATH, --user-data-dir PATH
Path to userdata directory.
下载特定计价货币的所有数据
通常,你可能希望下载某个特定计价货币的所有交易对数据。在这种情况下,可以使用以下简写方式:freqtrade download-data --exchange binance --pairs ".*/USDT" <...>。提供的“交易对”字符串将被扩展为包含交易所上所有活跃交易对。若要同时下载非活跃(已下架)交易对的数据,请在命令中添加 --include-inactive-pairs。
启动期
download-data 是一个与策略无关的命令。其设计思路是先一次性下载大量数据,然后逐步增加存储的数据量。
因此,download-data 不会考虑策略中定义的“启动期”。如果回测需要从特定时间点开始(并满足启动期要求),用户需自行下载额外的天数数据。
开始下载¶
一个非常简单的命令(假设已有可用的 config.json 文件)如下所示。
freqtrade download-data --exchange binance
这将下载配置文件中定义的所有交易对的历史 K 线(OHLCV)数据。
或者,直接指定交易对
freqtrade download-data --exchange binance --pairs ETH/USDT XRP/USDT BTC/USDT
或使用正则表达式(例如,下载所有活跃的 USDT 交易对)
freqtrade download-data --exchange binance --pairs ".*/USDT"
其他注意事项¶
- 若要使用不同于交易所默认的目录,请使用
--datadir user_data/data/some_directory。 - 若要更改用于下载历史数据的交易所,可使用
--exchange <exchange>,或指定一个不同的配置文件。 - 若要使用其他目录中的
pairs.json文件,请使用--pairs-file some_other_dir/pairs.json。 - 若要仅下载过去 10 天的历史 K 线(OHLCV)数据,请使用
--days 10(默认为 30 天)。 - 若要从固定起始时间点下载历史 K 线(OHLCV)数据,请使用
--timerange 20200101-,这将下载从 2020 年 1 月 1 日起的所有数据。 - 如果数据已存在,则起始时间点将被忽略,仅下载从现有数据结束到当前日期之间的缺失数据。
- 使用
--timeframes指定要下载历史 K 线(OHLCV)数据的时间周期。默认为--timeframes 1m 5m,即下载 1 分钟和 5 分钟级别的数据。 - 若要使用配置文件中定义的交易所、时间周期和交易对列表,请使用
-c/--config选项。此选项将使用配置文件中的白名单作为待下载数据的交易对列表,无需pairs.json文件。你可以将-c/--config与其他大多数选项结合使用。
权限被拒绝错误
如果你的配置目录 user_data 是由 Docker 创建的,你可能会遇到以下错误:
cp: cannot create regular file 'user_data/data/binance/pairs.json': Permission denied
你可以通过以下命令修复用户数据目录的权限问题:
sudo chown -R $UID:$GID user_data
在当前时间范围之前下载额外数据¶
假设你已下载了 2022 年以来的所有数据(--timerange 20220101-),但现在希望使用更早的数据进行回测。你可以使用 --prepend 标志,并结合 --timerange 指定一个结束日期来实现。
freqtrade download-data --exchange binance --pairs ETH/USDT XRP/USDT BTC/USDT --prepend --timerange 20210101-20220101
注意
在此模式下,如果已有数据存在,Freqtrade 将忽略指定的结束日期,并自动将结束日期更新为现有数据的起始点。
数据格式¶
目前 Freqtrade 支持以下数据格式:
feather- 基于 Apache Arrow 的数据格式json- 普通的“文本”JSON 文件jsongz- 经 gzip 压缩的 JSON 文件parquet- 列式存储数据(仅支持 OHLCV)
默认情况下,OHLCV 数据和交易数据均以 feather 格式存储。
可通过 --data-format-ohlcv 和 --data-format-trades 命令行参数分别更改。若要使更改持久生效,你还应在配置文件中添加相应配置,以免每次运行时都需输入上述参数:
// ...
"dataformat_ohlcv": "feather",
"dataformat_trades": "feather",
// ...
如果在下载过程中更改了默认数据格式,则配置文件中的 dataformat_ohlcv 和 dataformat_trades 键也需相应调整为所选的数据格式。
注意
你可以使用 convert-data 和 convert-trade-data 方法在不同数据格式之间进行转换。
数据格式对比¶
以下比较基于如下数据,并通过 Linux 的 time 命令得出。
Found 6 pair / timeframe combinations.
+----------+-------------+--------+---------------------+---------------------+
| Pair | Timeframe | Type | From | To |
|----------+-------------+--------+---------------------+---------------------|
| BTC/USDT | 5m | spot | 2017-08-17 04:00:00 | 2022-09-13 19:25:00 |
| ETH/USDT | 1m | spot | 2017-08-17 04:00:00 | 2022-09-13 19:26:00 |
| BTC/USDT | 1m | spot | 2017-08-17 04:00:00 | 2022-09-13 19:30:00 |
| XRP/USDT | 5m | spot | 2018-05-04 08:10:00 | 2022-09-13 19:15:00 |
| XRP/USDT | 1m | spot | 2018-05-04 08:11:00 | 2022-09-13 19:22:00 |
| ETH/USDT | 5m | spot | 2017-08-17 04:00:00 | 2022-09-13 19:20:00 |
+----------+-------------+--------+---------------------+---------------------+
计时方式并不十分科学,使用了以下命令进行测量,该命令会强制将数据读入内存。
time freqtrade list-data --show-timerange --data-format-ohlcv <dataformat>
| 格式 | 大小 | 耗时 |
|---|---|---|
feather |
72MB | 3.5 秒 |
json |
149MB | 25.6 秒 |
jsongz |
39MB | 27 秒 |
parquet |
83MB | 3.8 秒 |
文件大小数据来源于上述指定时间段内的 BTC/USDT 现货 1 分钟 K 线组合。
为了获得最佳的性能与体积平衡,我们推荐使用默认的 feather 格式或 parquet 格式。
交易对文件¶
除了使用 config.json 中的白名单外,还可以使用 pairs.json 文件。例如,如果你正在使用币安(Binance):
- 创建一个目录
user_data/data/binance,并将pairs.json文件复制或创建到该目录中。 - 更新
pairs.json文件,包含你感兴趣的交易对。
mkdir -p user_data/data/binance
touch user_data/data/binance/pairs.json
pairs.json 文件的格式是一个简单的 JSON 列表。此文件允许混合不同的计价货币,因为它仅用于下载数据。
[
"ETH/BTC",
"ETH/USDT",
"BTC/USDT",
"XRP/ETH"
]
注意
pairs.json 文件仅在未加载配置时使用(即未通过命名隐式加载,也未通过 --config 参数指定)。你可以通过 --pairs-file pairs.json 强制使用此文件,但我们建议在配置文件中使用交易对列表,即通过配置中的 exchange.pair_whitelist 或 pairs 设置。
子命令:转换数据¶
usage: freqtrade convert-data [-h] [-v] [--no-color] [--logfile FILE] [-V]
[-c PATH] [-d PATH] [--userdir PATH]
[-p PAIRS [PAIRS ...]] --format-from
{json,jsongz,feather,parquet} --format-to
{json,jsongz,feather,parquet} [--erase]
[--exchange EXCHANGE]
[-t TIMEFRAMES [TIMEFRAMES ...]]
[--trading-mode {spot,margin,futures}]
[--candle-types {spot,futures,mark,index,premiumIndex,funding_rate} [{spot,futures,mark,index,premiumIndex,funding_rate} ...]]
options:
-h, --help show this help message and exit
-p PAIRS [PAIRS ...], --pairs PAIRS [PAIRS ...]
Limit command to these pairs. Pairs are space-
separated.
--format-from {json,jsongz,feather,parquet}
Source format for data conversion.
--format-to {json,jsongz,feather,parquet}
Destination format for data conversion.
--erase Clean all existing data for the selected
exchange/pairs/timeframes.
--exchange EXCHANGE Exchange name. Only valid if no config is provided.
-t TIMEFRAMES [TIMEFRAMES ...], --timeframes TIMEFRAMES [TIMEFRAMES ...]
Specify which tickers to download. Space-separated
list. Default: `1m 5m`.
--trading-mode {spot,margin,futures}, --tradingmode {spot,margin,futures}
Select Trading mode
--candle-types {spot,futures,mark,index,premiumIndex,funding_rate} [{spot,futures,mark,index,premiumIndex,funding_rate} ...]
Select candle type to convert. Defaults to all
available types.
Common arguments:
-v, --verbose Verbose mode (-vv for more, -vvv to get all messages).
--no-color Disable colorization of hyperopt results. May be
useful if you are redirecting output to a file.
--logfile FILE, --log-file FILE
Log to the file specified. Special values are:
'syslog', 'journald'. See the documentation for more
details.
-V, --version show program's version number and exit
-c PATH, --config PATH
Specify configuration file (default:
`userdir/config.json` or `config.json` whichever
exists). Multiple --config options may be used. Can be
set to `-` to read config from stdin.
-d PATH, --datadir PATH, --data-dir PATH
Path to the base directory of the exchange with
historical backtesting data. To see futures data, use
trading-mode additionally.
--userdir PATH, --user-data-dir PATH
Path to userdata directory.
数据转换示例¶
以下命令将把 ~/.freqtrade/data/binance 目录下所有可用的 K 线(OHLCV)数据从 json 格式转换为 jsongz 格式,从而节省磁盘空间。同时,它还会删除原始的 json 数据文件(由 --erase 参数控制)。
freqtrade convert-data --format-from json --format-to jsongz --datadir ~/.freqtrade/data/binance -t 5m 15m --erase
子命令 转换交易数据¶
usage: freqtrade convert-trade-data [-h] [-v] [--no-color] [--logfile FILE]
[-V] [-c PATH] [-d PATH] [--userdir PATH]
[-p PAIRS [PAIRS ...]] --format-from
{json,jsongz,feather,parquet,kraken_csv}
--format-to {json,jsongz,feather,parquet}
[--erase] [--exchange EXCHANGE]
options:
-h, --help show this help message and exit
-p PAIRS [PAIRS ...], --pairs PAIRS [PAIRS ...]
Limit command to these pairs. Pairs are space-
separated.
--format-from {json,jsongz,feather,parquet,kraken_csv}
Source format for data conversion.
--format-to {json,jsongz,feather,parquet}
Destination format for data conversion.
--erase Clean all existing data for the selected
exchange/pairs/timeframes.
--exchange EXCHANGE Exchange name. Only valid if no config is provided.
Common arguments:
-v, --verbose Verbose mode (-vv for more, -vvv to get all messages).
--no-color Disable colorization of hyperopt results. May be
useful if you are redirecting output to a file.
--logfile FILE, --log-file FILE
Log to the file specified. Special values are:
'syslog', 'journald'. See the documentation for more
details.
-V, --version show program's version number and exit
-c PATH, --config PATH
Specify configuration file (default:
`userdir/config.json` or `config.json` whichever
exists). Multiple --config options may be used. Can be
set to `-` to read config from stdin.
-d PATH, --datadir PATH, --data-dir PATH
Path to the base directory of the exchange with
historical backtesting data. To see futures data, use
trading-mode additionally.
--userdir PATH, --user-data-dir PATH
Path to userdata directory.
交易数据转换示例¶
以下命令将把 ~/.freqtrade/data/kraken 中所有可用的交易数据从 jsongz 格式转换为 json 格式。同时,它还会删除原始的 jsongz 数据文件(使用 --erase 参数)。
freqtrade convert-trade-data --format-from jsongz --format-to json --datadir ~/.freqtrade/data/kraken --erase
子命令 交易数据转 OHLCV¶
当你需要使用 --dl-trades(仅 Kraken)下载数据时,将交易数据转换为 OHLCV 数据是最后一步。此命令允许你在无需重新下载数据的情况下,对更多时间周期重复执行这最后一步。
usage: freqtrade trades-to-ohlcv [-h] [-v] [--no-color] [--logfile FILE] [-V]
[-c PATH] [-d PATH] [--userdir PATH]
[-p PAIRS [PAIRS ...]]
[-t TIMEFRAMES [TIMEFRAMES ...]]
[--exchange EXCHANGE]
[--data-format-ohlcv {json,jsongz,feather,parquet}]
[--data-format-trades {json,jsongz,feather,parquet}]
[--trading-mode {spot,margin,futures}]
options:
-h, --help show this help message and exit
-p PAIRS [PAIRS ...], --pairs PAIRS [PAIRS ...]
Limit command to these pairs. Pairs are space-
separated.
-t TIMEFRAMES [TIMEFRAMES ...], --timeframes TIMEFRAMES [TIMEFRAMES ...]
Specify which tickers to download. Space-separated
list. Default: `1m 5m`.
--exchange EXCHANGE Exchange name. Only valid if no config is provided.
--data-format-ohlcv {json,jsongz,feather,parquet}
Storage format for downloaded candle (OHLCV) data.
(default: `feather`).
--data-format-trades {json,jsongz,feather,parquet}
Storage format for downloaded trades data. (default:
`feather`).
--trading-mode {spot,margin,futures}, --tradingmode {spot,margin,futures}
Select Trading mode
Common arguments:
-v, --verbose Verbose mode (-vv for more, -vvv to get all messages).
--no-color Disable colorization of hyperopt results. May be
useful if you are redirecting output to a file.
--logfile FILE, --log-file FILE
Log to the file specified. Special values are:
'syslog', 'journald'. See the documentation for more
details.
-V, --version show program's version number and exit
-c PATH, --config PATH
Specify configuration file (default:
`userdir/config.json` or `config.json` whichever
exists). Multiple --config options may be used. Can be
set to `-` to read config from stdin.
-d PATH, --datadir PATH, --data-dir PATH
Path to the base directory of the exchange with
historical backtesting data. To see futures data, use
trading-mode additionally.
--userdir PATH, --user-data-dir PATH
Path to userdata directory.
交易转 OHLCV 转换示例¶
freqtrade trades-to-ohlcv --exchange kraken -t 5m 1h 1d --pairs BTC/EUR ETH/EUR
子命令 list-data¶
你可以使用 list-data 子命令来获取已下载数据的列表。
usage: freqtrade list-data [-h] [-v] [--no-color] [--logfile FILE] [-V]
[-c PATH] [-d PATH] [--userdir PATH]
[--exchange EXCHANGE]
[--data-format-ohlcv {json,jsongz,feather,parquet}]
[--data-format-trades {json,jsongz,feather,parquet}]
[--trades] [-p PAIRS [PAIRS ...]]
[--trading-mode {spot,margin,futures}]
[--show-timerange]
options:
-h, --help show this help message and exit
--exchange EXCHANGE Exchange name. Only valid if no config is provided.
--data-format-ohlcv {json,jsongz,feather,parquet}
Storage format for downloaded candle (OHLCV) data.
(default: `feather`).
--data-format-trades {json,jsongz,feather,parquet}
Storage format for downloaded trades data. (default:
`feather`).
--trades Work on trades data instead of OHLCV data.
-p PAIRS [PAIRS ...], --pairs PAIRS [PAIRS ...]
Limit command to these pairs. Pairs are space-
separated.
--trading-mode {spot,margin,futures}, --tradingmode {spot,margin,futures}
Select Trading mode
--show-timerange Show timerange available for available data. (May take
a while to calculate).
Common arguments:
-v, --verbose Verbose mode (-vv for more, -vvv to get all messages).
--no-color Disable colorization of hyperopt results. May be
useful if you are redirecting output to a file.
--logfile FILE, --log-file FILE
Log to the file specified. Special values are:
'syslog', 'journald'. See the documentation for more
details.
-V, --version show program's version number and exit
-c PATH, --config PATH
Specify configuration file (default:
`userdir/config.json` or `config.json` whichever
exists). Multiple --config options may be used. Can be
set to `-` to read config from stdin.
-d PATH, --datadir PATH, --data-dir PATH
Path to the base directory of the exchange with
historical backtesting data. To see futures data, use
trading-mode additionally.
--userdir PATH, --user-data-dir PATH
Path to userdata directory.
list-data 示例¶
> freqtrade list-data --userdir ~/.freqtrade/user_data/
Found 33 pair / timeframe combinations.
┏━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━┓
┃ Pair ┃ Timeframe ┃ Type ┃
┡━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━┩
│ ADA/BTC │ 5m, 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d │ spot │
│ ADA/ETH │ 5m, 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d │ spot │
│ ETH/BTC │ 5m, 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d │ spot │
│ ETH/USDT │ 5m, 15m, 30m, 1h, 2h, 4h │ spot │
└───────────────┴───────────────────────────────────────────┴──────┘
显示所有交易数据,包括时间范围的起止
> freqtrade list-data --show --trades
Found trades data for 1 pair.
┏━━━━━━━━━┳━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━┓
┃ Pair ┃ Type ┃ From ┃ To ┃ Trades ┃
┡━━━━━━━━━╇━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━┩
│ XRP/ETH │ spot │ 2019-10-11 00:00:11 │ 2019-10-13 11:19:28 │ 12477 │
└─────────┴──────┴─────────────────────┴─────────────────────┴────────┘
交易(逐笔)数据¶
默认情况下,download-data 子命令下载的是蜡烛图(OHLCV)数据。大多数交易所也通过其 API 提供历史交易数据。如果你需要多个不同的时间周期,这类数据会非常有用,因为它只需下载一次,然后可在本地重新采样为所需的时间周期。
由于此类数据通常体积较大,默认使用 feather 文件格式存储。它们保存在你的数据目录中,命名规则为 <交易对>-trades.feather(例如 ETH_BTC-trades.feather)。与历史 OHLCV 数据一样,也支持增量模式,因此每周使用 --days 8 下载一次数据,即可构建一个增量的数据仓库。
要使用此模式,只需在调用时添加 --dl-trades 参数。这会切换下载方式以获取交易数据。如果同时提供 --convert 参数,则会自动执行重采样步骤,并覆盖已存在的指定交易对/时间周期组合的 OHLCV 数据。
请勿使用
除非你是 Kraken 用户(Kraken 不提供历史 OHLCV 数据),否则不应使用此方法。
大多数其他交易所都提供足够历史长度的 OHLCV 数据,因此通过该方法下载多个时间周期的数据仍远快于下载交易数据。
Kraken 用户
Kraken 用户在开始下载数据前应先阅读 本文。
调用示例:
freqtrade download-data --exchange kraken --pairs XRP/EUR ETH/EUR --days 20 --dl-trades
注意
尽管此方法使用异步调用,但由于它需要前一次调用的结果才能生成下一次请求,因此速度较慢。
下一步¶
很好,你现在已经下载了一些数据,可以开始回测你的策略了。