India Equity

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 TradeBar argument, it only receives TradeBar 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 TradeBars DataDictionary is made up of TradeBar objects. To access individual data points in the dictionary, you can index the dictionary with the security ticker or Symbol, but we recommend you use the Symbol.

Trades

TradeBar objects are price bars that consolidate individual trades from the exchanges. They contain the open, high, low, close, and volume of trading activity over a period of time.

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 security Symbol. If the security doesn't actively trade or you are in the same time step as when you added the security subscription, the Slice may not contain data for your Symbol. To avoid issues, check if the Slice contains data for your security before you index the Slice with the security Symbol.

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

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

You can also iterate through the TradeBars dictionary. The keys of the dictionary are the Symbol objects and the values are the TradeBar objects.

public override void OnData(Slice slice)
{
    foreach (var kvp in slice.Bars)
    {
        var symbol = kvp.Key;
        var tradeBar = kvp.Value;
    }
}

public void OnData(TradeBars tradeBars)
{
    foreach (var kvp in tradeBars)
    {
        var symbol = kvp.Key;
        var tradeBar = kvp.Value;
    }
}
def OnData(self, slice: Slice) -> None:
    for symbol, trade_bar in slice.Bars.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: