create_backtest
Generate backtests for compiled trading strategies by specifying project ID, compile ID, and parameters. Analyze strategy performance with custom inputs.
Instructions
Create a new backtest for a compiled project.
Args: project_id: ID of the project to backtest compile_id: Compile ID from a successful project compilation backtest_name: Name for the backtest parameters: Optional dictionary of parameters for the backtest (e.g., {"ema_fast": 10, "ema_slow": 100})
Returns: Dictionary containing backtest creation result and backtest details
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| backtest_name | Yes | ||
| compile_id | Yes | ||
| parameters | No | ||
| project_id | Yes |
Implementation Reference
- The core handler function for the 'create_backtest' tool. Decorated with @mcp.tool() for automatic registration. Handles authentication, API request to create backtest, parameter injection, and detailed error handling.@mcp.tool() async def create_backtest( project_id: int, compile_id: str, backtest_name: str, parameters: Optional[Dict[str, Any]] = None, ) -> Dict[str, Any]: """ Create a new backtest for a compiled project. Args: project_id: ID of the project to backtest compile_id: Compile ID from a successful project compilation backtest_name: Name for the backtest parameters: Optional dictionary of parameters for the backtest (e.g., {"ema_fast": 10, "ema_slow": 100}) Returns: Dictionary containing backtest creation result and backtest details """ 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, "backtestName": backtest_name, } # Add parameters if provided if parameters: for key, value in parameters.items(): request_data[f"parameters[{key}]"] = value # Make API request response = await auth.make_authenticated_request( endpoint="backtests/create", method="POST", json=request_data ) # Parse response if response.status_code == 200: data = response.json() if data.get("success", False): backtest_results = data.get("backtest", []) debugging = data.get("debugging", False) if backtest_results: backtest = backtest_results[0] return { "status": "success", "project_id": project_id, "compile_id": compile_id, "backtest_name": backtest_name, "backtest": backtest, "debugging": debugging, "message": f"Successfully created backtest '{backtest_name}' for project {project_id}", } else: return { "status": "success", "project_id": project_id, "compile_id": compile_id, "backtest_name": backtest_name, "debugging": debugging, "message": f"Backtest '{backtest_name}' created but no results yet", "note": "Backtest may still be initializing", } else: # API returned success=false errors = data.get("errors", ["Unknown error"]) if any("Compile id not found" in e for e in errors): return { "status": "error", "error": "Compile ID not found. Please compile the project first using the 'compile_project' tool.", "details": errors, "project_id": project_id, "compile_id": compile_id, } return { "status": "error", "error": "Backtest creation failed", "details": errors, "project_id": project_id, "compile_id": compile_id, "backtest_name": backtest_name, } 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 backtest: {str(e)}", "project_id": project_id, "compile_id": compile_id, "backtest_name": backtest_name, }
- quantconnect_mcp/main.py:50-50 (registration)Calls register_backtest_tools(mcp) which registers the create_backtest tool among backtest tools.register_backtest_tools(mcp)
- quantconnect_mcp/src/server.py:77-77 (registration)Calls register_backtest_tools(mcp) to register backtest tools including create_backtest.register_backtest_tools(mcp)
- Function signature defining input parameters and return type for the create_backtest tool.async def create_backtest( project_id: int, compile_id: str, backtest_name: str, parameters: Optional[Dict[str, Any]] = None, ) -> Dict[str, Any]: