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)}`)