中控增加下单,关键位,系统设置

This commit is contained in:
dekun
2026-05-22 10:32:51 +08:00
parent 6695e72e72
commit 94d4c3b777
7 changed files with 133 additions and 138 deletions
+1 -1
View File
@@ -200,4 +200,4 @@ python hub.py
**5. 局域网里别的电脑打不开中控** **5. 局域网里别的电脑打不开中控**
默认应已可访问:中控监听 `0.0.0.0``HUB_TRUST_LAN` 默认开启。请检查:防火墙是否放行 `HUB_PORT`;浏览器是否使用 **中控机器的局域网 IP**(不要用另一台电脑上的 `127.0.0.1`)。若你曾设置 `HUB_TRUST_LAN=0``HUB_HOST=127.0.0.1`,改回默认或删掉环境变量后重启 hub。 默认应已可访问:中控监听 `0.0.0.0``HUB_TRUST_LAN` 默认开启。请检查:防火墙是否放行 `HUB_PORT`;浏览器是否使用 **中控机器的局域网 IP**(不要用另一台电脑上的 `127.0.0.1`)。若你曾设置 `HUB_TRUST_LAN=0``HUB_HOST=127.0.0.1`,改回默认或删掉环境变量后重启 hub。
**Linux 常驻(PM2**`python3 -m venv .venv``source .venv/bin/activate``pip install -r requirements.txt``pm2 start ecosystem.config.cjs`(进程名 `manual-trading-hub`)。PM2 经 `run_hub.sh` 使用 `.venv/bin/python`,启动前不必保持 shell 处于 activate 状态。详见 **《部署文档.md》**。 **Linux 常驻(PM2**`pm2 start ecosystem.config.cjs` 会**同时**启动 4 路子代理 + 中控 `manual-trading-hub`。详见 **《部署文档.md》**。
+5 -39
View File
@@ -1,45 +1,11 @@
/** /**
* PM2:四路子代理 agent.py(可选,与中控分开管理 * 仅子代理(一般不单独用;默认请 pm2 start ecosystem.config.cjs 一次起 hub+agent
* *
* 每个 app 在对应 crypto_monitor_* 目录启动,以加载该目录 .env 中的 API 密钥。 * 若只想重启子代理、不动中控:
* Python 解释器使用 manual_trading_hub/.venv(须已 pip install -r requirements.txt)。 * pm2 restart manual-agent-binance manual-agent-gate ...
*
* 启动:
* pm2 start ecosystem.agents.config.cjs
*
* 仅启动部分账户可编辑下方 apps 数组,或:
* pm2 start ecosystem.agents.config.cjs --only manual-agent-binance
*/ */
const path = require("path"); const main = require("./ecosystem.config.cjs");
const HUB_DIR = __dirname;
const REPO_ROOT = path.join(HUB_DIR, "..");
const PY = path.join(HUB_DIR, ".venv", "bin", "python");
const AGENT = path.join(HUB_DIR, "agent.py");
function agentApp(name, exchangeDir, exchange, port) {
return {
name,
cwd: path.join(REPO_ROOT, exchangeDir),
script: AGENT,
interpreter: PY,
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: "400M",
env: {
EXCHANGE: exchange,
PORT: String(port),
HOST: "127.0.0.1",
},
};
}
module.exports = { module.exports = {
apps: [ apps: main.apps.filter((a) => String(a.name).startsWith("manual-agent-")),
agentApp("manual-agent-binance", "crypto_monitor_binance", "binance", 15200),
agentApp("manual-agent-okx", "crypto_monitor_okx", "okx", 15201),
agentApp("manual-agent-gate", "crypto_monitor_gate", "gate", 15202),
agentApp("manual-agent-gate-bot", "crypto_monitor_gate_bot", "gate", 15203),
],
}; };
+32 -11
View File
@@ -1,28 +1,52 @@
/** /**
* PM2多账户交易中控 hub.pyUbuntu / Linux * PM2:中控 hub + 四路子代理 agent(一次启动全部
* *
* 前置: * 前置:
* cd manual_trading_hub * cd manual_trading_hub
* cp .env.example .env # 按需修改 HUB_PORT、HUB_BRIDGE_TOKEN 等 * source .venv/bin/activate && pip install -r requirements.txt
* python3 -m venv .venv && .venv/bin/pip install -r requirements.txt * cp .env.example .env
* *
* 启动: * 启动hub + 全部 agent
* pm2 start ecosystem.config.cjs * pm2 start ecosystem.config.cjs
* pm2 save && pm2 startup * pm2 save && pm2 startup
* *
* 常用: * 仅中控:pm2 start ecosystem.config.cjs --only manual-trading-hub
* pm2 logs manual-trading-hub * 仅某 agentpm2 start ecosystem.config.cjs --only manual-agent-binance
* pm2 restart manual-trading-hub
* *
* 子代理(agent)见 ecosystem.agents.config.cjs 或 scripts/后台运行-Ubuntu.md * 快捷:bash scripts/pm2_hub.sh start
*/ */
const path = require("path"); const path = require("path");
const HUB_DIR = __dirname; const HUB_DIR = __dirname;
const REPO_ROOT = path.join(HUB_DIR, "..");
const PY = path.join(HUB_DIR, ".venv", "bin", "python");
const AGENT = path.join(HUB_DIR, "agent.py");
const RUN_SH = path.join(HUB_DIR, "scripts", "run_hub.sh"); const RUN_SH = path.join(HUB_DIR, "scripts", "run_hub.sh");
function agentApp(name, exchangeDir, exchange, port) {
return {
name,
cwd: path.join(REPO_ROOT, exchangeDir),
script: AGENT,
interpreter: PY,
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: "400M",
env: {
EXCHANGE: exchange,
PORT: String(port),
HOST: "127.0.0.1",
},
};
}
module.exports = { module.exports = {
apps: [ apps: [
agentApp("manual-agent-binance", "crypto_monitor_binance", "binance", 15200),
agentApp("manual-agent-okx", "crypto_monitor_okx", "okx", 15201),
agentApp("manual-agent-gate", "crypto_monitor_gate", "gate", 15202),
agentApp("manual-agent-gate-bot", "crypto_monitor_gate_bot", "gate", 15203),
{ {
name: "manual-trading-hub", name: "manual-trading-hub",
cwd: HUB_DIR, cwd: HUB_DIR,
@@ -32,9 +56,6 @@ module.exports = {
autorestart: true, autorestart: true,
watch: false, watch: false,
max_memory_restart: "512M", max_memory_restart: "512M",
// 环境变量优先写在 manual_trading_hub/.env(由 run_hub.sh 加载)
// 也可在此覆盖,例如:
// env: { HUB_PORT: "5100", HUB_DISABLED_IDS: "1" },
}, },
], ],
}; };
+4 -7
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# 子代理 PM2 快捷脚本 # 子代理 PM2(中控请用 scripts/pm2_hub.sh 或 ecosystem.config.cjs 一次起全部)
set -euo pipefail set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -10,12 +10,9 @@ usage() {
cat <<'EOF' cat <<'EOF'
用法: bash scripts/pm2_agents.sh <start|stop|restart|status|logs|delete> 用法: bash scripts/pm2_agents.sh <start|stop|restart|status|logs|delete>
start 启动 ecosystem.agents.config.cjs 中全部子代理 一般请用: bash scripts/pm2_hub.sh start hub + agent 一起)
stop 停止全部 manual-agent-*
restart 重启全部 本脚本仅操作 4 路子代理(不含中控)
status pm2 status
logs 全部子代理日志
delete 从 PM2 移除
仅启动币安: pm2 start ecosystem.agents.config.cjs --only manual-agent-binance 仅启动币安: pm2 start ecosystem.agents.config.cjs --only manual-agent-binance
EOF EOF
+38 -16
View File
@@ -1,24 +1,32 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# 中控 PM2 快捷脚本(在 manual_trading_hub 目录或任意路径执行均可) # 中控 + 子代理 统一 PM2 快捷脚本
set -euo pipefail set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HUB_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" HUB_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
ECO="${HUB_DIR}/ecosystem.config.cjs" ECO="${HUB_DIR}/ecosystem.config.cjs"
# 与 ecosystem.config.cjs 中 name 一致
PM2_NAMES=(
manual-agent-binance
manual-agent-okx
manual-agent-gate
manual-agent-gate-bot
manual-trading-hub
)
usage() { usage() {
cat <<'EOF' cat <<'EOF'
用法: bash scripts/pm2_hub.sh <start|stop|restart|status|logs|delete> 用法: bash scripts/pm2_hub.sh <start|stop|restart|status|logs|delete>
start 启动 manual-trading-hub已存在则 pm2 restart start 启动 ecosystem.config.cjs4 路子代理 + 中控,已存在则 restart 全部
stop 停止 stop 停止全部
restart 重启 restart 重启全部
status pm2 status status pm2 status
logs 跟踪日志(Ctrl+C 退出) logs 全部相关进程日志
delete 从 PM2 列表移除 delete 从 PM2 列表移除全部
前置: npm i -g pm2manual_trading_hub 下已建 .venv 并 pip install 仅中控: pm2 start ecosystem.config.cjs --only manual-trading-hub
可选: cp .env.example .env
EOF EOF
} }
@@ -36,31 +44,45 @@ fi
cd "${HUB_DIR}" cd "${HUB_DIR}"
_any_running() {
local n
for n in "${PM2_NAMES[@]}"; do
if pm2 describe "${n}" >/dev/null 2>&1; then
return 0
fi
done
return 1
}
case "${cmd}" in case "${cmd}" in
start) start)
if pm2 describe manual-trading-hub >/dev/null 2>&1; then if _any_running; then
pm2 restart manual-trading-hub pm2 restart "${ECO}"
echo "已重启 manual-trading-hub" echo "已重启hub + 全部 agent"
else else
pm2 start "${ECO}" pm2 start "${ECO}"
echo "已启动 manual-trading-hub" echo "已启动hub + 全部 agent(共 ${#PM2_NAMES[@]} 个进程)"
fi fi
pm2 save 2>/dev/null || true pm2 save 2>/dev/null || true
;; ;;
stop) stop)
pm2 stop manual-trading-hub 2>/dev/null || echo "进程未在运行" pm2 stop "${PM2_NAMES[@]}" 2>/dev/null || echo "部分或全部进程未在运行"
;; ;;
restart) restart)
pm2 restart manual-trading-hub 2>/dev/null || pm2 start "${ECO}" if _any_running; then
pm2 restart "${ECO}"
else
pm2 start "${ECO}"
fi
;; ;;
status) status)
pm2 status pm2 status
;; ;;
logs) logs)
pm2 logs manual-trading-hub --lines 200 pm2 logs "${PM2_NAMES[@]}" --lines 100
;; ;;
delete) delete)
pm2 delete manual-trading-hub 2>/dev/null || echo "进程不存在" pm2 delete "${PM2_NAMES[@]}" 2>/dev/null || echo "部分或全部进程不存在"
;; ;;
*) *)
usage usage
+2 -2
View File
@@ -330,11 +330,11 @@ python3 -m venv .venv
source .venv/bin/activate source .venv/bin/activate
pip install -r requirements.txt pip install -r requirements.txt
cp .env.example .env cp .env.example .env
pm2 start ecosystem.config.cjs # 进程名 manual-trading-hubPM2 用 .venv/bin/python,无需保持 activate pm2 start ecosystem.config.cjs # 一次启动 4 个 agent + manual-trading-hub
pm2 save && pm2 startup pm2 save && pm2 startup
``` ```
子代理可选:`pm2 start ecosystem.agents.config.cjs`。快捷命令`bash scripts/pm2_hub.sh start|restart|logs` 快捷`bash scripts/pm2_hub.sh start|restart|logs`(同样 hub+agent 一起)
更细的安装顺序、反代、验收见 **《部署文档.md》**screen / systemd 见 **scripts/** 更细的安装顺序、反代、验收见 **《部署文档.md》**screen / systemd 见 **scripts/**
+51 -62
View File
@@ -52,51 +52,75 @@ deactivate # 可选;交给 PM2 时不必保持激活
## 四、推荐启动顺序 ## 四、推荐启动顺序
``` ```
1. 子代理 agent1520015203 ← PM2 或 screen 1. 各实例 FlaskAPP_PORT ← 各 crypto_monitor_* 目录 ecosystem.config.cjs
2. 各实例 FlaskAPP_PORT 目录 ecosystem.config.cjs 2. 中控 + 子代理(5100 + 1520015203目录一条 PM2 命令同时启动
3. 中控 hub5100 ← 本目录 ecosystem.config.cjs
``` ```
**`ecosystem.config.cjs` 会一次拉起 4 个 agent + 1 个 hub**,无需再单独 `pm2 start` 子代理。
仅反代中控到公网时:Flask / agent 仍只监听 **127.0.0.1**;系统设置里 URL 填 `http://127.0.0.1:端口` 仅反代中控到公网时:Flask / agent 仍只监听 **127.0.0.1**;系统设置里 URL 填 `http://127.0.0.1:端口`
--- ---
## 五、PM2 托管中控(推荐) ## 五、PM2 托管(hub + agent 一起启动,推荐)
### 5.1 进程名与文件 ### 5.1 一条命令启动全部
| 文件 | PM2 进程名 | 说明 | | 文件 | 包含进程 |
|------|------------|------| |------|----------|
| `ecosystem.config.cjs` | `manual-trading-hub` | 仅中控 | | `ecosystem.config.cjs` | `manual-agent-binance` / `okx` / `gate` / `gate-bot` + **`manual-trading-hub`** |
| `ecosystem.agents.config.cjs` | `manual-agent-binance` 等 4 个 | 可选,子代理 |
`run_hub.sh` 加载 **`manual_trading_hub/.env`** 执行 `hub.py` `run_hub.sh` 加载 **`manual_trading_hub/.env`** 执行 `hub.py`;各 agent 在对应策略目录启动以读取该目录 **`.env`** 中的 API 密钥
### 5.2 启动中控
```bash ```bash
cd /opt/crypto_monitor/manual_trading_hub cd /opt/crypto_monitor/manual_trading_hub
source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
# 方式 A:直接 PM2 pm2 start ecosystem.config.cjs # 5 个进程一起起
pm2 start ecosystem.config.cjs
pm2 save pm2 save
# 方式 B:快捷脚本 #
bash scripts/pm2_hub.sh start bash scripts/pm2_hub.sh start
``` ```
### 5.2 PM2 进程一览
| 进程名 | 工作目录 | 端口/说明 |
|--------|----------|-----------|
| manual-agent-binance | crypto_monitor_binance | agent `15200` |
| manual-agent-okx | crypto_monitor_okx | agent `15201` |
| manual-agent-gate | crypto_monitor_gate | agent `15202` |
| manual-agent-gate-bot | crypto_monitor_gate_bot | agent `15203` |
| manual-trading-hub | manual_trading_hub | hub `5100` |
OKX 子代理会启动,但中控默认 `HUB_DISABLED_IDS=1` 不参与监控;不用 OKX 可 `pm2 stop manual-agent-okx`
### 5.3 常用运维命令 ### 5.3 常用运维命令
```bash ```bash
pm2 status pm2 status
pm2 logs manual-trading-hub --lines 200 pm2 logs manual-trading-hub --lines 200
pm2 restart manual-trading-hub pm2 restart ecosystem.config.cjs # 重启 hub + 全部 agent
pm2 stop manual-trading-hub
# bash scripts/pm2_hub.sh restart # 同上
bash scripts/pm2_hub.sh status bash scripts/pm2_hub.sh stop
bash scripts/pm2_hub.sh logs bash scripts/pm2_hub.sh logs
bash scripts/pm2_hub.sh restart ```
仅重启中控、不动 agent
```bash
pm2 restart manual-trading-hub
```
仅重启子代理:
```bash
pm2 restart manual-agent-binance manual-agent-gate manual-agent-gate-bot
# 或
bash scripts/pm2_agents.sh restart
``` ```
### 5.4 开机自启 ### 5.4 开机自启
@@ -107,59 +131,26 @@ pm2 startup
# 按终端提示执行一行 sudo 命令后,再 pm2 save # 按终端提示执行一行 sudo 命令后,再 pm2 save
``` ```
### 5.5 可选:PM2 托管四路子代理 ### 5.5 与各实例 Flask 一起查看
```bash
cd /opt/crypto_monitor/manual_trading_hub
pm2 start ecosystem.agents.config.cjs
# 仅币安:
# pm2 start ecosystem.agents.config.cjs --only manual-agent-binance
bash scripts/pm2_agents.sh start # 等价启动全部
pm2 save
```
| 进程名 | 工作目录 | PORT |
|--------|----------|------|
| manual-agent-binance | crypto_monitor_binance | 15200 |
| manual-agent-okx | crypto_monitor_okx | 15201 |
| manual-agent-gate | crypto_monitor_gate | 15202 |
| manual-agent-gate-bot | crypto_monitor_gate_bot | 15203 |
OKX 默认被 `HUB_DISABLED_IDS=1` 关闭监控,但子代理仍可启动备用。
### 5.6 与各实例 Flask 一起查看
```bash ```bash
pm2 status pm2 status
# 示例同时存在: # 示例同时存在:
# manual-trading-hub # manual-trading-hub、manual-agent-*
# manual-agent-binance # crypto_binance / crypto_gate …(各策略目录自有 ecosystem.config.cjs
# crypto_binance (各目录自有 ecosystem.config.cjs
``` ```
--- ---
## 六、手动启动(不用 PM2 时) ## 六、手动启动(不用 PM2 时)
### 6.1 子代理(每账户一个终端或 screen) 需**分别**起 agent 与 hub(与 PM2 合并启动不同):
```bash
cd /opt/crypto_monitor/crypto_monitor_binance
export EXCHANGE=binance PORT=15200 HOST=127.0.0.1
/opt/crypto_monitor/manual_trading_hub/.venv/bin/python \
/opt/crypto_monitor/manual_trading_hub/agent.py
```
其余端口见上表;也可用 `scripts/start_agents_3screen.sh`(不含 OKX)。
### 6.2 中控
```bash ```bash
# 子代理:scripts/start_agents_3screen.sh 或每目录手动 agent.py
# 中控:
cd /opt/crypto_monitor/manual_trading_hub cd /opt/crypto_monitor/manual_trading_hub
bash scripts/run_hub.sh bash scripts/run_hub.sh
# 或
bash scripts/start_hub_screen.sh # screen 会话 mt-hub
``` ```
--- ---
@@ -211,9 +202,7 @@ cd /opt/crypto_monitor
git pull git pull
cd manual_trading_hub cd manual_trading_hub
.venv/bin/pip install -r requirements.txt .venv/bin/pip install -r requirements.txt
pm2 restart manual-trading-hub pm2 restart ecosystem.config.cjs
# 若改了 agent 依赖:
pm2 restart manual-agent-binance manual-agent-okx manual-agent-gate manual-agent-gate-bot
``` ```
- **`hub_settings.json`**、**`.env`** 不在 Git 中,`git pull` 不会覆盖。 - **`hub_settings.json`**、**`.env`** 不在 Git 中,`git pull` 不会覆盖。