Files
gogo2/TRADING_STATS_DISPLAY_FIX.md
2025-12-10 15:09:27 +02:00

3.0 KiB

Trading Stats Display Fix - Complete

Problem Identified

The ANNOTATE app was showing static placeholder values for trading stats:

  • Position: "NO POSITION" (never updated)
  • Session PnL: "+$0.00" (never updated)
  • Win Rate: "0% (0/0)" (never updated)
  • Model Accuracy/Loss: "--" (never updated)

Root Cause

The HTML template had the UI elements but there was no backend API or JavaScript to:

  1. Fetch real trading statistics from the orchestrator/trading executor
  2. Update the UI elements with live data
  3. Poll for updates during active trading

Fixes Applied

1. Added Trading Stats API Endpoint

@self.server.route('/api/trading-stats', methods=['GET'])
def get_trading_stats():

This endpoint provides:

  • Position status: Current open positions from TradingExecutor
  • Session PnL: Total profit/loss for the session
  • Win rate: Percentage of winning trades
  • Floating PnL: Unrealized P&L for open positions
  • Model performance: Latest accuracy and loss from transformer trainer
  • Trading activity: Whether trading is active or just predictions

2. Added JavaScript Polling System

  • Automatic polling: Updates every 2 seconds during active inference
  • Smart UI updates: Shows/hides elements based on trading state
  • Color coding: Green for profits, red for losses
  • Integration: Hooks into existing inference start/stop functions

3. Enhanced UI Display Logic

  • Position display: Shows "LONG ETH/USDT" instead of "NO POSITION" when trading
  • PnL formatting: Proper +/- signs and color coding
  • Floating PnL: Shows unrealized P&L for open positions
  • Model stats: Live accuracy and loss updates
  • Trading status: Clear indication of active vs inactive trading

Expected Behavior Now

During Active Trading:

  • Position: Shows actual position (e.g., "LONG ETH/USDT")
  • Session PnL: Updates with real profit/loss (e.g., "+$15.23" in green)
  • Win Rate: Shows actual performance (e.g., "75.0% (3/4)")
  • Floating PnL: Shows unrealized P&L for open positions
  • Model Stats: Live accuracy and loss from training

During Predictions Only:

  • Position: "NO POSITION"
  • Session PnL: "$0.00"
  • Win Rate: "0% (0/0)"
  • Warning: "PREDICTIONS ONLY" message shown
  • Model Stats: Still shows model performance

API Response Format

{
  "success": true,
  "position": "LONG ETH/USDT",
  "session_pnl": 15.23,
  "win_rate": "75.0% (3/4)",
  "floating_pnl": 8.45,
  "total_trades": 4,
  "winning_trades": 3,
  "trading_active": true,
  "model_accuracy": "73.2%",
  "model_loss": "0.1234"
}

Verification Steps

  1. Start inference: Trading stats should begin updating every 2 seconds
  2. Execute trades: Position and PnL should reflect actual trades
  3. Model training: Accuracy and loss should update with training progress
  4. Stop inference: Polling should stop, stats should reset to defaults

The trading dashboard now provides real-time visibility into both trading performance and model learning progress!