抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

今天才发现加密货币量化之路并不是一帆风顺的。

在安装完 Freqtrade 后,当我准备运行并下载 Binance 数据时,发现 api.binance.com 在国内网络环境下已经无法直接访问(可能是被封禁 IP,也可能是 Binance 主动限制大陆 IP)。

总之,目前必须另辟蹊径获取 K 线数据


🌐 当前解决方案:代理 + mihomo(Clash Core)

我目前使用的方案是代理转发。

代理来源是机场订阅,使用 Clash 内核的实现 —— mihomo。

环境说明:

  • 系统:树莓派 ARM64
  • 代理客户端:mihomo Linux ARM64 版本
  • 启动方式:单独运行 mihomo,再让 Docker 流量走代理

📦 安装 mihomo

下载并安装:

1
2
3
4
5
6
7
8
9
10
11
12
wget https://github.com/MetaCubeX/mihomo/releases/download/Prerelease-Alpha/mihomo-linux-arm64-alpha-1095055.gz

# 解压
gunzip mihomo-linux-arm64-alpha-1095055.gz

chmod +x mihomo-linux-arm64-alpha-1095055

sudo mv mihomo-linux-arm64-alpha-1095055 /usr/local/bin/mihomo

sudo mkdir -p ~/.config/mihomo

touch ~/.config/mihomo/config.yaml

⚙️ 配置文件说明(config.yaml)

这里的 config.yaml 是从 Windows 客户端 Clash Verge 导出的:

导出方式:

设置 → 当前配置 → 复制内容到 config.yaml

注意几点:

  • mode 必须是 rule
  • global 模式不可用(会导致规则失效)
  • 建议设置:log-level: info(便于排错)
  • 重点记录:mixed-port(代理端口,后面要用)

🚀 启动 mihomo

1
mihomo -d ~/.config/mihomo/

首次启动可能会报错:

❌ 常见问题:GeoIP 下载失败

1
2
can't download MMDB ... context deadline exceeded
Parse config error: can't download MMDB

原因:

  • 启动时会自动下载:

    • geoip.metadb
    • geoip.data
  • GitHub 在国内网络可能不可达


🛠 解决方案

手动下载(使用 GitHub 加速):

下载后放入:

1
~/.config/mihomo/

✅ 启动成功日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mihomo -d ~/.config/mihomo/

INFO[2026-06-29T13:20:10.683002234+08:00] Start initial configuration in progress
INFO[2026-06-29T13:20:10.791744783+08:00] Geodata Loader mode: memconservative
INFO[2026-06-29T13:20:10.791903005+08:00] Geosite Matcher implementation: succinct
INFO[2026-06-29T13:20:10.793755104+08:00] Initial configuration complete, total time: 110ms
INFO[2026-06-29T13:20:10.805048606+08:00] Sniffer is closed
INFO[2026-06-29T13:20:10.805168791+08:00] Use tcp concurrent
INFO[2026-06-29T13:20:10.805752046+08:00] DNS server(UDP) listening at: 127.0.0.1:1053
INFO[2026-06-29T13:20:10.805965489+08:00] Mixed(http+socks) proxy listening at: 127.0.0.1:7897
INFO[2026-06-29T13:20:10.805989656+08:00] DNS server(TCP) listening at: 127.0.0.1:1053
INFO[2026-06-29T13:20:10.806357839+08:00] Start initial compatible provider 自动选择
INFO[2026-06-29T13:20:10.806297043+08:00] Start initial compatible provider default
INFO[2026-06-29T13:20:10.806322024+08:00] Start initial compatible provider 一分机场
INFO[2026-06-29T13:20:10.806338672+08:00] Start initial compatible provider 故障转移
INFO[2026-06-29T13:20:17.564950347+08:00] Load MMDB file: /home/zhshang/.config/mihomo/geoip.metadb
INFO[2026-06-29T13:20:17.565651658+08:00] [TCP] 127.0.0.1:54044 --> api.binance.com:443 match Match using 一分机场[🇸🇬新加坡专线4]

说明:

  • 代理端口:7897
  • DNS 正常启动
  • 规则 provider 初始化完成

🔍 代理验证

测试 Binance 是否可访问:

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
curl -x http://127.0.0.1:7897 -I https://api.binance.com
HTTP/1.1 200 Connection established

