Trading and Orders

Liquidating Positions

Introduction

The Liquidateliquidate 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 Liquidateliquidate, LEAN tries to cancel them. The Liquidateliquidate 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 Liquidateliquidate 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 Liquidateliquidate 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 Liquidateliquidate 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 Liquidateliquidate 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 Truetrue.

var orderTickets = Liquidate(asynchronous: true, orderProperties: orderProperties);
order_tickets = self.liquidate(asynchronous=True, order_properties=order_properties)

Enable and Disable Liquidations

By default, the Liquidateliquidate method is functional. To enable and disable it, set the LiquidateEnabledliquidate_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.

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: