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:
mindesbunister
2025-07-21 17:08:48 +02:00
parent d7a1b96a80
commit 491ff51ba9
7 changed files with 859 additions and 53 deletions

View File

@@ -38,8 +38,8 @@ class PriceMonitor extends EventEmitter {
private priceCache: Map<string, { price: number; timestamp: number }> = new Map()
private alerts: Map<string, PriceAlert> = new Map()
private isRunning = false
private readonly UPDATE_INTERVAL = 5 * 60 * 1000 // 5 minutes
private readonly CACHE_DURATION = 6 * 60 * 1000 // 6 minutes (slightly longer than update)
private readonly UPDATE_INTERVAL = 1 * 60 * 1000 // 1 minute for more responsive monitoring
private readonly CACHE_DURATION = 2 * 60 * 1000 // 2 minutes (slightly longer than update)
// Thresholds for triggering analysis
private readonly TP_APPROACH_THRESHOLD = 0.15 // 15% away from TP
@@ -229,13 +229,34 @@ class PriceMonitor extends EventEmitter {
const entryPrice = trade.entryPrice || trade.price
const leverage = trade.leverage || 1
// Calculate PnL
// 🔥 FIX: Get actual trading amount from session settings
const session = await prisma.automationSession.findFirst({
where: { userId: trade.userId, symbol: trade.symbol },
orderBy: { createdAt: 'desc' }
})
const sessionSettings = session?.settings as any
const actualTradingAmount = trade.tradingAmount || sessionSettings?.tradingAmount || 34
const storedPositionValue = trade.amount * trade.price
const adjustmentRatio = actualTradingAmount / storedPositionValue
// Calculate PnL based on actual investment amount
const priceChange = trade.side === 'BUY' ?
(currentPrice - entryPrice) :
(entryPrice - currentPrice)
const currentPnL = priceChange * trade.amount * leverage
const pnlPercentage = (priceChange / entryPrice) * 100 * leverage
const rawPnL = priceChange * trade.amount * leverage
const currentPnL = rawPnL * adjustmentRatio // Adjust for actual investment
const pnlPercentage = (currentPnL / actualTradingAmount) * 100
console.log(`💰 Price Monitor P&L Calculation:`, {
tradeId: trade.id,
actualTradingAmount,
storedPositionValue: storedPositionValue.toFixed(2),
adjustmentRatio: adjustmentRatio.toFixed(4),
rawPnL: rawPnL.toFixed(2),
adjustedPnL: currentPnL.toFixed(2)
})
// Calculate distances to TP/SL
let distanceToTP: number | undefined