Strategy Lifecycle Hooks Before Market Open and Trading Start
Summary
This reference explains two strategy lifecycle hooks for handling setup before trading begins. The before-market-open hook runs each day before the market opens; an example use is canceling outstanding orders. If a strategy launches after the market has opened, this hook is skipped on that first day.
A second hook, before-starting-trading, is described as similar but runs before trading begins even when the strategy is first launched during market hours. The page says both hooks run in a defined order after the first execution, but the excerpt does not provide that order. It offers no performance evidence or trading strategy, and its value is operational: it clarifies when pre-trade logic runs and identifies a first-day timing caveat that strategy authors need to account for.
Key ideas
- The before-market-open hook runs daily before the market opens.
- It is skipped on the first day if the strategy starts after the market is already open.
- Canceling open orders is given as an example of work to perform before the session.
- The before-starting-trading hook runs before trading begins even when launched during market hours.
- The excerpt mentions an execution order for the hooks but does not state what that order is.
Tags
Full text
# lifecycle methods.before market opens
def before_market_opens
===================================
.. meta::
:description: This lifecycle method is executed each day before market opens. If the strategy is first run when the market is already open, this method will be skipped the first day.
This lifecycle method is executed each day before market opens. If the strategy is first run when the market is already open, this method will be skipped the first day. Use this lifecycle methods to execute business logic before starting trading like canceling all open orders.
.. code-block:: python
class MyStrategy(Strategy):
def before_market_opens(self):
self.cancel_open_orders()
.. code-block:: python
def before_starting_trading()
This lifecycle method is similar to before_market_opens. However, unlike before_market_opens, this method will always be executed before starting trading even if the market is already open when the strategy was first launched. After the first execution, both methods will be executed in the following order
Reference
----------
.. autofunction:: lumibot.strategies.strategy.Strategy.before_market_opensShown 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.