# Tradebook Summary Cards Improvements
## Overview
Improved the summary cards display on the Tradebook page to show only relevant metrics based on the selected position status filter, and replaced generic "Total Buy Value" with more meaningful "Total Investment" and "Current Value" metrics.
## Changes Made
### 1. Conditional Card Display
Summary cards now intelligently show/hide based on the position status filter:
#### When viewing **All Positions** (status = 'all'):
- ✅ Active Holdings count
- ✅ Sold Positions count
- ✅ Total Investment
- ✅ Current Value
- ✅ Realized P&L
- ✅ Unrealized P&L
#### When viewing **Active Holdings** (status = 'active'):
- ✅ Active Holdings count
- ❌ Sold Positions (hidden - always 0)
- ✅ Total Investment
- ✅ Current Value
- ✅ Realized P&L (from past closed positions)
- ✅ Unrealized P&L
#### When viewing **Sold Positions** (status = 'sold'):
- ❌ Active Holdings (hidden - always 0)
- ✅ Sold Positions count
- ❌ Total Investment (hidden - no active holdings)
- ❌ Current Value (hidden - no active holdings)
- ✅ Realized P&L
- ❌ Unrealized P&L (hidden - always 0)
### 2. New Metrics
#### Total Investment
- **Definition**: Cost basis of current active holdings only
- **Calculation**: Sum of `activePositionCost` from FIFO calculation
- **Use Case**: Shows how much capital is currently locked in active positions
- **Example**:
```
Stock A: Bought 100 @ ₹50, Sold 30, Holding 70
Stock B: Bought 50 @ ₹100, Holding 50
Total Investment = (70 × ₹50) + (50 × ₹100) = ₹3,500 + ₹5,000 = ₹8,500
```
#### Current Value
- **Definition**: Current market value of active holdings
- **Calculation**: Sum of `currentValue` for active positions
- **Use Case**: Shows total portfolio worth at current prices
- **Example**:
```
Stock A: 70 shares @ current ₹60 = ₹4,200
Stock B: 50 shares @ current ₹120 = ₹6,000
Current Value = ₹4,200 + ₹6,000 = ₹10,200
```
### 3. Removed Metrics
#### Total Buy Value (Removed)
- **Why Removed**: Confusing and misleading
- **Problem**: Included all historical buys, even for closed positions
- **Example of Confusion**:
```
You bought 100 shares @ ₹100 = ₹10,000
You sold all 100 shares
Old Display: Total Buy Value = ₹10,000 (misleading - nothing invested now!)
New Display: No investment shown (correct - nothing invested now!)
```
## Technical Implementation
### Backend Changes
**File**: `equity/app/api/tradebook/route.ts`
1. **Added `investment` field to TradeGroup interface**:
```typescript
interface TradeGroup {
// ... existing fields ...
investment: number; // Cost basis from FIFO activePositionCost
}
```
2. **Calculate investment per group**:
```typescript
group.investment = fifoResult.activePositionCost;
```
3. **Updated summary calculation**:
```typescript
const summary = {
// ... existing fields ...
totalInvestment: activeGroups.reduce((sum, g) => sum + g.investment, 0),
totalCurrentValue: activeGroups.reduce((sum, g) => sum + g.currentValue, 0),
};
```
### Frontend Changes
**File**: `equity/app/tradebook/page.tsx`
1. **Updated TradebookData interface**:
```typescript
interface TradebookData {
summary: {
// ... existing fields ...
totalInvestment: number;
totalCurrentValue: number;
};
}
```
2. **Conditional card rendering** based on `statusFilter`:
- Active Holdings: Show when `statusFilter !== 'sold'`
- Sold Positions: Show when `statusFilter !== 'active'`
- Investment & Current Value: Show when `statusFilter !== 'sold'` and value > 0
- Unrealized P&L: Show when `statusFilter !== 'sold'`
- Realized P&L: Always show
3. **Reduced grid columns**: Changed from `md:grid-cols-5` to `md:grid-cols-4` for better spacing
## User Benefits
### 1. Cleaner UI
- No more confusing "0" values cluttering the screen
- Only relevant metrics displayed
### 2. Better Context
- When viewing sold positions, focus on realized P&L
- When viewing active holdings, see investment vs current value
- When viewing all, see complete picture
### 3. More Meaningful Metrics
- "Total Investment" is clearer than "Total Buy Value"
- Shows actual capital deployed, not historical purchases
- Current Value enables quick P&L calculation (Current - Investment)
## Examples
### Example 1: Active Holdings Filter
**Before**:
```
Active: 5 Sold: 0 Total Buy Value: ₹50,000
Realized P&L: ₹0 Unrealized P&L: +₹5,000
```
**After**:
```
Active Holdings: 5
Total Investment: ₹45,000 Current Value: ₹50,000
Realized P&L: ₹0 Unrealized P&L: +₹5,000
```
✅ Cleaner, no useless "Sold: 0", shows meaningful investment metrics
### Example 2: Sold Positions Filter
**Before**:
```
Active: 0 Sold: 3 Total Buy Value: ₹100,000
Realized P&L: +₹15,000 Unrealized P&L: ₹0
```
**After**:
```
Sold Positions: 3
Realized P&L: +₹15,000
```
✅ Much cleaner, only relevant metrics, no confusing investment/value for sold stocks
### Example 3: All Positions
**Before**:
```
Active: 5 Sold: 3 Total Buy Value: ₹150,000
Realized P&L: +₹15,000 Unrealized P&L: +₹5,000
```
**After**:
```
Active Holdings: 5 Sold Positions: 3
Total Investment: ₹45,000 Current Value: ₹50,000
Realized P&L: +₹15,000 Unrealized P&L: +₹5,000
```
✅ Complete picture with meaningful metrics
## Database Changes
**None** ✅ - Pure calculation changes
## Migration Required
**None** ✅ - Works immediately on refresh
## Performance Impact
- **Minimal**: Added one simple field calculation per group
- **No additional queries**: Uses existing FIFO calculation results
- **No API calls**: Pure calculation from in-memory data
## Testing Checklist
- [x] Filter by "All Positions" - Shows all cards
- [x] Filter by "Active Holdings" - Hides sold-related cards
- [x] Filter by "Sold Positions" - Hides active-related cards
- [x] Investment value matches sum of active position costs
- [x] Current value matches sum of active position market values
- [x] No linter errors
- [x] Responsive layout works on mobile
## Future Enhancements (Not Implemented)
Potential additions:
1. **Total P&L card**: Combined realized + unrealized
2. **Portfolio Returns %**: (Current Value - Investment) / Investment × 100
3. **Average XIRR**: Weighted average XIRR across all positions
4. **Day Change**: Today's gain/loss
5. **Dividend Yield**: If dividend data available
---
**Status**: ✅ Implemented
**Date**: December 30, 2025
**Breaking Changes**: None
**Migration Required**: None