Live Trading
Algorithm Control
Introduction
The algorithm control features on the live results page let you adjust your algorithm while it is executing 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. For instance, you can create security subscriptions, place trades, stop the algorithm, and update the algorithm.
Add Security Subscriptions
The live results page enables you to 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 through the IDE without having to edit and redeploy the algorithm. Follow these steps to add security subscriptions:
- Open your algorithm's live results page.
- In the Holdings tab, click .
- Enter the symbol, security type, resolution, leverage, and market of the security you want to add.
- If you want the data for the security to be filled-forward, check the Fill Forward check box.
- If you want to subscribe to extended market hours for the security, check the Extended Market Hours check box.
- Click .
You can't manually remove security subscriptions from the IDE.
Place Manual Trades
The live results page lets you 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
Note that it's not currently possible to cancel manual orders.
Follow these steps to place manual orders:
- Open your algorithm's live results page.
- In the Holdings tab, if the security you want to trade isn't listed, click .
- If the security you want to trade still isn't listed, subscribe to the security.
- Click the security you want to trade.
- Click or .
- If you clicked , enter an order quantity.
- Click the Type field and then click an order type from the drop-down menu.
- Click .
Liquidate Positions
The live results page has a
button that 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 button let's you easily liquidate your portfolio instead of placing many manual trades. When you click the button, 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.Follow these steps to liquidate your positions:
- Open your algorithm's live results page.
- Click .
- Click again.
Stop the Algorithm
The live trading results page has a
button to immediately stop 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
- Upgrade the live trading node
- Update the settings you entered into the deployment wizard
- Place manual orders through your brokerage account instead of the web IDE
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.
Follow these steps to stop your algorithm:
- Open your algorithm's live results page.
- Click .
- Click again.
Update the Algorithm
If you need to adjust your algorithm's project files or parameter values, stop your algorithm, make your changes, and then redeploy your algorithm. You can't adjust your algorithm's code or parameter values while your algorithm executes. When you stop and redeploy a live algorithm, your project's live equity curve is retained between the deployments. To erase the equity curve history, clone the project and then redeploy the cloned version of the project.
To update parameters in live mode, add a Schedule Event that downloads a remote file and uses its contents to update the parameter values.
private Dictionary_parameters = new(); public override void Initialize() { if (LiveMode) { Schedule.On( DateRules.EveryDay(), TimeRules.Every(TimeSpan.FromMinutes(1)), ()=> { var content = Download(urlToRemoteFile); // Convert content to _parameters }); } }
def initialize(self): self.parameters = { } if self.live_mode: def download_parameters(): content = self.download(url_to_remote_file) # Convert content to self.parameters self.schedule.on(self.date_rules.every_day(), self.time_rules.every(timedelta(minutes=1)), download_parameters)