Optimization Management
Estimate Optimization Cost
Request
Project, compile and optimization parameters for estimating the cost of an optimization job. The /optimizations/estimate API accepts requests in the following format:
EstimateOptimizationRequest Model - Request to estimate the cost of an optimization job. | |
|---|---|
| projectId | integer Project ID of the project the optimization belongs to. |
| name | string Name of the optimization. |
| target | string Target of the optimization. |
| targetTo | string Target extremum of the optimization. |
| targetValue | float Optimization target value. |
| strategy | string Optimization strategy. |
| compileId | string Optimization compile ID. |
| parameters | OptimizationParameter Array Optimization parameters. |
| constraints | OptimizationConstraint Array Optimization constraints. |
| Example |
{
"projectId": 23456789,
"name": "Mia First Optimization Job",
"target": "TotalPerformance.PortfolioStatistics.SharpeRatio",
"targetTo": "\'max\' or \'min\'",
"targetValue": 1,
"strategy": "QuantConnect.Optimizer.Strategies.GridSearchOptimizationStrategy",
"compileId": "c0edc6-49048b",
"parameters": [
{
"name": "rsi_period",
"min": 10,
"max": 20,
"step": 1,
"minStep": 1
}
],
"constraints": [
{
"target": "TotalPerformance.PortfolioStatistics.SharpeRatio",
"operator": "greater",
"targetValue": 1
}
]
}
|
OptimizationParameter Model | |
|---|---|
| name | string Name of optimization parameter. |
| min | float Minimum value of optimization parameter, applicable for boundary conditions. |
| max | float Maximum value of optimization parameter, applicable for boundary conditions. |
| step | float Movement, should be positive. |
| minStep | float Minimal possible movement for current parameter, should be positive. Used by Strategies.EulerSearchOptimizationStrategy to determine when this parameter can no longer be optimized. |
| Example |
{
"name": "rsi_period",
"min": 10,
"max": 20,
"step": 1,
"minStep": 1
}
|
OptimizationConstraint Model | |
|---|---|
| target | string Property we want to track. |
| operator | string The target comparison operation. |
| targetValue | float The value of the property we want to track. |
| Example |
{
"target": "TotalPerformance.PortfolioStatistics.SharpeRatio",
"operator": "greater",
"targetValue": 1
}
|
Responses
The /optimizations/estimate API provides a response in the following format:
200 Success
EstimateOptimizationResponse Model - Response received when estimating the cost of an optimization. | |
|---|---|
| estimate | Estimate object Response received when estimating the cost of an optimization. |
| success | boolean Indicate if the API request was successful. |
| errors | string Array List of errors with the API call. |
| Example |
{
"estimate": {
"estimateId": "string",
"time": 60,
"balance": 10
},
"success": true,
"errors": [
"string"
]
}
|
Estimate Model - Response received when estimating the cost of an optimization. | |
|---|---|
| estimateId | string Estimate Id. |
| time | integer Estimate time in seconds. |
| balance | integer Estimate balance in QCC. |
| Example |
{
"estimateId": "string",
"time": 60,
"balance": 10
}
|
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 creating, reading, updating, deleting, aborting and listing backtests of a project 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())
# --------------------
# The project ID of the project to manage an optimization job
project_id = 12345678
### Estimate Optimization Cost
# Send a POST request to the /optimizations/estimate endpoint to estimate cost
response = post(f'{BASE_URL}/optimizations/estimate', headers=get_headers(), json={
"projectId": project_id, # ID of the project
"name": f"Optimization_{compileId[:5]}", # Name of the optimization (using compile ID prefix)
"target": "TotalPerformance.PortfolioStatistics.SharpeRatio", # Optimization target metric
"targetTo": "max", # Direction to optimize (maximize)
"targetValue": None, # Specific target value (None for max/min)
"strategy": "QuantConnect.Optimizer.Strategies.GridSearchOptimizationStrategy", # Optimization strategy
"compileId": compile_id, # Compilation ID for the optimization
"parameters[0][key]": "ema_fast", # First parameter key
"parameters[0][min]": 100, # Minimum value for first parameter
"parameters[0][max]": 200, # Maximum value for first parameter
"parameters[0][step]": 50, # Step size for first parameter
"parameters[1][key]": "ema_slow", # Second parameter key
"parameters[1][min]": 200, # Minimum value for second parameter
"parameters[1][max]": 300, # Maximum value for second parameter
"parameters[1][step]": 50, # Step size for second parameter
"constraints": [{ # Constraints for the optimization
"target": "TotalPerformance.PortfolioStatistics.SharpeRatio",
"operator": "greater",
"target-value": 1
}]
})
# Parse the JSON response into python managable dict
result = response.json()
# Check if the request was successful and print the estimated cost
if result['success']:
print("Optimization Cost Estimated Successfully:")
print(result)
### Create Optimization
# Send a POST request to the /optimizations/create endpoint to create an optimization
response = post(f'{BASE_URL}/optimizations/create', headers=get_headers(), json={
"projectId": project_id, # ID of the project
"name": f"Optimization_{compileId[:5]}", # Name of the optimization
"target": "TotalPerformance.PortfolioStatistics.SharpeRatio", # Optimization target
"targetTo": "max", # Direction to optimize
"targetValue": None, # Specific target value
"strategy": "QuantConnect.Optimizer.Strategies.GridSearchOptimizationStrategy", # Strategy
"compileId": compile_id, # Compilation ID
"parameters[0][key]": "ema_fast", # First parameter key
"parameters[0][min]": 100, # Minimum value
"parameters[0][max]": 200, # Maximum value
"parameters[0][step]": 50, # Step size
"parameters[1][key]": "ema_slow", # Second parameter key
"parameters[1][min]": 200, # Minimum value
"parameters[1][max]": 300, # Maximum value
"parameters[1][step]": 50, # Step size
"constraints": [{ # Constraints
"target": "TotalPerformance.PortfolioStatistics.SharpeRatio",
"operator": "greater",
"target-value": 1
}],
"estimatedCost": 10, # Estimated cost of optimization
"nodeType": "O2-8", # Node type for optimization
"parallelNodes": 4 # Number of parallel nodes
})
# Parse the JSON response into python managable dict
result = response.json()
# Extract the optimization ID from the response
optimization_id = result['optimizations'][0]['optimizationId']
# Check if the request was successful and print the result
if result['success']:
print("Optimization Created Successfully:")
print(result)
### Update Optimization
# Send a POST request to the /optimizations/update endpoint to update the optimization
response = post(f'{BASE_URL}/optimizations/update', headers=get_headers(), json={
"optimizationId": optimization_id, # ID of the optimization to update
"name": f"Optimization_{optimizationId[:5]}" # New name for the optimization
})
# Parse the JSON response into python managable dict
result = response.json()
# Check if the request was successful and print the result
if result['success']:
print("Optimization Updated Successfully:")
print(result)
### Read Optimization
# Prepare data payload to read optimization details
payload = {
"optimizationId": optimization_id # ID of the optimization to read
}
# Send a POST request to the /optimizations/read endpoint to get details
response = post(f'{BASE_URL}/optimizations/read', headers=get_headers(), json=payload)
# Parse the JSON response into python managable dict
result = response.json()
# Check if the request was successful and print the details
if result['success']:
print("Optimization Details:")
print(result)
### Abort Optimization
# Prepare data payload to abort the optimization
payload = {
"optimizationId": optimization_id # ID of the optimization to abort
}
# Send a POST request to the /optimizations/abort endpoint to abort
response = post(f'{BASE_URL}/optimizations/abort', headers=get_headers(), json=payload)
# Parse the JSON response into python managable dict
result = response.json()
# Check if the request was successful and print the result
if result['success']:
print("Optimization Aborted Successfully:")
print(result)
### Delete Optimization
# Prepare data payload to delete the optimization
payload = {
"optimizationId": optimization_id # ID of the optimization to delete
}
# Send a POST request to the /optimizations/delete endpoint to delete
response = post(f'{BASE_URL}/optimizations/delete', headers=get_headers(), json=payload)
# Parse the JSON response into python managable dict
result = response.json()
# Check if the request was successful and print the result
if result['success']:
print("Optimization Deleted Successfully:")
print(result)
### List Optimizations
# Prepare data payload to list optimizations
payload = {
"projectId": project_id # ID of the project
}
# Send a POST request to the /optimizations/list endpoint to list optimizations
response = post(f'{BASE_URL}/optimizations/list', headers=get_headers(), json=payload)
# Parse the JSON response into python managable dict
result = response.json()
# Check if the request was successful and print the list
if result['success']:
print("List of Optimizations:")
print(result)