mindesbunister
aa16daffa2
critical: Fix Bug #87 - Add 3-tier SL verification with circuit breaker
...
CRITICAL FIX: Prevents silent stop-loss placement failures that caused $1,000+ losses
Created lib/safety/sl-verification.ts (334 lines):
60s → 90s delays
- Queries Drift protocol directly via user.getOpenOrders()
- Filters SL orders: marketIndex + reduceOnly + TRIGGER_MARKET/LIMIT
- Circuit breaker: haltTrading() blocks new trades on verification failure
- Emergency shutdown: Force-closes position after 3 failed attempts
- Event-driven architecture: Triggered once post-open (not polling)
- Reduces Drift API calls by ~95% vs continuous polling
Integrated in app/api/trading/execute/route.ts:
- Line 54: Import shouldAcceptNewTrade for pre-execution check
- Lines 215-221: Circuit breaker validates trading allowed (HTTP 503 if halted)
- Lines 583-592: Triggers SL verification post-open (fire-and-forget)
Root Cause - Bug #76 : Silent SL placement failure
Database Evidence: Trade cmj8abpjo00w8o407m3fndmx0
- tp1OrderTx: 'DsRv7E8vtAS4dKFmoQoTZMdiLTUju9cfmr9DPCgquP3V...' ✅ EXISTS
- tp2OrderTx: '3cmYgGE828hZAhpepShXmpxqCTACFvXijqEjEzoed5PG...' ✅ EXISTS
- slOrderTx: NULL ❌
- softStopOrderTx: NULL ❌
- hardStopOrderTx: NULL ❌
User Report: 'RISK MANAGEMENT WAS REMOVED WHEN PRICE WENT TO SL!!!!! POSITION STILL OPEN'
Reality: SL orders never placed from start (not cancelled later)
Solution Philosophy: 'better safe than sorry' - user's words
Safety: Query on-chain state directly, don't trust internal success flags
Deployed: 2025-12-16 13:50:18 UTC
Docker Image: SHA256:80fd45004e71fa490fc4f472b252ecb25db91c6d90948de1516646b12a00446f
Container: trading-bot-v4 restarted successfully
2025-12-16 14:50:18 +01:00
mindesbunister
a8c1b2ca06
feat: Phase 2 Smart Entry Timing - COMPLETE
...
Implementation of 1-minute data enhancements Phase 2:
- Queue signals when price not at favorable pullback level
- Monitor every 15s for 0.15-0.5% pullback (LONG=dip, SHORT=bounce)
- Validate ADX hasn't dropped >2 points (trend still strong)
- Timeout at 2 minutes → execute at current price
- Expected improvement: 0.2-0.5% per trade = ,600-4,000 over 100 trades
Files:
- lib/trading/smart-entry-timer.ts (616 lines, zero TS errors)
- app/api/trading/execute/route.ts (integrated smart entry check)
- .env (SMART_ENTRY_* configuration, disabled by default)
Next steps:
- Test with SMART_ENTRY_ENABLED=true in development
- Monitor first 5-10 trades for improvement verification
- Enable in production after successful testing
2025-11-27 11:40:23 +01:00
mindesbunister
bfdb0ba779
feat: Implement adaptive leverage based on signal quality score
...
- Quality-based risk adjustment: 95+ = 15x, 90-94 = 10x, <90 = blocked
- Data-driven decision: v8 quality 95+ = 100% WR (4/4 wins)
- Config fields: useAdaptiveLeverage, highQualityLeverage, lowQualityLeverage, qualityLeverageThreshold
- Helper function: getLeverageForQualityScore() returns appropriate leverage tier
- Position sizing: Modified getActualPositionSizeForSymbol() to accept optional qualityScore param
- Execute endpoint: Calculate quality score early (before sizing) for leverage determination
- Test endpoint: Uses quality 100 for maximum leverage on manual test trades
- ENV variables: USE_ADAPTIVE_LEVERAGE, HIGH_QUALITY_LEVERAGE, LOW_QUALITY_LEVERAGE, QUALITY_LEVERAGE_THRESHOLD
- Impact: 33% less exposure on borderline quality signals (90-94)
- Example: $540 × 10x = $5,400 vs $8,100 (saves $2,700 exposure on volatile signals)
- Files changed:
* config/trading.ts (interface, config, ENV, helper function, position sizing)
* app/api/trading/execute/route.ts (early quality calculation, pass to sizing)
* app/api/trading/test/route.ts (quality 100 for test trades)
2025-11-24 00:47:09 +01:00