Commit Graph

226 Commits

Author SHA1 Message Date
dev-worker
c8c2d801a2 docs: 同步套利引擎私有文档站内容
从private.darkerilclaw.com同步以下文档:
- requirements-v1.3.md: 需求定稿v1.3
- v5-signal-system.mdx: V5信号系统文档
- v2-v4-plan.mdx: V2-V4方案
- funding-rate-arbitrage-plan.md: 资金费率套利方案
- phase0-progress.md: Phase0进度
- index.mdx: 项目首页

后续工作流:git仓库先写 → 再同步到私有文档站
2026-03-03 05:26:27 +00:00
dev-worker
548c7f0e33 scripts: replay v4 - 加COALESCE(calc_version,0)<2防重复跑 2026-03-03 04:51:11 +00:00
dev-worker
809fb60a50 scripts: replay_paper_trades v3修复
- 加status NOT IN (active,tp1_hit)保护活跃单
- 内置fee_r扣除: 2*0.0005*entry/rd
- 解决xiaofan审阅的3个硬问题
2026-03-03 04:50:00 +00:00
dev-worker
af732f1438 scripts: 全量重放paper_trades(真实价口径)
- 用agg_trades真实成交价替换VWAP作为entry_price
- 按时间戳判定事件优先级(非固定优先级)
- TP1后进入半仓状态机,防重复计R
- flip/价格触发tie-break:谁早用谁,同时间优先价格
- 保本价常量: BE_OFFSET_LONG=1.0005, BE_OFFSET_SHORT=0.9995
- 手续费: fee_r = 2*0.0005*entry/risk_distance
- 支持--dry-run模式
2026-03-03 04:47:37 +00:00
dev-worker
262594f16e fix: entry_price改用最新成交价(last_trade_price)而非VWAP
VWAP是30分钟加权均价,快速行情下与实际价偏差可达1-2%
历史587笔entry_price+596笔pnl_r已用agg_trades真实价修正
平均偏差31.9bps, 最大189.4bps(1.9%)
V5.1: 42.18R→15.32R | V5.2: 29.83R→0.76R
2026-03-03 02:53:56 +00:00
dev-worker
66b094c711 fix: 数据新鲜度熔断自动恢复 + 阈值10→30秒(允许启动回灌) 2026-03-02 17:41:23 +00:00
dev-worker
8bd5f8fa84 fix: 模拟盘前端ActivePositions组件自行fetch config计算paper1R 2026-03-02 17:37:03 +00:00
dev-worker
31e6e19ea6 fix: V3全面审阅修复 — 12项问题
P0-1: 风控Fail-Closed(状态文件缺失/过期/异常→拒绝开仓)
P0-2: 1R基准跨模块统一(position_sync+risk_guard从live_config动态读)
P0-3: close_all紧急全平校验返回值+二次验仓
P0-4: Coinbase Premium单位修复(premium_pct/100→比例值)
P1-3: 正向funding计入净PnL(不再只扣负值)
P1-4: 数据新鲜度检查落地(查signal_indicators最新ts)
P1-6: live表DDL补全到SCHEMA_SQL(live_config/live_events/live_trades)
P2-1: _get_risk_usd()加60秒缓存
P2-3: 模拟盘前端*200→从config动态算paper1R
P2-4: XRP/SOL跳过Coinbase Premium采集(无数据源)
P3-2: SQL参数化(fetch_pending_signals用ANY替代f-string)
额外: pnl_r公式修正(gross-fee+funding,funding正负都正确计入)
2026-03-02 17:28:23 +00:00
dev-worker
22df9152f2 fix: testnet环境跳过JWT_SECRET长度检查 2026-03-02 16:25:19 +00:00
dev-worker
18506f2a44 fix: P1/P2/P3剩余6项全部修复
P1-3: 前端持仓USDT从config读riskUsd(不再硬编码*2)
P1-4: 平仓兜底不盲目取最后成交,无明确平仓记录则延后结算
P2-1: LISTEN连接断线自动重建+重新LISTEN
P2-2: 余额风控LOW_BALANCE自动恢复(余额回升则解除暂停)
P2-3: fetch_pending_signals改用asyncio.to_thread避免阻塞事件循环
P3-1: dashboard页面改用新auth体系(authFetch+useAuth+/api/auth/me)
2026-03-02 16:19:03 +00:00
dev-worker
27a51b4d19 fix: P0第二轮修复 — JWT安全/DB密码/SL紧急平仓reduceOnly/TP1状态守卫/超时精度/跨策略去重 + 硬编码消除
P0-1: JWT_SECRET生产环境强制配置,测试环境保留默认
P0-2: DB密码生产环境强制从env读,测试环境保留fallback
P0-3: SL三次失败→查真实持仓→reduceOnly平仓→校验结果→写event
P0-4: TP1后SL重挂失败则不推进tp1_hit状态,continue等下轮重试
P0-5: 超时自动平仓用SYMBOL_QTY_PRECISION格式化+校验结果
P0-6: 同币种去重改为不区分策略(币安单向模式共享净仓位)
P1-1: 手续费窗口entry_ts-200→+200(避免纳入开仓前成交)
额外: 模拟盘*200和实盘*2硬编码→从配置动态读取
2026-03-02 16:11:43 +00:00
root
8694e5cf3a fix: 审阅P1/P2/P3全部修复
P1-1: exit_price按reduceOnly过滤平仓成交+加权平均价
P1-2: 资金费率窗口对齐8h结算周期(0/8/16 UTC),防双累加
P2-2: emergency文件先执行操作+写state再删除,消除TOCTOU
P2-3: API恢复auto_resume排除日限亏损/人工熔断
P2-4: fee汇总窗口从entry_ts-200ms起算(与P1-1一起修)
P3-1: SYMBOL_PRECISION提取到trade_config.py共用模块
P3-2: main.py 4处hardcoded risk_usd=2改为从live_config读
P3-3: CORS限制为arb.zhouyangclaw.com+localhost
P3-4: 三进程加RotatingFileHandler(10MB×5轮转)
2026-03-02 14:04:33 +00:00
root
638589852b fix: P0审阅修复 + P1/P2增强
P0-1: SL挂单失败→重试2次→3次失败紧急市价平仓+写event
P0-2: TP1检测改用DB qty字段(新增)比对仓位减少,不再用orderId
P0-3: emergency-close/block-new/resume/config PUT加admin权限验证
P0-5: risk_guard全平qty按币种精度格式化(BTC:3/ETH:3/XRP:0/SOL:2)

