fix: Add 99% safety buffer for 100% position sizing
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)
This commit is contained in:
@@ -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)}`)
|
||||
|
||||
Reference in New Issue
Block a user