Commit Graph

3 Commits

Author SHA1 Message Date
mindesbunister
a2f32eceac debug: Add logging to trace instrumentation execution 2025-12-05 15:38:31 +01:00
mindesbunister
66c3c42547 feat: Add automated database sync validator for ghost position detection
PROBLEM:
- Analytics page showed 3 open trades when only 1 actually open on Drift
- Ghost positions in database (realizedPnL set but exitReason = NULL)
- Happens when on-chain orders fill but database update fails
- Manual cleanup required = unreliable dashboard

SOLUTION: Automated Database Sync Validator
1. Runs every 10 minutes (independent of Position Manager)
2. Validates ALL 'open' database trades against actual Drift positions
3. Auto-fixes ghost positions (marks as closed with exitReason)
4. Provides manual validation endpoint: GET /api/admin/validate-db

FEATURES:
- Detects ghost positions (DB open, Drift closed)
- Detects orphan positions (DB closed, Drift open)
- Provides detailed validation reports
- Runs on server startup + periodic intervals
- Zero manual intervention required

FILES:
- lib/database/sync-validator.ts: Core validation logic
- app/api/admin/validate-db/route.ts: Manual validation endpoint
- instrumentation.ts: Auto-start on server initialization

RESULT: Reliable dashboard data - always matches Drift reality
2025-11-16 21:30:29 +01:00
mindesbunister
9bf83260c4 Add /close command and auto-flip logic with order cleanup
- Added /close Telegram command for full position closure
- Updated /reduce to accept 10-100% (was 10-90%)
- Implemented auto-flip logic: automatically closes opposite position when signal reverses
- Fixed risk check to allow opposite direction trades (signal flips)
- Enhanced Position Manager to cancel orders when removing trades
- Added startup initialization for Position Manager (restores trades on restart)
- Fixed analytics to show stopped-out trades (manual DB update for orphaned trade)
- Updated reduce endpoint to route 100% closes through closePosition for proper cleanup
- All position closures now guarantee TP/SL order cancellation on Drift
2025-10-27 23:27:48 +01:00