Files
crypto_monitor_user/策略交易说明.md
T
dekun 53863559f4 Initialize crypto_monitor_user (user edition) from monitor codebase.
Retarget git remote, install path, and deploy docs from crypto_monitor to crypto_monitor_user.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-17 16:18:13 +08:00

8.6 KiB
Raw Blame History

策略交易说明

本文档说明仓库根目录 共用策略逻辑 与三个 crypto_monitor_* 实例中的 策略交易 入口(顶栏「策略交易」,页内子 Tab:趋势回调 / 顺势加仓).


一,架构(精简共用)

strategy_trend_lib.py      # 趋势回调:网格价,补仓拆分,边界校验(纯计算)
strategy_roll_lib.py         # 顺势加仓:总风险反推,斐波限价,最多 3 腿(纯计算)
strategy_db.py               # roll_groups / roll_legs 表结构
strategy_config.py           # 各所 app → 统一回调配置(交易所 API)
strategy_register.py         # Flask POST:/strategy/roll/preview,/strategy/roll/execute
strategy_ui.py               # 主站 index 页数据(滚仓组,持仓列表等)
strategy_exchange_*.py       # 适配器说明(实际下单仍走各所 app 的 ccxt)
strategy_templates/          # strategy_trading_page.html(双栏),trend/roll 面板,records 页
strategy_snapshot_lib.py     # 结束快照表 strategy_trade_snapshots(最近 100 条)
strategy_records_register.py # /strategy/records 路由与列表数据
层级 职责
lib 不算 ccxt,不写库
config place_exchange_order,replace_active_monitor_tpsl_on_exchange 等接到统一 cfg
各所 app .env,DB,init_db,PM2,微信,监控轮询

部署时各实例 PYTHONPATH 需包含仓库根目录(ecosystem.config.cjsPYTHONPATH=..).


二,导航与页面

顶栏:策略交易/strategy(趋势回调 | 顺势加仓 左右并列);策略交易记录/strategy/records(已结束快照).旧链接 /strategy/trend,/strategy/roll 会跳转到 /strategy.

区域 说明
左栏 · 趋势回调 三所均可(预览,执行,自动补仓,程序止盈);运行中计划卡含 补仓计划明细
右栏 · 顺势加仓 须已有同向持仓;滚仓组/历史表在右栏内滚动
策略交易记录 趋势回调 / 顺势加仓 分两栏;每条约一行摘要,点击展开详情;库内保留最近 100
/trade 实盘下单

各所 app.py 注册 @app.route("/strategy/trend|roll")render_main_page(...);install_strategy_trading 仅注册滚仓 POST API.


三,趋势回调

  • 位置:各所顶栏 策略交易 → 趋势回调(共用 strategy_trend_register.py + 各所交易所 API).
  • 行为:与《docs/trend-pullback-strategy.md》一致——预览 → 确认执行 → 首仓 50% + 交易所止损 + 多档 自动 市价补仓 + 程序监控止盈.
  • 共用代码:parse_and_compute_trend_pullback_plan 中网格/拆档已改为调用 strategy_trend_lib.
  • 互斥:与「机器人下单监控」持仓上限,运行中趋势计划互斥(逻辑未改).

各所使用自己的 API 密钥与 crypto.db,互不影响.


四,顺势加仓(滚仓,仅人工)

详细说明(计仓公式,四种方式,程序监控,生命周期):仓库 顺势加仓滚仓说明.md;各实例策略页 /strategy/roll/docs 可在线阅读.

4.1 原则

  • 禁止自动加仓;仅页面按钮「执行滚仓」或挂限价单(无价格穿越自动下单).
  • 全币种(与各所合约列表一致).
  • 止盈:全程使用 首仓 order_monitors.take_profit,滚仓不改止盈.
  • 止损:每次人工填写 新统一止损;成交后调用各所 先撤后挂 TP/SL(止盈仍为首仓).
  • 总风险%:按「合并持仓 + 新止损」反推本次加仓张数,使触及新止损时亏损约 账户基数 × 风险%(默认 2%,可在表单修改).
  • 做多最多滚仓 3 次(首仓不计入,仅计 roll_legs 已成交次数);做空默认同样 3 次(见 strategy_roll_lib.ROLL_MAX_LEGS_SHORT).

4.2 斐波限价

  • 填写 上沿 H,下沿 L(H > L),仅用于计算限价加仓价(与 fib_key_monitor_lib.calc_fib_planentry 一致).
  • 做多:下沿 = 结构止损侧;做空:上沿 = 结构止损侧.
  • 可选 0.6180.786;与关键位自动单的 TP(H/L 对侧)不同,滚仓 TP 锁定首仓.

