Futures
Requesting Data
Introduction
Request Futures data in your algorithm to receive a feed of contract prices in the OnData
on_data
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 the QuantConnect data provider or one of the brokerage data providers.
Create Subscriptions
Before you can subscribe to a Futures contract, you must get the contract Symbol
symbol
.
Get Contract Symbols
To get Futures contract Symbol
objects, call the CreateFuture
create_future
method or use the FutureChainProvider
future_chain_provider
. If you use the CreateFuture
create_future
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.create_future(Futures.Indices.SP500E_MINI, Market.CME, datetime(2022,6,17))
If you use the FutureChainProvider
future_chain_provider
, you need to create the continuous contract Symbol
of the Future. The GetFutureContractList
get_future_contract_list
method of FutureChainProvider
future_chain_provider
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.SP500E_MINI, SecurityType.FUTURE, Market.CME) contract_symbols = self.future_chain_provider.get_future_contract_list(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
symbol
to the AddFutureContract
add_future_contract
method. Save a reference to the contract Symbol
symbol
so you can easily access the contract in the Futures Chains that LEAN passes to the OnData
on_data
method.
AddFutureContract(_contractSymbol);
self.add_future_contract(self._contract_symbol)
The AddFutureContract
add_future_contract
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
add_future_contract
, 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.
var seeder = new FuncSecuritySeeder(GetLastKnownPrices); SetSecurityInitializer(new BrokerageModelSecurityInitializer(BrokerageModel, seeder));
seeder = FuncSecuritySeeder(self.get_last_known_prices) self.set_security_initializer(BrokerageModelSecurityInitializer(self.brokerage_model, seeder))
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 TICK | ||||
Second SECOND | ||||
Minute MINUTE | ||||
Hour HOUR | ||||
Daily DAILY |
The default resolution for Futures contract subscriptions is Resolution.Minute
Resolution.MINUTE
. To change the resolution, pass a resolution
argument to the AddFutureContract
add_future_contract
method.
AddFutureContract(_contractSymbol, Resolution.Daily);
self.add_future_contract(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
fill_forward
argument to false when you create the security subscription.
AddFutureContract(_contractSymbol, fillForward: false);
self.add_future_contract(self._contract_symbol, fill_forward=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
extended_market_hours
argument when you create the security subscription.
AddFutureContract(_contractSymbol, extendedMarketHours: true);
self.add_future_contract(self._contract_symbol, extended_market_hours=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
on_data
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: