Add comprehensive stop loss and take profit functionality
- Added stop loss and take profit parameters to TradeParams interface - Implemented conditional order placement in executeTrade method - Added ZERO import and closePosition method to DriftTradingService - Enhanced trade API to handle stop loss/take profit parameters - Added position fetching and closing functionality to AdvancedTradingPanel - Added open positions display with close buttons - Implemented risk management calculations and UI - Added conditional order tracking in TradeResult interface
This commit is contained in:
46
app/api/drift/close-position/route.ts
Normal file
46
app/api/drift/close-position/route.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { driftTradingService } from '../../../../lib/drift-trading'
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { symbol, amount } = await request.json()
|
||||
|
||||
console.log(`🔒 Close position request: ${amount || 'ALL'} ${symbol}`)
|
||||
|
||||
// Validate inputs
|
||||
if (!symbol) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Symbol is required' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Execute position close
|
||||
const result = await driftTradingService.closePosition(symbol, amount)
|
||||
|
||||
if (result.success) {
|
||||
console.log(`✅ Position closed successfully: ${result.txId}`)
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
txId: result.txId,
|
||||
message: `Position in ${symbol} closed successfully`
|
||||
})
|
||||
} else {
|
||||
console.error(`❌ Failed to close position: ${result.error}`)
|
||||
return NextResponse.json(
|
||||
{ success: false, error: result.error },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('❌ Close position API error:', error)
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: error.message || 'Failed to close position'
|
||||
},
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user