Fix position sizing and stop loss placement issues
- Fixed insufficient collateral error by implementing proper position sizing - Added balance fetching before trade decisions for accurate calculations - Implemented minimum order size enforcement (0.01 SOL for Drift) - Improved stop loss placement with 2% minimum risk and better error logging - Enhanced risk management with conservative slippage buffers - Fixed 'Order Amount Too Small' errors by ensuring minimum order requirements Position sizing now: - Uses actual Drift account balance (5.69) - Calculates appropriate position size based on risk percentage - Ensures minimum 0.01 SOL order size for Drift compatibility - Provides detailed calculation logging for debugging
This commit is contained in:
@@ -258,7 +258,7 @@ export async function POST(request) {
|
||||
await new Promise(resolve => setTimeout(resolve, 5000))
|
||||
|
||||
// 2. Calculate stop loss and take profit prices
|
||||
const stopLossPercent = riskPercent / 100 // Convert 2% to 0.02
|
||||
const stopLossPercent = Math.max(riskPercent / 100, 0.02) // Minimum 2% to avoid "too close" issues
|
||||
const takeProfitPercent = stopLossPercent * 2 // 2:1 risk reward ratio
|
||||
|
||||
let stopLossPrice, takeProfitPrice
|
||||
@@ -274,8 +274,9 @@ export async function POST(request) {
|
||||
console.log(`🎯 Risk management:`, {
|
||||
stopLossPrice: stopLossPrice.toFixed(4),
|
||||
takeProfitPrice: takeProfitPrice.toFixed(4),
|
||||
riskPercent: `${riskPercent}%`,
|
||||
rewardRatio: '2:1'
|
||||
riskPercent: `${stopLossPercent * 100}%`,
|
||||
rewardRatio: '2:1',
|
||||
priceDifference: Math.abs(currentPrice - stopLossPrice).toFixed(4)
|
||||
})
|
||||
|
||||
let stopLossTx = null, takeProfitTx = null
|
||||
@@ -286,7 +287,13 @@ export async function POST(request) {
|
||||
console.log('🛡️ Placing stop loss order...')
|
||||
|
||||
const stopLossTriggerPrice = new BN(Math.floor(stopLossPrice * 1e6))
|
||||
const stopLossOrderPrice = new BN(Math.floor(stopLossPrice * 0.99 * 1e6)) // Slightly worse price to ensure execution
|
||||
const stopLossOrderPrice = new BN(Math.floor(stopLossPrice * 0.995 * 1e6)) // 0.5% slippage buffer
|
||||
|
||||
console.log(`🛡️ Stop Loss Details:`, {
|
||||
triggerPrice: (stopLossTriggerPrice.toNumber() / 1e6).toFixed(4),
|
||||
orderPrice: (stopLossOrderPrice.toNumber() / 1e6).toFixed(4),
|
||||
baseAssetAmount: baseAssetAmount.toString()
|
||||
})
|
||||
|
||||
stopLossTx = await driftClient.placePerpOrder({
|
||||
orderType: OrderType.TRIGGER_LIMIT,
|
||||
@@ -301,6 +308,13 @@ export async function POST(request) {
|
||||
console.log('✅ Stop loss placed:', stopLossTx)
|
||||
} catch (slError) {
|
||||
console.warn('⚠️ Stop loss failed:', slError.message)
|
||||
// Log more details about the stop loss failure
|
||||
console.warn('🛡️ Stop loss failure details:', {
|
||||
stopLossPrice,
|
||||
currentPrice,
|
||||
priceDiff: Math.abs(currentPrice - stopLossPrice),
|
||||
percentDiff: ((Math.abs(currentPrice - stopLossPrice) / currentPrice) * 100).toFixed(2) + '%'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -310,7 +324,14 @@ export async function POST(request) {
|
||||
console.log('🎯 Placing take profit order...')
|
||||
|
||||
const takeProfitTriggerPrice = new BN(Math.floor(takeProfitPrice * 1e6))
|
||||
const takeProfitOrderPrice = new BN(Math.floor(takeProfitPrice * 1.01 * 1e6)) // Slightly better price to ensure execution
|
||||
const takeProfitOrderPrice = new BN(Math.floor(takeProfitPrice * 1.005 * 1e6)) // 0.5% slippage for execution
|
||||
|
||||
console.log('🎯 Take Profit Details:', {
|
||||
takeProfitPrice: takeProfitPrice.toFixed(4),
|
||||
triggerPrice: (Number(takeProfitTriggerPrice) / 1e6).toFixed(4),
|
||||
orderPrice: (Number(takeProfitOrderPrice) / 1e6).toFixed(4),
|
||||
baseAssetAmount: baseAssetAmount.toString()
|
||||
})
|
||||
|
||||
takeProfitTx = await driftClient.placePerpOrder({
|
||||
orderType: OrderType.TRIGGER_LIMIT,
|
||||
@@ -322,9 +343,13 @@ export async function POST(request) {
|
||||
reduceOnly: true,
|
||||
})
|
||||
|
||||
console.log('✅ Take profit placed:', takeProfitTx)
|
||||
console.log('✅ Take profit placed successfully:', takeProfitTx)
|
||||
} catch (tpError) {
|
||||
console.warn('⚠️ Take profit failed:', tpError.message)
|
||||
console.error('❌ Take profit placement failed:', {
|
||||
error: tpError.message,
|
||||
code: tpError.code,
|
||||
logs: tpError.logs || 'No logs available'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user