Skip to content
All library documents

Graceful Strategy Shutdown with an Abrupt-Closing Lifecycle Hook

Article Lumibot

Summary

The document explains a strategy lifecycle method that runs when strategy execution is interrupted. It presents the hook as a place to stop trading gracefully, with selling all assets given as an example action. A brief Python example defines the method on a strategy class and calls its sell-all operation.

This is an operational concept for handling interruption, not a trading signal or portfolio method. The reference identifies the corresponding strategy API method, but the document gives no details about which interruptions trigger it, whether open orders are canceled, how failures inside the hook are handled, or whether liquidation completes before the process exits. Those behaviors depend on the platform and execution environment, so the example should not be assumed to guarantee that all exposure is closed.

Key ideas

  • The lifecycle hook runs when strategy execution is interrupted.
  • It can be used to perform graceful shutdown actions such as attempting to sell assets.
  • The example places the shutdown action inside a strategy class method.
  • The document does not explain interruption triggers, order cancellation, or completion guarantees.

Tags

Full text
# lifecycle methods.on abrupt closing


def on_abrupt_closing
===================================

.. meta::
   :description: This lifecycle method runs when the strategy execution gets interrupted. Use this lifecycle method to execute code to stop trading gracefully like selling all assets.

This lifecycle method runs when the strategy execution gets interrupted. Use this lifecycle method to execute code to stop trading gracefully like selling all assets

.. code-block:: python

    class MyStrategy(Strategy):
        def on_abrupt_closing(self):
            self.sell_all()

Reference
----------

.. autofunction:: lumibot.strategies.strategy.Strategy.on_abrupt_closing

Shown in full with attribution under the source's licence. Licence: GPL-3.0

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.