create_optimization
Configure and launch optimization runs for algorithmic trading strategies by specifying parameters like node type, runtime limits, and performance targets.
Instructions
Create an optimization with the specified parameters.
Args: project_id: ID of the project to optimize compile_id: Compile ID from successful project compilation node_type: Type of node to use for optimization parameters: Dictionary of optimization parameters name: Optional name for the optimization maximum_runtime: Optional maximum runtime in seconds output_target: Optional optimization target (e.g., "Sharpe Ratio")
Returns: Dictionary containing optimization creation result
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| project_id | Yes | ||
| compile_id | Yes | ||
| node_type | Yes | ||
| parameters | Yes | ||
| name | No | ||
| maximum_runtime | No | ||
| output_target | No |
Implementation Reference
- The core handler function for the 'create_optimization' MCP tool. It handles authentication, prepares the request data with project details and parameters, makes a POST request to the QuantConnect 'optimizations/create' endpoint, and parses the response to return success or error details including the new optimization ID.@mcp.tool() async def create_optimization( project_id: int, compile_id: str, node_type: str, parameters: Dict[str, Any], name: Optional[str] = None, maximum_runtime: Optional[int] = None, output_target: Optional[str] = None, ) -> Dict[str, Any]: """ Create an optimization with the specified parameters. Args: project_id: ID of the project to optimize compile_id: Compile ID from successful project compilation node_type: Type of node to use for optimization parameters: Dictionary of optimization parameters name: Optional name for the optimization maximum_runtime: Optional maximum runtime in seconds output_target: Optional optimization target (e.g., "Sharpe Ratio") Returns: Dictionary containing optimization creation result """ auth = get_auth_instance() if auth is None: return { "status": "error", "error": "QuantConnect authentication not configured. Use configure_auth() first.", } try: # Prepare request data request_data = { "projectId": project_id, "compileId": compile_id, "nodeType": node_type, "parameters": parameters, } # Add optional parameters if name is not None: request_data["name"] = name if maximum_runtime is not None: request_data["maximumRuntime"] = maximum_runtime if output_target is not None: request_data["outputTarget"] = output_target # Make API request response = await auth.make_authenticated_request( endpoint="optimizations/create", method="POST", json=request_data ) # Parse response if response.status_code == 200: data = response.json() if data.get("success", False): optimization = data.get("optimization", {}) optimization_id = optimization.get("optimizationId") return { "status": "success", "project_id": project_id, "compile_id": compile_id, "optimization_id": optimization_id, "optimization": optimization, "message": f"Successfully created optimization {optimization_id} for project {project_id}", } else: # API returned success=false errors = data.get("errors", ["Unknown error"]) return { "status": "error", "error": "Optimization creation failed", "details": errors, "project_id": project_id, "compile_id": compile_id, } elif response.status_code == 401: return { "status": "error", "error": "Authentication failed. Check your credentials and ensure they haven't expired.", } else: return { "status": "error", "error": f"API request failed with status {response.status_code}", "response_text": ( response.text[:500] if hasattr(response, "text") else "No response text" ), } except Exception as e: return { "status": "error", "error": f"Failed to create optimization: {str(e)}", "project_id": project_id, "compile_id": compile_id, }
- quantconnect_mcp/src/server.py:73-79 (registration)Calls register_optimization_tools(mcp) as part of initializing the FastMCP server, which registers the create_optimization tool along with other optimization tools.safe_print("🔧 Registering QuantConnect tools...") register_auth_tools(mcp) register_project_tools(mcp) register_file_tools(mcp) register_backtest_tools(mcp) register_live_tools(mcp) register_optimization_tools(mcp)