P1-3: NOTIFY收到后立即处理跳过sleep,减少信号延迟
P2-1: 三个进程加DB连接断线重连(ensure_db_conn)

DB: live_trades新增qty字段
2026-03-02 13:56:36 +00:00
root
855df24eba feat: signal-engine配置热加载,改paper_config不需要重启
- 每60轮(~15分钟)自动重读paper_config.json
- 检测enabled_strategies变更时输出日志
- 避免频繁重启导致:
  - 4h历史数据重新回灌
  - CVD/VWAP窗口状态丢失
  - 45秒warmup+10分钟冷却期
  - 信号准确度下降
2026-03-02 13:12:04 +00:00
root
3b6e7d8af3 feat: live_executor从DB读配置,前端改完60秒内生效
- reload_live_config()每60秒从live_config表读取
- RISK_PER_TRADE_USD和MAX_POSITIONS实时更新
- 范总在前端改1R金额→保存→下次开仓自动用新值
- 不需要重启进程
2026-03-02 10:58:31 +00:00
root
b0a463a22c feat: 实盘配置面板 (live_config)
DB:
- live_config表(key/value/label/updated_at)
- 默认7项: 1R金额/初始本金/风险比例/最大持仓/杠杆/策略/环境

API:
- GET /api/live/config — 读取全部配置
- PUT /api/live/config — 批量更新配置

前端:
- L1.5配置面板(L1止血和L2账户之间)
- 标题栏醒目显示 1R = $X.XX
- 7列网格: 图标+标签+值
- 编辑模式: 点编辑→改值→保存/取消
- 各项格式化: $前缀/%后缀/x后缀
2026-03-02 10:55:52 +00:00
root
b1731f0f79 fix: SL/TP挂单用Algo Order API降级
币安2025年底breaking change: STOP_MARKET/TAKE_PROFIT_MARKET
从/fapi/v1/order移到/fapi/v1/algo/order
- 先尝试传统endpoint
- 收到-4120错误码自动切换Algo Order API
- 实盘和测试网都兼容
2026-03-02 10:38:30 +00:00
root
cc1b2c33c1 fix: 测试网STOP_MARKET降级为STOP限价单
测试网不支持STOP_MARKET/TAKE_PROFIT_MARKET order type
检测TRADE_ENV=testnet时自动降级:
- STOP_MARKET → STOP + price + GTC
- TAKE_PROFIT_MARKET → TAKE_PROFIT + price + GTC
实盘(mainnet)不受影响
2026-03-02 10:34:04 +00:00
root
1bf880cebb chore: 三个实盘模块加dotenv加载.env
live_executor/position_sync/risk_guard启动时自动加载backend/.env
确保TRADE_ENV/BINANCE_API_KEY/SECRET_KEY/PG_HOST等环境变量生效
2026-03-02 10:28:39 +00:00
root
22d06efafe feat: 信号新鲜度保护 — 超过2秒弃仓
- execute_entry最前面检查signal_age = now - signal_ts
- >2000ms直接return None,不开仓
- 写warn级别event到live_events(含age_ms和score)
- 正常NOTIFY链路<100ms,超2秒说明有异常
2026-03-02 10:26:22 +00:00
root
d7788d3766 feat: L7事件流 — live_events表+API+前端+三模块事件写入
DB:
- live_events表(id/ts/level/category/symbol/message/detail)

后端:
- GET /api/live/events?limit=30&level=all|critical|warn|info&category=all|trade|risk
- log_live_event() 异步辅助函数

事件源:
- live_executor: 开仓成功写event(trade/info, 含滑点/score)
- position_sync: 平仓写event(trade, 含PnL拆解)
- risk_guard: 熔断/禁仓/恢复写event(risk/critical|warn|info)

前端L7:
- 5秒轮询, 最近30条事件
- 按级别筛选(全部/严重/警告/信息)
- 彩色分类标签(trade绿/risk红/system灰/reconciliation紫)
- 时间线布局, 最大高度56带滚动
2026-03-02 10:22:40 +00:00
root
fb0c3806b5 feat: PnL五项拆解 (gross/fee/funding/slippage/net)
后端:
- /api/live/trades 返回 gross_pnl_r, fee_r, funding_r, slippage_r, net_pnl_r
- gross = 方向盈亏(含TP1半仓锁定)
- fee_r = 实际手续费/risk_usd
- funding_r = 不利资金费/risk_usd
- slippage_r = 滑点损失估算
- net = pnl_r(已是净值)

前端L10:
- 表头改为: Gross | Fee | FR | Slip | Net
- 颜色: gross绿/红, fee橙, FR紫, slip灰, net加粗绿/红
- 每笔交易一目了然:赚了多少、扣了多少、净剩多少
2026-03-02 10:13:51 +00:00
root
ab27e5a4da feat: 资金费率结算追踪 + 平仓PnL用真实手续费
资金费率追踪(position_sync.py):
- 每8小时结算窗口(UTC 0/8/16后5分钟内)查询币安income API
- 按symbol汇总FUNDING_FEE,累加到live_trades.funding_fee_usdt
- 只查最近30分钟内的记录,防止重复计入

