feat: implement real-time price monitoring with automatic analysis triggering

New Features:
- Real-time price monitoring service with 5-minute update cycles
- Automatic analysis triggering when prices approach TP/SL levels (15%/25% thresholds)
- Comprehensive price monitoring UI component with live updates
- Integration with automation service for smart analysis scheduling
- PnL tracking and position status monitoring

- EventEmitter-based real-time updates
- CoinGecko API integration with rate limiting
- TP/SL approach detection with configurable thresholds
- Alert system for critical price movements
- Database integration for trade tracking

- Price monitor startup/shutdown with automation lifecycle
- Event listeners for TP_APPROACH, SL_APPROACH, CRITICAL alerts
- Automatic screenshot capture and AI analysis on price triggers
- Enhanced progress tracking for price-based analysis
- Intelligent analysis context with price movement data

- RealTimePriceMonitor component with live price display
- Trade monitoring cards with P&L and distance to TP/SL
- Active alerts panel with price threshold notifications
- Monitoring service controls (start/stop/force update)
- Integration with automation page for comprehensive oversight

- GET: Retrieve monitoring data, alerts, and current prices
- POST: Control monitoring service and force price updates
- Real-time data formatting and status management

- Comprehensive price monitor integration tests
- Basic functionality validation scripts
- API endpoint testing capabilities

This implements the user's request for real-time price monitoring with automatic analysis triggering when prices approach critical levels, providing enhanced oversight of active trading positions.
This commit is contained in:
mindesbunister
2025-07-21 10:31:49 +02:00
parent d0cabeb911
commit 7de3eaf7b8
8 changed files with 1171 additions and 1 deletions

View File

@@ -0,0 +1,84 @@
import { NextResponse } from 'next/server'
import priceMonitor from '../../../lib/price-monitor'
export async function GET() {
try {
// Get current monitoring data for all active trades
const monitoringData = await priceMonitor.getTradeMonitoringData()
const activeAlerts = priceMonitor.getActiveAlerts()
// Get price cache for quick reference
const priceData = {}
const symbols = [...new Set(monitoringData.map(trade => trade.symbol))]
for (const symbol of symbols) {
const price = priceMonitor.getCurrentPrice(symbol)
if (price) {
priceData[symbol] = price
}
}
return NextResponse.json({
success: true,
data: {
trades: monitoringData,
alerts: activeAlerts,
prices: priceData,
lastUpdated: new Date().toISOString(),
monitoringActive: true
}
})
} catch (error) {
console.error('Error getting trade monitoring data:', error)
return NextResponse.json({
success: false,
error: 'Failed to get monitoring data',
details: error.message
}, { status: 500 })
}
}
export async function POST(request) {
try {
const { action, symbol } = await request.json()
switch (action) {
case 'force_update':
if (symbol) {
const price = await priceMonitor.forceUpdatePrice(symbol)
return NextResponse.json({
success: true,
data: { symbol, price, updated: new Date().toISOString() }
})
}
break
case 'start_monitoring':
await priceMonitor.startMonitoring()
return NextResponse.json({
success: true,
message: 'Price monitoring started'
})
case 'stop_monitoring':
await priceMonitor.stopMonitoring()
return NextResponse.json({
success: true,
message: 'Price monitoring stopped'
})
default:
return NextResponse.json({
success: false,
error: 'Invalid action'
}, { status: 400 })
}
} catch (error) {
console.error('Error in price monitoring action:', error)
return NextResponse.json({
success: false,
error: 'Failed to execute action',
details: error.message
}, { status: 500 })
}
}