Trading and Orders

Liquidating Positions


The Liquidateliquidate 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 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 orderIds = Liquidate("IBM");
# Liquidate all IBM in your portfolio
order_ids = self.liquidate("IBM")

You can pass an order tag to the Liquidateliquidate method.

Liquidate("AAPL", "Liquidated");
self.liquidate("AAPL", "Liquidated")

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 orderIds = Liquidate();
// Liquidate your entire portfolio
order_ids = self.liquidate()

You can pass an order tag to the Liquidateliquidate method.

Liquidate(tag: "Liquidated");
self.liquidate(tag = "Liquidated")

Enable and Disable Liquidations

By default, the Liquidateliquidate 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.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: