diff --git a/test-ai-leverage-simple.js b/test-ai-leverage-simple.js new file mode 100644 index 0000000..19583e4 --- /dev/null +++ b/test-ai-leverage-simple.js @@ -0,0 +1,102 @@ +/** + * Test AI Leverage Calculator Logic + * + * Tests the core leverage calculation algorithms + */ + +function calculateMaxSafeLeverage(entryPrice, stopLossPrice, side, safetyBuffer = 0.10) { + // Calculate where liquidation should be (beyond stop loss + safety buffer) + let maxLiquidationPrice; + + if (side === 'long') { + // For longs: liquidation should be below stop loss + maxLiquidationPrice = stopLossPrice * (1 - safetyBuffer); + } else { + // For shorts: liquidation should be above stop loss + maxLiquidationPrice = stopLossPrice * (1 + safetyBuffer); + } + + console.log(`šŸ›”ļø Max Safe Liquidation Price: $${maxLiquidationPrice.toFixed(4)} (${side} position)`); + + // Calculate max leverage that keeps liquidation at safe distance + let maxLeverage; + + if (side === 'long') { + // For longs: liq = entry * (1 - 1/leverage) + maxLeverage = 1 / (1 - maxLiquidationPrice / entryPrice); + } else { + // For shorts: liq = entry * (1 + 1/leverage) + maxLeverage = 1 / (maxLiquidationPrice / entryPrice - 1); + } + + // Ensure reasonable bounds + maxLeverage = Math.max(1, Math.min(maxLeverage, 50)); + + console.log(`āš–ļø Max Safe Leverage: ${maxLeverage.toFixed(2)}x`); + + return maxLeverage; +} + +function calculateLiquidationPrice(entryPrice, leverage, side) { + if (side === 'long') { + return entryPrice * (1 - 1/leverage); + } else { + return entryPrice * (1 + 1/leverage); + } +} + +function testAILeverageLogic() { + console.log('🧠 Testing AI Leverage Calculator Core Logic...\n'); + + // Test Case 1: Conservative long position + console.log('šŸ“Š Test Case 1: Conservative Long Position'); + console.log('Entry: $185, Stop Loss: $175 (5.4% drop)'); + + const case1MaxLeverage = calculateMaxSafeLeverage(185, 175, 'long', 0.10); + const case1LiqPrice = calculateLiquidationPrice(185, case1MaxLeverage, 'long'); + + console.log(`āœ… With ${case1MaxLeverage.toFixed(1)}x leverage:`); + console.log(` Liquidation: $${case1LiqPrice.toFixed(2)}`); + console.log(` Stop Loss: $175.00`); + console.log(` Safety Buffer: ${((175 - case1LiqPrice) / 175 * 100).toFixed(1)}%\n`); + + // Test Case 2: Aggressive short position + console.log('šŸ“Š Test Case 2: Aggressive Short Position'); + console.log('Entry: $185, Stop Loss: $195 (5.4% rise)'); + + const case2MaxLeverage = calculateMaxSafeLeverage(185, 195, 'short', 0.10); + const case2LiqPrice = calculateLiquidationPrice(185, case2MaxLeverage, 'short'); + + console.log(`āœ… With ${case2MaxLeverage.toFixed(1)}x leverage:`); + console.log(` Liquidation: $${case2LiqPrice.toFixed(2)}`); + console.log(` Stop Loss: $195.00`); + console.log(` Safety Buffer: ${((case2LiqPrice - 195) / 195 * 100).toFixed(1)}%\n`); + + // Test Case 3: Very tight stop loss + console.log('šŸ“Š Test Case 3: Very Tight Stop Loss (2%)'); + console.log('Entry: $185, Stop Loss: $181.30 (2% drop)'); + + const case3MaxLeverage = calculateMaxSafeLeverage(185, 181.30, 'long', 0.10); + const case3LiqPrice = calculateLiquidationPrice(185, case3MaxLeverage, 'long'); + + console.log(`āœ… With ${case3MaxLeverage.toFixed(1)}x leverage:`); + console.log(` Liquidation: $${case3LiqPrice.toFixed(2)}`); + console.log(` Stop Loss: $181.30`); + console.log(` Safety Buffer: ${((181.30 - case3LiqPrice) / 181.30 * 100).toFixed(1)}%\n`); + + // Test Balance Strategies + console.log('šŸ’° Balance Strategy Tests:'); + + const smallAccount = 500; + const largeAccount = 2500; + const availableSmall = 450; + const availableLarge = 2000; + + console.log(`Small Account ($${smallAccount}): ${smallAccount < 1000 ? '100%' : '50%'} strategy = $${smallAccount < 1000 ? availableSmall : availableSmall * 0.5} used`); + console.log(`Large Account ($${largeAccount}): ${largeAccount < 1000 ? '100%' : '50%'} strategy = $${largeAccount < 1000 ? availableLarge : availableLarge * 0.5} used`); + + console.log('\nāœ… AI Leverage Calculator core logic tests completed!'); +} + +// Run the test +testAILeverageLogic(); diff --git a/test-ai-leverage.js b/test-ai-leverage.js new file mode 100644 index 0000000..e86f74b --- /dev/null +++ b/test-ai-leverage.js @@ -0,0 +1,95 @@ +/** + * Test AI Leverage Calculator + * + * Quick test to verify the AI leverage calculation system works correctly + */ + +const AILeverageCalculator = require('./lib/ai-leverage-calculator.ts').default + +async function testAILeverageCalculator() { + console.log('🧠 Testing AI Leverage Calculator...\n') + + // Test Case 1: Small account (<$1k) - Should use 100% balance aggressively + console.log('šŸ“Š Test Case 1: Small Account Strategy (<$1k)') + const smallAccountResult = AILeverageCalculator.calculateOptimalLeverage({ + accountValue: 500, + availableBalance: 450, + entryPrice: 185, + stopLossPrice: 175, // Long position, 5.4% stop loss + side: 'long', + maxLeverageAllowed: 20, + safetyBuffer: 0.10 + }) + + console.log('Small Account Result:', { + leverage: `${smallAccountResult.recommendedLeverage.toFixed(1)}x`, + riskLevel: smallAccountResult.riskAssessment, + liquidation: `$${smallAccountResult.liquidationPrice.toFixed(2)}`, + reasoning: smallAccountResult.reasoning + }) + console.log('') + + // Test Case 2: Large account (>$1k) - Should use 50% balance conservatively + console.log('šŸ“Š Test Case 2: Large Account Strategy (>$1k)') + const largeAccountResult = AILeverageCalculator.calculateOptimalLeverage({ + accountValue: 2500, + availableBalance: 2000, + entryPrice: 185, + stopLossPrice: 175, // Long position, 5.4% stop loss + side: 'long', + maxLeverageAllowed: 20, + safetyBuffer: 0.10 + }) + + console.log('Large Account Result:', { + leverage: `${largeAccountResult.recommendedLeverage.toFixed(1)}x`, + riskLevel: largeAccountResult.riskAssessment, + liquidation: `$${largeAccountResult.liquidationPrice.toFixed(2)}`, + reasoning: largeAccountResult.reasoning + }) + console.log('') + + // Test Case 3: Short position with tight stop loss + console.log('šŸ“Š Test Case 3: Short Position with Tight Stop') + const shortResult = AILeverageCalculator.calculateOptimalLeverage({ + accountValue: 800, + availableBalance: 750, + entryPrice: 185, + stopLossPrice: 195, // Short position, 5.4% stop loss above entry + side: 'short', + maxLeverageAllowed: 20, + safetyBuffer: 0.10 + }) + + console.log('Short Position Result:', { + leverage: `${shortResult.recommendedLeverage.toFixed(1)}x`, + riskLevel: shortResult.riskAssessment, + liquidation: `$${shortResult.liquidationPrice.toFixed(2)}`, + reasoning: shortResult.reasoning + }) + console.log('') + + // Test Case 4: Very tight stop loss - should limit leverage + console.log('šŸ“Š Test Case 4: Very Tight Stop Loss (2%)') + const tightStopResult = AILeverageCalculator.calculateOptimalLeverage({ + accountValue: 1000, + availableBalance: 900, + entryPrice: 185, + stopLossPrice: 181.3, // Only 2% stop loss - very tight + side: 'long', + maxLeverageAllowed: 20, + safetyBuffer: 0.10 + }) + + console.log('Tight Stop Result:', { + leverage: `${tightStopResult.recommendedLeverage.toFixed(1)}x`, + riskLevel: tightStopResult.riskAssessment, + liquidation: `$${tightStopResult.liquidationPrice.toFixed(2)}`, + reasoning: tightStopResult.reasoning + }) + + console.log('\nāœ… AI Leverage Calculator tests completed!') +} + +// Run the test +testAILeverageCalculator().catch(console.error)