fix: Resolve win rate and P&L discrepancies between Status and AI Learning sections

- Fixed analysis-details API to use stored profit field as fallback when exit prices missing
- Updated UI to use Status API data instead of calculating from limited recent trades
- Modified AI Learning Status to use real database trade data instead of demo numbers
- Enhanced price monitor with automatic trade closing logic for TP/SL hits
- Modified automation service to create trades with OPEN status for proper monitoring
- Added test scripts for creating OPEN trades and validating monitoring system

Key changes:
- Status section now shows accurate 50% win rate from complete database
- AI Learning Status shows consistent metrics based on real trading performance
- Both sections display same correct P&L (8.62) from actual trade results
- Real-time price monitor properly detects and tracks OPEN status trades
- Fixed trade lifecycle: OPEN → monitoring → COMPLETED when TP/SL hit

All trading performance metrics now display consistent, accurate data from the same source.
This commit is contained in:
mindesbunister
2025-07-21 12:56:14 +02:00
parent aae715dd07
commit d7a1b96a80
9 changed files with 1454 additions and 75 deletions

View File

@@ -95,10 +95,13 @@ export default function AutomationPage() {
const fetchRecentTrades = async () => {
try {
console.log('🔍 Fetching recent trades...')
// Get enhanced trade data from analysis-details instead of recent-trades
const response = await fetch('/api/automation/analysis-details')
const data = await response.json()
console.log('📊 Trade data response:', data.success, data.data?.recentTrades?.length || 0)
if (data.success && data.data.recentTrades) {
console.log('✅ Setting recent trades:', data.data.recentTrades.length)
setRecentTrades(data.data.recentTrades)
}
} catch (error) {
@@ -211,22 +214,14 @@ export default function AutomationPage() {
setSelectedTrade(null)
}
// Calculate win rate from recent trades
const calculateWinRate = () => {
if (!recentTrades.length) return 0
const completedTrades = recentTrades.filter(t => t.status === 'COMPLETED')
if (!completedTrades.length) return 0
const winningTrades = completedTrades.filter(t => t.result === 'WIN')
return (winningTrades.length / completedTrades.length * 100).toFixed(1)
// Use status API data instead of calculating from limited recent trades
const getWinRate = () => {
return status?.winRate || 0
}
// Calculate total P&L from recent trades
const calculateTotalPnL = () => {
if (!recentTrades.length) return 0
return recentTrades
.filter(t => t.status === 'COMPLETED' && t.pnl)
.reduce((total, trade) => total + parseFloat(trade.pnl), 0)
.toFixed(2)
// Use status API data for total P&L
const getTotalPnL = () => {
return status?.totalPnL || 0
}
return (
@@ -583,19 +578,19 @@ export default function AutomationPage() {
<div className="flex justify-between">
<span className="text-gray-300">Win Rate:</span>
<span className={`font-semibold ${
calculateWinRate() > 60 ? 'text-green-400' :
calculateWinRate() > 40 ? 'text-yellow-400' : 'text-red-400'
getWinRate() > 60 ? 'text-green-400' :
getWinRate() > 40 ? 'text-yellow-400' : 'text-red-400'
}`}>
{calculateWinRate()}%
{getWinRate()}%
</span>
</div>
<div className="flex justify-between">
<span className="text-gray-300">Total P&L:</span>
<span className={`font-semibold ${
calculateTotalPnL() > 0 ? 'text-green-400' :
calculateTotalPnL() < 0 ? 'text-red-400' : 'text-gray-300'
getTotalPnL() > 0 ? 'text-green-400' :
getTotalPnL() < 0 ? 'text-red-400' : 'text-gray-300'
}`}>
${calculateTotalPnL()}
${getTotalPnL()}
</span>
</div>
{status.lastAnalysis && (
@@ -620,7 +615,8 @@ 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</h2>
<h2 className="text-xl font-bold text-white mb-4">Latest 4 Automated Trades (Debug: {recentTrades.length})</h2>
{console.log('🎯 Rendering trades section, count:', recentTrades.length)}
{recentTrades.length > 0 ? (
<div className="space-y-4">
{recentTrades.slice(0, 4).map((trade, idx) => (