Key Concepts

Event Handlers

Introduction

Event handlers are called during an algorithm execution to pass information to your strategy. Most of the events are not needed for simple strategies, but can be helpful for debugging issues in more complex algorithms.

Data Events

// Get data for a specific symbol to analyze its price movements or trading signals.
public override void OnData(Slice slice)
{
    slice.TryGetValue(_symbol, out var myData);
}

// Get split information to adjust holdings and calculations for the asset.
public override void OnSplits(Splits splits)
{
    splits.TryGetValue(_symbol, out var split);
}

// Capture dividend events to account for income or adjust the strategy based on the dividend payout.
public override void OnDividends(Dividends dividends)
{
    dividends.TryGetValue(_symbol, out var dividend);
}

// Track events when a security changes its ticker, allowing the algorithm to adapt to these changes.
public override void OnSymbolChangedEvents(SymbolChangedEvents symbolChangedEvents)
{
    symbolChangedEvents.TryGetValue(_symbol, out var symbolChangedEvent);
}

// Handle delisting events to manage the position or adjust the strategy when the symbol is delisted.
public override void OnDelistings(Delistings delistings)
{
    delistings.TryGetValue(_symbol, out var delisting);
}
# Get data for a specific symbol to analyze its price movements or trading signals.
def on_data(self, slice: Slice) -> None:
    my_data = slice.get(self._symbol)

# Get split information to adjust holdings and calculations for the symbol.
def on_splits(self, splits: Splits) -> None:
    split = splits.get(self._symbol)
 
# Capture dividend events to account for income or adjust the strategy based on the dividend payout.
def on_dividends(self, dividends: Dividends) -> None:
    dividend = dividends.get(self._symbol)
   
# Track events when a security changes its ticker, allowing the algorithm to adapt to these changes.
def on_symbol_changed_events(self, symbol_changed_events: SymbolChangedEvents) -> None:
    symbol_changed_event = symbol_changed_events.get(self._symbol])

# Handle delisting events to manage the position or adjust the strategy when the symbol is delisted.
def on_delistings(self, delistings: OnDelistings) -> None:
    delisting = delistings.get(self._symbol)

The OnDataon_data method is the primary event handler for receiving financial data events to your algorithm. It is triggered sequentially at the point in time the data is available; in backtesting and live. For daily data, this means the event is processed at market close. In this method, all data for a given moment of time is grouped in a single event, including custom data types. This data is passed with the Slice object.

When fill-forward is enabled for your asset, the OnData event handler will be called regularly even if there was no new data. This is the default behavior.

In backtesting, if your algorithm takes a long time to process a slice, the following slice objects queue up and the next event triggers when your algorithm finishes processing the current slice. In live trading, if your algorithm takes longer to process the current slice than the time period that the slice spans, the next event triggers when your algorithm finishes processing the current slice, but the slice of the following event is the most recent slice. For example, say your algorithm consumes second resolution Crypto data but it takes your algorithm 3.5 seconds to process each slice. In backtesting, you'll get every slice. In live trading, if you deploy at 12:00:00 AM Coordinated Universal Time (UTC), you'll get the first slice at 12:00:01 AM UTC (spanning 12:00:00 AM UTC to 12:00:01 AM UTC) and you'll get the second slice at 12:00:04.5 AM UTC (roughly spanning 12:00:03 AM UTC to 12:00:04 AM UTC).

The OnSplitson_splits, OnDividendson_dividends, OnSymbolChangedEventson_symbol_changed_events , and OnDelistingson_delistings event handlers provide data for their respective types in an isolated way. However, all of the data for these corporate actions is also available in the Slice in OnDataon_data.

To perform intensive computation before the market opens, use a Scheduled Event or the Traintrain method.

For more information on the Slice object and OnData event, see Handling Data.

Securities Changed Event

// Log the universe changes.
public override void OnSecuritiesChanged(SecurityChanges changes)
{
    foreach (var security in changes.AddedSecurities)
    {
        Debug($"{Time}: Added {security.Symbol}");
    }
    foreach (var security in changes.RemovedSecurities)
    {
        Debug($"{Time}: Removed {security.Symbol}");
        
        if (security.Invested)
        {
            Liquidate(security.Symbol, "Removed from Universe");
        }
    }
}
# Log the universe changes.
def on_securities_changed(self, changes: SecurityChanges) -> None:
    for security in changes.added_securities:
        self.debug(f"{self.time}: Added {security.symbol}")

    for security in changes.removed_securities:
        self.debug(f"{self.time}: Removed {security.symbol}")
        
        if security.invested:
            self.liquidate(security.symbol, "Removed from Universe")