HTTP/2 200
content-type: text/html; charset=utf-8
content-length: 77
date: Mon, 29 Jun 2026 05:53:02 GMT
accept-ranges: bytes
server: nginx
last-modified: Thu, 30 Jan 2025 14:16:23 GMT
etag: "679b89b7-4d"
strict-transport-security: max-age=31536000; includeSubdomains
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
content-security-policy: default-src 'self'
x-content-security-policy: default-src 'self'
x-webkit-csp: default-src 'self'
cache-control: no-cache, no-store, must-revalidate
pragma: no-cache
expires: 0
access-control-allow-origin: *
access-control-allow-methods: GET, HEAD, OPTIONS
x-cache: Miss from cloudfront
via: 1.1 9b8a860d147083d2447cbe7648a624dc.cloudfront.net (CloudFront)
x-amz-cf-pop: SIN3-P5
x-amz-cf-id: y-J-ZLlzDrjSfRrQdD0C3n5uJ5KZTn3MHB4vPguOWhDf_Oxdj8u0iA==

说明代理链路已经完全打通。


⚙️ Freqtrade 配置

Freqtrade 配置文件如下:

关键点:

  • 启用 futures
  • 使用 isolated margin
  • 设置 proxy
  • ccxt 走 httpProxy

核心代理配置:

下一步需要配置freqtrade,我的配置是这样的:

1
cat ~/github/freqtrade/user_data/config.json
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{
"$schema": "https://schema.freqtrade.io/schema.json",
"max_open_trades": 1,
"stake_currency": "USDT",
"stake_amount": 20,
"tradable_balance_ratio": 0.99,
"fiat_display_currency": "USD",
"dry_run": true,
"dry_run_wallet": 1000,
"cancel_open_orders_on_exit": false,
"trading_mode": "futures",
"margin_mode": "isolated",
"unfilledtimeout": {
"entry": 10,
"exit": 10,
"exit_timeout_count": 0,
"unit": "minutes"
},
"entry_pricing": {
"price_side": "same",
"use_order_book": true,
"order_book_top": 1,
"price_last_balance": 0.0,
"check_depth_of_market": {
"enabled": false,
"bids_to_ask_delta": 1
}
},
"exit_pricing": {
"price_side": "same",
"use_order_book": true,
"order_book_top": 1
},
"exchange": {
"name": "binance",
"key": "",
"secret": "",
"ccxt_config": {
"enableRateLimit": true,
"timeout": 60000,
"httpProxy": "http://127.0.0.1:7897",
"options": {
"defaultType": "future"
}
},
"ccxt_async_config": {
"enableRateLimit": true,
"timeout": 60000,
"httpProxy": "http://127.0.0.1:7897"
},
"pair_whitelist": [
],
"pair_blacklist": [
"BNB/.*"
]
},
"pairlists": [
{
"method": "VolumePairList",
"number_assets": 20,
"sort_key": "quoteVolume",
"min_value": 0,
"refresh_period": 1800
}
],
"telegram": {
"enabled": true,
"token": "8848:AAE--VZHG************zlwLKShFmE",
"chat_id": "*******"
},
"api_server": {
"enabled": true,
"listen_ip_address": "0.0.0.0",
"listen_port": 8080,
"verbosity": "error",
"enable_openapi": false,
"jwt_secret_key": "2d20a************************b1e50e6f56",
"ws_token": "FRTJ2***************Tu4chKcNQ",
"CORS_origins": [],
"username": "freqtrader",
"password": "********"
},
"bot_name": "freqtrade",
"initial_state": "running",
"force_entry_enable": false,
"internals": {
"process_throttle_secs": 5
}
}

🐳 Docker 下载数据

运行数据下载:

1
2
3
4
5
6
7
8
docker run --rm --network host \
-v $(pwd)/user_data:/freqtrade/user_data \
freqtradeorg/freqtrade:stable \
download-data \
--config /freqtrade/user_data/config.json \
--pairs BTC/USDT:USDT ETH/USDT:USDT \
--timerange 20260615-20260629 \
-t 30m

⚠️ 关键坑点

1️⃣ volume 映射必须用绝对路径

1
-v $(pwd)/user_data:/freqtrade/user_data

