🔧 CRITICAL FIX: Price Data Sync & Position Monitor Enhancement

Fixed major price data sync issues:
- Removed hardcoded price (77.63) from position monitor
- Added real-time oracle data instead of stale TWAP pricing
- Implemented cache-busting headers for fresh data
- Updated fallback prices to current market levels

- Real-time P&L tracking with trend indicators (📈📉➡️)
- Enhanced stop loss proximity alerts with color-coded risk levels
- Analysis progress indicators during automation cycles
- Performance metrics (runtime, cycles, trades, errors)
- Fresh data validation and improved error handling

- Price accuracy: 77.63 → 84.47 (matches Drift UI)
- P&L accuracy: -.91 → -.59 (correct calculation)
- Risk assessment: CRITICAL → MEDIUM (proper evaluation)
- Stop loss distance: 0.91% → 4.8% (safe distance)

- CLI monitor script with 8-second updates
- Web dashboard component (PositionMonitor.tsx)
- Real-time automation status tracking
- Database and error monitoring improvements

This fixes the automation showing false emergency alerts when
position was actually performing normally.
This commit is contained in:
mindesbunister
2025-07-25 23:33:06 +02:00
parent 08f9a9b541
commit 9b6a393e06
18 changed files with 6783 additions and 361 deletions

View File

@@ -2,13 +2,18 @@ import { NextResponse } from 'next/server';
export async function GET() {
try {
// Get current positions
// Get current positions with real-time data
const baseUrl = process.env.INTERNAL_API_URL || 'http://localhost:3000';
const positionsResponse = await fetch(`${baseUrl}/api/drift/positions`);
const positionsResponse = await fetch(`${baseUrl}/api/drift/positions`, {
cache: 'no-store', // Force fresh data
headers: {
'Cache-Control': 'no-cache'
}
});
const positionsData = await positionsResponse.json();
// Get current price (you'd typically get this from an oracle)
const currentPrice = 177.63; // Placeholder - should come from price feed
// Use real-time price from Drift positions data
let currentPrice = 185.0; // Fallback price
const result = {
timestamp: new Date().toISOString(),
@@ -22,6 +27,10 @@ export async function GET() {
if (positionsData.success && positionsData.positions.length > 0) {
const position = positionsData.positions[0];
// Use real-time mark price from Drift
currentPrice = position.markPrice || position.entryPrice || currentPrice;
result.hasPosition = true;
result.position = {
symbol: position.symbol,
@@ -51,32 +60,23 @@ export async function GET() {
isNear: proximityPercent < 2.0 // Within 2% = NEAR
};
// Autonomous AI Risk Management
// Risk assessment
if (proximityPercent < 1.0) {
result.riskLevel = 'CRITICAL';
result.nextAction = 'AI EXECUTING: Emergency exit analysis - Considering position closure';
result.recommendation = 'AI_EMERGENCY_EXIT';
result.aiAction = 'EMERGENCY_ANALYSIS';
result.nextAction = 'IMMEDIATE ANALYSIS REQUIRED - Price very close to SL';
result.recommendation = 'EMERGENCY_ANALYSIS';
} else if (proximityPercent < 2.0) {
result.riskLevel = 'HIGH';
result.nextAction = 'AI ACTIVE: Reassessing position - May adjust stop loss or exit';
result.recommendation = 'AI_POSITION_REVIEW';
result.aiAction = 'URGENT_REASSESSMENT';
result.nextAction = 'Enhanced monitoring - Analyze within 5 minutes';
result.recommendation = 'URGENT_MONITORING';
} else if (proximityPercent < 5.0) {
result.riskLevel = 'MEDIUM';
result.nextAction = 'AI MONITORING: Enhanced analysis - Preparing contingency plans';
result.recommendation = 'AI_ENHANCED_WATCH';
result.aiAction = 'ENHANCED_ANALYSIS';
} else if (proximityPercent < 10.0) {
result.riskLevel = 'LOW';
result.nextAction = 'AI TRACKING: Standard monitoring - Position within normal range';
result.recommendation = 'AI_NORMAL_WATCH';
result.aiAction = 'STANDARD_MONITORING';
result.nextAction = 'Regular monitoring - Check every 10 minutes';
result.recommendation = 'NORMAL_MONITORING';
} else {
result.riskLevel = 'SAFE';
result.nextAction = 'AI RELAXED: Position secure - Looking for new opportunities';
result.recommendation = 'AI_OPPORTUNITY_SCAN';
result.aiAction = 'OPPORTUNITY_SCANNING';
result.riskLevel = 'LOW';
result.nextAction = 'Standard monitoring - Check every 30 minutes';
result.recommendation = 'RELAXED_MONITORING';
}
}