Crypto Futures

Handling Data

Introduction

LEAN passes the data you request to the OnDataon_data method so you can make trading decisions. The default OnDataon_data method accepts a Slice object, but you can define additional OnDataon_data methods that accept different data types. For example, if you define an OnDataon_data method that accepts a TradeBar argument, it only receives TradeBar objects. The Slice object that the OnDataon_data method receives groups all the data together at a single moment in time. To access the Slice outside of the OnDataon_data method, use the CurrentSlicecurrent_slice 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 Symbolsymbol, but we recommend you use the Symbolsymbol.

To view the resolutions that are available for Crypto Futures data, see Resolutions.

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 decomposition

TradeBar objects have the following properties:

To get the TradeBar objects in the Slice, index the Slice or index the Barsbars property of the Slice with the security Symbolsymbol. 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 Symbolsymbol. To avoid issues, check if the Slice contains data for your security before you index the Slice with the security Symbolsymbol.

public override void OnData(Slice slice)
{
    if (slice.Bars.ContainsKey(_symbol))
    {
        var tradeBar = slice.Bars[_symbol];
    }
}
def on_data(self, slice: Slice) -> None:
    trade_bar = slice.bars.get(self._symbol)   # None if not found

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;
        var closePrice = tradeBar.Close;
    }
}
def on_data(self, slice: Slice) -> None:
    for symbol, trade_bar in slice.bars.items():
        close_price = trade_bar.close

Quotes

QuoteBar objects are bars that consolidate NBBO quotes from the exchanges. They contain the open, high, low, and close prices of the bid and ask. The Openopen, Highhigh, Lowlow, and Closeclose properties of the QuoteBar object are the mean of the respective bid and ask prices. If the bid or ask portion of the QuoteBar has no data, the Openopen, Highhigh, Lowlow, and Closeclose properties of the QuoteBar copy the values of either the Bidbid or Askask instead of taking their mean.

Quotebar decomposition

QuoteBar objects have the following properties:

To get the QuoteBar objects in the Slice, index the QuoteBars property of the Slice with the security Symbolsymbol. If the security doesn't actively get quotes or you are in the same time step as when you added the security subscription, the Slice may not contain data for your Symbolsymbol. To avoid issues, check if the Slice contains data for your security before you index the Slice with the security Symbolsymbol.

public override void OnData(Slice slice)
{
    if (slice.QuoteBars.ContainsKey(_symbol))
    {
        var quoteBar = slice.QuoteBars[_symbol];
    }
}
def on_data(self, slice: Slice) -> None:
    quote_bar = slice.quote_bars.get(self._symbol)   # None if not found

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

public override void OnData(Slice slice)
{
    foreach (var kvp in slice.QuoteBars)
    {
        var symbol = kvp.Key;
        var quoteBar = kvp.Value;
        var askPrice = quoteBar.Ask.Close;
    }
}
def on_data(self, slice: Slice) -> None:
    for symbol, quote_bar in slice.quote_bars.items():
        ask_price = quote_bar.ask.close

QuoteBar objects let LEAN incorporate spread costs into your simulated trade fills to make backtest results more realistic.

Ticks

Tick objects represent a single trade or quote at a moment in time. A trade tick is a record of a transaction for the security. A quote tick is an offer to buy or sell the security at a specific price. Tick objects have the following properties:

Trade ticks have a non-zero value for the Quantityquantity and Priceprice properties, but they have a zero value for the BidPricebid_price, BidSizebid_size, AskPriceask_price, and AskSizeask_size properties. Quote ticks have non-zero values for BidPricebid_price and BidSizebid_size properties or have non-zero values for AskPriceask_price and AskSizeask_size properties. To check if a tick is a trade or a quote, use the TickTypeticktype property.

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 Symbolsymbol. 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 Symbolsymbol. To avoid issues, check if the Slice contains data for your security before you index the Slice with the security Symbolsymbol.

public override void OnData(Slice slice)
{
    if (slice.Ticks.ContainsKey(_symbol))
    {
        var ticks = slice.Ticks[_symbol];
        foreach (var tick in ticks)
        {
            var price = tick.Price;
        }
    }
}
def on_data(self, slice: Slice) -> None:
    ticks = slice.ticks.get(self._symbol, [])   # Empty if not found
    for tick in ticks:
        price = tick.price

You can also iterate through the Ticks dictionary. The keys of the dictionary are the Symbol objects and the values are the List<Tick>list[Tick] objects.

public override void OnData(Slice slice)
{
    foreach (var kvp in slice.Ticks)
    {
        var symbol = kvp.Key;
        var ticks = kvp.Value;
        foreach (var tick in ticks)
        {
            var price = tick.Price;
        }
    }
}
def on_data(self, slice: Slice) -> None:
    for symbol, ticks in slice.ticks.items():
        for tick in ticks:
            price = tick.price

Tick data is raw and unfiltered, so it can contain bad ticks that skew your trade results. For example, some ticks come from dark pools, which aren't tradable. We recommend you only use tick data if you understand the risks and are able to perform your own online tick filtering.

Margin Interest Rates

MarginInterestRate objects contain the margin interest rate, which is a cost associated with trading on margin. MarginInterestRate objects have the following properties:

To get the MarginInterestRatemargin_interest_rate objects in the Slice, index the MarginInterestRatemargin_interest_rate property of the Slice with the Crypto Future Symbol. The MarginInterestRatemargin_interest_rate property of the Slice may not contain data for your Symbol. To avoid issues, check if the property contains data for your Crypto Future before you index it with the Crypto Future Symbol.

public override void OnData(Slice slice)
{
    if (slice.MarginInterestRates.ContainsKey(_symbol))
    {
        var interestRate = slice.MarginInterestRates[_symbol].InterestRate;
    }
}
def on_data(self, slice: Slice) -> None:
    margin_interest_rate = slice.margin_interest_rate.get(self.symbol)
    if margin_interest_rate:
        interest_rate = margin_interest_rate.interest_rate

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

public override void OnData(Slice slice)
{
    foreach (var kvp in slice.MarginInterestRates)
    {
        var symbol = kvp.Key;
        var marginInterestRate = kvp.Value;
        var interestRate = marginInterestRate.InterestRate;
    }
}
def on_data(self, slice: Slice) -> None:
    for symbol, margin_interest_rate in slice.MarginInterestRates.items():
        interest_rate = margin_interest_rate.interest_rate

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: