Commit Graph

98 Commits

Author SHA1 Message Date
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
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
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
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
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
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
root
7d2bb9f392 fix: FR/Liq scoring bugs + compact UI
Backend:
- FR threshold: 0.001→0.0003 (extreme), 0.0003→0.00005 (moderate)
- Liquidation SQL: side='SELL'/'BUY' → 'LONG'/'SHORT' (was never matching!)

Frontend:
- FR/Liq scores now inline in score column (compact)
- Removed bulky green badge buttons from position cards
2026-03-01 23:14:07 +00:00
root
58c72b4d90 fix: V5.2 TP/SL parameters - wider stops for better risk/reward
V5.2: SL=3.0×ATR, TP1=2.0×ATR, TP2=4.5×ATR
V5.1: SL=2.0×ATR, TP1=1.5×ATR, TP2=3.0×ATR (unchanged)

Expected improvement: win/loss ratio 0.72→0.84, breakeven winrate 58%→54%
2026-03-01 23:06:29 +00:00
root
a9c3523a24 feat: independent strategy paper trading controls
- Each strategy has its own position count (max 4 each)
- paper_config.enabled_strategies: per-strategy toggle
- is_strategy_enabled() checks both master switch and strategy list
- API: /api/paper/config now supports enabled_strategies array
- Config auto-loads on API startup

Usage: POST /api/paper/config {enabled_strategies: ['v52_8signals']}
2026-03-01 12:43:46 +00:00
root
778cf8cce1 feat: V5.2 frontend differentiation - strategy tabs, side-by-side scores, visual badges
- Paper page: prominent strategy tabs (全部/V5.1/V5.2) at top
- Paper trades: strategy column with color-coded badges (blue=V5.1, green=V5.2)
- Paper positions: FR/Liq scores displayed prominently for V5.2
- Signals page: side-by-side V5.1 vs V5.2 score comparison cards
- Signals page title updated to 'V5.1 vs V5.2'
- New API endpoint for strategy comparison data
- Layout: local font fallback for build stability
2026-03-01 12:21:19 +00:00
root
f6156a2cfe Add strategy-aware paper trade schema and API endpoints 2026-03-01 11:55:00 +00:00
root
732b01691b Implement V5.2 FR/liquidation scoring and strategy AB loop 2026-03-01 11:54:58 +00:00
root
a7600e8db1 Add V5.2 strategy configuration files 2026-03-01 11:54:54 +00:00
root
2f9dce483c fix: simulate limit orders for TP/SL (match real trading)
- TP/SL now exit at order price (limit order), not market price
- SL exits at sl_price, TP1 at tp1_price, TP2 at tp2_price
- Only timeout and signal_flip use market price (current price)
- Updated fix_historical_pnl.py to also correct exit_price
- This eliminates fake slippage in paper trading stats
2026-03-01 09:40:00 +00:00
root
d351949a7c fix: historical pnl_r correction script 2026-03-01 09:31:13 +00:00
root
45bad25156 fix(P0): pnl_r calculation + cooldown bypass + partition month bug
P0-1: Reverse signal now bypasses cooldown - evaluate_signal always
      outputs direction, main loop checks direction+score>=60 for
      closing positions against trend (not blocked by COOLDOWN_MS)

P0-2: pnl_r unified to (exit-entry)/risk_distance across all exit
      scenarios (tp, sl, sl_be, timeout) in both paper_monitor.py
      and signal_engine.py. Old hardcoded values (1.5R/2.25R) were
      ~2x too high vs actual risk_distance basis.

P1-1: ensure_partitions month calculation fixed from timedelta(30d)
      to proper month arithmetic. Also fixed UTC timezone for
      partition boundaries.

docs: V52-TODO.md with full audit backlog for V5.2
2026-03-01 09:29:32 +00:00
root
9528d69a42 fix: deduplicate recent_large_trades to prevent memory bloat
Each evaluate cycle was re-appending all qualifying trades from
win_fast, causing duplicates. Added seen-set dedup.
2026-03-01 07:58:51 +00:00
root
8b73500d22 feat: migrate auth system from SQLite to PostgreSQL
- auth.py: rewrite to use PG via db.py (was sqlite3)
- admin_cli.py: rewrite to use PG
- migrate_auth_sqlite_to_pg.py: one-time migration script
- SQLite arb.db no longer needed after migration
2026-03-01 07:29:14 +00:00
root
4f54e36d1a feat: dual-write agg_trades to local PG + Cloud SQL
- db.py: add Cloud SQL connection pool (CLOUD_PG_ENABLED env toggle)
- agg_trades_collector: flush_buffer writes to both local and cloud
- Cloud SQL write failure is non-fatal (log warning, don't block local)
2026-03-01 07:16:03 +00:00
root
77d38c7269 fix: add total_pnl to stats API top-level response 2026-03-01 01:42:39 +00:00
root
bda42e669a feat: stats panel with per-symbol tabs (ALL/BTC/ETH/XRP/SOL) - full stats for each coin 2026-03-01 01:37:02 +00:00
root
abfdc63705 feat: liquidation_collector.py - Binance WS forceOrder realtime + 5min aggregation to market_indicators 2026-02-28 15:38:14 +00:00
root
6659c4524c feat: market_data_collector add funding_rate collection for all 4 symbols 2026-02-28 15:34:24 +00:00
root
022ead6d6c feat: paper_trades store score_factors JSONB (direction/crowding/environment/confirmation/auxiliary breakdown) 2026-02-28 13:45:26 +00:00
root
95b45d0f07 fix: skip first 3 cycles after cold start to prevent duplicate paper trades 2026-02-28 12:03:48 +00:00
root
d0e626a437 feat: paper summary add balance + pnl_usdt 2026-02-28 12:00:19 +00:00