Skip to content

Latest commit

ย 

History

History
444 lines (364 loc) ยท 10.9 KB

File metadata and controls

444 lines (364 loc) ยท 10.9 KB

Portfolio Analytics - Feature Overview

๐ŸŽฏ What Was Built

A comprehensive Portfolio Analytics Dashboard for the Stellar Dev Dashboard that provides users with professional-grade portfolio insights, visualizations, and performance metrics.

๐Ÿ“‹ Component Structure

PortfolioAnalytics Dashboard
โ”‚
โ”œโ”€โ”€ ๐Ÿ“Š Header Section
โ”‚   โ”œโ”€โ”€ Title: "Portfolio Analytics"
โ”‚   โ”œโ”€โ”€ Last Update Timestamp
โ”‚   โ”œโ”€โ”€ Auto-Refresh Toggle Button
โ”‚   โ””โ”€โ”€ Manual Refresh Button
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ˆ Key Metrics Grid (4-6 cards)
โ”‚   โ”œโ”€โ”€ Total Portfolio Value (USD)
โ”‚   โ”œโ”€โ”€ 24h Change (% and $)
โ”‚   โ”œโ”€โ”€ Diversification Score (0-100)
โ”‚   โ”œโ”€โ”€ Risk Level (Low/Medium/High)
โ”‚   โ”œโ”€โ”€ Volatility (%) [Desktop]
โ”‚   โ””โ”€โ”€ Sharpe Ratio [Desktop]
โ”‚
โ”œโ”€โ”€ ๐Ÿ“Š Charts Section (2-column grid)
โ”‚   โ”œโ”€โ”€ Asset Allocation Pie Chart
โ”‚   โ”‚   โ”œโ”€โ”€ Color-coded segments
โ”‚   โ”‚   โ”œโ”€โ”€ Percentage labels
โ”‚   โ”‚   โ”œโ”€โ”€ Interactive tooltips
โ”‚   โ”‚   โ””โ”€โ”€ Legend with asset codes
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ Portfolio Value Over Time (Area Chart)
โ”‚       โ”œโ”€โ”€ Time range selector (7D/30D/90D/1Y)
โ”‚       โ”œโ”€โ”€ Gradient fill
โ”‚       โ”œโ”€โ”€ Smooth line interpolation
โ”‚       โ”œโ”€โ”€ Date X-axis
โ”‚       โ”œโ”€โ”€ USD Y-axis
โ”‚       โ””โ”€โ”€ Hover tooltips
โ”‚
โ”œโ”€โ”€ ๐Ÿ“Š Asset Performance Bar Chart
โ”‚   โ”œโ”€โ”€ Horizontal layout
โ”‚   โ”œโ”€โ”€ 24h change per asset
โ”‚   โ”œโ”€โ”€ Color-coded (green/red)
โ”‚   โ””โ”€โ”€ Percentage values
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‹ Detailed Holdings Table
โ”‚   โ”œโ”€โ”€ Columns:
โ”‚   โ”‚   โ”œโ”€โ”€ Asset (with color dot)
โ”‚   โ”‚   โ”œโ”€โ”€ Balance
โ”‚   โ”‚   โ”œโ”€โ”€ Price (USD)
โ”‚   โ”‚   โ”œโ”€โ”€ Value (USD)
โ”‚   โ”‚   โ”œโ”€โ”€ Allocation (%)
โ”‚   โ”‚   โ””โ”€โ”€ 24h Change (with icon)
โ”‚   โ”œโ”€โ”€ Sortable
โ”‚   โ”œโ”€โ”€ Hover effects
โ”‚   โ””โ”€โ”€ Mobile responsive
โ”‚
โ”œโ”€โ”€ โš ๏ธ Risk Assessment Panel
โ”‚   โ”œโ”€โ”€ Risk Metrics Cards
โ”‚   โ”‚   โ”œโ”€โ”€ Risk Level
โ”‚   โ”‚   โ”œโ”€โ”€ Risk Score (0-100)
โ”‚   โ”‚   โ””โ”€โ”€ Concentration Risks Count
โ”‚   โ”œโ”€โ”€ Risk Factors List
โ”‚   โ”‚   โ”œโ”€โ”€ Factor description
โ”‚   โ”‚   โ”œโ”€โ”€ Impact level
โ”‚   โ”‚   โ””โ”€โ”€ Visual indicators
โ”‚   โ””โ”€โ”€ Concentrated Assets List
โ”‚       โ”œโ”€โ”€ Asset code
โ”‚       โ”œโ”€โ”€ Allocation percentage
โ”‚       โ””โ”€โ”€ Risk level badge
โ”‚
โ”œโ”€โ”€ ๐Ÿ“Š Performance Metrics Grid [Desktop]
โ”‚   โ”œโ”€โ”€ Average Daily Return
โ”‚   โ”œโ”€โ”€ Highest Value Asset
โ”‚   โ””โ”€โ”€ Number of Assets
โ”‚
โ””โ”€โ”€ ๐Ÿ“œ Account History Panel
    โ”œโ”€โ”€ Description text
    โ””โ”€โ”€ Period Comparison Cards
        โ”œโ”€โ”€ Period Start Value & Date
        โ”œโ”€โ”€ Current Value & Date
        โ””โ”€โ”€ Period Change (% and $)

๐ŸŽจ Visual Design

Color Scheme

Chart Colors (10 colors for assets):

  1. #00d4ff - Cyan (primary)
  2. #00ff88 - Green
  3. #ff6b6b - Red
  4. #ffd93d - Yellow
  5. #a78bfa - Purple
  6. #fb923c - Orange
  7. #ec4899 - Pink
  8. #14b8a6 - Teal
  9. #f59e0b - Amber
  10. #8b5cf6 - Violet

Status Colors:

  • Green (#00e676): Positive performance, low risk, good diversification
  • Red (#ff1744): Negative performance, high risk
  • Amber (#ffb300): Medium risk, moderate diversification
  • Cyan (#00e5ff): Primary actions, highlights

Typography

  • Headings: Syne font, 22px, bold
  • Body: System font, 13px
  • Numbers: Space Mono (monospace), 13px
  • Labels: 11px, uppercase, letter-spacing

Layout

  • Desktop: 4-column grid for metrics
  • Tablet: 3-column grid
  • Mobile: 2-column grid
  • Spacing: 12-20px gaps
  • Border Radius: 8-12px
  • Borders: 1px solid with theme colors

๐Ÿ”ข Metrics Explained

1. Total Portfolio Value

Sum of (Asset Balance ร— Asset Price) for all assets
Displayed in USD with 2 decimal places
Example: $12,345.67

2. 24h Change

Percentage: ((Current Value - 24h Ago Value) / 24h Ago Value) ร— 100
Absolute: Current Value - 24h Ago Value
Example: +5.23% ($645.12)

3. Diversification Score (0-100)

Uses Herfindahl-Hirschman Index (HHI)
HHI = ฮฃ(allocation%)ยฒ
Score = ((10000 - HHI) / (10000 - 10000/n)) ร— 100

Interpretation:
- 70-100: Well diversified
- 40-69: Moderately diversified
- 0-39: Poorly diversified

4. Risk Level

Calculated from 3 factors:
- Volatility (0-40 points)
- Diversification (0-30 points)
- Concentration (0-30 points)

Total Score โ†’ Risk Level:
- 0-30: Low
- 31-60: Medium
- 61-100: High

5. Volatility

Standard deviation of daily returns
returns[i] = (value[i] - value[i-1]) / value[i-1]
volatility = ฯƒ(returns) ร— 100

Interpretation:
- <5%: Low volatility
- 5-10%: Moderate volatility
- >10%: High volatility

6. Sharpe Ratio

(Portfolio Return - Risk-Free Rate) / Volatility
Default risk-free rate: 4%

Interpretation:
- >1.0: Good risk-adjusted returns
- 0-1.0: Fair returns
- <0: Poor returns

๐Ÿ“Š Chart Types

1. Pie Chart - Asset Allocation

Purpose: Show percentage distribution of portfolio across assets

Features:

  • Interactive segments
  • Percentage labels on slices
  • Hover tooltips with USD values
  • Color-coded for easy identification
  • Responsive sizing (80px mobile, 100px desktop)

Data Format:

[
  { code: 'XLM', allocation: 45.5, valueUsd: 5465.23 },
  { code: 'USDC', allocation: 30.2, valueUsd: 3624.50 },
  { code: 'BTC', allocation: 24.3, valueUsd: 2916.10 }
]

2. Area Chart - Portfolio Value Over Time

Purpose: Track portfolio value changes over selected time period

Features:

  • Multiple time ranges (7D, 30D, 90D, 1Y)
  • Gradient fill for visual appeal
  • Smooth monotone line
  • Grid lines for readability
  • Formatted axes (dates and USD)

Data Format:

[
  { timestamp: 1234567890, date: '2024-01-01', value: 10500.50 },
  { timestamp: 1234654290, date: '2024-01-02', value: 10750.25 },
  ...
]

3. Bar Chart - Asset Performance

Purpose: Compare 24h performance across all assets

Features:

  • Horizontal layout for readability
  • Color-coded by performance (green/red)
  • Sorted by performance (best to worst)
  • Asset codes as labels
  • Percentage values

Data Format:

[
  { code: 'XLM', change24h: 5.2, valueUsd: 5465.23 },
  { code: 'USDC', change24h: 0.1, valueUsd: 3624.50 },
  { code: 'BTC', change24h: -2.3, valueUsd: 2916.10 }
]

๐Ÿ“ฑ Responsive Behavior

Desktop (>1024px)

  • 4-column metrics grid
  • 2-column charts layout
  • 6 metric cards visible
  • All features enabled
  • Larger chart sizes (320px)

Tablet (769-1024px)

  • 3-column metrics grid
  • 1-column charts layout
  • 4 metric cards visible
  • All features enabled
  • Medium chart sizes (280px)

Mobile (โ‰ค768px)

  • 2-column metrics grid
  • 1-column charts layout
  • 4 metric cards visible
  • Reduced font sizes (9px)
  • Smaller chart sizes (280px)
  • Stacked table columns
  • Touch-friendly targets (48ร—48px)

๐Ÿ”„ Data Flow

User Opens Dashboard
       โ†“
Extract Asset Codes from Balances
       โ†“
Fetch Prices from CoinGecko API
       โ†“
Calculate Portfolio Value (USD)
       โ†“
Fetch Historical Effects from Horizon
       โ†“
Reconstruct Balance History
       โ†“
Calculate Analytics Metrics
       โ†“
Render All Visualizations
       โ†“
Start Auto-Refresh Timer (60s)
       โ†“
Update Prices โ†’ Recalculate โ†’ Re-render

๐ŸŽฏ User Interactions

Header Controls

  1. Auto-Refresh Toggle

    • Click to enable/disable
    • Active state: Cyan background
    • Inactive state: Grey background
    • Icon: Spinning refresh icon when active
  2. Manual Refresh Button

    • Click to fetch latest prices immediately
    • Shows "Refreshing..." during fetch
    • Disabled state while loading
    • Updates last update timestamp

Time Range Selector

  • Four buttons: 7D, 30D, 90D, 1Y
  • Active range: Cyan background
  • Inactive ranges: Grey background
  • Click to switch time period
  • Chart updates immediately

Charts

  • Hover: Shows tooltip with detailed values
  • Mobile: Pinch to zoom, pan to scroll
  • Reset: Button appears when zoomed >1.1x

Table

  • Hover: Row highlights with background color
  • Click: (Could be extended for details view)
  • Scroll: Horizontal scroll on mobile

๐Ÿšจ Error Handling

No Data States

  1. No Account Connected

    • Shows empty state with info icon
    • Message: "Connect an account with balances"
  2. No Balances

    • Shows empty state
    • Message: "No Portfolio Data"
  3. Price Fetch Error

    • Shows error message
    • Allows retry
    • Fallbacks to previous data if available

Loading States

  1. Initial Load

    • Full-page spinner
    • Message: "Loading portfolio data..."
  2. Price Refresh

    • Button shows "Refreshing..."
    • Button disabled during fetch
    • No page disruption
  3. Historical Data

    • Chart area shows spinner
    • Other sections remain visible

โœจ Special Features

1. Real-Time Updates

  • Automatic price refresh every 60 seconds
  • Manual refresh button for immediate updates
  • Last update timestamp display
  • Non-intrusive updates (no page reload)

2. Risk Awareness

  • Concentration risk detection (>25% allocation)
  • Multi-factor risk scoring
  • Visual risk indicators (color-coded)
  • Actionable risk factors

3. Mobile Optimization

  • Touch-friendly charts with gestures
  • Responsive grid layouts
  • Adaptive font sizes
  • Safe area insets for notched devices

4. Accessibility

  • ARIA labels on all interactive elements
  • Keyboard navigation support
  • Screen reader friendly
  • High contrast colors
  • Touch targets โ‰ฅ48ร—48px

๐Ÿ”ฎ Future Possibilities

The component is architected to support future enhancements:

  1. Export Features

    • Download charts as images
    • Export data as CSV/JSON
    • Generate PDF reports
  2. Advanced Analytics

    • Correlation matrix
    • Beta vs benchmarks
    • Monte Carlo simulations
    • Drawdown analysis
  3. Alerts

    • Price alerts
    • Risk threshold warnings
    • Rebalancing notifications
  4. Customization

    • Drag-and-drop layout
    • Custom chart configurations
    • Personalized metrics

๐Ÿ“„ Files Reference

Component File

src/components/dashboard/PortfolioAnalytics.tsx (650+ lines)

Integration Files

  • src/App.tsx - Route registration
  • src/components/layout/Sidebar.tsx - Navigation link

Used Libraries

  • src/lib/portfolioAnalytics.js - Analytics calculations
  • src/lib/priceFeed.js - Price fetching
  • src/lib/stellar.ts - Horizon API integration
  • src/lib/store.ts - State management

UI Components

  • src/components/dashboard/Card.tsx - Card and StatCard
  • src/components/charts/MobileChartContainer.tsx - Touch gestures
  • src/hooks/useResponsive.ts - Breakpoint detection

This document provides a complete overview of the Portfolio Analytics feature implementation.

For technical details, see PORTFOLIO_ANALYTICS_IMPLEMENTATION.md For PR information, see PR_SUMMARY.md