From 7129cbfb8ab8b1d21b60c39d7efb3bfe0a21b881 Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Sun, 16 Nov 2025 01:57:13 +0100 Subject: [PATCH] fix: Add 99% safety buffer for 100% position sizing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: Bot trying to use exact 100% of collateral causes InsufficientCollateral errors due to Drift's margin calculation including fees, slippage buffers, etc. Example: - Collateral: $85.55 - Bot tries: $85.55 (100%) - Margin required: $85.58 (includes fees) - Result: Insufficient by $0.03 → Rejected Drift UI works because it automatically applies safety buffer internally. Solution: When user configures 100% position size, apply 99% safety buffer to leave room for fees and slippage calculations. Code change (config/trading.ts calculateActualPositionSize): - Check if configuredSize >= 100 - Use 99% (0.99) instead of 100% (1.0) - Log warning about safety buffer application Impact: Bot can now use 'full' collateral without InsufficientCollateral errors. Position size: $85.55 × 99% = $84.69 (well within margin requirements) Math proof (from user's Drift UI): - Available: $85.55 - UI max: $1,282.57 (15x leverage) - Bot now: $84.69 × 15 = $1,270.35 (safe margin) --- config/trading.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/config/trading.ts b/config/trading.ts index dd76693..51e847a 100644 --- a/config/trading.ts +++ b/config/trading.ts @@ -271,7 +271,16 @@ export function calculateActualPositionSize( } // Percentage of free collateral - const percentDecimal = configuredSize / 100 + let percentDecimal = configuredSize / 100 + + // CRITICAL: Safety buffer for 100% positions + // Drift's margin calculation includes fees and buffer, so 100% exact causes InsufficientCollateral + // Use 99% when user configures 100% to leave room for fees/slippage + if (configuredSize >= 100) { + percentDecimal = 0.99 + console.log(`⚠️ Applying 99% safety buffer for 100% position (prevents InsufficientCollateral from fees/slippage)`) + } + const calculatedSize = freeCollateral * percentDecimal console.log(`📊 Percentage sizing: ${configuredSize}% of $${freeCollateral.toFixed(2)} = $${calculatedSize.toFixed(2)}`)