Spaces:
Sleeping
Sleeping
Update pyfolio/mcp_output/mcp_plugin/mcp_service.py
Browse files
pyfolio/mcp_output/mcp_plugin/mcp_service.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
|
|
|
|
|
|
| 3 |
|
| 4 |
source_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), "source")
|
| 5 |
sys.path.insert(0, source_path)
|
|
@@ -17,155 +19,301 @@ from pyfolio.tears import (
|
|
| 17 |
create_perf_attrib_tear_sheet,
|
| 18 |
)
|
| 19 |
from pyfolio.plotting import (
|
| 20 |
-
axes_style,
|
| 21 |
-
customize,
|
| 22 |
plot_annual_returns,
|
| 23 |
-
plot_capacity_sweep,
|
| 24 |
-
plot_cones,
|
| 25 |
-
plot_daily_turnover_hist,
|
| 26 |
-
plot_daily_volume,
|
| 27 |
-
plot_drawdown_periods,
|
| 28 |
-
plot_drawdown_underwater,
|
| 29 |
-
plot_exposures,
|
| 30 |
-
plot_gross_leverage,
|
| 31 |
-
plot_holdings,
|
| 32 |
-
plot_long_short_holdings,
|
| 33 |
-
plot_max_median_position_concentration,
|
| 34 |
-
plot_monthly_returns_dist,
|
| 35 |
plot_monthly_returns_heatmap,
|
| 36 |
-
|
| 37 |
-
plot_perf_stats,
|
| 38 |
-
plot_prob_profit_trade,
|
| 39 |
-
plot_return_quantiles,
|
| 40 |
-
plot_returns,
|
| 41 |
-
plot_rolling_beta,
|
| 42 |
plot_rolling_returns,
|
| 43 |
-
plot_rolling_sharpe,
|
| 44 |
-
plot_rolling_volatility,
|
| 45 |
-
plot_round_trip_lifetimes,
|
| 46 |
-
plot_sector_allocations,
|
| 47 |
-
plot_slippage_sensitivity,
|
| 48 |
-
plot_slippage_sweep,
|
| 49 |
plot_turnover,
|
| 50 |
-
plot_txn_time_hist,
|
| 51 |
-
plotting_context,
|
| 52 |
-
show_and_plot_top_positions,
|
| 53 |
-
show_perf_stats,
|
| 54 |
-
show_profit_attribution,
|
| 55 |
-
show_worst_drawdown_periods,
|
| 56 |
)
|
| 57 |
|
| 58 |
mcp = FastMCP("pyfolio_service")
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
@mcp.tool(name="generate_full_tear_sheet", description="Generate a comprehensive tear sheet for portfolio analysis.")
|
| 61 |
def generate_full_tear_sheet(returns: list, positions: dict = None, transactions: dict = None, benchmark_rets: list = None) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
try:
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
return {"success": True, "result": "Full tear sheet generated successfully.", "error": None}
|
| 65 |
except Exception as e:
|
| 66 |
return {"success": False, "result": None, "error": str(e)}
|
| 67 |
|
| 68 |
@mcp.tool(name="generate_simple_tear_sheet", description="Generate a basic tear sheet for portfolio analysis.")
|
| 69 |
def generate_simple_tear_sheet(returns: list) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
try:
|
| 71 |
-
|
|
|
|
| 72 |
return {"success": True, "result": "Simple tear sheet generated successfully.", "error": None}
|
| 73 |
except Exception as e:
|
| 74 |
return {"success": False, "result": None, "error": str(e)}
|
| 75 |
|
| 76 |
@mcp.tool(name="generate_returns_tear_sheet", description="Generate a tear sheet for returns analysis.")
|
| 77 |
def generate_returns_tear_sheet(returns: list) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
try:
|
| 79 |
-
|
|
|
|
| 80 |
return {"success": True, "result": "Returns tear sheet generated successfully.", "error": None}
|
| 81 |
except Exception as e:
|
| 82 |
return {"success": False, "result": None, "error": str(e)}
|
| 83 |
|
| 84 |
@mcp.tool(name="generate_position_tear_sheet", description="Generate a tear sheet for position analysis.")
|
| 85 |
def generate_position_tear_sheet(positions: dict) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
try:
|
| 87 |
-
|
|
|
|
| 88 |
return {"success": True, "result": "Position tear sheet generated successfully.", "error": None}
|
| 89 |
except Exception as e:
|
| 90 |
return {"success": False, "result": None, "error": str(e)}
|
| 91 |
|
| 92 |
@mcp.tool(name="generate_transaction_tear_sheet", description="Generate a tear sheet for transaction analysis.")
|
| 93 |
def generate_transaction_tear_sheet(transactions: dict) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
try:
|
| 95 |
-
|
|
|
|
| 96 |
return {"success": True, "result": "Transaction tear sheet generated successfully.", "error": None}
|
| 97 |
except Exception as e:
|
| 98 |
return {"success": False, "result": None, "error": str(e)}
|
| 99 |
|
| 100 |
@mcp.tool(name="generate_round_trip_tear_sheet", description="Generate a tear sheet for round trip analysis.")
|
| 101 |
def generate_round_trip_tear_sheet(round_trips: dict) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
try:
|
| 103 |
-
|
|
|
|
| 104 |
return {"success": True, "result": "Round trip tear sheet generated successfully.", "error": None}
|
| 105 |
except Exception as e:
|
| 106 |
return {"success": False, "result": None, "error": str(e)}
|
| 107 |
|
| 108 |
@mcp.tool(name="generate_interesting_times_tear_sheet", description="Generate a tear sheet for performance during key events.")
|
| 109 |
def generate_interesting_times_tear_sheet(returns: list, events: list) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
try:
|
| 111 |
-
|
|
|
|
| 112 |
return {"success": True, "result": "Interesting times tear sheet generated successfully.", "error": None}
|
| 113 |
except Exception as e:
|
| 114 |
return {"success": False, "result": None, "error": str(e)}
|
| 115 |
|
| 116 |
@mcp.tool(name="generate_capacity_tear_sheet", description="Generate a tear sheet for strategy capacity analysis.")
|
| 117 |
def generate_capacity_tear_sheet(returns: list, positions: dict) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
try:
|
| 119 |
-
|
|
|
|
|
|
|
| 120 |
return {"success": True, "result": "Capacity tear sheet generated successfully.", "error": None}
|
| 121 |
except Exception as e:
|
| 122 |
return {"success": False, "result": None, "error": str(e)}
|
| 123 |
|
| 124 |
@mcp.tool(name="generate_performance_attribution_tear_sheet", description="Generate a tear sheet for performance attribution analysis.")
|
| 125 |
def generate_performance_attribution_tear_sheet(returns: list, factors: dict) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
try:
|
| 127 |
-
|
|
|
|
|
|
|
| 128 |
return {"success": True, "result": "Performance attribution tear sheet generated successfully.", "error": None}
|
| 129 |
except Exception as e:
|
| 130 |
return {"success": False, "result": None, "error": str(e)}
|
| 131 |
|
| 132 |
@mcp.tool(name="plot_annual_returns", description="Plot annual returns as a bar chart.")
|
| 133 |
def plot_annual_returns_tool(returns: list) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
try:
|
| 135 |
-
|
| 136 |
-
|
|
|
|
| 137 |
except Exception as e:
|
| 138 |
return {"success": False, "result": None, "error": str(e)}
|
| 139 |
|
| 140 |
@mcp.tool(name="plot_monthly_returns_heatmap", description="Plot a heatmap of monthly returns.")
|
| 141 |
def plot_monthly_returns_heatmap_tool(returns: list) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
try:
|
| 143 |
-
|
| 144 |
-
|
|
|
|
| 145 |
except Exception as e:
|
| 146 |
return {"success": False, "result": None, "error": str(e)}
|
| 147 |
|
| 148 |
@mcp.tool(name="plot_drawdown_periods", description="Plot cumulative returns highlighting top drawdown periods.")
|
| 149 |
def plot_drawdown_periods_tool(returns: list, top: int = 10) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
try:
|
| 151 |
-
|
| 152 |
-
|
|
|
|
| 153 |
except Exception as e:
|
| 154 |
return {"success": False, "result": None, "error": str(e)}
|
| 155 |
|
| 156 |
@mcp.tool(name="plot_rolling_returns", description="Plot cumulative rolling returns versus benchmarks.")
|
| 157 |
def plot_rolling_returns_tool(returns: list, factor_returns: list = None, live_start_date: str = None) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
try:
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
| 161 |
except Exception as e:
|
| 162 |
return {"success": False, "result": None, "error": str(e)}
|
| 163 |
|
| 164 |
@mcp.tool(name="plot_turnover", description="Plot turnover over time.")
|
| 165 |
def plot_turnover_tool(returns: list, transactions: dict, positions: dict, turnover_denom: str = "AGB") -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
try:
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
| 169 |
except Exception as e:
|
| 170 |
return {"success": False, "result": None, "error": str(e)}
|
| 171 |
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from typing import Optional, Dict, List, Any
|
| 5 |
|
| 6 |
source_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), "source")
|
| 7 |
sys.path.insert(0, source_path)
|
|
|
|
| 19 |
create_perf_attrib_tear_sheet,
|
| 20 |
)
|
| 21 |
from pyfolio.plotting import (
|
|
|
|
|
|
|
| 22 |
plot_annual_returns,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
plot_monthly_returns_heatmap,
|
| 24 |
+
plot_drawdown_periods,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
plot_rolling_returns,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
plot_turnover,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
mcp = FastMCP("pyfolio_service")
|
| 30 |
|
| 31 |
+
def _convert_to_series(data: List[float], name: str = "returns") -> pd.Series:
|
| 32 |
+
"""Convert list to pandas Series with date index."""
|
| 33 |
+
if isinstance(data, list):
|
| 34 |
+
return pd.Series(
|
| 35 |
+
data,
|
| 36 |
+
index=pd.date_range("2020-01-01", periods=len(data), freq="D"),
|
| 37 |
+
name=name
|
| 38 |
+
)
|
| 39 |
+
return data
|
| 40 |
+
|
| 41 |
+
def _convert_to_dataframe(data: Dict[str, Any]) -> pd.DataFrame:
|
| 42 |
+
"""Convert dict to pandas DataFrame."""
|
| 43 |
+
if isinstance(data, dict):
|
| 44 |
+
return pd.DataFrame(data)
|
| 45 |
+
return data
|
| 46 |
+
|
| 47 |
@mcp.tool(name="generate_full_tear_sheet", description="Generate a comprehensive tear sheet for portfolio analysis.")
|
| 48 |
def generate_full_tear_sheet(returns: list, positions: dict = None, transactions: dict = None, benchmark_rets: list = None) -> dict:
|
| 49 |
+
"""
|
| 50 |
+
Generate a comprehensive tear sheet for portfolio analysis.
|
| 51 |
+
|
| 52 |
+
Args:
|
| 53 |
+
returns: List of daily returns
|
| 54 |
+
positions: Dictionary of positions over time (optional)
|
| 55 |
+
transactions: Dictionary of transactions (optional)
|
| 56 |
+
benchmark_rets: List of benchmark returns (optional)
|
| 57 |
+
|
| 58 |
+
Returns:
|
| 59 |
+
Dictionary with success status and result/error message
|
| 60 |
+
"""
|
| 61 |
try:
|
| 62 |
+
returns_series = _convert_to_series(returns, "returns")
|
| 63 |
+
positions_df = _convert_to_dataframe(positions) if positions else None
|
| 64 |
+
transactions_df = _convert_to_dataframe(transactions) if transactions else None
|
| 65 |
+
benchmark_series = _convert_to_series(benchmark_rets, "benchmark") if benchmark_rets else None
|
| 66 |
+
|
| 67 |
+
create_full_tear_sheet(returns_series, positions_df, transactions_df, benchmark_series)
|
| 68 |
return {"success": True, "result": "Full tear sheet generated successfully.", "error": None}
|
| 69 |
except Exception as e:
|
| 70 |
return {"success": False, "result": None, "error": str(e)}
|
| 71 |
|
| 72 |
@mcp.tool(name="generate_simple_tear_sheet", description="Generate a basic tear sheet for portfolio analysis.")
|
| 73 |
def generate_simple_tear_sheet(returns: list) -> dict:
|
| 74 |
+
"""
|
| 75 |
+
Generate a basic tear sheet for portfolio analysis.
|
| 76 |
+
|
| 77 |
+
Args:
|
| 78 |
+
returns: List of daily returns
|
| 79 |
+
|
| 80 |
+
Returns:
|
| 81 |
+
Dictionary with success status and result/error message
|
| 82 |
+
"""
|
| 83 |
try:
|
| 84 |
+
returns_series = _convert_to_series(returns)
|
| 85 |
+
create_simple_tear_sheet(returns_series)
|
| 86 |
return {"success": True, "result": "Simple tear sheet generated successfully.", "error": None}
|
| 87 |
except Exception as e:
|
| 88 |
return {"success": False, "result": None, "error": str(e)}
|
| 89 |
|
| 90 |
@mcp.tool(name="generate_returns_tear_sheet", description="Generate a tear sheet for returns analysis.")
|
| 91 |
def generate_returns_tear_sheet(returns: list) -> dict:
|
| 92 |
+
"""
|
| 93 |
+
Generate a tear sheet for returns analysis.
|
| 94 |
+
|
| 95 |
+
Args:
|
| 96 |
+
returns: List of daily returns
|
| 97 |
+
|
| 98 |
+
Returns:
|
| 99 |
+
Dictionary with success status and result/error message
|
| 100 |
+
"""
|
| 101 |
try:
|
| 102 |
+
returns_series = _convert_to_series(returns)
|
| 103 |
+
create_returns_tear_sheet(returns_series)
|
| 104 |
return {"success": True, "result": "Returns tear sheet generated successfully.", "error": None}
|
| 105 |
except Exception as e:
|
| 106 |
return {"success": False, "result": None, "error": str(e)}
|
| 107 |
|
| 108 |
@mcp.tool(name="generate_position_tear_sheet", description="Generate a tear sheet for position analysis.")
|
| 109 |
def generate_position_tear_sheet(positions: dict) -> dict:
|
| 110 |
+
"""
|
| 111 |
+
Generate a tear sheet for position analysis.
|
| 112 |
+
|
| 113 |
+
Args:
|
| 114 |
+
positions: Dictionary of positions over time
|
| 115 |
+
|
| 116 |
+
Returns:
|
| 117 |
+
Dictionary with success status and result/error message
|
| 118 |
+
"""
|
| 119 |
try:
|
| 120 |
+
positions_df = _convert_to_dataframe(positions)
|
| 121 |
+
create_position_tear_sheet(positions_df)
|
| 122 |
return {"success": True, "result": "Position tear sheet generated successfully.", "error": None}
|
| 123 |
except Exception as e:
|
| 124 |
return {"success": False, "result": None, "error": str(e)}
|
| 125 |
|
| 126 |
@mcp.tool(name="generate_transaction_tear_sheet", description="Generate a tear sheet for transaction analysis.")
|
| 127 |
def generate_transaction_tear_sheet(transactions: dict) -> dict:
|
| 128 |
+
"""
|
| 129 |
+
Generate a tear sheet for transaction analysis.
|
| 130 |
+
|
| 131 |
+
Args:
|
| 132 |
+
transactions: Dictionary of transactions
|
| 133 |
+
|
| 134 |
+
Returns:
|
| 135 |
+
Dictionary with success status and result/error message
|
| 136 |
+
"""
|
| 137 |
try:
|
| 138 |
+
transactions_df = _convert_to_dataframe(transactions)
|
| 139 |
+
create_txn_tear_sheet(transactions_df)
|
| 140 |
return {"success": True, "result": "Transaction tear sheet generated successfully.", "error": None}
|
| 141 |
except Exception as e:
|
| 142 |
return {"success": False, "result": None, "error": str(e)}
|
| 143 |
|
| 144 |
@mcp.tool(name="generate_round_trip_tear_sheet", description="Generate a tear sheet for round trip analysis.")
|
| 145 |
def generate_round_trip_tear_sheet(round_trips: dict) -> dict:
|
| 146 |
+
"""
|
| 147 |
+
Generate a tear sheet for round trip analysis.
|
| 148 |
+
|
| 149 |
+
Args:
|
| 150 |
+
round_trips: Dictionary of round trip trades
|
| 151 |
+
|
| 152 |
+
Returns:
|
| 153 |
+
Dictionary with success status and result/error message
|
| 154 |
+
"""
|
| 155 |
try:
|
| 156 |
+
round_trips_df = _convert_to_dataframe(round_trips)
|
| 157 |
+
create_round_trip_tear_sheet(round_trips_df)
|
| 158 |
return {"success": True, "result": "Round trip tear sheet generated successfully.", "error": None}
|
| 159 |
except Exception as e:
|
| 160 |
return {"success": False, "result": None, "error": str(e)}
|
| 161 |
|
| 162 |
@mcp.tool(name="generate_interesting_times_tear_sheet", description="Generate a tear sheet for performance during key events.")
|
| 163 |
def generate_interesting_times_tear_sheet(returns: list, events: list) -> dict:
|
| 164 |
+
"""
|
| 165 |
+
Generate a tear sheet for performance during key events.
|
| 166 |
+
|
| 167 |
+
Args:
|
| 168 |
+
returns: List of daily returns
|
| 169 |
+
events: List of event periods
|
| 170 |
+
|
| 171 |
+
Returns:
|
| 172 |
+
Dictionary with success status and result/error message
|
| 173 |
+
"""
|
| 174 |
try:
|
| 175 |
+
returns_series = _convert_to_series(returns)
|
| 176 |
+
create_interesting_times_tear_sheet(returns_series, events)
|
| 177 |
return {"success": True, "result": "Interesting times tear sheet generated successfully.", "error": None}
|
| 178 |
except Exception as e:
|
| 179 |
return {"success": False, "result": None, "error": str(e)}
|
| 180 |
|
| 181 |
@mcp.tool(name="generate_capacity_tear_sheet", description="Generate a tear sheet for strategy capacity analysis.")
|
| 182 |
def generate_capacity_tear_sheet(returns: list, positions: dict) -> dict:
|
| 183 |
+
"""
|
| 184 |
+
Generate a tear sheet for strategy capacity analysis.
|
| 185 |
+
|
| 186 |
+
Args:
|
| 187 |
+
returns: List of daily returns
|
| 188 |
+
positions: Dictionary of positions over time
|
| 189 |
+
|
| 190 |
+
Returns:
|
| 191 |
+
Dictionary with success status and result/error message
|
| 192 |
+
"""
|
| 193 |
try:
|
| 194 |
+
returns_series = _convert_to_series(returns)
|
| 195 |
+
positions_df = _convert_to_dataframe(positions)
|
| 196 |
+
create_capacity_tear_sheet(returns_series, positions_df)
|
| 197 |
return {"success": True, "result": "Capacity tear sheet generated successfully.", "error": None}
|
| 198 |
except Exception as e:
|
| 199 |
return {"success": False, "result": None, "error": str(e)}
|
| 200 |
|
| 201 |
@mcp.tool(name="generate_performance_attribution_tear_sheet", description="Generate a tear sheet for performance attribution analysis.")
|
| 202 |
def generate_performance_attribution_tear_sheet(returns: list, factors: dict) -> dict:
|
| 203 |
+
"""
|
| 204 |
+
Generate a tear sheet for performance attribution analysis.
|
| 205 |
+
|
| 206 |
+
Args:
|
| 207 |
+
returns: List of daily returns
|
| 208 |
+
factors: Dictionary of factor returns
|
| 209 |
+
|
| 210 |
+
Returns:
|
| 211 |
+
Dictionary with success status and result/error message
|
| 212 |
+
"""
|
| 213 |
try:
|
| 214 |
+
returns_series = _convert_to_series(returns)
|
| 215 |
+
factors_df = _convert_to_dataframe(factors)
|
| 216 |
+
create_perf_attrib_tear_sheet(returns_series, factors_df)
|
| 217 |
return {"success": True, "result": "Performance attribution tear sheet generated successfully.", "error": None}
|
| 218 |
except Exception as e:
|
| 219 |
return {"success": False, "result": None, "error": str(e)}
|
| 220 |
|
| 221 |
@mcp.tool(name="plot_annual_returns", description="Plot annual returns as a bar chart.")
|
| 222 |
def plot_annual_returns_tool(returns: list) -> dict:
|
| 223 |
+
"""
|
| 224 |
+
Plot annual returns as a bar chart.
|
| 225 |
+
|
| 226 |
+
Args:
|
| 227 |
+
returns: List of daily returns
|
| 228 |
+
|
| 229 |
+
Returns:
|
| 230 |
+
Dictionary with success status and result/error message
|
| 231 |
+
"""
|
| 232 |
try:
|
| 233 |
+
returns_series = _convert_to_series(returns)
|
| 234 |
+
ax = plot_annual_returns(returns_series)
|
| 235 |
+
return {"success": True, "result": "Annual returns plot generated successfully.", "error": None}
|
| 236 |
except Exception as e:
|
| 237 |
return {"success": False, "result": None, "error": str(e)}
|
| 238 |
|
| 239 |
@mcp.tool(name="plot_monthly_returns_heatmap", description="Plot a heatmap of monthly returns.")
|
| 240 |
def plot_monthly_returns_heatmap_tool(returns: list) -> dict:
|
| 241 |
+
"""
|
| 242 |
+
Plot a heatmap of monthly returns.
|
| 243 |
+
|
| 244 |
+
Args:
|
| 245 |
+
returns: List of daily returns
|
| 246 |
+
|
| 247 |
+
Returns:
|
| 248 |
+
Dictionary with success status and result/error message
|
| 249 |
+
"""
|
| 250 |
try:
|
| 251 |
+
returns_series = _convert_to_series(returns)
|
| 252 |
+
ax = plot_monthly_returns_heatmap(returns_series)
|
| 253 |
+
return {"success": True, "result": "Monthly returns heatmap generated successfully.", "error": None}
|
| 254 |
except Exception as e:
|
| 255 |
return {"success": False, "result": None, "error": str(e)}
|
| 256 |
|
| 257 |
@mcp.tool(name="plot_drawdown_periods", description="Plot cumulative returns highlighting top drawdown periods.")
|
| 258 |
def plot_drawdown_periods_tool(returns: list, top: int = 10) -> dict:
|
| 259 |
+
"""
|
| 260 |
+
Plot cumulative returns highlighting top drawdown periods.
|
| 261 |
+
|
| 262 |
+
Args:
|
| 263 |
+
returns: List of daily returns
|
| 264 |
+
top: Number of top drawdown periods to highlight
|
| 265 |
+
|
| 266 |
+
Returns:
|
| 267 |
+
Dictionary with success status and result/error message
|
| 268 |
+
"""
|
| 269 |
try:
|
| 270 |
+
returns_series = _convert_to_series(returns)
|
| 271 |
+
ax = plot_drawdown_periods(returns_series, top)
|
| 272 |
+
return {"success": True, "result": "Drawdown periods plot generated successfully.", "error": None}
|
| 273 |
except Exception as e:
|
| 274 |
return {"success": False, "result": None, "error": str(e)}
|
| 275 |
|
| 276 |
@mcp.tool(name="plot_rolling_returns", description="Plot cumulative rolling returns versus benchmarks.")
|
| 277 |
def plot_rolling_returns_tool(returns: list, factor_returns: list = None, live_start_date: str = None) -> dict:
|
| 278 |
+
"""
|
| 279 |
+
Plot cumulative rolling returns versus benchmarks.
|
| 280 |
+
|
| 281 |
+
Args:
|
| 282 |
+
returns: List of daily returns
|
| 283 |
+
factor_returns: List of factor returns (optional)
|
| 284 |
+
live_start_date: Start date for live trading period (optional)
|
| 285 |
+
|
| 286 |
+
Returns:
|
| 287 |
+
Dictionary with success status and result/error message
|
| 288 |
+
"""
|
| 289 |
try:
|
| 290 |
+
returns_series = _convert_to_series(returns)
|
| 291 |
+
factor_series = _convert_to_series(factor_returns, "factor") if factor_returns else None
|
| 292 |
+
ax = plot_rolling_returns(returns_series, factor_series, live_start_date)
|
| 293 |
+
return {"success": True, "result": "Rolling returns plot generated successfully.", "error": None}
|
| 294 |
except Exception as e:
|
| 295 |
return {"success": False, "result": None, "error": str(e)}
|
| 296 |
|
| 297 |
@mcp.tool(name="plot_turnover", description="Plot turnover over time.")
|
| 298 |
def plot_turnover_tool(returns: list, transactions: dict, positions: dict, turnover_denom: str = "AGB") -> dict:
|
| 299 |
+
"""
|
| 300 |
+
Plot turnover over time.
|
| 301 |
+
|
| 302 |
+
Args:
|
| 303 |
+
returns: List of daily returns
|
| 304 |
+
transactions: Dictionary of transactions
|
| 305 |
+
positions: Dictionary of positions over time
|
| 306 |
+
turnover_denom: Turnover denominator calculation method
|
| 307 |
+
|
| 308 |
+
Returns:
|
| 309 |
+
Dictionary with success status and result/error message
|
| 310 |
+
"""
|
| 311 |
try:
|
| 312 |
+
returns_series = _convert_to_series(returns)
|
| 313 |
+
transactions_df = _convert_to_dataframe(transactions)
|
| 314 |
+
positions_df = _convert_to_dataframe(positions)
|
| 315 |
+
ax = plot_turnover(returns_series, transactions_df, positions_df, turnover_denom)
|
| 316 |
+
return {"success": True, "result": "Turnover plot generated successfully.", "error": None}
|
| 317 |
except Exception as e:
|
| 318 |
return {"success": False, "result": None, "error": str(e)}
|
| 319 |
|