Meta Analysis
Live Deployment Automation
Introduction
This page explains how use QuantConnect API in an interactive notebook to deploy and stop a set of live trading algorithms in QC Cloud.
Get Project Ids
To automate live deployments for multiple projects, save the projects under a single directory in QuantConnect Cloud. This tutorial assumes you save all the projects under a /Live directory.
Follow the below steps to get the project Ids of all projects under the /Live directory:
- Open a research notebook.
- Load the assembly files and data types in their own cell.
- Import the data types.
- Call the
list_projectsmethod to get a list of all project responses. - Obtain the project Ids for the projects in
/Livedirectory.
#load "../Initialize.csx"
#load "../QuantConnect.csx" #r "../Microsoft.Data.Analysis.dll" using QuantConnect; using QuantConnect.Data; using QuantConnect.Algorithm; using QuantConnect.Research; using QuantConnect.Indicators; using Microsoft.Data.Analysis;
list_project_response = api.list_projects()
var listProjectResponse = api.ListProjects();
project_ids = [
project.project_id for project in list_project_response.projects
if project.name.split("/")[0] == "Live"
] var projectIds = listProjectResponse.Projects
.Where(project => project.Name.Split('/').First() == "Live")
.Select(project => project.ProjectId)
.ToList();
Deploy Live Algorithms
Follow these steps to progromatically deploy the preceding projects with the QuantConnect API:
- Compile all the projects and cache the compilation Ids with a dictionary.
- Get the Ids of all the live nodes that are available and sort them by their speed.
- Configure your brokerage and environment.
- Deploy the projects and cache the project Ids of the successful deployments.
compile_id_by_project_id = {}
for project_id in project_ids:
compile_response = api.create_compile(project_id)
if not compile_response.success:
print(f"Errors compiling project {project_id}: \n{compile_response.errors}")
else:
compile_id_by_project_id[project_id] = compile_response.compile_id var compileIdsByProjectIds = new Dictionary<int, string>();
foreach (var projectId in projectIds)
{
var compileResponse = api.CreateCompile(projectId);
if (!compileResponse.Success)
{
Console.WriteLine($"Errors compiling project {projectId}: \n{compileResponse.Errors}");
}
else
{
compileIdsByProjectIds[projectId] = compileResponse.CompileId;
}
}
live_nodes = []
node_response = api.read_project_nodes(project_ids[0])
if not node_response.success:
print(f"Error getting nodes: \n{node_response.errors}")
else:
nodes = sorted(
[node for node in node_response.nodes.live_nodes if not node.busy],
key=lambda node: node.speed,
reverse=True
)
node_ids = [node.id for node in nodes] var liveNodes = new List<string>();
var nodeResponse = api.ReadProjectNodes(projectIds[0]);
if (!nodeResponse.Success)
{
Console.WriteLine($"Error getting nodes: \n{nodeResponse.Errors}");
}
else
{
nodesIds = nodeResponse.Nodes.LiveNodes
.Where(node => !node.Busy)
.OrderByDescending(node => node.Speed)
.Select(node => node.Id)
.ToList();
}
Check the length of node_idsnodeIds is greater than 0 to ensure there are live nodes available.
For example, to use the QC paper brokerage, run:
base_live_algorithm_settings = {
"id": "QuantConnectBrokerage",
"user": "",
"password": "",
"environment": "paper",
"account": ""
}
version_id = "-1" # Master branch var baseLiveAlgorithmSettings = new Dictionary<string, object>
{
{"id", "QuantConnectBrokerage"},
{"user", ""},
{"password", ""},
{"environment", "paper"},
{"account", ""}
};
// Master branch
var versionId = "-1";
deployed_ids = []
for project_id, compile_id in compile_id_by_project_id.items():
# Deploy live algorithm
node_id = node_ids[len(deployed_ids)] # Fastest node available
live_response = api.create_live_algorithm(project_id, compile_id, node_id, base_live_algorithm_settings, version_id)
if not live_response.success:
print(f"Errors deploying project {project_id}: \n{live_response.errors}")
else:
print(f"Deployed {project_id}")
deployed_ids.append(project_id) var deployedIds = new List<int>();
foreach (var kvp in compileIdsByProjectIds)
{
var projectId = kvp.Key;
var compileId = kvp.Value;
// Deploy live algorithm
var nodeId = nodeIds[deployedIds.Count()];
var liveResponse = api.CreateLiveAlgorithm(projectId, compileId, nodeId, baseLiveAlgorithmSettings, versionId);
if (!liveResponse.Success)
{
Console.WriteLine($"Errors deploying project {projectId}: \n{liveResponse.Errors}");
}
else
{
Console.WriteLine($"Deployed {projectId}");
deployedIds.Add(projectId);
}
}
Stop Live Algorithms
To stop multiple live algorithms from an interactive notebook through the QuantConnect API, call the api.StopLiveAlgorithmapi.stop_live_algorithm method with each project Id.
for project_id in project_ids:
stop_response = api.stop_live_algorithm(project_id)
if not stop_response.success:
print(f"Errors stopping live algorithm {project_id}: \n{stop_response.errors}")
else:
print(f"Successfully stopped live algorithm {project_id}") foreach (var projectId in projectIds)
{
var stopResponse = api.StopLiveAlgorithm(projectId);
if (!stopResponse.Success)
{
Console.WriteLine($"Errors stopping live algorithm {projectId}: \n{stopResponse.Errors}");
}
else
{
Console.WriteLine($"Successfully stopped live algorithm {projectId}");
}
}
Examples
Example 1: Create New GPU Deployment
The following example will create a new live algorithm deployment using a GPU live node with the Interactive Brokers in a jupyter notebook. It can help with a streamline deployment.
// Load the necessary assemblies.
#load "../Initialize.csx"
#load "../QuantConnect.csx"
using QuantConnect;
using QuantConnect.Api;
using QuantConnect.Research;
// Instantiate QuantBook instance for researching.
var qb = new QuantBook();
// Get all the nodes available in the current project.
var projectNodes = api.ReadProjectNodes(qb.ProjectId);
// Obtain the most powerful live GPU node which is idling.
var liveNodeId = projectNodes.Nodes.LiveNodes
.Where(x => x.HasGPU && !x.Busy)
.MaxBy(x => x.CpuCount)
.Id;
// Compile the project before deploying.
var compilation = api.CreateCompile(qb.project_id);
var compileId = compilation.CompileId;
// Deploy the live algorithm with the brokerage settings.
var brokerageSettings = new Dictionary<string, object>() {
{"id", "InteractiveBrokersBrokerage"},
{"ib-user-name": "userName"},
{"ib-account": "accountNumber"},
{"ib-password": "password"},
{"ib-weekly-restart-utc-time": "00:00:00"}
}
var result = api.CreateLiveAlgorithm(qb.ProjectId, compileId, liveNodeId, brokerageSettings); # Instantiate QuantBook instance for researching.
qb = QuantBook()
# Get all the nodes available in the current project.
project_nodes = api.read_project_nodes(qb.project_id)
# Obtain the most powerful live GPU node which is idling.
live_node_id = max(
[x for x in project_nodes.nodes.live_nodes if x.has_gpu and not x.busy],
key=lambda x: x.cpu_count
).id
# Compile the project before deploying.
compilation = api.create_compile(qb.project_id)
compile_id = compilation.compile_id
# Deploy the live algorithm with the brokerage settings.
brokerage_settings = {
"id": "InteractiveBrokersBrokerage",
"ib-user-name": "user-name",
"ib-account": "account-number",
"ib-password": "password",
"ib-weekly-restart-utc-time": "00:00:00"
}
result = api.CreateLiveAlgorithm(qb.project_id, compile_id, live_node_id, brokerage_settings)