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 contract ticker or Symbolsymbol, but we recommend you use the Symbolsymbol.

To view the resolutions that are available for 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

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

public override void OnData(Slice slice)
{
    // Check if the symbol is contained in TradeBars object
    if (slice.Bars.ContainsKey(_contractSymbol))
    {
        // Obtain the mapped TradeBar of the symbol
        var tradeBar = slice.Bars[_contractSymbol];
    }
}
def on_data(self, slice: Slice) -> None:
    # Obtain the mapped TradeBar of the symbol if any
    trade_bar = slice.bars.get(self._contract_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)
{
    // Iterate all received Symbol-TradeBar key-value pairs
    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:
    # Iterate all received Symbol-TradeBar key-value pairs
    for symbol, trade_bar in slice.bars.items():
        close_price = trade_bar.close

TradeBar objects have the following properties:

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

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

public override void OnData(Slice slice)
{
    // Check if the symbol is contained in QuoteBars object
    if (slice.QuoteBars.ContainsKey(_contractSymbol))
    {
        // Obtain the mapped QuoteBar of the symbol
        var quoteBar = slice.QuoteBars[_contractSymbol];
    }
}
def on_data(self, slice: Slice) -> None:
    # Obtain the mapped QuoteBar of the symbol if any
    quote_bar = slice.quote_bars.get(self._contract_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)
{
    // Iterate all received Symbol-QuoteBar key-value pairs
    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:
    # Iterate all received Symbol-QuoteBar key-value pairs
    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.

QuoteBar objects have the following properties:

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 contract. A quote tick is an offer to buy or sell the contract at a specific price.

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

public override void OnData(Slice slice)
{
    if (slice.Ticks.ContainsKey(_contractSymbol))
    {
        var ticks = slice.Ticks[_contractSymbol];
        foreach (var tick in ticks)
        {
            var price = tick.Price;
        }
    }
}
def on_data(self, slice: Slice) -> None:
    ticks = slice.ticks.get(self._contract_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.

Tick objects have the following properties:

Futures Chains

FuturesChain objects represent an entire chain of contracts for a single underlying Future. To get the FuturesChain, index the FuturesChainsfutures_chains property of the Slice with the continuous contract Symbol.

public override void OnData(Slice slice)
{
    // Try to get the FutureChain using the canonical symbol
    if (slice.FuturesChains.TryGetValue(_contractSymbol.Canonical, out var chain))
    {
        // Get all contracts if the FutureChain contains any member
        var contracts = chain.Contracts;
    }
}
def on_data(self, slice: Slice) -> None:
    # Try to get the FutureChain using the canonical symbol (None if no FutureChain return)
    chain = slice.futures_chains.get(self._contract_symbol.canonical)
    if chain:
        # Get all contracts if the FutureChain contains any member
        contracts = chain.contracts

You can also loop through the FuturesChainsfutures_chains property to get each FuturesChain.

public override void OnData(Slice slice)
{
    // Iterate all received Canonical Symbol-FutureChain key-value pairs
    foreach (var kvp in slice.FuturesChains)
    {
        var continuousContractSymbol = kvp.Key;
        var chain = kvp.Value;
        var contracts = chain.Contracts;
    }
}

// Using this overload will only handle any FutureChains object received
public void OnData(FuturesChains futuresChains)
{
    // Iterate all received Canonical Symbol-FutureChain key-value pairs
    foreach (var kvp in futuresChains)
    {
        var continuousContractSymbol = kvp.Key;
        var chain = kvp.Value;
        var contracts = chain.Contracts;
    }
}
def on_data(self, slice: Slice) -> None:
    # Iterate all received Canonical Symbol-FutureChain key-value pairs
    for continuous_contract_symbol, chain in slice.futures_chains.items():
        contracts = chain.contracts

FuturesChain objects have the following properties:

Futures Contracts

FuturesContract objects represent the data of a single Futures contract in the market. To get the Futures contracts in the Slice, use the Contractscontracts property of the FuturesChain.

public override void OnData(Slice slice)
{
    // Try to get the FutureChain using the canonical symbol
    if (slice.FuturesChains.TryGetValue(_contractSymbol.Canonical, out var chain))
    {
        // Get individual contract data
        if (chain.Contracts.TryGetValue(_contractSymbol, out var contract))
        {
            var price = contract.LastPrice;
        }
    }
}

// // Using this overload will only handle any FutureChains object received
public void OnData(FuturesChains futuresChains)
{
    // Try to get the FutureChain using the canonical symbol
    if (futuresChains.TryGetValue(_contractSymbol.Canonical, out var chain))
    {
        // Get individual contract data
        if (chain.Contracts.TryGetValue(_contractSymbol, out var contract))
        {
            var price = contract.LastPrice;
        }
    }
}
def on_data(self, slice: Slice) -> None:
    # Try to get the FutureChain using the canonical symbol
    chain = slice.future_chains.get(self._contract_symbol.canonical)
    if chain:
        # Get individual contract data (None if not contained)
        contract = chain.contracts.get(self._contract_symbol)
        if contract:
            price = contract.last_price

Open interest is the number of outstanding contracts that haven't been settled. It provides a measure of investor interest and the market liquidity, so it's a popular metric to use for contract selection. Open interest is calculated once per day. To get the latest open interest value, use the OpenInterestopen_interest property of the Future or FutureContractfuture_contract.

public override void OnData(Slice slice)
{
    // Try to get the FuturesChains using the canonical symbol
    if (slice.FuturesChains.TryGetValue(_contractSymbol.Canonical, out var chain))
    {
        // Get individual contract data
        if (chain.Contracts.TryGetValue(_contractSymbol, out var contract))
        {
            // Get the open interest of the selected contracts
            var openInterest = contract.OpenInterest;
        }
    }
}

def on_data(self, slice: Slice) -> None:
    # Try to get the futures_chains using the canonical symbol
    chain = slice.futures_chains.get(self._contract_symbol.canonical)
    if chain:
        # Get individual contract data
        contract = chain.contracts.get(self._contract_symbol)
        if contract:
            # Get the open interest of the selected contracts
            open_interest = contract.open_interest

FuturesContract objects have the following properties:

Symbol Changes

When the continuous contract rolls over, LEAN passes a SymbolChangedEvent to your OnDataon_data method, which contains the old contract Symbol and the new contract Symbol. To get the SymbolChangedEvent, use the SymbolChangedEventssymbol_changed_events property of the Slice. You can use the SymbolChangedEvent to roll over contracts.

public override void OnData(Slice slice)
{
    foreach (var (symbol, changedEvent) in slice.SymbolChangedEvents)
    {
        var oldSymbol = changedEvent.OldSymbol;
        var newSymbol = changedEvent.NewSymbol;
        var tag = $"Rollover - Symbol changed at {Time}: {oldSymbol} -> {newSymbol}";
        var quantity = Portfolio[oldSymbol].Quantity;
        // Rolling over: to liquidate any position of the old mapped contract and switch to the newly mapped contract
        Liquidate(oldSymbol, tag: tag);
        if (quantity != 0) MarketOrder(newSymbol, quantity, tag: tag);
        Log(tag);
    }
}
def on_data(self, slice: Slice) -> None:
    for symbol, changed_event in  slice.symbol_changed_events.items():
        old_symbol = changed_event.old_symbol
        new_symbol = changed_event.new_symbol
        tag = f"Rollover - Symbol changed at {self.time}: {old_symbol} -> {new_symbol}"
        quantity = self.portfolio[old_symbol].quantity

        # Rolling over: to liquidate any position of the old mapped contract and switch to the newly mapped contract
        self.liquidate(old_symbol, tag=tag)
        if quantity: self.market_order(new_symbol, quantity, tag=tag)
        self.log(tag)

In backtesting, the SymbolChangedEvent occurs at midnight Eastern Time (ET). In live trading, the live data for continuous contract mapping arrives at 6/7 AM ET, so that's when it occurs.

SymbolChangedEvent objects have the following properties:

Examples

The following examples demonstrate some common practices for handling Futures data.

Example 1: Rollovers

Spot Future is referred to as the continuous Future contract, which is usually mapped by the front month contract or the contract with the most open interest. When a contract expires or is very close to expiring, traders usually rollover from the current contract to the next contract to avoid price settlement and remain invested. The following algorithm demonstrates rolling over with limit orders.

public class FutureExampleAlgorithm : QCAlgorithm
{
    private Future _future;

    public override void Initialize()
    {
        // Add the E-mini Futures and set the continuous contract mapping criteria for the rollovers.
        _future = AddFuture(
            Futures.Indices.SP500EMini,
            extendedMarketHours: true,
            dataMappingMode: DataMappingMode.OpenInterest,
            dataNormalizationMode: DataNormalizationMode.BackwardsRatio,
            contractDepthOffset: 0
        );
        _future.SetFilter(0, 182);
    }
    
    public override void OnData(Slice slice)
    {
        // Place the initial order so you can start rolling over contracts later.
        if (!Portfolio.Invested && Transactions.GetOpenOrders().Count == 0)
        {
            // Buy the contract that's currently selected in the continous contract series.
            MarketOrder(_future.Mapped, 1m);
        }
    }
    
    // Track rollover events.
    public override void OnSymbolChangedEvents(SymbolChangedEvents symbolChangedEvents)
    {
        foreach (var (symbol, changedEvent) in symbolChangedEvents)
        {
            var oldSymbol = changedEvent.OldSymbol;
            var newSymbol = changedEvent.NewSymbol;
            // The quantity to roll over should be consistent.
            var quantity = Portfolio[oldSymbol].Quantity;
            // Rolling over: Liquidate the old mapped contract and switch to the newly mapped contract.
            var tag = $"Rollover: {oldSymbol} -> {newSymbol}";
            Liquidate(oldSymbol, tag: tag);
            if (quantity != 0)
            {
                // Place a limit order to avoid extreme quote filling.
                var newContract = Securities[newSymbol];
                LimitOrder(
                    newSymbol, quantity, 
                    // To avoid warnings, round the target limit price to a price that respects 
                    // the minimum price variation for the Future.
                    GetLimitPrice(newContract, newContract.Price), 
                    tag: tag
                );
            }
        }
    }

    private decimal GetLimitPrice(Security security, decimal targetLimitPrice, bool roundUp = true)
    {
        var parameters = new GetMinimumPriceVariationParameters(security, targetLimitPrice);
        var pip = security.PriceVariationModel.GetMinimumPriceVariation(parameters);
        return ((int)(targetLimitPrice / pip) + (roundUp ? 1 : 0)) * pip;
    }
}
class FutureExampleAlgorithm(QCAlgorithm):

    def initialize(self):
        # Add the E-mini Futures and set the continuous contract mapping criteria for the rollovers.
        self._future = self.add_future(
            Futures.Indices.SP_500_E_MINI,
            extended_market_hours=True,
            data_mapping_mode=DataMappingMode.OPEN_INTEREST,
            data_normalization_mode=DataNormalizationMode.BACKWARDS_RATIO,
            contract_depth_offset=0
        )
        self._future.set_filter(0, 182)
    
    def on_data(self, data):
        # Place the initial order so you can start rolling over contracts later.
        if not self.portfolio.invested and not self.transactions.get_open_orders():
            # Buy the contract that's currently selected in the continous contract series.
            self.market_order(self._future.mapped, 1)
    
    # Track rollover events.
    def on_symbol_changed_events(self, symbol_changed_events):
        for symbol, changed_event in symbol_changed_events.items():
            old_symbol = changed_event.old_symbol
            new_symbol = changed_event.new_symbol
            # The quantity to roll over should be consistent.
            quantity = self.portfolio[old_symbol].quantity
            # Rolling over: Liquidate the old mapped contract and switch to the newly mapped contract.
            tag = f"Rollover: {old_symbol} -> {new_symbol}"
            self.liquidate(old_symbol, tag=tag)
            if quantity:
                # Place a limit order to avoid extreme quote filling.
                new_contract = self.securities[new_symbol]
                self.limit_order(
                    new_symbol, quantity, 
                    # To avoid warnings, round the target limit price to a price that respects 
                    # the minimum price variation for the Future.
                    self._get_limit_price(new_contract, new_contract.price), 
                    tag=tag
                )
    
    def _get_limit_price(self, security, target_limit_price, round_up=True):
        parameters = GetMinimumPriceVariationParameters(security, target_limit_price)
        pip = security.price_variation_model.get_minimum_price_variation(parameters)
        return (int(target_limit_price / pip) + int(round_up)) * pip

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: