get_portfolio_summary
Retrieve a comprehensive summary of your trading portfolio, including account details and current open positions, to monitor investment performance.
Instructions
Get a comprehensive summary of the portfolio including account details and open positions.
Returns: Portfolio summary with account and positions information
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/server.py:547-614 (handler)The handler function decorated with @mcp.tool(), which serves as both the tool implementation and registration in MCP. It retrieves account information and positions using helper functions, then formats a comprehensive portfolio summary including account details, position breakdowns, P/L calculations, and allocations.@mcp.tool() def get_portfolio_summary() -> str: """ Get a comprehensive summary of the portfolio including account details and open positions. Returns: Portfolio summary with account and positions information """ try: # Get account info account = calls.get_account(trading_client) # Get all positions positions = calls.get_positions(trading_client) # Generate summary summary = ( f"Portfolio Summary\n" f"=================\n\n" f"Account Information:\n" f"-------------------\n" f"Status: {account.status}\n" f"Cash: ${account.cash:.2f}\n" f"Portfolio Value: ${account.portfolio_value:.2f}\n" f"Buying Power: ${account.buying_power:.2f}\n" f"Equity: ${account.equity:.2f}\n" f"Daytrade Count: {account.daytrade_count}\n" f"Pattern Day Trader: {account.pattern_day_trader}\n\n" ) if positions: summary += f"Open Positions ({len(positions)}):\n-------------------\n" # Calculate total P/L and allocation total_pl = sum(pos.unrealized_pl for pos in positions) total_value = account.portfolio_value - account.cash for pos in positions: pl_percent = pos.unrealized_plpc * 100 pl_sign = "+" if pos.unrealized_pl >= 0 else "" allocation = (pos.market_value / account.portfolio_value) * 100 if account.portfolio_value > 0 else 0 summary += ( f"{pos.symbol} ({pos.side.value.upper()}):\n" f" Quantity: {pos.qty}\n" f" Avg Entry: ${pos.avg_entry_price:.2f}\n" f" Current: ${pos.current_price:.2f}\n" f" Value: ${pos.market_value:.2f} ({allocation:.2f}% of portfolio)\n" f" P/L: {pl_sign}${pos.unrealized_pl:.2f} ({pl_sign}{pl_percent:.2f}%)\n\n" ) # Add overall P/L summary overall_pl_percent = (total_pl / total_value) * 100 if total_value > 0 else 0 pl_sign = "+" if total_pl >= 0 else "" summary += ( f"Overall Position Summary:\n" f"------------------------\n" f"Total Position Value: ${total_value:.2f}\n" f"Total Unrealized P/L: {pl_sign}${total_pl:.2f} ({pl_sign}{overall_pl_percent:.2f}%)\n" f"Cash Allocation: ${account.cash:.2f} ({(account.cash / account.portfolio_value) * 100:.2f}% of portfolio)\n" ) else: summary += "No open positions." return summary except Exception as e: return f"Error generating portfolio summary: {str(e)}"