Add Position Sync feature for recovering tracking after partial fills

- New /api/trading/sync-positions endpoint (no auth)
- Fetches actual Drift positions and compares with Position Manager
- Removes stale tracking, adds missing positions with calculated TP/SL
- Settings UI: Orange 'Sync Positions' button added
- CLI script: scripts/sync-positions.sh for terminal access
- Full documentation in docs/guides/POSITION_SYNC_GUIDE.md
- Quick reference: POSITION_SYNC_QUICK_REF.md
- Updated AI instructions with pitfall #23

Problem solved: Manual Telegram trades with partial fills can cause
Position Manager to lose tracking, leaving positions without software-
based stop loss protection. This feature restores dual-layer protection.

Note: Docker build not picking up route yet (cache issue), needs investigation
This commit is contained in:
mindesbunister
2025-11-10 17:05:32 +01:00
parent 2e47731e8e
commit 089308a07e
6 changed files with 440 additions and 0 deletions

View File

@@ -251,6 +251,7 @@ const driftSymbol = normalizeTradingViewSymbol(body.symbol)
- `/api/trading/test` - Test trades from settings UI (no auth required, **respects symbol enable/disable**)
- `/api/trading/close` - Manual position closing
- `/api/trading/positions` - Query open positions from Drift
- `/api/trading/sync-positions` - **Re-sync Position Manager with actual Drift positions** (no auth, for recovery from partial fills/restarts)
- `/api/settings` - Get/update config (writes to .env file, **includes per-symbol settings**)
- `/api/analytics/last-trade` - Fetch most recent trade details for dashboard (includes quality score)
- `/api/analytics/version-comparison` - Compare performance across signal quality logic versions (v1/v2/v3)
@@ -486,6 +487,16 @@ trade.realizedPnL += actualRealizedPnL // NOT: result.realizedPnL from SDK
- Result: Win rate improved from 43.8% to 55.6%, profit per trade +86%
- Implementation: `lib/trading/signal-quality.ts` checks both conditions before price position scoring
23. **Position Manager sync issues:** Partial fills from on-chain orders can cause Position Manager to lose tracking:
- Symptom: Database shows position "closed", but Drift shows position still open without stop loss
- Cause: On-chain orders partially fill (0.29 SOL × 3 times), Position Manager closes database record, but remainder stays open
- Impact: Remaining position has NO software-based stop loss protection (only relies on on-chain orders)
- Solution: Use `/api/trading/sync-positions` endpoint to re-sync Position Manager with actual Drift positions
- Access: Settings UI "Sync Positions" button (orange), or CLI `scripts/sync-positions.sh`
- When: After manual Telegram trades, bot restarts, rate limiting issues, or suspected tracking loss
- Recovery: Endpoint fetches actual Drift positions, re-adds missing ones to Position Manager with calculated TP/SL
- Documentation: See `docs/guides/POSITION_SYNC_GUIDE.md` for details
## File Conventions
- **API routes:** `app/api/[feature]/[action]/route.ts` (Next.js 15 App Router)