- Real-time positions tracking with live P&L updates - PositionsPanel component with auto-refresh every 10s - Position creation on trade execution (DEX, Perp, Standard) - One-click position closing functionality - Stop Loss and Take Profit display with monitoring - /api/trading/positions API for CRUD operations - Real-time price updates via CoinGecko integration - Automatic position creation on successful trades - In-memory positions storage with P&L calculations - Enhanced trading page layout with positions panel - Entry price, current price, and unrealized P&L - Percentage-based P&L calculations - Portfolio summary with total value and total P&L - Transaction ID tracking for audit trail - Support for leverage positions and TP/SL orders Confirmed Working: - Position created: SOL/USDC BUY 0.02 @ 68.10 - Real-time P&L: -/bin/bash.0052 (-0.15%) - TP/SL monitoring: SL 60, TP 80 - Transaction: 5qYx7nmpgE3fHEZpjJCMtJNb1jSQVGfKhKNzJNgJ5VGV4xG2cSSpr1wtfPfbmx8zSjwHnzSgZiWsMnAWmCFQ2RVx - Clear positions display on trading page - Real-time updates without manual refresh - Intuitive close buttons for quick position management - Separate wallet holdings vs active trading positions - Professional trading interface with P&L visualization
78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const body = await request.json()
|
|
const { symbol, side, amount, type = 'market' } = body
|
|
|
|
// Validate input
|
|
if (!symbol || !side || !amount) {
|
|
return NextResponse.json({
|
|
error: 'Missing required fields: symbol, side, amount'
|
|
}, { status: 400 })
|
|
}
|
|
|
|
// Mock trading execution
|
|
const mockTrade = {
|
|
id: `trade_${Date.now()}`,
|
|
symbol,
|
|
side, // 'buy' or 'sell'
|
|
amount: parseFloat(amount),
|
|
type,
|
|
price: side === 'buy' ? 144.11 : 144.09, // Mock prices
|
|
status: 'executed',
|
|
timestamp: new Date().toISOString(),
|
|
fee: parseFloat(amount) * 0.001 // 0.1% fee
|
|
}
|
|
|
|
console.log('Simulated trade executed:', mockTrade)
|
|
|
|
// Automatically create position for this trade
|
|
try {
|
|
const positionResponse = await fetch('http://localhost:3000/api/trading/positions', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
action: 'add',
|
|
symbol: mockTrade.symbol,
|
|
side: mockTrade.side.toUpperCase(),
|
|
amount: mockTrade.amount,
|
|
entryPrice: mockTrade.price,
|
|
txId: mockTrade.id
|
|
})
|
|
})
|
|
|
|
if (positionResponse.ok) {
|
|
const positionData = await positionResponse.json()
|
|
console.log('Position created:', positionData.position?.id)
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to create position:', error)
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
trade: mockTrade,
|
|
message: `Successfully ${side} ${amount} ${symbol}`
|
|
})
|
|
} catch (error) {
|
|
return NextResponse.json({
|
|
error: 'Failed to execute trade',
|
|
message: error instanceof Error ? error.message : 'Unknown error'
|
|
}, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function GET() {
|
|
return NextResponse.json({
|
|
message: 'Trading endpoint is active. Use POST to execute trades.',
|
|
supportedMethods: ['POST'],
|
|
requiredFields: ['symbol', 'side', 'amount'],
|
|
optionalFields: ['type'],
|
|
positions: [
|
|
{ symbol: 'SOL', size: 1.5, entryPrice: 140.50, markPrice: 144.11, unrealizedPnl: 5.415 },
|
|
{ symbol: 'ETH', size: 0.1, entryPrice: 2350, markPrice: 2400, unrealizedPnl: 5.0 }
|
|
]
|
|
})
|
|
}
|