Tools
Backtest Initialization
Request
Run a backtest for a few seconds to initialize the algorithm and get inialization errors if any. The /ai/tools/backtest-init
API accepts requests in the following format:
BasicFilesRequest Model - Request to process files. | |
---|---|
language | string Enum Programming language. Options : ['C#', 'Py'] |
files | File Array Files to process. |
Example |
{ "language": "Py", "files": "[{"name": "file.py", "content": "fileContent"}]" ] } |
File Model - File for a AI. | |
---|---|
name | string Name of a file. |
content | string Contents of the file. |
Example |
{ "name": "string", "content": "string" } |
Responses
The /ai/tools/backtest-init
API provides a response in the following format:
200 Success
BacktestInitResponse Model - Response to a backtest initialization request. | |
---|---|
state | string State of the backtest (e.g. Error, End). |
version | string Version of the response. |
payload | string Information about the backtest initialization. |
payloadType | string Type of the payload, e.g. String. |
Example |
{ "state": "End", "version": 2.0, "payload": "string", "payloadType": "String" } |
401 Authentication Error
UnauthorizedError Model - Unauthorized response from the API. Key is missing, invalid, or timestamp is too old for hash. | |
---|---|
www_authenticate | string Header |
Examples
The following example demonstates initializing a backtest for a specific algorithm through the cloud API.
from base64 import b64encode from hashlib import sha256 from time import time from requests import get, post BASE_URL = 'https://www.quantconnect.com/api/v2/' # You need to replace these with your actual credentials. # You can request your credentials at https://www.quantconnect.com/settings/ # You can find our organization ID at https://www.quantconnect.com/organization/ USER_ID = 0 API_TOKEN = '____' ORGANIZATION_ID = '____' def get_headers(): # Get timestamp timestamp = f'{int(time())}' time_stamped_token = f'{API_TOKEN}:{timestamp}'.encode('utf-8') # Get hased API token hashed_token = sha256(time_stamped_token).hexdigest() authentication = f'{USER_ID}:{hashed_token}'.encode('utf-8') authentication = b64encode(authentication).decode('ascii') # Create headers dictionary. return { 'Authorization': f'Basic {authentication}', 'Timestamp': timestamp } # Authenticate to verify credentials response = post(f'{BASE_URL}/authenticate', headers = get_headers()) print(response.json()) # -------------------- ### Initialize Backtest # Send a POST request to the /ai/tools/backtest-init endpoint to initialize a backtest response = post(f'{BASE_URL}/ai/tools/backtest-init', headers=get_headers(), json={ "language": "Python", # Programming language of the algorithm "files": [ # List of files for the backtest { "name": "utils.py", # Name of the file "content": ''' # region imports from AlgorithmImports import * # endregion class Project(QCAlgorithm): def Initialize(self): self.AddEquity("SPY", Resolution.Minute) ''' # Content of the file (Python code) } ] }) # Parse the JSON response into python managable dict result = response.json() # Check if the request was successful and print the backtest initialization result if result['success']: print("Backtest Initialized Successfully:") print(result)