Trading and Orders
Liquidating Positions
Introduction
The Liquidate
method lets you liquidate individual assets or your entire portfolio. The method creates market orders to close positions and returns the IDs of the liquidation orders. If you have pending open orders for the security when you call Liquidate
, LEAN tries to cancel them. The Liquidate
method works for all asset classes, except Crypto. To liquidate Crypto positions, see Crypto Trades.
Liquidate Individual Positions
To liquidate your holdings in an individual security, call the Liquidate
method and provide a ticker or Symbol
.
// Liquidate all IBM in your portfolio var orderIds = Liquidate("IBM");
# Liquidate all IBM in your portfolio order_ids = self.Liquidate("IBM")
You can pass an order tag to the Liquidate
method.
Liquidate("AAPL", "Liquidated");
self.Liquidate("AAPL", "Liquidated")
Liquidate All Positions
To liquidate all of the positions in your portfolio, call the Liquidate
method without any ticker of Symbol
arguments.
// Liquidate your entire portfolio var orderIds = Liquidate();
// Liquidate your entire portfolio order_ids = self.Liquidate()
You can pass an order tag to the Liquidate
method.
Liquidate(tag: "Liquidated");
self.Liquidate(tag = "Liquidated")
Enable and Disable Liquidations
By default, the Liquidate
method is functional. To enable and disable it, set the Settings.LiquidateEnabled
property.
// Disable liquidations Settings.LiquidateEnabled = false; // Enable liquidations Settings.LiquidateEnabled = true;
# Disable liquidations self.Settings.LiquidateEnabled = False # Enable liquidations self.Settings.LiquidateEnabled = True
Market Closed Considerations
If you liquidate your positions when the market is closed, LEAN converts the orders into market on open orders. If your brokerage doesn't support market on open orders, the order is invalid.