Futures
Requesting Data
Introduction
Request Futures data in your algorithm to receive a feed of contract prices in the OnData
method. For more information about the specific dataset we use for backtests, see the US Futures dataset listing. To trade Futures live, you can use our Futures data feed or one of the brokerage data feeds.
Create Subscriptions
Before you can subscribe to a Futures contract, you must get the contract Symbol
.
Get Contract Symbols
To get Futures contract Symbol
objects, call the CreateFuture
method or use the FutureChainProvider
. If you use the CreateFuture
method, you need to know the specific contract details.
_contractSymbol = QuantConnect.Symbol.CreateFuture(Futures.Indices.SP500EMini, Market.CME, new DateTime(2022, 6, 17));
self.contract_symbol = Symbol.CreateFuture(Futures.Indices.SP500EMini, Market.CME, datetime(2022,6,17))
If you use the FutureChainProvider
, you need to create the continuous contract Symbol
of the Future. The GetFutureContractList
method of FutureChainProvider
returns a list of Symbol
objects that reference the available Futures contracts for a given underlying Future on a given date.
var continuousFutureSymbol = QuantConnect.Symbol.Create(Futures.Indices.SP500EMini, SecurityType.Future, Market.CME); var contractSymbols = FutureChainProvider.GetFutureContractList(continuousFutureSymbol, Time); _contractSymbol = contractSymbols.OrderByDescending(symbol => symbol.ID.Date).Last();
continuous_future_symbol = Symbol.Create(Futures.Indices.SP500EMini, SecurityType.Future, Market.CME) contract_symbols = self.FutureChainProvider.GetFutureContractList(continuous_future_symbol, self.Time) self.contract_symbol = sorted(contract_symbols, key=lambda symbol: symbol.ID.Date)[0]
Subscribe to Contracts
To create a Futures contract subscription, pass the contract Symbol
to the AddFutureContract
method. Save a reference to the contract Symbol
so you can easily access the contract in the FuturesChain that LEAN passes to the OnData
method.
AddFutureContract(_contractSymbol);
self.AddFutureContract(self.contract_symbol)
The AddFutureContract
method creates a subscription for a single Future contract and adds it to your user-defined universe. To create a dynamic universe of Futures contracts, add a Futures universe or a Futures Universe Selection model.
Warm Up Contract Prices
If you subscribe to a Futures contract with AddFutureContract
, you'll need to wait until the next Slice
to receive data and trade the contract. To trade the contract in the same time step you subscribe to the contract, set the current price of the contract in a security initializer.
SetSecurityInitializer(new BrokerageModelSecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrices)));
self.SetSecurityInitializer(BrokerageModelSecurityInitializer(self.BrokerageModel, FuncSecuritySeeder(self.GetLastKnownPrices)));
Supported Assets
To view the supported assets in the US Futures dataset, see Supported Assets.
Resolutions
The following table shows the available resolutions and data formats for Futures subscriptions:
Resolution | TradeBar | QuoteBar | Trade Tick | Quote Tick |
---|---|---|---|---|
Tick | ![]() | ![]() | ||
Second | ![]() | ![]() | ||
Minute | ![]() | ![]() | ||
Hour | ![]() | ![]() | ||
Daily | ![]() | ![]() |
The default resolution for Futures contract subscriptions is Resolution.Minute
. To change the resolution, pass a resolution
argument to the AddFutureContract
method.
AddFutureContract(_contractSymbol, Resolution.Daily);
self.AddFutureContract(self.contract_symbol, Resolution.Daily)
To create custom resolution periods, see Consolidating Data.
Fill Forward
Fill forward means if there is no data point for the current slice, LEAN uses the previous data point. Fill forward is the default data setting. If you disable fill forward, you may get stale fills or you may see trade volume as zero.
To disable fill forward for a security, set the fillForward
argument to false when you create the security subscription.
AddFutureContract(_contractSymbol, fillForward: false);
self.AddFutureContract(self.contract_symbol, fillForward=False)
Margin and Leverage
LEAN models buying power and margin calls to ensure your algorithm stays within the margin requirements. Futures are already leveraged products, so you can't change their leverage with the default margin model.
Extended Market Hours
By default, your security subscriptions only cover regular trading hours. To subscribe to pre and post-market trading hours for a specific asset, enable the extendedMarketHours
argument when you create the security subscription.
AddFutureContract(_contractSymbol, extendedMarketHours: true);
self.AddFutureContract(self.contract_symbol, extendedMarketHours=True)
You only receive extended market hours data if you create the subscription with minute, second, or tick resolution. If you create the subscription with daily or hourly resolution, the bars only reflect the regular trading hours.
To view the schedule of regular and extended market hours, see Market Hours.
In general, we model most Futures market hours with the following segments:
Market Segment | Time |
---|---|
Pre-market | 00:00:00 to 09:30:00 |
Market | 09:30:00 to 17:00:00 |
Post-market | 18:00:00 to 00:00:00 |
We model it this way because some Futures, like VIX, have pre- and post-market hours, so we standardized it. With this segmentation, if you set a Scheduled Events for the market open, it's set for 9:30 AM instead of midnight.
Continuous Contracts
A continuous Futures contract represents a single contract that maps to other contracts over time as the rollover rules are met. For more information about continuous Futures contracts, see Continuous Contracts.
Data Normalization
The data normalization mode doesn't affect the data that LEAN passes to OnData
or the data from history request for Futures contracts. If you change the data normalization mode, it won't change the outcome.
The following data normalization modes are available for continuous Futures contracts: