'use client' import React, { useState, useEffect } from 'react' export default function PositionsPanel() { const [positions, setPositions] = useState([]) const [loading, setLoading] = useState(true) const [totalPnl, setTotalPnl] = useState(0) const [totalValue, setTotalValue] = useState(0) useEffect(() => { fetchPositions() // Refresh positions every 10 seconds const interval = setInterval(fetchPositions, 10000) return () => clearInterval(interval) }, []) const fetchPositions = async () => { try { const response = await fetch('/api/trading/positions') const data = await response.json() if (data.success) { setPositions(data.positions || []) setTotalPnl(data.totalPnl || 0) setTotalValue(data.totalValue || 0) } } catch (error) { console.error('Failed to fetch positions:', error) } finally { setLoading(false) } } const closePosition = async (positionId, currentPrice) => { try { const response = await fetch('/api/trading/positions', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'close', positionId: positionId, exitPrice: currentPrice }) }) if (response.ok) { fetchPositions() // Refresh positions } } catch (error) { console.error('Failed to close position:', error) } } const getPnlColor = (pnl) => { if (pnl > 0) return 'text-green-400' if (pnl < 0) return 'text-red-400' return 'text-gray-400' } const formatCurrency = (amount) => { return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2, maximumFractionDigits: 4 }).format(amount) } if (loading) { return (

Open Positions

Loading positions...
) } return (

Open Positions

{/* Portfolio Summary */} {positions.length > 0 && (
Total Positions
{positions.length}
Total Value
{formatCurrency(totalValue)}
Unrealized P&L
{formatCurrency(totalPnl)}
)} {positions.length === 0 ? (
📊 No open positions
Execute a trade to see positions here
) : (
{positions.map((position) => (
{position.symbol} {position.side} {position.leverage > 1 && ( {position.leverage}x )}
Size
{position.amount}
Entry Price
{formatCurrency(position.entryPrice)}
Current Price
{position.currentPrice ? formatCurrency(position.currentPrice) : 'Loading...'}
P&L
{position.unrealizedPnl ? formatCurrency(position.unrealizedPnl) : '$0.00'} {position.pnlPercentage && ( ({position.pnlPercentage > 0 ? '+' : ''}{position.pnlPercentage.toFixed(2)}%) )}
{/* Stop Loss / Take Profit */} {(position.stopLoss || position.takeProfit) && (
{position.stopLoss && (
🛑 SL: {formatCurrency(position.stopLoss)}
)} {position.takeProfit && (
🎯 TP: {formatCurrency(position.takeProfit)}
)}
)} {/* Position Info */}
Opened: {new Date(position.timestamp).toLocaleString()} {position.txId && ( • TX: {position.txId.substring(0, 8)}... )}
))}
)}
) }