critical: Fix exit order token sizing - TP/SL now use exact position size

BUG #92: Exit orders (TP1, TP2, SL) had different token sizes than position
- Position: 142.91 SOL but TP1=140.87 SOL, SL=147.03 SOL (WRONG)
- Root cause: usdToBase() calculated tokens as USD/price per order
- Each exit order price produced different token amounts

FIX: Pass actual token count via positionSizeTokens parameter
- Added positionSizeTokens to PlaceExitOrdersOptions interface
- Added tokensToBase() helper (tokens * 1e9 directly)
- All exit sections now use token-based calculation when available

Files updated to pass positionSizeTokens:
- app/api/trading/execute/route.ts: openResult.fillSize
- lib/trading/smart-entry-timer.ts: openResult.fillSize
- lib/trading/sync-helper.ts: Math.abs(driftPos.size)
- lib/trading/position-manager.ts: Math.abs(position.size) + fetch patterns
- lib/startup/init-position-manager.ts: Math.abs(position.size)
- lib/health/position-manager-health.ts: Drift position fetch + token size

Result: When position = X tokens, ALL exit orders close portions of X tokens
- TP1: X * tp1SizePercent / 100 tokens
- TP2: remaining * tp2SizePercent / 100 tokens
- SL: X tokens (full position)

Backward compatible: Falls back to USD calculation if positionSizeTokens not provided
This commit is contained in:
mindesbunister
2026-01-07 09:59:36 +01:00
parent efbe4d0c04
commit 361f3ba183
7 changed files with 151 additions and 32 deletions

View File

@@ -515,6 +515,7 @@ export class SmartEntryTimer {
const exitRes = await placeExitOrders({
symbol: signal.symbol,
positionSizeUSD,
positionSizeTokens: openResult.fillSize, // CRITICAL FIX (Jan 6, 2026): Use actual token count
entryPrice: fillPrice,
tp1Price,
tp2Price,