Trading and Orders
Liquidating Positions
Introduction
The Liquidate
liquidate
method lets you liquidate individual assets or your entire portfolio. The method creates market orders to close positions and returns the tickets of the liquidation orders. If you have pending open orders for the security when you call Liquidate
liquidate
, LEAN tries to cancel them. The Liquidate
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
liquidate
method and provide a ticker or Symbol
.
// Liquidate all IBM in your portfolio var orderTickets = Liquidate("IBM");
# Liquidate all IBM in your portfolio order_tickets = self.liquidate("IBM")
You can pass an order tag and properties to the Liquidate
liquidate
method.
var orderProperties = new OrderProperties() { TimeInForce = TimeInForce.Day }; var orderTickets = Liquidate("AAPL", tag: "Liquidated", orderProperties: orderProperties);
order_properties = OrderProperties() order_properties.time_in_force = TimeInForce.DAY order_tickets = self.liquidate("AAPL", tag="Liquidated", order_properties=order_properties)
Liquidate All Positions
To liquidate all of the positions in your portfolio, call the Liquidate
liquidate
method without any ticker of Symbol
arguments.
// Liquidate your entire portfolio var orderTickets = Liquidate();
// Liquidate your entire portfolio order_tickets = self.liquidate()
You can pass an order tag and properties to the Liquidate
liquidate
method.
var orderProperties = new OrderProperties() { TimeInForce = TimeInForce.Day }; var orderTickets = Liquidate(tag: "Liquidated", orderProperties: orderProperties);
order_properties = OrderProperties() order_properties.time_in_force = TimeInForce.DAY order_tickets = self.liquidate(tag="Liquidated", order_properties=order_properties)
Place Asynchronous Liquidations
When you trade a large portfolio of assets, you may want to send orders in batches and not wait for the response of each one. To send asynchronous liquidation orders, set the asynchronous
argument to True
true
.
var orderTickets = Liquidate(asynchronous: true, orderProperties: orderProperties);
order_tickets = self.liquidate(asynchronous=True, order_properties=order_properties)
Enable and Disable Liquidations
By default, the Liquidate
liquidate
method is functional. To enable and disable it, set the LiquidateEnabled
liquidate_enabled
algorithm setting.
// Disable liquidations Settings.LiquidateEnabled = false; // Enable liquidations Settings.LiquidateEnabled = true;
# Disable liquidations self.settings.liquidate_enabled = False # Enable liquidations self.settings.liquidate_enabled = 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.