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:
mindesbunister
2025-07-23 14:16:55 +02:00
parent 2bbaa072d6
commit abc94c06e2
4 changed files with 68 additions and 81 deletions

View File

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