Securities

Handling Data

Introduction

LEAN packages the data for your subscriptions in a Slice object and passes it to the OnDataon_data method of your algorithm. To avoid look-ahead bias, LEAN only provides the data that's available at the current time in your algorithm. This processing style ensures that your algorithm backtests in the same manner that it trades live. Your algorithm uses the data to make trading decisions and LEAN uses the data to update your positions, track your portfolio value, and simulate orders.

Security Cache

To get the most recent data for a security, call the GetLastDataget_last_data method on the Security object.

var data = Securities["SPY"].GetLastData();
data = self.securities["SPY"].get_last_data()

Timeslice

The Slice that LEAN passes to the OnDataon_data method represents all of the data for your subscriptions at a single point in time. The Slice object contains data like TickTICK objects, TradeBar objects, QuoteBar objects, corporate actions, and chains for Option and Future contracts. You can use the data in the Slice to make trading decisions.

To access data in the Slice, index it with the security Symbol. If you use the security ticker instead of the Symbol, LEAN automatically finds the Symbol.

public override void OnData(Slice slice)
{
    if (slice.ContainsKey(_symbol))
    {
        var myData = slice[_symbol];
    }
}
def on_data(self, slice: Slice) -> None:
    if slice.contains_key(self.symbol):
        my_data = slice[self.symbol]

The following table shows the Slice property to index based on the data format you want:

Data FormatSlice Property
TradeBarBarsbars
QuoteBarQuoteBarsquote_bars
TickTicksticks
public override void OnData(Slice slice)
{
    if (slice.ContainsKey(_symbol))
    {
        var bar = slice.Bars[_symbol];
    }
}
def on_data(self, slice: Slice) -> None:
    if slice.contains_key(self.symbol):
        bar = slice.bars[self.symbol]

If you just index the Slice instead of the preceding properties, it returns the correct object. If your data subscription provides QuoteBar objects and you index the Slice with the security Symbol, it returns the QuoteBar.

Slice objects have the following properties:

For more information about the Slice class, see Timeslices.

Corporate Actions

The Slice that LEAN passes to the OnDataon_data method may contain corporate action data for the Equities in your algorithm. Instead of adding logic to your OnDataon_data defintion to respond to corporate actions (for example, resetting indicators), you can isolate the logic in one of the following event handlers.

Splits

The OnSplitson_splits event handler provides information on stock splits.

public override void OnSplits(Splits splits)
{
    foreach (var kvp in splits)
    {
        var symbol = kvp.Key;
        var split = kvp.Value;
    }
}
def on_splits(self, splits: Splits) -> None:
    for symbol, split in splits.items():
        pass

Dividends

The OnDividendson_dividends event handler provides information on dividend payments.

public override void OnDividends(Dividends dividends)
{
    foreach (var kvp in dividends)
    {
        var symbol = kvp.Key;
        var dividend = kvp.Value;
    }
}
def on_dividends(self, dividends: Dividends) -> None:
    for symbol, dividend in dividends.items():
        pass

Symbol Changes

The OnSymbolChangedEventson_symbol_changed_events event handler provides information on ticker changes.

public override void OnSymbolChangedEvents(SymbolChangedEvents symbolChangedEvents)
{
    foreach (var kvp in symbolChangedEvents)
    {
        var symbol = kvp.Key;
        var symbolChangedEvent = kvp.Value;
    }
}
def on_symbol_changed_events(self, symbol_changed_events: SymbolChangedEvents) -> None:
    for symbol, symbol_changed_event in symbol_changed_events.items():
        pass

Delistings

The OnDelistingson_delistings event handler provides information on asset delistings.

public override void OnDelistings(Delistings delistings)
{
    foreach (var kvp in delistings)
    {
        var symbol = kvp.Key;
        var delisting = kvp.Value;
    }
}
def on_delistings(self, delistings: Delistings) -> None:
    for symbol, delisting in delistings.items():
        pass

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: