Read Backtest
Charts
Request
Request body to obtain a chart from a backtest. The /backtests/chart/read API accepts requests in the following format:
ReadBacktestChartRequest Model - Request body to obtain a chart from a backtest. | |
|---|---|
| projectId | integer Id of the project that contains the backtest. |
| backtestId | string Id of the backtest for this chart request. |
| name | string The requested chart name. |
| count | integer The number of data points to request. |
| start | integer The start timestamp of the request in Unix time. |
| end | integer The end timestamp of the request in Unix time. |
| Example |
{
"projectId": 0,
"backtestId": "string",
"name": "string",
"count": 0,
"start": 0,
"end": 0
}
|
Responses
The /backtests/chart/read API provides a response in the following format:
200 Success
LoadingResponse Model - Response when the requested chart or orders are being generated. | |
|---|---|
| progress | number Loading percentage of the data generation process. |
| status | string Enum Status of the data generation process. Options : ['loading'] |
| success | boolean Indicate if the API request was successful. |
| Example |
{
"progress": 0,
"status": "loading",
"success": true
}
|
ReadChartResponse Model - Response with the requested chart from a live algorithm or backtest | |
|---|---|
| chart | Chart object Single Parent Chart Object for Custom Charting. |
| success | boolean Indicate if the API request was successful. |
| errors | string Array List of errors with the API call. |
| Example |
{
"chart": {
"name": "string",
"chartType": 0,
"series": {
"name": "string",
"unit": "string",
"index": 0,
"values": ,
"seriesType": 0,
"color": "string",
"scatterMarkerSymbol": "none"
}
},
"success": true,
"errors": [
"string"
]
}
|
Chart Model - Single Parent Chart Object for Custom Charting. | |
|---|---|
| name | string Name of the Chart. |
| chartType | integer Enum Type of the Chart. 0=Overlayed, 1=Stacked. Options : [0, 1] |
| series | Series object Series Objects for this Chart. |
| Example |
{
"name": "string",
"chartType": 0,
"series": {
"name": "string",
"unit": "string",
"index": 0,
"values": ,
"seriesType": 0,
"color": "string",
"scatterMarkerSymbol": "none"
}
}
|
Series Model - Chart Series Object - Series data and properties for a chart. | |
|---|---|
| name | string Name of the series. |
| unit | string Axis for the chart series. |
| index | integer Index/position of the series on the chart. |
| values | object Values for the series plot. These values are assumed to be in ascending time order (first points earliest, last points latest). |
| seriesType | integer Enum Chart type for the series. 0=Line, 1=Scatter, 2=Candle, 3=Bar, 4=Flag, 5=StackedArea, 6=Pie, 7=Treemap, 9=Heatmap, 10=Scatter3d. Options : [0, 1, 2, 3, 4, 5, 6, 7, 9, 19] |
| color | string Color of the series. |
| scatterMarkerSymbol | string Enum Shape or symbol for the marker in a scatter plot. Options : ['none', 'circle', 'square', 'diamond', 'triangle', 'triangle-down'] |
| Example |
{
"name": "string",
"unit": "string",
"index": 0,
"values": ,
"seriesType": 0,
"color": "string",
"scatterMarkerSymbol": "none"
}
|
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, 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())
# --------------------
### Create Backtest
# Define placeholder compilation ID (replace with actual value)
compile_id = "compile_id..."
# Send a POST request to the /backtests/create endpoint to create a backtest
response = post(f'{BASE_URL}/backtests/create', headers=get_headers(), json={
"projectId": project_id, # ID of the project to backtest
"compileId": compile_id, # Compilation ID for the backtest
"backtestName": f"Backtest {int(time())}" # Unique name for the backtest using current timestamp
})
# Parse the JSON response into python managable dict
result = response.json()
# Extract the backtest ID from the response
backtest_id = result['backtest']['backtestId']
# Check if the request was successful and print the result
if result['success']:
print("Backtest Created Successfully:")
print(result)
### Read Backtest Statistics
# Prepare data payload to read backtest statistics
payload = {
"projectId": project_id, # ID of the project
"backtestId": backtest_id # ID of the backtest to read
}
# Send a POST request to the /backtests/read endpoint to get statistics
response = post(f'{BASE_URL}/backtests/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 statistics
if result['success']:
print("Backtest Statistics:")
print(result)
### Update Backtest
# Send a POST request to the /backtests/update endpoint to update backtest details
response = post(f'{BASE_URL}/backtests/update', headers=get_headers(), json={
"projectId": project_id, # ID of the project
"backtestId": backtest_id, # ID of the backtest to update
"name": f"Backtest_{backtest_id}", # New name for the backtest
"note": "The new backtest name is awesome!" # Additional note
})
# 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("Backtest Updated Successfully:")
print(result)
### Delete Backtest
# Prepare data payload to delete the backtest
payload = {
"projectId": project_id, # ID of the project
"backtestId": backtest_id # ID of the backtest to delete
}
# Send a POST request to the /backtests/delete endpoint to delete the backtest
response = post(f'{BASE_URL}/backtests/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("Backtest Deleted Successfully:")
print(result)
### List Backtests
# Prepare data payload to list backtests with statistics
payload = {
"projectId": project_id, # ID of the project
"includeStatistics": True # Include statistics in the response
}
# Send a POST request to the /backtests/list endpoint to list backtests
response = post(f'{BASE_URL}/backtests/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 Backtests:")
print(result)