diff --git a/lib/drift/orders.ts b/lib/drift/orders.ts index 458e9ff..ccd09e8 100644 --- a/lib/drift/orders.ts +++ b/lib/drift/orders.ts @@ -527,10 +527,18 @@ export async function closePosition( console.log('๐Ÿงช DRY RUN MODE: Simulating close order (not executing on blockchain)') // Calculate realized P&L with leverage (default 10x in dry run) - const profitPercent = ((oraclePrice - position.entryPrice) / position.entryPrice) * 100 * (position.side === 'long' ? 1 : -1) + // For LONG: profit when exit > entry โ†’ (exit - entry) / entry + // For SHORT: profit when exit < entry โ†’ (entry - exit) / entry + const priceDiff = position.side === 'long' + ? (oraclePrice - position.entryPrice) // Long: profit when price rises + : (position.entryPrice - oraclePrice) // Short: profit when price falls + + const profitPercent = (priceDiff / position.entryPrice) * 100 const closedNotional = sizeToClose * oraclePrice - const realizedPnL = (closedNotional * profitPercent) / 100 - const accountPnLPercent = profitPercent * 10 // display using default leverage + const leverage = 10 + const collateral = closedNotional / leverage + const realizedPnL = collateral * (profitPercent / 100) * leverage + const accountPnLPercent = profitPercent * leverage const mockTxSig = `DRY_RUN_CLOSE_${Date.now()}_${Math.random().toString(36).substring(7)}` @@ -578,8 +586,13 @@ export async function closePosition( console.log('โœ… Transaction confirmed on-chain') // Calculate realized P&L with leverage - // CRITICAL: P&L must account for leverage and be calculated on USD notional, not base asset size - const profitPercent = ((oraclePrice - position.entryPrice) / position.entryPrice) * 100 * (position.side === 'long' ? 1 : -1) + // For LONG: profit when exit > entry โ†’ (exit - entry) / entry + // For SHORT: profit when exit < entry โ†’ (entry - exit) / entry + const priceDiff = position.side === 'long' + ? (oraclePrice - position.entryPrice) // Long: profit when price rises + : (position.entryPrice - oraclePrice) // Short: profit when price falls + + const profitPercent = (priceDiff / position.entryPrice) * 100 // Get leverage from user account (defaults to 10x if not found) let leverage = 10 @@ -593,9 +606,10 @@ export async function closePosition( console.log('โš ๏ธ Could not determine leverage from account, using 10x default') } - // Calculate closed notional value (USD) + // Calculate closed notional value (USD) and actual P&L with leverage const closedNotional = sizeToClose * oraclePrice - const realizedPnL = (closedNotional * profitPercent) / 100 + const collateral = closedNotional / leverage + const realizedPnL = collateral * (profitPercent / 100) * leverage // Leveraged P&L const accountPnLPercent = profitPercent * leverage console.log(`๐Ÿ’ฐ Close details:`)