Drafts

Automate Deployment

Introduction

This page provides tutorial on how to automate live deployment instance(s), from start to stop, from an interactive notebook through QuantConnect API. By this method, you will be able to deploy many algorithms at once.

Get Project Ids

To automate live deployments of a number of projects, save the projects under the same directory first. In this tutorial, the projects are saved under the directory /Live.

Follow the below steps to get the project Ids of all projects under the /Live directory.

  1. Load the assembly files and data types in their own cell.
  2. #load "../Initialize.csx"
  3. Import the data types.
  4. #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;
  5. Call the API to get a list of all project responses.
  6. list_project_response = api.list_projects()
    var listProjectResponse = api.ListProjects();
  7. Obtain the project Ids for the projects in /Live directory.
  8. 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 the below steps to deploy live algorithm(s) through QuantConnect API with project Ids.

  1. Compile all projects with their project Ids, cache the compilation Ids with a Dictionary.
  2. 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;
        }
    }
  3. Obtain the all Ids of live nodes available, ordering by their speeds.
  4. 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:
        live_nodes = [node.id for node in 
            sorted([node for node in node_response.nodes.live_nodes if not node.busy], 
                key=lambda node: node.speed, 
                reverse=True)]
    var liveNodes = new List<string>();
    var nodeResponse = api.ReadProjectNodes(projectIds[0]);
    
    if (!nodeResponse.Success)
    {
        Console.WriteLine($"Error getting nodes: \n{nodeResponse.Errors}");
    }
    else
    {
        liveNodes = nodeResponse.Nodes.LiveNodes
            .Where(node => !node.Busy)
            .OrderByDescending(node => node.Speed)
            .Select(node => node.Id)
            .ToList();
    }

    Make sure you check there are remaining live nodes available, by inspecting the length of live_nodesliveNodes is greater than 0.

  5. Configure your brokerage and environment.
  6. In this example, we use QC paper brokerage.

    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";
  7. Deploy the selected projects, with the project Ids, compilation Ids, and live nodes available. Cache the project Ids that are successfully deployed.
  8. deployed_ids = []
    
    for project_id, compile_id in compile_id_by_project_id.items():
        # Deploy live algorithm
        node_id = live_nodes[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 = liveNodes[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

Live algorithms can also be stopped all together by QuantConnect API through an interactive notebook.

Iterate through the project Ids of the algorithms you wished to stop from live deployment.

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}");
    }
}

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: