Spaces:
Runtime error
Runtime error
Update qlib/mcp_output/mcp_plugin/mcp_service.py
Browse files
qlib/mcp_output/mcp_plugin/mcp_service.py
CHANGED
|
@@ -37,7 +37,7 @@ def initialize_exchange(
|
|
| 37 |
min_cost: float = 5.0,
|
| 38 |
limit_threshold: Union[Tuple[str, str], float, None] = None,
|
| 39 |
deal_price: Union[str, Tuple[str, str], List[str]] = None,
|
| 40 |
-
|
| 41 |
) -> dict:
|
| 42 |
"""
|
| 43 |
Initialize and return an Exchange object for backtesting or trading simulations.
|
|
@@ -54,12 +54,16 @@ def initialize_exchange(
|
|
| 54 |
min_cost (float): Minimum transaction cost.
|
| 55 |
limit_threshold (Union[Tuple[str, str], float, None]): Price movement limits.
|
| 56 |
deal_price (Union[str, Tuple[str, str], List[str]]): Price configuration.
|
| 57 |
-
|
| 58 |
|
| 59 |
Returns:
|
| 60 |
dict: A dictionary containing success, result, or error fields.
|
| 61 |
"""
|
| 62 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
exchange_obj = get_exchange(
|
| 64 |
exchange=exchange,
|
| 65 |
freq=freq,
|
|
@@ -72,9 +76,9 @@ def initialize_exchange(
|
|
| 72 |
min_cost=min_cost,
|
| 73 |
limit_threshold=limit_threshold,
|
| 74 |
deal_price=deal_price,
|
| 75 |
-
**
|
| 76 |
)
|
| 77 |
-
return {"success": True, "result": exchange_obj}
|
| 78 |
except Exception as e:
|
| 79 |
return {"success": False, "error": str(e)}
|
| 80 |
|
|
@@ -108,7 +112,7 @@ def create_account(
|
|
| 108 |
account=account,
|
| 109 |
pos_type=pos_type,
|
| 110 |
)
|
| 111 |
-
return {"success": True, "result": account_obj}
|
| 112 |
except Exception as e:
|
| 113 |
return {"success": False, "error": str(e)}
|
| 114 |
|
|
@@ -151,7 +155,7 @@ def initialize_strategy_executor(
|
|
| 151 |
exchange_kwargs=exchange_kwargs,
|
| 152 |
pos_type=pos_type,
|
| 153 |
)
|
| 154 |
-
return {"success": True, "result": strategy_executor}
|
| 155 |
except Exception as e:
|
| 156 |
return {"success": False, "error": str(e)}
|
| 157 |
|
|
@@ -194,7 +198,13 @@ def run_backtest(
|
|
| 194 |
exchange_kwargs=exchange_kwargs,
|
| 195 |
pos_type=pos_type,
|
| 196 |
)
|
| 197 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
except Exception as e:
|
| 199 |
return {"success": False, "error": str(e)}
|
| 200 |
|
|
@@ -241,7 +251,7 @@ def collect_trade_data(
|
|
| 241 |
return_value=return_value,
|
| 242 |
)
|
| 243 |
data = list(data_generator)
|
| 244 |
-
return {"success": True, "result": data}
|
| 245 |
except Exception as e:
|
| 246 |
return {"success": False, "error": str(e)}
|
| 247 |
|
|
@@ -259,7 +269,7 @@ def format_trade_decisions(decisions: List[Any]) -> dict:
|
|
| 259 |
"""
|
| 260 |
try:
|
| 261 |
formatted_decisions = format_decisions(decisions)
|
| 262 |
-
return {"success": True, "result": formatted_decisions}
|
| 263 |
except Exception as e:
|
| 264 |
return {"success": False, "error": str(e)}
|
| 265 |
|
|
|
|
| 37 |
min_cost: float = 5.0,
|
| 38 |
limit_threshold: Union[Tuple[str, str], float, None] = None,
|
| 39 |
deal_price: Union[str, Tuple[str, str], List[str]] = None,
|
| 40 |
+
extra_kwargs: Dict[str, Any] = None,
|
| 41 |
) -> dict:
|
| 42 |
"""
|
| 43 |
Initialize and return an Exchange object for backtesting or trading simulations.
|
|
|
|
| 54 |
min_cost (float): Minimum transaction cost.
|
| 55 |
limit_threshold (Union[Tuple[str, str], float, None]): Price movement limits.
|
| 56 |
deal_price (Union[str, Tuple[str, str], List[str]]): Price configuration.
|
| 57 |
+
extra_kwargs (Dict[str, Any]): Additional keyword arguments as a dictionary.
|
| 58 |
|
| 59 |
Returns:
|
| 60 |
dict: A dictionary containing success, result, or error fields.
|
| 61 |
"""
|
| 62 |
try:
|
| 63 |
+
# Handle extra_kwargs
|
| 64 |
+
if extra_kwargs is None:
|
| 65 |
+
extra_kwargs = {}
|
| 66 |
+
|
| 67 |
exchange_obj = get_exchange(
|
| 68 |
exchange=exchange,
|
| 69 |
freq=freq,
|
|
|
|
| 76 |
min_cost=min_cost,
|
| 77 |
limit_threshold=limit_threshold,
|
| 78 |
deal_price=deal_price,
|
| 79 |
+
**extra_kwargs,
|
| 80 |
)
|
| 81 |
+
return {"success": True, "result": str(exchange_obj)}
|
| 82 |
except Exception as e:
|
| 83 |
return {"success": False, "error": str(e)}
|
| 84 |
|
|
|
|
| 112 |
account=account,
|
| 113 |
pos_type=pos_type,
|
| 114 |
)
|
| 115 |
+
return {"success": True, "result": str(account_obj)}
|
| 116 |
except Exception as e:
|
| 117 |
return {"success": False, "error": str(e)}
|
| 118 |
|
|
|
|
| 155 |
exchange_kwargs=exchange_kwargs,
|
| 156 |
pos_type=pos_type,
|
| 157 |
)
|
| 158 |
+
return {"success": True, "result": str(strategy_executor)}
|
| 159 |
except Exception as e:
|
| 160 |
return {"success": False, "error": str(e)}
|
| 161 |
|
|
|
|
| 198 |
exchange_kwargs=exchange_kwargs,
|
| 199 |
pos_type=pos_type,
|
| 200 |
)
|
| 201 |
+
return {
|
| 202 |
+
"success": True,
|
| 203 |
+
"result": {
|
| 204 |
+
"portfolio_metrics": portfolio_metrics.to_dict() if hasattr(portfolio_metrics, 'to_dict') else str(portfolio_metrics),
|
| 205 |
+
"trading_indicators": trading_indicators.to_dict() if hasattr(trading_indicators, 'to_dict') else str(trading_indicators)
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
except Exception as e:
|
| 209 |
return {"success": False, "error": str(e)}
|
| 210 |
|
|
|
|
| 251 |
return_value=return_value,
|
| 252 |
)
|
| 253 |
data = list(data_generator)
|
| 254 |
+
return {"success": True, "result": [str(item) for item in data]}
|
| 255 |
except Exception as e:
|
| 256 |
return {"success": False, "error": str(e)}
|
| 257 |
|
|
|
|
| 269 |
"""
|
| 270 |
try:
|
| 271 |
formatted_decisions = format_decisions(decisions)
|
| 272 |
+
return {"success": True, "result": str(formatted_decisions)}
|
| 273 |
except Exception as e:
|
| 274 |
return {"success": False, "error": str(e)}
|
| 275 |
|