平仓PnL改进:
- 从币安userTrades获取真实commission手续费(不再估算)
- PnL拆解: net = gross - fee_r - funding_r
- fee_usdt写入DB
- 日志输出完整拆解: gross/fee/funding/net
2026-03-02 10:10:11 +00:00
root
7e8f83fd5a feat: risk_guard读前端紧急指令并执行
- 每5秒检查/tmp/risk_guard_emergency.json
- close_all: 触发全平熔断
- block_new: 禁止新开仓(warning状态)
- resume: 恢复正常交易
- 读完立即删除文件防重复执行
- 记录操作人+时间
2026-03-02 10:06:50 +00:00
root
fe754cf628 feat: live_executor开仓前检查risk_guard状态+紧急指令
- 读/tmp/risk_guard_state.json: block_new_entries=true → 拒绝开仓
- 读/tmp/risk_guard_state.json: reduce_only=true → 拒绝开仓
- 读/tmp/risk_guard_emergency.json: close_all/block_new → 拒绝开仓
- risk_guard未启动(文件不存在) → 允许交易(不阻塞)
2026-03-02 10:05:45 +00:00
root
cb869926e2 feat: 完整11层实盘页面 + 补充API
前端(/live page.tsx 594行):
- L0: 顶部固定风险条(sticky) - 交易状态/R预算/对账/清算/连亏
- L1: 一键止血区 - 全平/禁新仓/恢复(双重确认)
- L2: 账户概览8卡片 - 权益/保证金/杠杆/今日PnL/总PnL/成本/胜率PF
- L3: 当前持仓(WS实时) - 含清算距离/滑点/裸奔/延迟/OrderID
- L4: 执行质量面板 - 滑点/延迟P50/P95按币种分组
- L5: 对账面板 - 本地vs币安持仓+挂单+差异列表
- L6: 风控状态 - 规则检查+熔断原因+恢复条件
- L8: 实盘vs模拟盘对照 - signal_id匹配+入场差/R差
- L9: 权益曲线+回撤 - 双Area叠加
- L10: 历史交易 - 含成交价/滑点/费用+筛选
- L11: 系统健康 - PM2进程状态+数据新鲜度

后端新增API:
- /api/live/account: 币安账户数据
- /api/live/health: PM2进程+数据新鲜度
- /api/live/reconciliation: 对账(本地vs币安)
- /api/live/execution-quality: 执行质量统计
- /api/live/paper-comparison: 实盘vs模拟盘
2026-03-02 09:38:14 +00:00
root
1ef1f97b5d feat: /live 实盘交易前端页面
- 风控状态面板: 实时显示(正常/警告/熔断)、已实现R+未实现R+合计、连亏次数
- 紧急操作: 全平(双重确认)、禁止开仓、恢复交易
- 总览卡片: 盈亏R+USDT、胜率、持仓数、PF、手续费、资金费
- 当前持仓: WebSocket实时价格、滑点/裸奔/延迟指标、OrderID
- 权益曲线: Recharts AreaChart
- 历史交易: 含成交价/滑点/费用列、币种/盈亏筛选
- 详细统计: 滑点P50/P95/均值、按币种分组
- 导航栏: 新增实盘入口(Bolt图标)

