Live Trading

Algorithm Control

Introduction

The algorithm control features let you adjust your algorithm while it executes live so that you can perform actions that are not written in the project files. The control features let you intervene in the execution of your algorithm and make adjustments. The control features that are available to you depend on if you deploy the algorithm on your local machine or on the QuantConnect cloud servers.

Control Local Algorithms

While your local algorithms run, you can add security subscriptions, submit orders, adjust orders, and stop their execution.

Add Security Subscriptions

You can manually create security subscriptions for your algorithm instead of calling the AddsecurityType methods in your code files. If you add security subscriptions to your algorithm, you can place manual trades without having to edit and redeploy the algorithm. To add security subscriptions, open a terminal in the organization workspace that contains the project and then run lean live add-security "My Project".

$ lean live add-security "My Project" --ticker "SPY" --market "usa" --security-type "equity"

For more information about the command options, see Options.

You can't manually remove security subscriptions.

Submit Orders

In local deployments, you can manually place orders instead of calling the automated methods in your project files. You can use any order type that is supported by the brokerage that you used when deploying the algorithm. To view the supported order types of your brokerage, see the Orders section of your brokerage model. Some example situations where it may be helpful to place manual orders instead of stopping and redeploying the algorithm include the following:

  • Your brokerage account had holdings in it before you deployed your algorithm
  • Your algorithm had bugs in it that caused it to purchase the wrong security
  • You want to add a hedge to your portfolio without adjusting the algorithm code
  • You want to rebalance your portfolio before the rebalance date

To submit orders, open a terminal in the organization workspace that contains the project and then run lean live submit-order "My Project".

$ lean live submit-order "My Project" --ticker "SPY" --market "usa" --security-type "equity" --order-type "market" --quantity 10

For more information about the command options, see Options.

Update Orders

To update an existing order, open a terminal in the organization workspace that contains the project and then run lean live update-order "My Project".

$ lean live update-order "My Project" --order-id 1 --quantity 5

For more information about the command options, see Options.

Cancel Orders

To cancel an existing order, open a terminal in the organization workspace that contains the project and then run lean live cancel-order "My Project".

$ lean live cancel-order "My Project" --order-id 1

For more information about the command options, see Options.

Liquidate Positions

To liquidate a specific asset in your algorithm, open a terminal in the organization workspace that contains the project and then run lean live liquidate "My Project".

$ lean live liquidate "My Project" --ticker "SPY" --market "usa" --security-type "equity"

When you run the command, if the market is open for the asset, the algorithm liquidates it with market orders. If the market is not open, the algorithm places market on open orders.

For more information about the command options, see Options.

Stop Algorithms

The lean live stop command immediately stops your algorithm from executing. When you stop a live algorithm, your portfolio holdings are retained. Stop your algorithm if you want to perform any of the following actions:

  • Update your project's code files
  • Update the settings you entered into the deployment command
  • Place manual orders through your brokerage account

Furthermore, if you receive new securities in your portfolio because of a reverse merger, you also need to stop and redeploy the algorithm.

LEAN actively terminates live algorithms when it detects interference outside of the algorithm's control to avoid conflicting race conditions between the owner of the account and the algorithm, so avoid manipulating your brokerage account and placing manual orders on your brokerage account while your algorithm is running. If you need to adjust your brokerage account holdings, stop the algorithm, manually place your trades, and then redeploy the algorithm.

To stop an algorithm, open a terminal in the organization workspace that contains the project and then run lean live stop "My Project".

$ lean live stop "My Project"

For more information about the command options, see Options.

Control Cloud Algorithms

While your cloud algorithms run, you can liquidate their positions and stop their exeuction.

Liquidate Positions

The lean cloud live liquidate command acts as a "kill switch" to sell all of your portfolio holdings. If your algorithm has a bug in it that caused it to purchase a lot of securities that you didn't want, this command let's you easily liquidate your portfolio instead of placing many manual trades. When you run the command, if the market is open for an asset you hold, the algorithm liquidates it with market orders. If the market is not open, the algorithm places market on open orders. After the algorithm submits the liquidation orders, it stops executing.

To stop an algorithm, open a terminal in the organization workspace that contains the project and then run lean cloud live liquidate "My Project".

$ lean cloud live liquidate "My Project"

For more information about the command options, see Options.

Stop Algorithms

The lean live stop command immediately stops your algorithm from executing. When you stop a live algorithm, your portfolio holdings are retained. Stop your algorithm if you want to perform any of the following actions:

  • Update your project's code files
  • Update the settings you entered into the deployment command
  • Place manual orders through your brokerage account

Furthermore, if you receive new securities in your portfolio because of a reverse merger, you also need to stop and redeploy the algorithm.

LEAN actively terminates live algorithms when it detects interference outside of the algorithm's control to avoid conflicting race conditions between the owner of the account and the algorithm, so avoid manipulating your brokerage account and placing manual orders on your brokerage account while your algorithm is running. If you need to adjust your brokerage account holdings, stop the algorithm, manually place your trades, and then redeploy the algorithm.

To stop an algorithm, open a terminal in the organization workspace that contains the project and then run lean cloud live stop "My Project".

$ lean cloud live stop "My Project"

For more information about the command options, see Options.

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: