Fix position sizing and improve automation UI
Major fixes: - Fixed position size calculation: converts USD amount to SOL tokens properly - Fixed insufficient collateral error by using correct position sizing - Added proper TP/SL parameter passing through automation chain - Enhanced position sizing UI with balance percentage slider Position Sizing Fixes: - Convert 2 USD to SOL tokens using current price (2 ÷ 97.87 = ~0.162 SOL) - Remove incorrect 32 SOL token calculation (was 32,000,000,000 base units) - Use USD position value for perpetual futures trading correctly Take Profit & Stop Loss Improvements: - Pass TP/SL percentages from config through automation → trade → drift chain - Use actual config percentages instead of hardcoded 2:1 ratio - Enable proper risk management with user-defined TP/SL levels UI/UX Enhancements: - Remove redundant 'Risk Per Trade (%)' field that caused confusion - Remove conflicting 'Auto-Size (%)' dropdown - Keep clean balance percentage slider (10% - 100% of available balance) - Simplify position sizing to: Balance % → Position Size → Leverage → TP/SL Technical Changes: - Update Drift API position calculation from SOL tokens to USD conversion - Fix automation trade route parameter passing - Clean up AutomationConfig interface - Improve position size validation and safety margins These changes enable proper leveraged perpetual futures trading with correct position sizing, collateral usage, and automated TP/SL order placement.
This commit is contained in:
@@ -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)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user