风格与模拟盘一致: 白底+slate+emerald/red配色
2026-03-02 09:28:07 +00:00
root
832f78a1d7 feat: live API endpoints (/api/live/*)
- /api/live/summary: 实盘总览(含风控状态+手续费+资金费)
- /api/live/positions: 当前持仓(含binance_order_id/滑点/裸奔时间/持仓时长)
- /api/live/trades: 历史交易(含成交价/滑点/手续费/资金费)
- /api/live/equity-curve: 权益曲线
- /api/live/stats: 详细统计(含滑点P50/P95/按币种)
- /api/live/risk-status: 风控状态读取
- POST /api/live/emergency-close: 紧急全平
- POST /api/live/block-new: 禁止新开仓
- POST /api/live/resume: 恢复交易
2026-03-02 09:14:05 +00:00
root
b08ea8f772 feat: risk_guard.py - 风控熔断模块
- 单日亏损超限(-5R): 已实现+未实现亏损预估 → 全平+停机
- 连续亏损(5连亏): 暂停开仓1小时,冷却期后自动恢复
- API连接异常(>30秒): 暂停开仓,恢复后自动解除
- 余额不足(<风险×2): 拒绝开仓
- 持仓超时: 45min黄/60min红+10min人工窗口/70min自动平仓
- 写/tmp/risk_guard_state.json供live_executor读取
- 每5秒检查一次,每60秒输出状态日志
2026-03-02 09:06:31 +00:00
root
fab3a3d909 feat: position_sync.py - 仓位对账+SL自动补挂+TP1监控+平仓检测
- 每30秒对账: 本地DB vs 币安持仓/挂单
- SL缺失自动补挂: 立即补→3s复检→再补→2次失败告警
- TP1触发监控: 自动移SL到保本+重挂TP2
- 平仓检测: 币安无仓→查成交记录→计算pnl_r→更新DB
- 清算距离三档告警: 20%黄/12%红/8%紧急
- 幽灵仓位检测: 币安有仓但本地无记录
2026-03-02 09:04:27 +00:00
root
21970038df feat: live_executor.py + signal_engine NOTIFY + live_trades table
- live_executor.py: PG LISTEN/NOTIFY接收信号, 币安API开仓+挂保护单
- signal_engine: 有信号时发NOTIFY new_signal
- live_trades表: 含binance_order_id, slippage_bps, protection_gap_ms等实盘字段
- 支持GCP Secret Manager + env fallback读API Key
- 支持testnet/production环境切换
2026-03-02 08:55:36 +00:00
root
a7dec3fe14 fix: frontend use DB risk_distance + add entry time with ms
- Both paper pages now read risk_distance from API (not hardcoded 2.0*0.7*ATR)
- Added entry timestamp display (精确到毫秒) in active positions
- Applies to both V5.1 and V5.2 paper pages
2026-03-02 07:13:52 +00:00
root
88290882f9 fix: add risk_distance to positions API query
SQL SELECT was missing risk_distance column, causing fallback to
abs(entry-sl) which is wrong after TP1 moves SL to breakeven
2026-03-02 07:08:49 +00:00
root
72ea0ffd0e fix: remove 0.7 ATR multiplier, store risk_distance in DB
- Removed 0.7*ATR multiplier from signal_engine and paper_monitor
- risk_distance now stored per-trade in paper_trades table
- paper_monitor/signal_engine read risk_distance from DB (no hardcoded values)
- V5.1 TP/SL: 2.0/1.5/3.0 -> 1.4/1.05/2.1 (same actual distance)
- V5.2 TP/SL: 3.0/2.0/4.5 -> 2.1/1.4/3.15 (same actual distance)
- Fixed V5.2 pnl_r: all historical values corrected by *2/3
- API unrealized_pnl_r also reads from DB risk_distance
2026-03-02 07:00:05 +00:00
root
02a769f513 fix: FR collector use premiumIndex API for real-time 5min data
- Changed from /fapi/v1/fundingRate (8h settlement only) to /fapi/v1/premiumIndex (real-time)
- Each 5-min poll now stores a new row with current timestamp
- Payload includes both fundingRate and lastFundingRate for compat
- Fixes: FR data was only 6 rows per 4 days, now ~288/day per coin
2026-03-02 04:22:12 +00:00
root
526b6359ca chore: clean up temp files and branches 2026-03-02 03:36:03 +00:00
root
984019d9ab fix: paper latest signals use signal-history with strategy filter + layer scores
- V5.1 paper: signal-history?strategy=v51_baseline
- V5.2 paper: signal-history?strategy=v52_8signals
- API now returns factors in signal-history
- Each signal shows layer score badges below
2026-03-02 03:08:19 +00:00
root
5b704a0a0e feat: paper pages show per-strategy signals with layer scores
- V5.1 paper reads /api/signals/latest?strategy=v51_baseline
- V5.2 paper reads /api/signals/latest?strategy=v52_8signals
- Each coin shows layer score badges (方向/拥挤/环境/确认/辅助)
- V5.2 additionally shows FR and 清算 badges
2026-03-02 02:59:39 +00:00
root
7ebdb98643 feat: store and serve indicators per strategy
- signal_indicators table: added strategy column
- Each strategy gets its own row per cycle
- API /api/signals/latest?strategy=v51_baseline|v52_8signals
- API /api/signals/signal-history?strategy=...
- V5.1 page reads v51_baseline data, V5.2 reads v52_8signals
- Now V5.1 and V5.2 show truly independent scores
2026-03-02 02:52:17 +00:00
root
7dee6bffbd fix: FR score 0~5 only, no negative scores
Direction is already decided. FR layer measures how favorable
the funding rate is for that direction. Unfavorable = 0, not negative.
2026-03-02 02:30:56 +00:00
root
318bcb99a3 fix: clamp direction score to max weight (accel_bonus included) 2026-03-02 01:36:20 +00:00
root
1a45c5725a fix: V5.2 tier labels - no light tier, <75=不开仓, 75-84=标准, ≥85=加仓 2026-03-02 01:12:12 +00:00
root
d4f0bbbcb1 fix: read fundingRate field (not lastFundingRate) from market_indicators
DB stores Binance API response with 'fundingRate' key, but code was
looking for 'lastFundingRate' - always got 0.
2026-03-02 01:07:05 +00:00
root
1fa6f178b6 feat: linear FR scoring based on historical max
- FR score = (|current_FR| / max_historical_FR) × 5, clamped to [-5, +5]
- Contrarian direction: positive score (going against crowded side)
- Same direction: negative score (going with crowded side)
- Historical max FR cached per symbol, refreshed every hour
- Score is float with 2 decimal places
- Total score supports 1 decimal place
2026-03-02 00:56:22 +00:00
root
e405a9c21e fix: parse factors JSON string in /api/signals/latest
psycopg2 writes factors as JSON string, asyncpg reads it back as string
instead of dict. Parse it before returning to frontend.
2026-03-02 00:44:36 +00:00
root
05673c0850 fix: V5.2 scoring uses strategy weights, total capped at 100
Backend:
- Each layer score scaled by strategy config weights
- direction: 0~40, crowding: 0~18, FR: 0~5, environment: 0~12
- confirmation: 0~15, liquidation: 0~5, auxiliary: 0~5
- total_score clamped to 0~100
- factors include max field for frontend

Frontend:
- V5.2 signals page reads max from factors
2026-03-02 00:34:22 +00:00
root
5849bf6522 refactor: completely separate V5.1 and V5.2 pages
- /signals: V5.1 ONLY, 5-layer scoring, no FR/Liq
- /signals-v52: V5.2 ONLY, 7-layer scoring with FR/Liq
- /paper: V5.1 ONLY, no strategy tabs/badges
- /paper-v52: V5.2 ONLY, with FR/Liq display
- Sidebar: V5.1 and V5.2 separate sections
- V5.2 weights: 40+18+5+12+15+5+5=100 (no more >100)
- Zero cross-contamination between V5.1 and V5.2
2026-03-02 00:25:07 +00:00
root
01b1992643 feat: save factors to signal_indicators + show FR/Liq on signals page
- DB: added factors JSONB column to signal_indicators
- Backend: save_indicator now includes factors JSON
- API: /api/signals/latest returns factors field
- Frontend: signals page shows FR and 清算 score bars
2026-03-01 23:57:55 +00:00
root
a571a221e5 feat: gradient liquidation scoring - no threshold, ratio-based
Removed USD threshold gates. Now any liquidation data triggers scoring:
- ratio ≥ 2.0 → +5 (extreme imbalance)
- ratio ≥ 1.5 → +3
- ratio ≥ 1.2 → +1
- ratio 0.8~1.2 → 0 (balanced)
- Inverse for SHORT direction
2026-03-01 23:35:45 +00:00
root
8279b16c68 fix: FR thresholds based on actual data
Historical FR range: BTC ±0.005%, SOL/XRP max -0.022%
Old thresholds were unreachable (0.03% extreme / 0.005% moderate)

New thresholds:
- Extreme: ±0.01% (0.0001) — clearly biased market
- Moderate: ±0.003% (0.00003) — slight bias, reward contrarian
2026-03-01 23:23:43 +00:00