jetpackjules Claude commited on
Commit
32685f8
·
1 Parent(s): aca5d01

Add Date column and sort Investment Performance table by date

Browse files

- Added Date column on the right showing first buy date (YYYY-MM-DD format)
- Sort table by date with most recent trades first
- Captures first buy date for each symbol from order history
- Maintains all existing styling and functionality

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +10 -2
app.py CHANGED
@@ -351,7 +351,7 @@ def refresh_investment_performance_table():
351
  positions = get_current_positions()
352
 
353
  # Create proper empty DataFrame with correct column names
354
- columns = ['Symbol', 'Status', 'IPO Price', 'Buy Price', 'Sell Price', 'Investment', 'P&L ($)', 'P&L (%)']
355
 
356
  logger.info(f"Found {len(orders)} total orders for performance analysis")
357
 
@@ -401,6 +401,9 @@ def refresh_investment_performance_table():
401
  # Get IPO price if available
402
  ipo_price = ipo_price_lookup.get(symbol, 0)
403
 
 
 
 
404
  # Calculate sell price (average of all sells)
405
  if sell_orders:
406
  avg_sell_price = sum(float(o.filled_qty or 0) * float(o.filled_avg_price or 0) for o in sell_orders) / sum(float(o.filled_qty or 0) for o in sell_orders)
@@ -459,9 +462,14 @@ def refresh_investment_performance_table():
459
  'Investment': f"${investment:.2f}",
460
  'P&L ($)': pl_dollar_str,
461
  'P&L (%)': pl_percent_str,
462
- '_row_bg': row_bg # Store background color for styling
 
 
463
  })
464
 
 
 
 
465
  df = pd.DataFrame(invested_data)
466
  return df
467
 
 
351
  positions = get_current_positions()
352
 
353
  # Create proper empty DataFrame with correct column names
354
+ columns = ['Symbol', 'Status', 'IPO Price', 'Buy Price', 'Sell Price', 'Investment', 'P&L ($)', 'P&L (%)', 'Date']
355
 
356
  logger.info(f"Found {len(orders)} total orders for performance analysis")
357
 
 
401
  # Get IPO price if available
402
  ipo_price = ipo_price_lookup.get(symbol, 0)
403
 
404
+ # Get first buy date
405
+ first_buy_date = min(buy_orders, key=lambda x: x.filled_at).filled_at.strftime('%Y-%m-%d')
406
+
407
  # Calculate sell price (average of all sells)
408
  if sell_orders:
409
  avg_sell_price = sum(float(o.filled_qty or 0) * float(o.filled_avg_price or 0) for o in sell_orders) / sum(float(o.filled_qty or 0) for o in sell_orders)
 
462
  'Investment': f"${investment:.2f}",
463
  'P&L ($)': pl_dollar_str,
464
  'P&L (%)': pl_percent_str,
465
+ 'Date': first_buy_date,
466
+ '_row_bg': row_bg, # Store background color for styling
467
+ '_sort_date': min(buy_orders, key=lambda x: x.filled_at).filled_at # Store datetime for sorting
468
  })
469
 
470
+ # Sort by date (most recent first)
471
+ invested_data.sort(key=lambda x: x['_sort_date'], reverse=True)
472
+
473
  df = pd.DataFrame(invested_data)
474
  return df
475