first commit
This commit is contained in:
@@ -0,0 +1,333 @@
|
||||
# `crypto_monitor_gate` 部署指南:SSH SOCKS + Gate.io + PM2(Ubuntu)
|
||||
|
||||
本文面向:**在本机运行本项目**,但 **直连 Gate.io API 不稳定或被重置** 的场景。思路是:
|
||||
|
||||
- 本机用 `ssh -D` 做动态转发,把 **SOCKS5 出口**放到能正常访问 Gate 的机器(常见为一台境外 VPS)
|
||||
- 项目在 `.env` 中设置 **`GATE_SOCKS_PROXY=socks5h://127.0.0.1:1080`**(或你实际端口),`ccxt` 经 SOCKS 访问交易所
|
||||
- **SSH 隧道**:用 `ssh -D` 在本机常驻即可(screen / tmux / systemd 等),**不必交给 PM2**
|
||||
- 使用 **PM2** 仅托管 **Flask 应用**;仓库根目录 **`ecosystem.config.cjs`** 只定义 `crypto-monitor-gate`
|
||||
|
||||
> 安全提醒:不要把 `.env`、私钥 `.pem`、Gate API Key 提交到 Git;下文只用占位符。
|
||||
|
||||
---
|
||||
|
||||
## 0. 你需要准备的东西
|
||||
|
||||
- 一台 **Ubuntu**(或同类 Linux)运行项目的机器(下文称「本机」)
|
||||
- 一台可 SSH 登录、且 **能正常访问 Gate.io API** 的 VPS(示例:`HostName` 填你的服务器 IP,用户如 `root`)
|
||||
- SSH:**私钥登录**(推荐,便于隧道脚本无人值守)
|
||||
- 本机已安装:`python3`、`python3-venv`、`pip`、`curl`、`ssh`、`git`(可选)、`node` + `npm`(安装 PM2)
|
||||
|
||||
---
|
||||
|
||||
## 1. 获取代码与目录
|
||||
|
||||
将包含 `app.py` 的项目放到固定目录,例如:
|
||||
|
||||
```bash
|
||||
mkdir -p /opt/crypto_monitor
|
||||
cd /opt/crypto_monitor
|
||||
git clone https://git.bz121.com/dekun/crypto_monitor.git
|
||||
cd crypto_monitor/crypto_monitor_gate_bot
|
||||
```
|
||||
|
||||
下文用 **`/opt/crypto_monitor/crypto_monitor_gate_bot`** 仅为示例,请换成你的实际绝对路径。
|
||||
|
||||
拉取代码后,若目录下尚无 `.env`:
|
||||
|
||||
```bash
|
||||
cp -n .env.example .env
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 2. 配置 SSH 私钥与 `~/.ssh/config`
|
||||
|
||||
```bash
|
||||
mkdir -p ~/.ssh
|
||||
chmod 700 ~/.ssh
|
||||
# 私钥示例:~/.ssh/vps1.pem
|
||||
chmod 600 ~/.ssh/vps1.pem
|
||||
```
|
||||
|
||||
编辑 `~/.ssh/config`(示例别名 **`gate-vps`**,与你手工启动 `ssh -D ... gate-vps` 一致即可):
|
||||
|
||||
```sshconfig
|
||||
Host gate-vps
|
||||
HostName 你的_VPS_IP
|
||||
User root
|
||||
IdentityFile ~/.ssh/vps1.pem
|
||||
IdentitiesOnly yes
|
||||
ServerAliveInterval 30
|
||||
ServerAliveCountMax 3
|
||||
ExitOnForwardFailure yes
|
||||
BatchMode yes
|
||||
```
|
||||
|
||||
测试:
|
||||
|
||||
```bash
|
||||
ssh gate-vps true
|
||||
```
|
||||
|
||||
> 若尚未完全改为密钥登录,可暂时注释 `BatchMode yes`,调试完成后再打开。
|
||||
|
||||
---
|
||||
|
||||
## 3. 手工验证:SSH SOCKS + Gate API
|
||||
|
||||
### 3.1 本地 SOCKS(示例端口 1080)
|
||||
|
||||
```bash
|
||||
ssh -N -D 127.0.0.1:1080 gate-vps
|
||||
```
|
||||
|
||||
保持运行,另开终端继续。
|
||||
|
||||
### 3.2 验证经 SOCKS 可访问 Gate
|
||||
|
||||
```bash
|
||||
curl -4 -sS --max-time 15 --proxy socks5h://127.0.0.1:1080 https://api.gateio.ws/api/v4/spot/time
|
||||
```
|
||||
|
||||
应返回 JSON(含服务器时间字段)。若此处失败,**不要先启动应用**:先修隧道或 VPS 出站。
|
||||
|
||||
---
|
||||
|
||||
## 4. Python 虚拟环境
|
||||
|
||||
```bash
|
||||
cd /opt/crypto_monitor/crypto_monitor_gate_bot
|
||||
|
||||
python3 -m venv .venv
|
||||
source .venv/bin/activate
|
||||
python -m pip install -U pip
|
||||
pip install flask requests ccxt werkzeug PySocks Pillow
|
||||
```
|
||||
|
||||
走 SOCKS 时 **必须** 安装 **`PySocks`**,否则易出现代理相关报错。
|
||||
|
||||
可选:
|
||||
|
||||
```bash
|
||||
export PYTHONDONTWRITEBYTECODE=1
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. 配置环境变量(`.env.example` → `.env`)
|
||||
|
||||
| 文件 | 是否进 Git | 说明 |
|
||||
|------|------------|------|
|
||||
| **`.env.example`** | ✅ 是 | 变量模板与注释,可随 `git pull` 更新 |
|
||||
| **`.env`** | ❌ 否 | 本机真实配置;`app.py` **只读此文件** |
|
||||
|
||||
### 5.1 首次配置
|
||||
|
||||
```bash
|
||||
cd /opt/crypto_monitor/crypto_monitor_gate_bot
|
||||
|
||||
cp -n .env.example .env
|
||||
nano .env
|
||||
```
|
||||
|
||||
### 5.2 备份与 `git pull`
|
||||
|
||||
- **`.env` 不在 Git 中**:`git pull` **不会**覆盖本地 `.env`。
|
||||
- 远端若更新 **`.env.example`**,pull 后请**手动**把新增变量补进你的 `.env`。
|
||||
- **升级前备份**:`cp .env .env.backup.$(date +%Y%m%d)`;恢复:`cp .env.backup.YYYYMMDD .env`。
|
||||
- **换机**:`scp` 复制 `.env`,或新机 `cp .env.example .env` 后重填。
|
||||
|
||||
### 5.3 自动备份(数据库 + 复盘图片)
|
||||
|
||||
每天 **北京时间 0:00** 备份到 **`/root/backups`**,保留 **30 天**(`crypto.db` + `static/images`)。
|
||||
|
||||
```bash
|
||||
cd /opt/crypto_monitor/crypto_monitor_gate_bot
|
||||
chmod +x scripts/backup_data.sh scripts/install_backup_cron.sh
|
||||
bash scripts/install_backup_cron.sh
|
||||
bash scripts/backup_data.sh # 试跑
|
||||
```
|
||||
|
||||
备份目录:`/root/backups/crypto_monitor_gate_bot/YYYY-MM-DD/`。与 Binance / Gate 实例规则相同,详见 `crypto_monitor_binance/部署文档.md` 第 5.3 节(恢复步骤、可选 `.env` 变量)。
|
||||
|
||||
若服务器同时跑 **binance、gate、gate_bot** 三个实例,请在**各自项目目录**各执行一次 `install_backup_cron.sh`。
|
||||
|
||||
### 5.4 必填项检查(Gate + 代理)
|
||||
|
||||
与交易所相关的变量必须是 **Gate** 前缀(**不要**再写 OKX 变量,否则代理不会生效、密钥也不会被识别)。至少确认:
|
||||
|
||||
```env
|
||||
APP_HOST=127.0.0.1
|
||||
APP_PORT=5000
|
||||
|
||||
# 实盘(按需)
|
||||
LIVE_TRADING_ENABLED=false
|
||||
GATE_API_KEY=你的_Key
|
||||
GATE_API_SECRET=你的_Secret
|
||||
|
||||
# 经本机 SSH 动态转发访问 Gate(端口与隧道一致)
|
||||
GATE_SOCKS_PROXY=socks5h://127.0.0.1:1080
|
||||
|
||||
# 若不用 SOCKS,可改用 HTTP 代理(一般二选一)
|
||||
# GATE_HTTP_PROXY=http://127.0.0.1:7890
|
||||
# GATE_HTTPS_PROXY=http://127.0.0.1:7890
|
||||
```
|
||||
|
||||
说明:**推荐 `socks5h://`**,由 SOCKS 端解析域名,与 `curl --proxy socks5h://...` 行为一致。
|
||||
|
||||
### 5.4 趋势回调策略(可选)
|
||||
|
||||
若使用「交易执行」页的 **趋势回调** 计划:
|
||||
|
||||
- 详细规则见项目根目录 **`趋势回调策略说明.md`**。
|
||||
- **两阶段**:先「生成预览」(默认 **120 秒**内有效),再「确认执行」;执行时若可用余额与预览快照偏差超过 **5%** 会拒绝(可调 `.env`)。
|
||||
- 补仓档位数默认 **5**,预览有效期与余额偏差阈值可在 `.env` 覆盖:
|
||||
|
||||
```env
|
||||
TREND_PULLBACK_DCA_LEGS=5
|
||||
TREND_PULLBACK_PREVIEW_TTL_SECONDS=120
|
||||
TREND_PREVIEW_MAX_BALANCE_DRIFT_PCT=5
|
||||
```
|
||||
|
||||
- **生成预览**与**确认执行**时都会读取 **Gate 永续账户 USDT 可用余额**;请尽量使用 **单独子账户** 承载策略资金。
|
||||
|
||||
**界面与对账(与策略说明 3.4–3.5 节一致)**
|
||||
|
||||
- 页顶 **计划历史**:仅 **已结束** 的趋势计划(不含未执行预览);可 **删除** 计划行,并删除 `trend_plan_id` 关联的「趋势回调」`trade_records`(新数据;旧行无 `trend_plan_id` 不级联)。
|
||||
- **运行中计划**展示交易所 **未实现盈亏**(浮盈亏)。
|
||||
- **交易记录**:趋势单在配置 API Key 后,打开「交易执行 / 交易记录」页会按节流(约 **25 秒**内同进程最多一次)拉取 Gate **平仓历史**,回填 **`exchange_realized_pnl`** 等;列表展示优先用交易所口径(见策略说明)。
|
||||
|
||||
**与交易所对齐的可选环境变量**
|
||||
|
||||
```env
|
||||
# 平仓历史同步起点:北京日期 YYYY-MM-DD 的 0 点(与 APP_TIMEZONE 一致);留空则从近 90 天拉取
|
||||
# EXCHANGE_POSITION_SYNC_FROM_BJ=2026-05-14
|
||||
# EXCHANGE_POSITION_HISTORY_LIMIT=200
|
||||
```
|
||||
|
||||
说明:同步 **只读** 交易所接口,**不要求** `LIVE_TRADING_ENABLED=true`;无 Key 时不拉取,界面仍可用(浮盈亏可能为「—」、交易记录仍为本地「估」)。
|
||||
|
||||
**交易记录 CSV**:导出为 **v3**,含 `trend_plan_id` 与交易所对齐列(详见策略说明数据库一节)。
|
||||
|
||||
---
|
||||
|
||||
## 6. 手工启动 Flask(验证)
|
||||
|
||||
1. SOCKS 已监听 `127.0.0.1:1080`
|
||||
2. 已 `source .venv/bin/activate`
|
||||
3. `.env` 已含 `GATE_SOCKS_PROXY`
|
||||
|
||||
```bash
|
||||
cd /opt/crypto_monitor/crypto_monitor_gate_bot
|
||||
source .venv/bin/activate
|
||||
python app.py
|
||||
```
|
||||
|
||||
浏览器访问:`http://127.0.0.1:5000`(或你在 `.env` 中的端口)。
|
||||
|
||||
---
|
||||
|
||||
## 7. 安装 PM2
|
||||
|
||||
```bash
|
||||
sudo npm i -g pm2
|
||||
pm2 -v
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 8. PM2:使用仓库内 `ecosystem.config.cjs`(推荐)
|
||||
|
||||
在项目根目录:
|
||||
|
||||
```bash
|
||||
cd /opt/crypto_monitor/crypto_monitor_gate_bot
|
||||
pm2 start ecosystem.config.cjs
|
||||
pm2 status
|
||||
pm2 logs --lines 200
|
||||
```
|
||||
|
||||
默认只启动 **`crypto-monitor-gate`**(`.venv/bin/python app.py`)。
|
||||
|
||||
### 本机已可直连 Gate、不需要隧道时
|
||||
|
||||
`.env` 里应 **去掉或留空** `GATE_SOCKS_PROXY`(除非仍要走别的代理),再 `pm2 start ecosystem.config.cjs`。
|
||||
|
||||
### 开机自启
|
||||
|
||||
```bash
|
||||
pm2 save
|
||||
pm2 startup
|
||||
# 按屏幕提示执行一条 sudo 命令
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 9. 等价手工命令(不使用 ecosystem 文件时)
|
||||
|
||||
### 9.1 SSH SOCKS(自行后台常驻,不推荐用 PM2)
|
||||
|
||||
示例(前台;实际可用 `screen`/`tmux`/`-f` 后台化或 systemd):
|
||||
|
||||
```bash
|
||||
ssh -N -D 127.0.0.1:1080 gate-vps \
|
||||
-o ServerAliveInterval=30 -o ServerAliveCountMax=3 \
|
||||
-o ExitOnForwardFailure=yes
|
||||
```
|
||||
|
||||
### 9.2 Flask
|
||||
|
||||
```bash
|
||||
cd /opt/crypto_monitor/crypto_monitor_gate_bot
|
||||
pm2 start /opt/crypto_monitor/crypto_monitor_gate_bot/.venv/bin/python --name crypto-monitor-gate -- \
|
||||
/opt/crypto_monitor/crypto_monitor_gate_bot/app.py
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 10. 交易所「连接不上」排查清单
|
||||
|
||||
1. **`.env` 是否为 Gate 变量**:必须是 `GATE_SOCKS_PROXY` / `GATE_API_KEY` / `GATE_API_SECRET`,不是 OKX。
|
||||
2. **隧道是否在本机端口监听**(若配置了 `GATE_SOCKS_PROXY`):
|
||||
```bash
|
||||
ss -lntp | grep 1080 || true
|
||||
```
|
||||
3. **curl 复测 Gate**(与第 3.2 节相同);curl 不通则应用也不会通。
|
||||
4. **PySocks**:`pip show PySocks`,缺失则 `pip install PySocks`。
|
||||
5. **SSH 隧道连不上**:检查私钥权限、`~/.ssh/config`、VPS 出站与端口是否与 `.env` 一致。
|
||||
6. **启动顺序**:先保证 SOCKS 已监听,再 `pm2 start` 应用(或重启应用)。
|
||||
|
||||
---
|
||||
|
||||
## 11. 推荐启动顺序(习惯)
|
||||
|
||||
1. 若走代理:先启动并确认 SSH SOCKS 已监听,再 `curl --proxy socks5h://127.0.0.1:1080 https://api.gateio.ws/api/v4/spot/time` 成功
|
||||
2. `pm2 start ecosystem.config.cjs`
|
||||
3. 再确认页面与余额等接口正常
|
||||
|
||||
---
|
||||
|
||||
## 12. 免责声明
|
||||
|
||||
交易所有合规与地区政策要求。请确保使用方式符合当地法律法规与交易所条款。本文仅描述网络与工程部署路径。
|
||||
|
||||
---
|
||||
|
||||
## 附录:数据库标签修复脚本 `scripts/fix_breakeven_labels.py`
|
||||
|
||||
在 Ubuntu 上:
|
||||
|
||||
1)预览(不写库):
|
||||
|
||||
```bash
|
||||
python scripts/fix_breakeven_labels.py --db ./crypto.db --dry-run
|
||||
```
|
||||
|
||||
2)确认后执行:
|
||||
|
||||
```bash
|
||||
python scripts/fix_breakeven_labels.py --db ./crypto.db --apply
|
||||
```
|
||||
|
||||
默认修复条件:`monitor_type='下单监控'` 且 `result='止损'` 且 `pnl_amount > 0` → 改为 `result='保本止盈'`。
|
||||
Reference in New Issue
Block a user