auto_trade_sys/scripts/SYNC_BINANCE_README.md
薇薇安 7bc384a58f 1
2026-03-01 13:56:50 +08:00

58 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 币安订单同步
## 1. 建表
首次使用需执行 SQL 建表:
```bash
mysql -u user -p auto_trade_sys < backend/database/add_binance_sync_tables.sql
```
## 2. 定时任务脚本
`sync_binance_orders.py` 从币安拉取各账号最近 6 小时的订单/成交,去重写入 DB。
```bash
# 同步所有有效账号
python scripts/sync_binance_orders.py
# 指定账号
python scripts/sync_binance_orders.py -a 2
# 拉取最近 12 小时
python scripts/sync_binance_orders.py --hours 12
# 多账号时减少账号间隔(默认 90 秒,避免限频)
python scripts/sync_binance_orders.py --delay-between-accounts 60
```
## 3. Crontab 配置示例
每 3 小时的第 0 分钟执行一次(与 6 小时拉取窗口重叠,便于去重):
```cron
0 */3 * * * cd /path/to/auto_trade_sys && /path/to/.venv/bin/python scripts/sync_binance_orders.py >> logs/sync_binance.log 2>&1
```
**延时说明**:因定时任务为每 3 小时跑一次,页面或统计里看到的「币安成交」数据存在一定延时,最多可能滞后约 3 小时;如需最新可手动执行一次脚本。
或每 6 小时:
```cron
0 */6 * * * cd /path/to/auto_trade_sys && /path/to/.venv/bin/python scripts/sync_binance_orders.py >> logs/sync_binance.log 2>&1
```
## 4. 限频说明
- 多账号时每个账号之间默认等待 90 秒,可用 `--delay-between-accounts` 调整
- 单账号内已降低并发Semaphore 2和请求间隔减少触发 "Way too many requests" 封 IP
- 若已被封,需等待提示时间后重试;建议 crontab 间隔不少于 3 小时
## 5. 数据管理
管理后台「数据管理」-「币安订单/成交查询」从 DB 读取,不再调用币安 API。
## 6. 账户余额快照(总资产缓存)
同步脚本在拉取每个账号的订单/成交后,会顺带调用一次该账号的 `futures_account` 取余额,并写入 `account_snapshots` 表。管理员仪表板「总资产 (USDT)」即对各账号该表最近一条快照的 `total_balance` 汇总,**不每次请求币安**。若从未跑过本脚本(或脚本报错未写入快照),总资产会显示 0跑一次后即可有数据。