Index

Handling Data

Introduction

LEAN passes the data you request to the OnData method so you can make trading decisions. The default OnData method accepts a Slice object, but you can define additional OnData methods that accept different data types. For example, if you define an OnData method that accepts a Tick argument, it only receives Tick objects. The Slice object that the OnData method receives groups all the data together at a single moment in time. To access the Slice outside of the OnData method, use the CurrentSlice property of your algorithm.

All the data formats use DataDictionary objects to group data by Symbol and provide easy access to information. The plural of the type denotes the collection of objects. For instance, the Ticks DataDictionary is made up of Tick objects. To access individual data points in the dictionary, you can index the dictionary with the Index ticker or Symbol, but we recommend you use the Symbol.

Bars

You can't trade Indices, but TradeBar objects are bars that represent the open, high, low, and close of an Index price over a period of time.

Trade bar breakdown

TradeBar objects have the following properties:

To get the TradeBar objects in the Slice, index the Slice or index the Bars property of the Slice with the Index Symbol. The Slice may not contain data for your Symbol at every time step. To avoid issues, check if the Slice contains data for your Index before you index the Slice with the Index Symbol.

public override void OnData(Slice slice)
{
    if (slice.Bars.ContainsKey(_symbol))
    {
        var tradeBar = slice.Bars[_symbol];
        var value = tradeBar.Value;
    }
}

public void OnData(TradeBars tradeBars)
{
    if (tradeBars.ContainsKey(_symbol))
    {
        var tradeBar = tradeBars[_symbol];
        var value = tradeBar.Value;
    }
}
def OnData(self, slice: Slice) -> None:
    if self.symbol in slice.Bars:
        trade_bar = slice.Bars[self.symbol]
        value = trade_bar.Value

Ticks

Tick objects represent a price for the Index at a moment in time. Tick objects have the following properties:

Index ticks have a non-zero value for the Price property, but they have a zero value for the BidPrice, BidSize, AskPrice, and AskSize properties.

In backtests, LEAN groups ticks into one millisecond buckets. In live trading, LEAN groups ticks into ~70-millisecond buckets. To get the Tick objects in the Slice, index the Ticks property of the Slice with a Symbol. The Slice may not contain data for your Symbol at every time step. To avoid issues, check if the Slice contains data for your Index before you index the Slice with the Index Symbol.

public override void OnData(Slice slice)
{
    if (slice.Ticks.ContainsKey(_symbol))
    {
        var ticks = slice.Ticks[_symbol];
        foreach (var tick in ticks)
        {
            var value = tick.Value;
        }
    }
}

public void OnData(Ticks ticks)
{
    if (ticks.ContainsKey(_symbol))
    {
        foreach (var tick in ticks[_symbol])
        {
            var value = tick.Value;
        }
    }
}
def OnData(self, slice: Slice) -> None:
    if self.symbol in slice.Ticks:
        ticks = slice.Ticks[self.symbol]
        for tick in ticks:
            value = tick.Value

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: