Contents
Live Algorithms
Managing Deployments
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.
- Load the required assembly files and data types.
- Call
CreateCompile
to compile the project. - Call
CompileId
attribute to obtain the compiled ID.
#load "../Initialize.csx" #load "../QuantConnect.csx" using QuantConnect; using QuantConnect.Api;
var projectID = <yourProjectId>; var compiled = api.CreateCompile(projectID);
project_id = <your_project_id> compiled = api.CreateCompile(project_id)
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.
- Call
LiveAlgorithmSettings
with your brokerage credentials and live algorithm's setting. - Call
CreateLiveAlgorithm
with the project ID, compile ID, live node ID, theLiveAlgorithmSetting
and (optional) a custom version ID.
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="")
// 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.
- Call
LiquidateLiveAlgorithm
with the project ID. This will return aRestResponse
. - Call
Success
attribute to check if the liquidation request is successful.
var response = api.LiquidateLiveAlgorithm(projectId);
response = api.LiquidateLiveAlgorithm(project_id)
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.
- Call
StopLiveAlgorithm
with the project ID. This will return aRestResponse
. - Call
Success
attribute to check if the stop request is successful.
var response = api.StopLiveAlgorithm(projectId);
response = api.StopLiveAlgorithm(project_id)
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.
- Call
ListLiveAlgorithms
with (optional) filtering criteria on its status, start and end time. - Call
Algorithms
attribute. This will return a list of live algorithms. - Display the live algorithms.
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))
var liveAlgos = liveList.Algorithms;
live_list = live_list.Algorithms
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""")