Live Algorithms

Managing Deployments

Introduction

This page explains how to create and manage live trading algorithms in the research environment.

Prerequisites

Working knowledge of C#.

If you use Python, you must understand how to work with pandas DataFrames and Series. If you are not familiar with pandas, refer to the pandas documentation.

You'll need to understand how to do Project Management through the QuantConnect Api.

Compile Projects

Follow these steps to compile the project.

  1. Load the required assembly files and data types.
  2. #load "../Initialize.csx"
    #load "../QuantConnect.csx"
    
    using QuantConnect;
    using QuantConnect.Api;
  3. Call CreateCompile to compile the project.
  4. var projectID = <yourProjectId>;
    var compiled = api.CreateCompile(projectID);
    project_id = <your_project_id>
    compiled = api.CreateCompile(project_id)
  5. Call CompileId attribute to obtain the compiled ID.
  6. var compileId = compiled.CompileId;
    compile_id = compiled.CompileId

Create Algorithms

You need to understand how to Compile Algorithms to create a live algorithm.

Follow these steps to create a live algorithm by using the QuantConnect Api.

  1. Call LiveAlgorithmSettings with your brokerage credentials and live algorithm's setting.
  2. In this tutorial, DefaultLiveAlgorithmSettings is used for using QuantConnect's paper trading. For other brokerages, please refer to LiveAlgorithmSettings.cs for reference.

    var liveSettings = new DefaultLiveAlgorithmSettings(user: "",
                                                        password: "",
                                                        environment: BrokerageEnvironment.Paper,
                                                        account: "");
    live_settings = DefaultLiveAlgorithmSettings(user="",
                                                 password="",
                                                 environment=BrokerageEnvironment.Paper,
                                                 account="")
  3. Call CreateLiveAlgorithm with the project ID, compile ID, live node ID, the LiveAlgorithmSetting and (optional) a custom version ID.
  4. // Using the first available free live node of your organization.
    var nodeId = api.ReadNodes("<yourOrganizationId>")
                    .LiveNodes.Where(x => x.Busy == false)
                    .FirstOrDefault().Id;
    var versionId = "1.0 Alpha";
    
    var newLiveAlgorithm = api.CreateLiveAlgorithm(projectId, compileId, nodeId, liveSettings, versionId);
    # Using the first available free live node of your organization.
    node_id = [x for x in api.ReadNodes("<your_organization_id>").LiveNodes
               if not x.Busy][0].Id
    version_id = "1.0 Alpha"
    
    new_live_algorithm = api.CreateLiveAlgorithm(project_id, compile_id, node_id, live_settings, version_id)

Liquidate Algorithms

Follow these steps to liquidate a live algorithm by using QuantConnect Api. Note that this will also stop the algorithm.

  1. Call LiquidateLiveAlgorithm with the project ID. This will return a RestResponse.
  2. var response = api.LiquidateLiveAlgorithm(projectId);
    response = api.LiquidateLiveAlgorithm(project_id)
  3. Call Success attribute to check if the liquidation request is successful.
  4. Console.WriteLine($"{response.Success}");
    print(f"{response.Success}");

Stop Algorithms

Follow these steps to stop a live algorithm by using QuantConnect Api. Note that open positions are susceptible to uncontrolled risk.

  1. Call StopLiveAlgorithm with the project ID. This will return a RestResponse.
  2. var response = api.StopLiveAlgorithm(projectId);
    response = api.StopLiveAlgorithm(project_id)
  3. Call Success attribute to check if the stop request is successful.
  4. Console.WriteLine($"{response.Success}");
    print(f"{response.Success}");

List Algorithms

Follow these steps to list out all live algorithms of a user by the QuantConnect Api.

  1. Call ListLiveAlgorithms with (optional) filtering criteria on its status, start and end time.
  2. var liveList = api.ListLiveAlgorithms(status: AlgorithmStatus.Running,
                                          startTime: new DateTime(2022, 1, 30),
                                          endTime: new DateTime(2021, 2, 10));
    live_list = api.ListLiveAlgorithms(status=AlgorithmStatus.Running,
                                       startTime=datetime(2022, 1, 30),
                                       endTime=datetime(2022, 2, 10))
  3. Call Algorithms attribute. This will return a list of live algorithms.
  4. var liveAlgos = liveList.Algorithms;
    live_list = live_list.Algorithms
  5. Display the live algorithms.
  6. In this tutorial, DefaultLiveAlgorithmSettings is used for using QuantConnect's paper trading. For other brokerages, please refer to LiveAlgorithmSettings.cs for reference.

    foreach(var algo in liveAlgos)
    {
        Console.WriteLine($@"Project ID: {algo.ProjectId}
    Deploy ID: {algo.DeployId}
    Status: {algo.Status}
    Launched Time: {algo.Launched}
    Stopped?: {algo.Stopped}
    Brokerage: {algo.Brokerage}
    Subscription: {algo.Subscription}
    ");
    }
    status = ['DeployError', 'InQueue', 'Running', 'Stopped', 'Liquidated', 'Deleted', 'Completed', 'RuntimeError', 'Invalid', 'LoggingIn', 'Initializing', 'History']
    for algo in live_list[-3:]:
        print(f"""Project ID: {algo.ProjectId}
    Deploy ID: {algo.DeployId}
    Status: {status[algo.Status]}
    Launched Time: {algo.Launched.strftime('%Y/%m/%d %H:%M:%S')}
    Stopped?: {algo.Stopped}
    Brokerage: {algo.Brokerage}
    Subscription: {algo.Subscription}\n""")

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: