diff --git a/app/api/automation/trade/route.js b/app/api/automation/trade/route.js index 7b41e13..28698b0 100644 --- a/app/api/automation/trade/route.js +++ b/app/api/automation/trade/route.js @@ -11,6 +11,10 @@ export async function POST(request) { amount, side, leverage = 1, + stopLoss, + takeProfit, + stopLossPercent, + takeProfitPercent, mode = 'SIMULATION' } = await request.json() @@ -29,6 +33,10 @@ export async function POST(request) { amount, side, leverage, + stopLoss, + takeProfit, + stopLossPercent, + takeProfitPercent, mode }) @@ -65,15 +73,16 @@ export async function POST(request) { 'Content-Type': 'application/json' }, body: JSON.stringify({ - action: 'place_order', // This was missing! Was defaulting to 'get_balance' + action: 'place_order', symbol: symbol.replace('USD', ''), // Convert SOLUSD to SOL amount, side, leverage, - // Add stop loss and take profit parameters - stopLoss: true, - takeProfit: true, - riskPercent: 2 // 2% risk per trade + // Pass through stop loss and take profit parameters + stopLoss: stopLoss !== undefined ? stopLoss : true, + takeProfit: takeProfit !== undefined ? takeProfit : true, + riskPercent: stopLossPercent || 2, // Use actual stop loss percentage from config + takeProfitPercent: takeProfitPercent || 4 // Use actual take profit percentage from config }) }) diff --git a/app/api/drift/trade/route.js b/app/api/drift/trade/route.js index 81df958..d91f454 100644 --- a/app/api/drift/trade/route.js +++ b/app/api/drift/trade/route.js @@ -106,7 +106,8 @@ export async function POST(request) { leverage = 1, stopLoss = true, takeProfit = true, - riskPercent = 2 + riskPercent = 2, + takeProfitPercent = 4 } = await request.json() // Import Drift SDK components @@ -220,13 +221,17 @@ export async function POST(request) { console.log(`📊 Current ${symbol} price: $${currentPrice}`) - // Convert amount to base units (SOL uses 9 decimals) - const baseAssetAmount = new BN(Math.floor(amount * 1e9)) + // For perpetual futures: amount is USD position size, convert to base asset amount + // Example: $32 position at $197.87/SOL = 0.162 SOL base asset amount + const solTokenAmount = amount / currentPrice + const baseAssetAmount = new BN(Math.floor(solTokenAmount * 1e9)) - console.log(`💰 Amount conversion:`, { - inputAmount: amount, - calculatedAmount: amount * 1e9, - flooredAmount: Math.floor(amount * 1e9), + console.log(`💰 Position size conversion:`, { + usdPositionSize: amount, + solPrice: currentPrice, + solTokenAmount: solTokenAmount, + calculatedBaseAsset: solTokenAmount * 1e9, + flooredBaseAsset: Math.floor(solTokenAmount * 1e9), baseAssetAmount: baseAssetAmount.toString() }) @@ -236,7 +241,8 @@ export async function POST(request) { console.log(`📊 Placing ${side} order:`, { symbol, marketIndex, - amount, + usdAmount: amount, + solAmount: solTokenAmount, leverage, currentPrice, baseAssetAmount: baseAssetAmount.toString() @@ -257,25 +263,25 @@ export async function POST(request) { // Wait for main order to fill await new Promise(resolve => setTimeout(resolve, 5000)) - // 2. Calculate stop loss and take profit prices - const stopLossPercent = Math.max(riskPercent / 100, 0.02) // Minimum 2% to avoid "too close" issues - const takeProfitPercent = stopLossPercent * 2 // 2:1 risk reward ratio + // 2. Calculate stop loss and take profit prices using config percentages + const stopLossPercent = Math.max(riskPercent / 100, 0.02) // Use riskPercent from config, minimum 2% + const takeProfitPercentCalc = Math.max(takeProfitPercent / 100, 0.04) // Use takeProfitPercent from config, minimum 4% let stopLossPrice, takeProfitPrice if (direction === PositionDirection.LONG) { stopLossPrice = currentPrice * (1 - stopLossPercent) - takeProfitPrice = currentPrice * (1 + takeProfitPercent) + takeProfitPrice = currentPrice * (1 + takeProfitPercentCalc) } else { stopLossPrice = currentPrice * (1 + stopLossPercent) - takeProfitPrice = currentPrice * (1 - takeProfitPercent) + takeProfitPrice = currentPrice * (1 - takeProfitPercentCalc) } console.log(`🎯 Risk management:`, { stopLossPrice: stopLossPrice.toFixed(4), takeProfitPrice: takeProfitPrice.toFixed(4), - riskPercent: `${stopLossPercent * 100}%`, - rewardRatio: '2:1', + stopLossPercent: `${stopLossPercent * 100}%`, + takeProfitPercent: `${takeProfitPercentCalc * 100}%`, priceDifference: Math.abs(currentPrice - stopLossPrice).toFixed(4) }) diff --git a/app/automation-v2/page.js b/app/automation-v2/page.js index 895e1dd..07090bb 100644 --- a/app/automation-v2/page.js +++ b/app/automation-v2/page.js @@ -20,10 +20,10 @@ export default function AutomationPageV2() { timeframe: '1h', // Primary timeframe for backwards compatibility selectedTimeframes: ['60'], // Multi-timeframe support tradingAmount: 100, + balancePercentage: 50, // Default to 50% of available balance maxLeverage: 5, stopLossPercent: 2, - takeProfitPercent: 6, - riskPercentage: 2 + takeProfitPercent: 6 }) const [status, setStatus] = useState(null) @@ -242,7 +242,7 @@ export default function AutomationPageV2() { {/* Symbol and Position Size */} -