必须使用 $(pwd) 获取当前绝对路径。Docker 的 -v 左侧是宿主机路径,如果直接写相对路径(如 user_data),Docker 会基于当前工作目录解析,容易因目录不一致导致找不到文件。使用 $(pwd) 可确保路径绝对准确。


2️⃣ 网络模式

1
--network host

必须与:

1
"httpProxy": "http://127.0.0.1:7897"

配合使用,也就是代理地址。


3️⃣ ccxt 代理必须配置

1
2
3
4
5
6
7
8
9
10
11
12
"exchange": {
"name": "binance",
"key": "",
"secret": "",
"ccxt_config": {
"enableRateLimit": true,
"timeout": 60000,
"httpProxy": "http://127.0.0.1:7897",
"options": {
"defaultType": "futures"
}
},

否则仍然会直连 Binance 导致失败。


📊 成功下载日志

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
41
42
43
44
45
46
47
48
docker run --rm --network host   -v $(pwd)/user_data:/freqtrade/user_data   freqtradeorg/freqtrade:stable   download-data   --config /freqtrade/user_data/config.json  --pairs BTC/USDT:USDT ETH/USDT:USDT --timerange 20260615-20260629 -t 30m
2026-06-29 08:03:22,393 - freqtrade - INFO - freqtrade 2026.5.1
2026-06-29 08:03:23,680 - numexpr.utils - INFO - NumExpr defaulting to 4 threads.
2026-06-29 08:03:28,276 - freqtrade.configuration.load_config - INFO - Using config: /freqtrade/user_data/config.json ...
2026-06-29 08:03:28,282 - freqtrade.loggers - INFO - Enabling colorized output.
2026-06-29 08:03:28,283 - freqtrade.loggers - INFO - Logfile configured
2026-06-29 08:03:28,285 - freqtrade.loggers - INFO - Verbosity set to 0
2026-06-29 08:03:28,287 - freqtrade.configuration.configuration - INFO - Parameter --timerange detected: 20260615-20260629 ...
2026-06-29 08:03:28,328 - freqtrade.configuration.configuration - INFO - Using user-data directory: /freqtrade/user_data ...
2026-06-29 08:03:28,330 - freqtrade.configuration.configuration - INFO - Using data directory: /freqtrade/user_data/data/binance ...
2026-06-29 08:03:28,331 - freqtrade.configuration.configuration - INFO - Using pairs ['BTC/USDT:USDT', 'ETH/USDT:USDT']
2026-06-29 08:03:28,333 - freqtrade.configuration.configuration - INFO - timeframes --timeframes: ['30m']
2026-06-29 08:03:28,334 - freqtrade.configuration.configuration - INFO - Filter trades by timerange: 20260615-20260629
2026-06-29 08:03:28,337 - freqtrade.exchange.check_exchange - INFO - Checking exchange...
2026-06-29 08:03:28,368 - freqtrade.exchange.check_exchange - INFO - Exchange "binance" is officially supported by the Freqtrade development team.
2026-06-29 08:03:28,370 - freqtrade.configuration.config_validation - INFO - Validating configuration ...
2026-06-29 08:03:28,783 - freqtrade.exchange.exchange - INFO - Instance is running with dry_run enabled
2026-06-29 08:03:28,784 - freqtrade.exchange.exchange - INFO - Using CCXT 4.5.55
2026-06-29 08:03:28,786 - freqtrade.exchange.exchange - INFO - Applying additional ccxt config: {'options': {'defaultType': 'spot'}, 'enableRateLimit': True, 'timeout': 60000, 'httpProxy':
'http://127.0.0.1:7897'}
2026-06-29 08:03:28,814 - freqtrade.exchange.exchange - INFO - Applying additional ccxt config: {'options': {'defaultType': 'spot'}, 'enableRateLimit': True, 'timeout': 60000, 'httpProxy':
'http://127.0.0.1:7897'}
2026-06-29 08:03:28,846 - freqtrade.exchange.exchange - INFO - Using Exchange "Binance"
2026-06-29 08:03:28,847 - freqtrade.resolvers.exchange_resolver - INFO - Using resolved exchange 'Binance'...
2026-06-29 08:03:28,850 - freqtrade.exchange.exchange - INFO - Markets were not loaded. Loading them now..
2026-06-29 08:03:31,747 - freqtrade.data.history.history_utils - INFO - About to download pairs: ['BTC/USDT:USDT', 'ETH/USDT:USDT'], intervals: ['30m'] to /freqtrade/user_data/data/binance
2026-06-29 08:03:31,811 - freqtrade.data.history.history_utils - INFO - Download history data for "BTC/USDT:USDT", 30m, futures and store in /freqtrade/user_data/data/binance. From 2026-06-15T00:00:00
to 2026-06-29T00:00:00
2026-06-29 08:03:32,874 - freqtrade.data.history.history_utils - INFO - Downloaded data for BTC/USDT:USDT, 30m, futures with length 688.
2026-06-29 08:03:32,894 - freqtrade.data.history.history_utils - INFO - Downloading parallel candles for 1h for all pairs since 2026-06-15T00:00:00
2026-06-29 08:03:33,463 - freqtrade.data.history.history_utils - INFO - Download history data for "BTC/USDT:USDT", 1h, mark and store in /freqtrade/user_data/data/binance. From 2026-06-15T00:00:00 to
2026-06-29T00:00:00
2026-06-29 08:03:34,035 - freqtrade.data.history.history_utils - INFO - Downloaded data for BTC/USDT:USDT, 1h, mark with length 344.
2026-06-29 08:03:34,047 - freqtrade.data.history.history_utils - INFO - Downloading parallel candles for 1h for all pairs since 2026-06-15T00:00:00
2026-06-29 08:03:34,408 - freqtrade.data.history.history_utils - INFO - Download history data for "BTC/USDT:USDT", 1h, funding_rate and store in /freqtrade/user_data/data/binance. From
2026-06-15T00:00:00 to 2026-06-29T00:00:00
2026-06-29 08:03:34,649 - freqtrade.data.history.history_utils - INFO - Downloaded data for BTC/USDT:USDT, 1h, funding_rate with length 44.
2026-06-29 08:03:34,662 - freqtrade.data.history.history_utils - INFO - Download history data for "ETH/USDT:USDT", 30m, futures and store in /freqtrade/user_data/data/binance. From 2026-06-15T00:00:00
to 2026-06-29T00:00:00
2026-06-29 08:03:36,011 - freqtrade.data.history.history_utils - INFO - Downloaded data for ETH/USDT:USDT, 30m, futures with length 688.
2026-06-29 08:03:36,038 - freqtrade.data.history.history_utils - INFO - Download history data for "ETH/USDT:USDT", 1h, mark and store in /freqtrade/user_data/data/binance. From 2026-06-15T00:00:00 to
2026-06-29T00:00:00
2026-06-29 08:03:36,615 - freqtrade.data.history.history_utils - INFO - Downloaded data for ETH/USDT:USDT, 1h, mark with length 344.
2026-06-29 08:03:36,629 - freqtrade.data.history.history_utils - INFO - Download history data for "ETH/USDT:USDT", 1h, funding_rate and store in /freqtrade/user_data/data/binance. From
2026-06-15T00:00:00 to 2026-06-29T00:00:00
2026-06-29 08:03:36,920 - freqtrade.data.history.history_utils - INFO - Downloaded data for ETH/USDT:USDT, 1h, funding_rate with length 44.
Timeframe ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3/3 100% • 0:00:02 • 0:00:00
Downloading ETH/USDT:USDT ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2/2 100% • 0:00:05 • 0:00:00

并同时下载:

  • mark price
  • funding rate
  • multiple timeframes(30m / 1h)

📁 数据存储位置

1
2
3
ls user_data/data/binance/futures/
BTC_USDT_USDT-1h-funding_rate.feather BTC_USDT_USDT-30m-futures.feather ETH_USDT_USDT-1h-mark.feather
BTC_USDT_USDT-1h-mark.feather ETH_USDT_USDT-1h-funding_rate.feather ETH_USDT_USDT-30m-futures.feather

📌 总结

整体流程:

1
2
3
4
5
6
7
8
9
mihomo代理

Docker freqtrade

ccxt (httpProxy)

Binance API

成功下载K线数据

评论



Powered by Hexo | Theme keep Volantis

本站总访问量 总访客数 🌎