feat: Enhanced Jupiter DEX with full bidirectional trading support
MAJOR ENHANCEMENTS:
- Added SELL signal processing in automation service
- Smart position management with SOL holdings verification
- Risk-adjusted sell amounts based on current portfolio
- Proper swap direction logic (SOL → USDC for shorts)
- Enhanced stop loss/take profit for both BUY and SELL orders
- Fixed investment amount calculations (corrected from 00 to actual 4)
- Implemented proportional P&L adjustment for historical trades
- Synchronized price data between analysis-details and price-monitor APIs
- Enhanced active trades display with priority sorting and visual indicators
- checkCurrentPosition(): Verifies SOL holdings before SELL orders
- calculateSellAmount(): Risk-based position sizing for shorts
- Enhanced TP/SL calculations for bidirectional trading
- Real-time price synchronization across all endpoints
- Active trades monitoring with visual enhancements
- BUY: USDC → SOL (profit from price increases)
- SELL: SOL → USDC (profit from price decreases)
- Position-aware risk management
- Confidence-based position sizing
- Proper decimal handling (SOL=9, USDC=6)
- Comprehensive Jupiter shorting test suite
- P&L calculation verification
- Position management validation
- API endpoint testing
- P&L corrected from .15 to /bin/bash.78 for 4 investment
- Active trades display enhanced with blue borders and pulsing indicators
- Full bidirectional trading now available
- Risk-managed shorting based on actual holdings
This enables making money in both bull and bear markets! 🎯
This commit is contained in:
@@ -23,6 +23,7 @@ export default function AutomationPage() {
|
||||
const [analysisDetails, setAnalysisDetails] = useState(null)
|
||||
const [selectedTrade, setSelectedTrade] = useState(null)
|
||||
const [tradeModalOpen, setTradeModalOpen] = useState(false)
|
||||
const [configCollapsed, setConfigCollapsed] = useState(true)
|
||||
|
||||
useEffect(() => {
|
||||
fetchStatus()
|
||||
@@ -275,11 +276,165 @@ export default function AutomationPage() {
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 xl:grid-cols-2 gap-8">
|
||||
{/* Configuration Panel */}
|
||||
{/* Real-Time Price Monitor */}
|
||||
<div className="space-y-6">
|
||||
<RealTimePriceMonitor symbol={config.symbol} />
|
||||
</div>
|
||||
|
||||
{/* Right Side Panel - Active Trades or Status */}
|
||||
<div className="space-y-6">
|
||||
{/* Active Trades Monitor */}
|
||||
{status && status.activeTrades && status.activeTrades.length > 0 ? (
|
||||
<div className="card card-gradient p-6">
|
||||
<h2 className="text-xl font-bold text-white mb-4">Active Trades Monitor</h2>
|
||||
<div className="space-y-3">
|
||||
{status.activeTrades.map((trade, idx) => (
|
||||
<div key={idx} className="p-3 bg-gray-800 rounded border border-gray-700">
|
||||
<div className="flex justify-between items-center mb-2">
|
||||
<span className="font-semibold text-white">
|
||||
{trade.side} {trade.amount} @ ${trade.entryPrice}
|
||||
</span>
|
||||
<span className={`px-2 py-1 rounded text-xs ${
|
||||
trade.unrealizedPnl > 0 ? 'bg-green-600' : 'bg-red-600'
|
||||
} text-white`}>
|
||||
${trade.unrealizedPnl}
|
||||
</span>
|
||||
</div>
|
||||
<div className="text-xs text-gray-400">
|
||||
SL: ${trade.stopLoss} | TP: ${trade.takeProfit}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
/* Trading Status Summary when no active trades */
|
||||
<div className="card card-gradient p-6">
|
||||
<h2 className="text-xl font-bold text-white mb-4">Trading Status</h2>
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-gray-300">Mode:</span>
|
||||
<span className={`px-2 py-1 rounded text-sm ${
|
||||
config.mode === 'LIVE' ? 'bg-red-600 text-white' : 'bg-blue-600 text-white'
|
||||
}`}>
|
||||
{config.mode}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-gray-300">Status:</span>
|
||||
<span className={`px-2 py-1 rounded text-sm ${
|
||||
status?.isActive ? 'bg-green-600 text-white' :
|
||||
status?.status === 'PAUSED' ? 'bg-yellow-600 text-white' :
|
||||
'bg-gray-600 text-white'
|
||||
}`}>
|
||||
{status?.isActive ? 'ACTIVE' : (status?.status || 'STOPPED')}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-gray-300">Symbol:</span>
|
||||
<span className="text-white font-semibold">{config.symbol}</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-gray-300">Timeframe:</span>
|
||||
<span className="text-white">{config.timeframe}</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-gray-300">Active Trades:</span>
|
||||
<span className="text-white">
|
||||
{status?.activeTrades?.length || 0}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-gray-300">Trading Amount:</span>
|
||||
<span className="text-white">${config.tradingAmount}</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-gray-300">Max Leverage:</span>
|
||||
<span className="text-yellow-400">{config.maxLeverage}x</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-gray-300">Stop Loss:</span>
|
||||
<span className="text-red-400">{config.stopLossPercent}%</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-gray-300">Take Profit:</span>
|
||||
<span className="text-green-400">{config.takeProfitPercent}%</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-gray-300">Max Daily Trades:</span>
|
||||
<span className="text-white">{config.maxDailyTrades}</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-gray-300">Risk Percentage:</span>
|
||||
<span className="text-orange-400">{config.riskPercentage}%</span>
|
||||
</div>
|
||||
{status && (
|
||||
<>
|
||||
<div className="border-t border-gray-700 pt-3 mt-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-gray-300">Total Trades:</span>
|
||||
<span className="text-white">{status.totalTrades || 0}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-gray-300">Win Rate:</span>
|
||||
<span className={`font-bold ${
|
||||
(status.winRate || 0) >= 50 ? 'text-green-400' : 'text-red-400'
|
||||
}`}>
|
||||
{(status.winRate || 0).toFixed(1)}%
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-gray-300">Total P&L:</span>
|
||||
<span className={`font-bold ${
|
||||
(status.totalPnL || 0) > 0 ? 'text-green-400' :
|
||||
(status.totalPnL || 0) < 0 ? 'text-red-400' : 'text-gray-400'
|
||||
}`}>
|
||||
${(status.totalPnL || 0).toFixed(2)}
|
||||
</span>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* Quick Actions */}
|
||||
<div className="border-t border-gray-700 pt-3 mt-4">
|
||||
<div className="text-sm text-gray-400 mb-2">Quick Actions:</div>
|
||||
<div className="flex space-x-2">
|
||||
<button
|
||||
onClick={() => setConfigCollapsed(!configCollapsed)}
|
||||
className="px-3 py-1 bg-blue-600 text-white rounded text-xs hover:bg-blue-700 transition-colors"
|
||||
>
|
||||
{configCollapsed ? 'Show Config' : 'Hide Config'}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => window.location.reload()}
|
||||
className="px-3 py-1 bg-gray-600 text-white rounded text-xs hover:bg-gray-700 transition-colors"
|
||||
>
|
||||
Refresh
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 xl:grid-cols-2 gap-8">
|
||||
{/* Configuration Panel - Collapsible */}
|
||||
<div className="space-y-6">
|
||||
<div className="card card-gradient p-6">
|
||||
<h2 className="text-xl font-bold text-white mb-4">Configuration</h2>
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h2 className="text-xl font-bold text-white">Configuration</h2>
|
||||
<button
|
||||
onClick={() => setConfigCollapsed(!configCollapsed)}
|
||||
className="px-3 py-1 bg-gray-700 text-white rounded hover:bg-gray-600 transition-colors"
|
||||
>
|
||||
{configCollapsed ? 'Show' : 'Hide'}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{!configCollapsed && (
|
||||
<div className="space-y-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300 mb-2">
|
||||
@@ -413,6 +568,7 @@ export default function AutomationPage() {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* AI Learning Status */}
|
||||
@@ -535,9 +691,6 @@ export default function AutomationPage() {
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Real-Time Price Monitor */}
|
||||
<RealTimePriceMonitor />
|
||||
</div>
|
||||
|
||||
{/* Status and Performance */}
|
||||
@@ -615,19 +768,53 @@ export default function AutomationPage() {
|
||||
|
||||
{/* Recent Trades */}
|
||||
<div className="card card-gradient p-6">
|
||||
<h2 className="text-xl font-bold text-white mb-4">Latest 4 Automated Trades (Debug: {recentTrades.length})</h2>
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h2 className="text-xl font-bold text-white">
|
||||
Active & Latest Trades
|
||||
<span className="text-sm text-gray-400 ml-2">(Top 4)</span>
|
||||
</h2>
|
||||
<div className="flex items-center space-x-4 text-xs">
|
||||
<div className="flex items-center space-x-1">
|
||||
<div className="w-2 h-2 bg-blue-500 rounded-full animate-pulse"></div>
|
||||
<span className="text-blue-400">Active</span>
|
||||
</div>
|
||||
<div className="text-gray-400">
|
||||
Debug: {recentTrades.length} total trades
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{console.log('🎯 Rendering trades section, count:', recentTrades.length)}
|
||||
{recentTrades.length > 0 ? (
|
||||
<div className="space-y-4">
|
||||
{recentTrades.slice(0, 4).map((trade, idx) => (
|
||||
{/* Sort trades to show active trades first, then latest completed trades */}
|
||||
{recentTrades
|
||||
.sort((a, b) => {
|
||||
// Active trades first
|
||||
if (a.isActive && !b.isActive) return -1
|
||||
if (!a.isActive && b.isActive) return 1
|
||||
// Then by creation date (most recent first)
|
||||
return new Date(b.createdAt) - new Date(a.createdAt)
|
||||
})
|
||||
.slice(0, 4)
|
||||
.map((trade, idx) => (
|
||||
<div
|
||||
key={idx}
|
||||
className="p-4 bg-gray-800 rounded-lg border border-gray-700 cursor-pointer hover:bg-gray-750 transition-colors"
|
||||
className={`p-4 rounded-lg border cursor-pointer hover:bg-gray-750 transition-colors ${
|
||||
trade.isActive
|
||||
? 'bg-blue-900/30 border-blue-500/50 ring-2 ring-blue-500/20'
|
||||
: 'bg-gray-800 border-gray-700'
|
||||
}`}
|
||||
onClick={() => openTradeModal(trade.id)}
|
||||
>
|
||||
{/* Trade Header */}
|
||||
{/* Trade Header - Enhanced for active trades */}
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<div className="flex items-center space-x-2">
|
||||
{trade.isActive && (
|
||||
<div className="flex items-center space-x-1">
|
||||
<div className="w-2 h-2 bg-blue-500 rounded-full animate-pulse"></div>
|
||||
<span className="text-blue-400 text-xs font-semibold">LIVE</span>
|
||||
</div>
|
||||
)}
|
||||
<span className={`font-semibold px-2 py-1 rounded text-xs ${
|
||||
trade.side === 'BUY' ? 'bg-green-600 text-white' : 'bg-red-600 text-white'
|
||||
}`}>
|
||||
@@ -636,7 +823,7 @@ export default function AutomationPage() {
|
||||
<span className="text-white font-semibold">{trade.amount}</span>
|
||||
<span className="text-yellow-400 font-semibold">{trade.leverage}x</span>
|
||||
<span className={`px-2 py-1 rounded text-xs ${
|
||||
trade.isActive ? 'bg-blue-600 text-white' :
|
||||
trade.isActive ? 'bg-blue-600 text-white animate-pulse' :
|
||||
trade.result === 'WIN' ? 'bg-green-600 text-white' :
|
||||
trade.result === 'LOSS' ? 'bg-red-600 text-white' :
|
||||
'bg-gray-600 text-white'
|
||||
@@ -647,6 +834,11 @@ export default function AutomationPage() {
|
||||
<div className="text-right">
|
||||
<div className="text-white font-semibold">${trade.entryPrice?.toFixed(2) || trade.price?.toFixed(2) || '0.00'}</div>
|
||||
<div className="text-sm text-gray-400">{trade.confidence || 0}% confidence</div>
|
||||
{trade.isActive && (
|
||||
<div className="text-xs text-blue-400 font-semibold">
|
||||
Current: ${trade.currentPrice?.toFixed(2) || '0.00'}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -673,27 +865,37 @@ export default function AutomationPage() {
|
||||
{/* Trading Details */}
|
||||
<div className="mb-3 p-3 bg-gray-900/30 rounded border border-gray-700">
|
||||
<div className="grid grid-cols-2 gap-2 text-xs">
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-300">Trading Mode:</span>
|
||||
<span className={`font-semibold ${
|
||||
trade.tradingMode === 'LIVE' ? 'text-red-400' : 'text-blue-400'
|
||||
}`}>
|
||||
{trade.tradingMode || 'SIMULATION'}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-300">Trading Amount:</span>
|
||||
<span className="text-white">${trade.tradingAmount}</span>
|
||||
<span className="text-white">
|
||||
{trade.realTradingAmount ? `$${trade.realTradingAmount}` : `$${trade.tradingAmount || '0'}`}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-300">Leverage:</span>
|
||||
<span className="text-yellow-400">{trade.leverage}x</span>
|
||||
<span className="text-yellow-400">{trade.leverage || 1}x</span>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-300">Position Size:</span>
|
||||
<span className="text-white">${trade.positionSize}</span>
|
||||
<span className="text-white">{trade.amount?.toFixed(6) || '0.000000'} SOL</span>
|
||||
</div>
|
||||
{/* Entry Price - Always show for completed trades */}
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-300">Entry Price:</span>
|
||||
<span className="text-white">${trade.entryPrice?.toFixed(2) || trade.price?.toFixed(2) || '0.00'}</span>
|
||||
</div>
|
||||
{/* Exit Price or Current Price */}
|
||||
{/* Current/Exit Price with real-time updates for active trades */}
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-300">{trade.isActive ? 'Current' : 'Exit'} Price:</span>
|
||||
<span className="text-white">
|
||||
<span className={`${trade.isActive ? 'text-blue-400 font-semibold' : 'text-white'}`}>
|
||||
{trade.isActive ?
|
||||
`$${trade.currentPrice?.toFixed(2) || '0.00'}` :
|
||||
(trade.exitPrice ?
|
||||
@@ -703,6 +905,22 @@ export default function AutomationPage() {
|
||||
}
|
||||
</span>
|
||||
</div>
|
||||
{/* Live Trade Transaction ID */}
|
||||
{trade.tradingMode === 'LIVE' && trade.driftTxId && (
|
||||
<div className="flex justify-between col-span-2 pt-1 border-t border-gray-700">
|
||||
<span className="text-gray-300">Transaction ID:</span>
|
||||
<span className="text-green-400 font-mono text-xs truncate max-w-24" title={trade.driftTxId}>
|
||||
{trade.driftTxId.slice(0, 8)}...{trade.driftTxId.slice(-8)}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
{/* Live Trade Fees */}
|
||||
{trade.tradingMode === 'LIVE' && trade.fees > 0 && (
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-300">Fees Paid:</span>
|
||||
<span className="text-orange-400">${trade.fees?.toFixed(4) || '0.0000'}</span>
|
||||
</div>
|
||||
)}
|
||||
{/* Price difference for completed trades */}
|
||||
{!trade.isActive && trade.exitPrice && trade.entryPrice && (
|
||||
<div className="flex justify-between col-span-2 pt-1 border-t border-gray-700">
|
||||
@@ -719,10 +937,16 @@ export default function AutomationPage() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* P&L Display */}
|
||||
<div className="mb-3 p-3 bg-gray-900/50 rounded border border-gray-600">
|
||||
{/* P&L Display - Enhanced for active trades */}
|
||||
<div className={`mb-3 p-3 rounded border ${
|
||||
trade.isActive
|
||||
? 'bg-blue-900/50 border-blue-600'
|
||||
: 'bg-gray-900/50 border-gray-600'
|
||||
}`}>
|
||||
<div className="flex justify-between items-center text-sm">
|
||||
<span className="text-gray-300">P&L:</span>
|
||||
<span className="text-gray-300">
|
||||
{trade.isActive ? 'Unrealized P&L:' : 'Realized P&L:'}
|
||||
</span>
|
||||
<div className="flex items-center space-x-2">
|
||||
<span className={`font-bold ${
|
||||
trade.isActive ?
|
||||
@@ -745,11 +969,27 @@ export default function AutomationPage() {
|
||||
({trade.pnlPercent})
|
||||
</span>
|
||||
)}
|
||||
<span className="text-xs text-gray-400">
|
||||
{trade.isActive ? '(Unrealized)' : '(Realized)'}
|
||||
<span className={`text-xs ${
|
||||
trade.isActive ? 'text-blue-400 font-semibold' : 'text-gray-400'
|
||||
}`}>
|
||||
{trade.isActive ? '⚡ Live' : '✓ Final'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
{/* Additional active trade info */}
|
||||
{trade.isActive && trade.currentPrice && trade.entryPrice && (
|
||||
<div className="mt-2 pt-2 border-t border-blue-500/20">
|
||||
<div className="flex justify-between text-xs">
|
||||
<span className="text-gray-400">Price Change:</span>
|
||||
<span className={`font-semibold ${
|
||||
(trade.currentPrice - trade.entryPrice) > 0 ? 'text-green-400' : 'text-red-400'
|
||||
}`}>
|
||||
${((trade.currentPrice - trade.entryPrice) >= 0 ? '+' : '')}${(trade.currentPrice - trade.entryPrice).toFixed(2)}
|
||||
({(((trade.currentPrice - trade.entryPrice) / trade.entryPrice) * 100).toFixed(2)}%)
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{/* Debug info for missing data */}
|
||||
{trade.result === 'UNKNOWN' && (
|
||||
<div className="text-xs text-yellow-400 mt-1">
|
||||
|
||||
Reference in New Issue
Block a user