The OnSecuritiesChangedon_securities_changed event notifies the algorithm when assets are added or removed from the universe. This can be due to changes in the Universe constituents, delisting, contract expiration, or an explicit call to the RemoveSecurityremove_security method.

The event is triggered immediately when the asset is removed from the universe; however the data feed for the asset may remain active if the algorithm has open orders.

For more information, see how to use Security Changed Events.

Order Events

// Track filled orders and get the details of each fill.
public override void OnOrderEvent(OrderEvent orderEvent)
{
    var order = Transactions.GetOrderById(orderEvent.OrderId);
    if (orderEvent.Status == OrderStatus.Filled)
    {
        Debug($"{Time}: {order.Type}: {orderEvent}");
    }
}

// Track Option assignment events to handle assigned Equity holdings. 
public override void OnAssignmentOrderEvent(OrderEvent assignmentEvent)
{
    Log(assignmentEvent.ToString());
}
# Track filled orders and get the details of each fill.
def on_order_event(self, order_event: OrderEvent) -> None:
    order = self.transactions.get_order_by_id(order_event.order_id)
    if order_event.status == OrderStatus.FILLED:
        self.debug(f"{self.time}: {order.type}: {order_event}")

# Track Option assignment events to handle assigned Equity holdings. 
def on_assignment_order_event(self, assignment_event: OrderEvent) -> None:
    self.log(str(assignment_event))

The OnOrderEventon_order_event method notifies the algorithm of new orders, and changes in the order status such as fill events and cancelations. For options assignment events there is a dedicated event handler OnAssignmentOrderEventon_assignment_order_event.

In backtesting order events are triggered synchronously after the main data events. In live trading, order events are asynchronously as they occur. To avoid infinite loops, we recommend not to place orders in the OnOrderEvent.

For more information, see how to use Order Events.

Brokerage Events

// Track brokerage messages.
public override void OnBrokerageMessage(BrokerageMessageEvent message)
{
    if (message.Type== BrokerageMessageType.Reconnect)
    {
        Log($"{Time}: {message.Type}: Message: {message.Message}");
    }
}

// Track brokerage disconnections and respond to connectivity interruptions.
public override void OnBrokerageDisconnect()
{
    Log($"Brokerage disconnection detected at {Time}.");
}

// Track brokerage reconnections to resume trading operations.
public override void OnBrokerageReconnect()
{
    Log($"Brokerage reconnected at {Time}.");
}
# Track brokerage messages.
def on_brokerage_message(self, message: BrokerageMessageEvent) -> None: 
    if message.type == BrokerageMessageType.RECONNECT:
        self.log(f"{self.time}: {message.type}: Message: {message.message}")

# Track brokerage disconnections and respond to connectivity interruptions.
def on_brokerage_disconnect(self) -> None:
    self.log(f"Brokerage disconnection detected at {self.time}")

 # Track brokerage reconnections to resume trading operations.
def on_brokerage_reconnect(self) -> None:
    self.log(f"Brokerage reconnected at {self.time}")

The OnBrokerageDisconnecton_brokerage_disconnect and OnBrokerageReconnecton_brokerage_reconnect event handlers pass information to the algorithm about the brokerage connection status. This can be helpful for considering when to place a trade when a brokerage API is unreliable or under maintenance. The OnBrokerageMessageon_brokerage_message event handler provides information from the brokerages, including the disconnect and reconnect messages. Message content varies with each brokerage.

Brokerage events are triggered asynchronously in live trading, and are not created in backtesting.

Margin Call Events

// Track when remaining margin is low.
public override void OnMarginCallWarning()
{
    Log("Margin call warning, 5% margin remaining");
}
    
// Review and adjust liquidation orders in response to a margin call.
public override void OnMarginCall(List requests)
{
    // Modify order requests to choose what to liquidate.
    foreach (var orderRequest in requests) 
    {
    }
}
# Track when remaining margin is low.
def on_margin_call_warning(self) -> None:
    self.log("Margin call warning, 5% margin remaining")
    
# Review and adjust liquidation orders in response to a margin call.
def on_margin_call(self, requests): -> List[SubmitOrderRequest]: 
    # Modify order requests to choose what to liquidate.
    return requests