4.3 前置条件

  1. 实盘下单 已有同 symbol,同方向 active order_monitors.
  2. 交易所有同向持仓(读 get_live_position_contracts).
  3. active trend_pullback_plans(与趋势回调互斥).

4.4 数据表(各所 crypto.db)

  • roll_groups:绑定 order_monitor_id,首仓 TP/SL,当前 SL,已滚仓次数.
  • roll_legs:每腿方式(市价 / 斐波0.618 / 斐波0.786),张数,新 SL,状态(filled / pending).

init_db() 时自动 CREATE TABLE IF NOT EXISTS(strategy_db.init_strategy_tables).

4.5 操作步骤

  1. 打开顶栏 策略交易 /strategy,在 右栏·顺势加仓 操作.
  2. 选择持仓币种,方向,加仓方式,填写 H/L(斐波时),新统一止损,总风险%.
  3. 点击 执行滚仓(市价立即加仓并更新止损;限价则挂委托,成交后需再处理止损——当前版本限价 pending 后提示手动同步).
  4. 查看页底 滚仓腿历史.

可选:对表单字段 POST /strategy/roll/preview(JSON)查看 strategy_roll_lib.preview_roll 结果.


五,策略交易记录(三所统一)

  • 入口:顶栏 策略交易记录/strategy/records(strategy_records_register.register_strategy_records).
  • 写入时机:趋势计划结束(止盈 / 止损 / 手动结束),保本移交,顺势加仓组结案时,写入表 strategy_trade_snapshots(strategy_snapshot_lib).
  • 与交易记录区别:策略记录写 strategy_trade_snapshots;顶栏 交易记录与复盘trade_records.中控手动结束计划时 两者都应写入(详见 docs/trend-hub-close-and-trade-records.md).
  • 保留条数:每次写入后自动修剪,仅保留按 closed_at 倒序 的最近 100 条.
  • 页面布局:
    • 左栏卡片:趋势回调记录;右栏卡片:顺势加仓记录.
    • 每条默认 一行简略(品种,方向,结果,盈亏,补仓进度,结束时间);点击行展开均价/止损/止盈/补仓档位表或滚仓腿表.
    • 筛选:币种,时间排序(最新/最早),芯片 盈利 / 亏损 / 未补仓 / 补仓(前端过滤,数据来自服务端 enrich 字段 filter_pnl,dca_tag,dca_done).
  • 共用模板:strategy_templates/strategy_records_page.html(三所 index.html include).

六,中控全屏 · 趋势回调展示

各所 Flask 经 hub_bridge + enrich_trend_plan_for_hub 向中控提供 active 计划(含 dca_levels).在 manual_trading_hub 全屏 趋势回调 区,单所通常仅 一仓,计划卡为 横向两列(与实例字段一致,操作在实例完成):

区域 内容
顶栏 #ID 品种,方向徽章,结束计划(SSO 打开实例并确认)
左列 来源/风险/补仓边界/已补仓;均价,止损,止盈,盈亏比,标记价,浮盈亏(% 按 计划保证金)
右列 补仓计划明细 表(首仓 + 各档;未成交显示 待补仓)
底栏 保本移交(偏移%,跳转实例策略页),快照可用 / 计划保证金 / 杠杆

静态资源版本见 manual_trading_hub/static/index.htmlapp.js / app.css?v= 参数;改 UI 后请 强刷 中控页.


七,升级与重启

cd /opt/crypto_monitor_user
git pull
pm2 restart crypto-monitor-binance crypto-monitor-okx crypto-monitor-gate manual-trading-hub
pm2 save

部署约定:Ubuntu + root + /opt/crypto_monitor_user + PM2docs/ubuntu-server.md.


八,相关文档

文档 内容
docs/trend-pullback-strategy.md 趋势回调细则(三所共用逻辑)
AI复盘与模型配置说明.md 复盘页 AI(与策略无关)
manual_trading_hub/使用说明.md 中控监控,全屏趋势卡两列布局
docs/trend-hub-close-and-trade-records.md 中控平仓,交易记录写入,补仓展示统一,漏记补录
docs/hub-symbol-archive-kline.md 币种档案,永久 5m K 线,建档与 4h 增量同步
docs/ubuntu-server.md Ubuntu / root /opt / PM2 部署
fib_key_monitor_lib.py 斐波公式共用

九,后续可增强(未实现)

  • 滚仓页内嵌预览按钮,限价成交后一键同步止损.
  • 策略交易记录按 UTC 时间窗与顶栏「列表筛选」联动.