The OnMarginCallWarningon_margin_call_warning and OnMarginCallon_margin_call event handlers provide information and control over how to reduce algorithm leverage when performance is poor.

The OnMarginCallWarningon_margin_call_warning method is called when there is less than 5% margin remaining to give you an opportunity to reduce leverage before an official margin call is performed by the brokerage. The OnMarginCallon_margin_call event handler is passed a list of SubmitOrderRequest objects which will be executed on exiting the method.

Margin events are called before the data events are processed.

The following demonstration demonstration processes suggested orders and modifies the qualities to liquidate more than necessary and prevent a second margin call. For more information, see how to handle Margin Calls.

Warmup Finished Event

// This event handler runs when the algorithm is done warming up.
public override void OnWarmupFinished()
{
    // Done warming up.
}
# This event handler runs when the algorithm is done warming up.
def on_warmup_finished(self) -> None:
    pass # Done warming up.

The OnWarmupFinishedon_warmup_finished event handler runs once initialization and warm up is complete.

Command Events

// An event handler for receiving external commands. 
public override bool? OnCommand(dynamic data)
{
    Log($"Got command at {Time} with data: {data}");
    return true;
}
# An event handler for receiving external commands. 
def on_command(self, data):
    self.log(f'Got command at {self.time} with data: {data}')
    return True

The OnCommandon_command event handler runs when you send external commands to your algorithm.

End Of Algorithm Events

# The final step after algorithm execution. 
def on_end_of_algorithm(self) -> None:
    self.debug("Algorithm done")
// The final step after algorithm execution. 
public override void OnEndOfAlgorithm()
{
    Debug("Algorithm done");
}

The OnEndOfAlgorithmon_end_of_algorithm event handler is when the algorithm has finished executing as its final step. This event handler is helpful for performing final analysis, or saving algorithm state.

Event Flow

When you deploy an algorithm, LEAN first calls the Initializeinitialize method. In live mode, the engine loads your holdings and open orders from your brokerage account to add data subscriptions and populate the Securitiessecurities, Portfolioportfolio, and Transactionstransactions objects. LEAN receives the data from the subscriptions, synchronizes the data to create a timeslice, and then performs the following steps. Your algorithm can spend up to 10 minutes on each timeslice unless you call the Traintrain method.

  1. If it's a backtest, check if there are Scheduled Events in the past that didn't fire because there was no data between the previous slice and the current slice. LEAN automatically creates a Scheduled Event to call the OnEndOfDayon_end_of_day method at the end of each day.
  2. In live mode, Scheduled Events occur in a separate thread from the algorithm manager, so they run at the correct time.

  3. Update the algorithm time.
  4. Update the CurrentSlicecurrent_slice.
  5. Pass the SymbolChangedEvents to the OnSymbolChangedEventson_symbol_changed_events method.
  6. Cancel all open orders for securities that changed their ticker.
  7. Add a Security object to the Securitiessecurities collection for each new security in the universe.
  8. Update the Security objects with the latest data.
  9. Update the Cash objects in the CashBook with the latest data.
  10. Process fill models for non-market orders.
  11. Submit market on open orders to liquidate Equity Option contracts if the underlying Equity has a split warning.
  12. Process margin calls.
  13. If it's time to settle unsettled cash, perform settlement.
  14. Call the OnSecuritiesChangedon_securities_changed method with the latest security changes.
  15. Apply dividends to the portfolio.
  16. For securities that have a split warning, update their portfolio holdings and adjust their open orders to account for the split.
  17. Update consolidators with the latest data.
  18. Pass custom data to the OnDataon_data method.
  19. Pass the Dividends to the OnDividendson_dividends method.
  20. Pass the Splits to the OnSplitson_splits method.
  21. Pass the Delistings to the OnDelistingson_delistings method.
  22. Pass the Slice to the OnDataon_data method.
  23. Perform universe selection.
  24. Pass the Slice to the Updateupdate method of each Alpha model.
  25. Pass the Insight objects from the Alpha model to the Portfolio Construction model.
  26. Pass the PortfolioTarget objects from the Portfolio Construction model to each Risk Management model.
  27. Pass the risk-adjusted PortfolioTarget objects from the Risk Management models to the Execution model.

When your algorithm stops executing, LEAN calls the OnEndOfAlgorithmon_end_of_algorithm method.

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: