QuantConnect
US Futures Security Master
Introduction
The US Futures Security Master dataset by QuantConnect provides mapping reference data for the most liquid contracts of the CME Group exchanges, calculated with popular rolling techniques. The data covers 162 root Future contracts, starts in May 2009, and is delivered on a daily frequency with a zip file with all the contract mappings. This dataset is created by daily processing of the US historical Future chains.
This dataset, paired with the US Futures dataset, supports the following rolling techniques: forward panama canal, backwards panama canal, and backwards ratio. You can set the specific rolling date to occur on the last trading day, first day month, or the day when the contract with the greatest open interest changes.
VIX Futures don't support continous contract rolling with open interest.
This is not the underlying Futures data (US Futures dataset), which you need to purchase separately with a license from AlgoSeek. This security master dataset is required to purchase the US Futures or US Future Options datasets.
For more information about the US Futures Security Master dataset, including CLI commands and pricing, see the dataset listing.
About the Provider
QuantConnect was founded in 2012 to serve quants everywhere with the best possible algorithmic trading technology. Seeking to disrupt a notoriously closed-source industry, QuantConnect takes a radically open-source approach to algorithmic trading. Through the QuantConnect web platform, more than 50,000 quants are served every month.
Getting Started
You don't need any special code to utilize the US Futures Security Master. It automatically loads when you request US Futures data.
Key Concept
The US Futures Security Master lets you to construct continuous Futures, allowing the access of normalized historical data of the underlying assets, as well as trading the “lead” Future contracts for those assets.
Continuous Futures refer to sets of rolling lead Future contracts during their actively trading periods. Since Future contracts expire at their maturities, to analyze the historical price of a Future and to apply technical indicators, you need the continuous Future price series.
To access the continuous Future, use the Future's Symbol
symbol
property.
# Set the data normalization mode and data mapping mode to select the contract as the continuous contract with price adjustment for smooth price series future = self.add_future(Futures.Energy.CRUDE_OIL_WTI, data_normalization_mode=DataNormalizationMode.BACKWARDS_RATIO, data_mapping_mode=DataMappingMode.OPEN_INTEREST, contract_depth_offset=0) self.continuous_contract_symbol = future.symbol
// Set the data normalization mode and data mapping mode to select the contract as the continuous contract with price adjustment for smooth price series var future = AddFuture(Futures.Energy.CrudeOilWTI, dataNormalizationMode: DataNormalizationMode.BackwardsRatio, dataMappingMode: DataMappingMode.OpenInterest, contractDepthOffset: 0 ); _continuousFutureSymbol = future.Symbol;
The dataNormalizationMode
and dataMappingMode
arguments makes the transition of the underlying contracts seemless. However, the Future Symbol
doesn't map to an underlying Future contract. It works fine to trade within backtests, but could be subjected to friction costs during live trading since the order price could be a normalized price. For more information about this topic, see the Live Trading Considerations
section.
Data Normalization Modes
The data normalization mode defines how the price series of two contracts are stitched together when the contract rollovers occur. The DataNormalizatoinMode
enumeration has the following members available for continuous contracts:
If you use a data normalization mode that's not in the preceding list, LEAN automatically converts it to DataNormalizationMode.BackwardsRatio
.
Data Mapping Modes
The data mapping mode defines when contract rollovers occur. The DataMappingMode
enumeration has the following members:
VIX Futures don't support continous contract rolling with DataMappingMode.OpenInterest
DataMappingMode.OPEN_INTEREST
and DataMappingMode.OpenInterestAnnuak
DataMappingMode.OPEN_INTEREST_ANNUAL
.
Tracking Contract Changes
As the contracts roll over, the Mapped
mapped
property of the Future
object references the next contract in the series and you receive a SymbolChangedEvent
. To get the current Symbol
change events, index the SymbolChangedEvents
symbol_changed_events
property of the current Slice
with the continuous Futures Symbol
. Slice objects deliver unique events to your algorithm as they happen, but the Slice
may not contain data for your Future at every time step. To avoid issues, check if the Slice
contains the data you want before you index it.
def on_data(self, slice: Slice) -> None: # Get Symbol Change Event of the Continuous Future (change in mapped contract) changed_event = slice.symbol_changed_events.get(self.continuous_future_symbol) if changed_event: old_symbol = changed_event.old_symbol new_symbol = self.add_future_contract(changed_event.new_symbol).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 != 0: self.market_order(new_symbol, quantity, tag = tag) self.log(tag)
public override void OnData(Slice slice) { // Get Symbol Change Event of the Continuous Future (change in mapped contract) if (slice.SymbolChangedEvents.TryGetValue(_continuousFutureSymbol, out var changedEvent)) { var oldSymbol = changedEvent.OldSymbol; var newSymbol = AddFutureContract(changedEvent.NewSymbol).Symbol; 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); } }
SymbolChangedEvent
objects have the following attributes:
Live Trading Considerations
You can trade continuous Futures, but the continuous Future Symbol
doesn't map to a single underlying Future contract. Instead, it represents a set of rolling contracts. Thus, the prices could be frictional during a contract rollover, which could be catastrophic in live trading! For live trading, you should place your orders directly on the underlying contracts. To get the current underlying contract in the continuous Future series, use the Mapped
mapped
property.
# Get the mapped symbol current_contract = self.continuous_contract.mapped self.buy(current_contract, 1)
// Get the mapped symbol var currentContract = _continuousContract.Mapped; Buy(currentContract, 1);
Data Format
If you download the files in the US Futures Security Master, you get a factor file and a map file for each of the exchanges with supported continuous Futures. To view the files, see the \data\future\<exchange_name> directory under your LEAN CLI base directory.
For the factor file, it is a .zip
collection of REST API styled .csv
files for each Future Symbol
, including the date, scaling factors for each type of data normalization and the data mapping mode that indicates the Symbol changing event is on that day for that mapping mode. The following line is an example line in the .csv
file:
{"Date":"2009-10-31T00:00:00","BackwardsRatioScale":[0.9914163090128755364806866953,1.0,1.0],"BackwardsPanamaCanalScale":[-2.0,0.0,0.0],"ForwardPanamaCanalScale":[0.0,0.0,0.0],"DataMappingMode":1}
For the map file, it is a .zip
collection of .csv
files for each Future Symbol
, including the date, new underlying contract Symbol, the exchange code, and the data mapping mode that indicates the Symbol changing event is on that day for that mapping mode. The following line is an example line in the .csv
file:
20091130,aw uii3j0m6zbj9,CBOT,1
Historical Data
You can get historical US Future Security Master data in an algorithm and the Research Environment.
Historical Data In Algorithms
To get historical US Future Security Master data in an algorithm, call the History<SymbolChangedEvent>
history
method with continuous Future Symbol
and a lookback period. This method returns the old and new contract of each rollover. If there is no data for the period you requested, the history result is empty.
# Add the Future and define its rollover settings. future = self.add_future( Futures.Indices.SP_500_E_MINI, data_mapping_mode=DataMappingMode.OPEN_INTEREST, data_normalization_mode=DataNormalizationMode.BACKWARDS_RATIO, contract_depth_offset=0 ) # DataFrame example where the columns are the SymbolChangedEvent attributes: history_df = self.history(SymbolChangedEvent, future.symbol, timedelta(365)) # SymbolChangedEvent objects example: history = self.history[SymbolChangedEvent](future.symbol, timedelta(365))
// Add the Future and define its rollover settings. var future = AddFuture( Futures.Indices.SP500EMini, dataMappingMode: DataMappingMode.OpenInterest, dataNormalizationMode: DataNormalizationMode.BackwardsRatio, contractDepthOffset: 0 ); // Get historical data. var history = History<SymbolChangedEvent>(future.Symbol, TimeSpan.FromDays(365));
For more information about historical US Future Security Master data in algorithms, see Symbol Changes.
Historical Data In Research
To get historical US Future Security Master data in the Research Environment, call the History<SymbolChangedEvent>
history
method with continuous Future Symbol
and a time period. This method returns the old and new contract of each rollover. If there is no data for the period you requested, the history result is empty.
# Add the Future and define its rollover settings. future = qb.add_future( Futures.Indices.SP_500_E_MINI, data_mapping_mode=DataMappingMode.OPEN_INTEREST, data_normalization_mode=DataNormalizationMode.BACKWARDS_RATIO, contract_depth_offset=0 ) # DataFrame example where the columns are the SymbolChangedEvent attributes: history_df = qb.history(SymbolChangedEvent, future.symbol, datetime(2023, 1, 1), datetime(2025, 1, 1)) # SymbolChangedEvent objects example: history = qb.history[SymbolChangedEvent](future.symbol, datetime(2023, 1, 1), datetime(2025, 1, 1))
// Add the Future and define its rollover settings. var future = qb.AddFuture( Futures.Indices.SP500EMini, dataMappingMode: DataMappingMode.OpenInterest, dataNormalizationMode: DataNormalizationMode.BackwardsRatio, contractDepthOffset: 0 ); // Get historical data. var history = qb.History<SymbolChangedEvent>(future.Symbol, new DateTime(2023, 1, 1), new DateTime(2025, 1, 1));
For more information about historical US Future Security Master data in the Research Environment, see Symbol Changes History.
Supported Assets
The following list shows the available (162) Futures:
Futures.Currencies.AUD
: Australian Dollar Futures (CME: 6A)Futures.Currencies.AUDCAD
: Australian Dollar/Canadian Dollar Futures (CME: ACD)Futures.Currencies.AUDJPY
: Australian Dollar/Japanese Yen Futures (CME: AJY)Futures.Currencies.AUDNZD
: Australian Dollar/New Zealand Dollar Futures (CME: ANE)Futures.Currencies.BRL
: Brazillian Real Futures (CME: 6L)Futures.Currencies.BTC
: Bitcoin Futures (CME: BTC)Futures.Currencies.CAD
: Canadian Dollar Futures (CME: 6C)Futures.Currencies.CADJPY
: Canadian Dollar/Japanese Yen Futures (CME: CJY)Futures.Currencies.CHF
: Swiss Franc Futures (CME: 6S)Futures.Currencies.ETH
: Ether Futures (CME: ETH)Futures.Currencies.EUR
: Euro FX Futures (CME: 6E)Futures.Currencies.EURAUD
: Euro/Australian Dollar Futures (CME: EAD)Futures.Currencies.EURCAD
: Euro/Canadian Dollar Futures (CME: ECD)Futures.Currencies.EuroFXEmini
Futures.Currencies.EURO_FX_EMINI
: E-mini Euro FX Futures (CME: E7)Futures.Currencies.EURSEK
: Euro/Swedish Krona Futures (CME: ESK)Futures.Currencies.GBP
: British Pound Futures (CME: 6B)Futures.Currencies.JapaneseYenEmini
Futures.Currencies.JAPANESE_YEN_EMINI
: E-mini Japanese Yen Futures (CME: J7)Futures.Currencies.JPY
: Japanese Yen Futures (CME: 6J)Futures.Currencies.MicroAUD
Futures.Currencies.MICRO_AUD
: Micro AUD/USD Futures (CME: M6A)Futures.Currencies.MicroBTC
Futures.Currencies.MICRO_BTC
: Micro Bitcoin Futures (CME: MBT)Futures.Currencies.MicroCAD
Futures.Currencies.MICRO_CAD
: Micro USD/CAD Futures (CME: M6C)Futures.Currencies.MicroCADUSD
Futures.Currencies.MICRO_CADUSD
: Micro CAD/USD Futures (CME: MCD)Futures.Currencies.MicroCHF
Futures.Currencies.MICRO_CHF
: Micro CHF/USD Futures (CME: MSF)Futures.Currencies.MicroEther
Futures.Currencies.MICRO_ETHER
: Micro Ether Futures (CME: MET)Futures.Currencies.MicroEUR
Futures.Currencies.MICRO_EUR
: Micro EUR/USD Futures (CME: M6E)Futures.Currencies.MicroGBP
Futures.Currencies.MICRO_GBP
: Micro GBP/USD Futures (CME: M6B)Futures.Currencies.MicroINRUSD
Futures.Currencies.MICRO_INRUSD
: Micro INR/USD Futures (CME: MIR)Futures.Currencies.MicroJPY
Futures.Currencies.MICRO_JPY
: Micro JPY/USD Futures (CME: MJY)Futures.Currencies.MicroUSDCHF
Futures.Currencies.MICRO_USDCHF
: Micro USD/CHF Futures (CME: M6S)Futures.Currencies.MicroUSDCNH
Futures.Currencies.MICRO_USDCNH
: Micro USD/CNH Futures (CME: MNH)Futures.Currencies.MicroUSDJPY
Futures.Currencies.MICRO_USDJPY
: Micro USD/JPY Futures (CME: M6J)Futures.Currencies.MXN
: Mexican Peso Futures (CME: 6M)Futures.Currencies.NZD
: New Zealand Dollar Futures (CME: 6N)Futures.Currencies.RUB
: Russian Ruble Futures (CME: 6R)Futures.Currencies.StandardSizeUSDOffshoreRMBCNH
Futures.Currencies.STANDARD_SIZE_USD_OFFSHORE_RMBCNH
: Standard-Size USD/Offshore RMB (CNH) Futures (CME: CNH)Futures.Currencies.ZAR
: South African Rand Futures (CME: 6Z)Futures.Dairy.CashSettledButter
Futures.Dairy.CASH_SETTLED_BUTTER
: Cash-settled Butter Futures (CME: CB)Futures.Dairy.CashSettledCheese
Futures.Dairy.CASH_SETTLED_CHEESE
: Cash-settled Cheese Futures (CME: CSC)Futures.Dairy.ClassIIIMilk
Futures.Dairy.CLASS_III_MILK
: Class III Milk Futures (CME: DC)Futures.Dairy.ClassIVMilk
Futures.Dairy.CLASS_IV_MILK
: Class IV Milk Futures (CME: GDK)Futures.Dairy.DryWhey
Futures.Dairy.DRY_WHEY
: Dry Whey Futures (CME: DY)Futures.Dairy.NonfatDryMilk
Futures.Dairy.NONFAT_DRY_MILK
: Non-fat Dry Milk Futures (CME: GNF)Futures.Energy.ArgusLLSvsWTIArgusTradeMonth
Futures.Energy.ARGUS_LL_SVS_WTI_ARGUS_TRADE_MONTH
: Argus LLS vs. WTI (Argus) Trade Month Futures (NYMEX: AE5)Futures.Energy.ArgusPropaneFarEastIndex
Futures.Energy.ARGUS_PROPANE_FAR_EAST_INDEX
: Argus Propane Far East Index Futures (NYMEX: A7E)Futures.Energy.ArgusPropaneSaudiAramco
Futures.Energy.ARGUS_PROPANE_SAUDI_ARAMCO
: Argus Propane (Saudi Aramco) Futures (NYMEX: A9N)Futures.Energy.BrentCrudeOilVsDubaiCrudeOilPlatts
Futures.Energy.BRENT_CRUDE_OIL_VS_DUBAI_CRUDE_OIL_PLATTS
: Brent Crude Oil vs. Dubai Crude Oil (Platts) Futures (NYMEX: ADB)Futures.Energy.BrentLastDayFinancial
Futures.Energy.BRENT_LAST_DAY_FINANCIAL
: Brent Last Day Financial Futures (NYMEX: BZ)Futures.Energy.ChicagoEthanolPlatts
Futures.Energy.CHICAGO_ETHANOL_PLATTS
: Chicago Ethaanol (Platts) Futures (NYMEX: CU)Futures.Energy.ClearbrookBakkenSweetCrudeOilMonthlyIndexNetEnergy
Futures.Energy.CLEARBROOK_BAKKEN_SWEET_CRUDE_OIL_MONTHLY_INDEX_NET_ENERGY
: Clearbrook Bakken Sweet Crude Oil Monthly Index (Net Energy) Futures (NYMEX: CSW)Futures.Energy.ConwayPropaneOPIS
Futures.Energy.CONWAY_PROPANE_OPIS
: Conway Propane (OPIS) Futures (NYMEX: A8K)Futures.Energy.CrudeOilWTI
Futures.Energy.CRUDE_OIL_WTI
: Crude Oil WTI Futures (NYMEX: CL)Futures.Energy.DubaiCrudeOilPlattsFinancial
Futures.Energy.DUBAI_CRUDE_OIL_PLATTS_FINANCIAL
: Dubai Crude Oil (Platts) Financial Futures (NYMEX: DCB)Futures.Energy.EastWestGasolineSpreadPlattsArgus
Futures.Energy.EAST_WEST_GASOLINE_SPREAD_PLATTS_ARGUS
: East-West Gasoline Spread (Platts-Argus) Futures (NYMEX: EWG)Futures.Energy.EastWestNaphthaJapanCFvsCargoesCIFNWESpreadPlatts
Futures.Energy.EAST_WEST_NAPHTHA_JAPAN_C_FVS_CARGOES_CIFNWE_SPREAD_PLATTS
: East-West Naphtha: Japan C&F vs. Cargoes CIF NWE Spread (Platts) Futures (NYMEX: EWN)Futures.Energy.Ethanol
Futures.Energy.ETHANOL
: Ethanol Futures (CBOT: EH)Futures.Energy.EthanolT2FOBRdamIncludingDutyPlatts
Futures.Energy.ETHANOL_T_2_FOB_RDAM_INCLUDING_DUTY_PLATTS
: Ethanol T2 FOB Rdam Including Duty (Platts) Futures (NYMEX: AZ1)Futures.Energy.EuropeanNaphthaPlattsCrackSpread
Futures.Energy.EUROPEAN_NAPHTHA_PLATTS_CRACK_SPREAD
: European Naphtha (Platts) Crack Spread Futures (NYMEX: EN)Futures.Energy.EuropeanPropaneCIFARAArgus
Futures.Energy.EUROPEAN_PROPANE_CIFARA_ARGUS
: European Propane CIF ARA (Argus) Futures (NYMEX: APS)Futures.Energy.EuropeanPropaneCIFARAArgusVsNaphthaCargoesCIFNWEPlatts
Futures.Energy.EUROPEAN_PROPANE_CIFARA_ARGUS_VS_NAPHTHA_CARGOES_CIFNWE_PLATTS
: European Propane CIF ARA (Argus) vs. Naphtha Cargoes CIF NWE (Platts) Futures (NYMEX: EPN)Futures.Energy.FreightRouteTC14Baltic
Futures.Energy.FREIGHT_ROUTE_TC_14_BALTIC
: Freight Route TC14 (Baltic) Futures (NYMEX: FRC)Futures.Energy.Gasoline
Futures.Energy.GASOLINE
: Gasoline RBOB Futures (NYMEX: RB)Futures.Energy.GasolineEurobobOxyNWEBargesArgus
Futures.Energy.GASOLINE_EUROBOB_OXY_NWE_BARGES_ARGUS
: Gasoline Euro-bob Oxy NWE Barges (Argus) Futures (NYMEX: B7H)Futures.Energy.GroupThreeSuboctaneGasolinePlattsVsRBOB
Futures.Energy.GROUP_THREE_SUBOCTANE_GASOLINE_PLATTS_VS_RBOB
: Group Three Sub-octane Gasoliine (Platts) vs. RBOB Futures (NYMEX: AA8)Futures.Energy.GroupThreeULSDPlattsVsNYHarborULSD
Futures.Energy.GROUP_THREE_ULSD_PLATTS_VS_NY_HARBOR_ULSD
: Group Three ULSD (Platts) vs. NY Harbor ULSD Futures (NYMEX: AA6)Futures.Energy.GulfCoastCBOBGasolineA2PlattsVsRBOBGasoline
Futures.Energy.GULF_COAST_CBOB_GASOLINE_A_2_PLATTS_VS_RBOB_GASOLINE
: Gulf Coast CBOB Gasoline A2 (Platts) vs. RBOB Gasoline Futures (NYMEX: CRB)Futures.Energy.GulfCoastHSFOPlattsVsEuropeanThreePointFivePercentFuelOilBargesFOBRdamPlatts
Futures.Energy.GULF_COAST_HSFO_PLATTS_VS_EUROPEAN_THREE_POINT_FIVE_PERCENT_FUEL_OIL_BARGES_FOB_RDAM_PLATTS
: Gulf Coast HSFO (Platts) vs. European 3.5% Fuel Oil Barges FOB Rdam (Platts) Futures (NYMEX: GCU)Futures.Energy.HeatingOil
Futures.Energy.HEATING_OIL
: Heating Oil Futures (NYMEX: HO)Futures.Energy.LosAngelesCARBOBGasolineOPISvsRBOBGasoline
Futures.Energy.LOS_ANGELES_CARBOB_GASOLINE_OPI_SVS_RBOB_GASOLINE
: Los Angeles CARBOB Gasoline (OPIS) vs. RBOB Gasoline Futures (NYMEX: AJL)Futures.Energy.LosAngelesCARBDieselOPISvsNYHarborULSD
Futures.Energy.LOS_ANGELES_CARB_DIESEL_OPI_SVS_NY_HARBOR_ULSD
: Los Angeles CARB Diesel (OPIS) vs. NY Harbor ULSD Futures (NYMEX: AKL)Futures.Energy.LosAngelesJetOPISvsNYHarborULSD
Futures.Energy.LOS_ANGELES_JET_OPI_SVS_NY_HARBOR_ULSD
: Los Angeles Jet (OPIS) vs. NY Harbor ULSD Futures (NYMEX: AJS)Futures.Energy.MarsArgusVsWTIFinancial
Futures.Energy.MARS_ARGUS_VS_WTI_FINANCIAL
: Mars (Argus) vs. WTI Financial Futures (NYMEX: AYX)Futures.Energy.MarsArgusVsWTITradeMonth
Futures.Energy.MARS_ARGUS_VS_WTI_TRADE_MONTH
: Mars (Argus) vs. WTI Trade Month Futures (NYMEX: AYV)Futures.Energy.MicroCrudeOilWTI
Futures.Energy.MICRO_CRUDE_OIL_WTI
: Micro WTI Crude Oil Futures (NYMEX: MCL)Futures.Energy.MicroEuropeanFOBRdamMarineFuelZeroPointFivePercentBargesPlatts
Futures.Energy.MICRO_EUROPEAN_FOB_RDAM_MARINE_FUEL_ZERO_POINT_FIVE_PERCENT_BARGES_PLATTS
: Micro European FOB Rdam Marine Fuel 0.5% Barges (Platts) Futures (NYMEX: R5O)Futures.Energy.MicroEuropeanThreePointFivePercentOilBargesFOBRdamPlatts
Futures.Energy.MICRO_EUROPEAN_THREE_POINT_FIVE_PERCENT_OIL_BARGES_FOB_RDAM_PLATTS
: Micro European 3.5% Fuel Oil Barges FOB Rdam (Platts) Futures (NYMEX: MEF)Futures.Energy.MicroGasoilZeroPointOnePercentBargesFOBARAPlatts
Futures.Energy.MICRO_GASOIL_ZERO_POINT_ONE_PERCENT_BARGES_FOBARA_PLATTS
: Micro Gasoil 0.1% Barges FOB ARA (Platts) Futures (NYMEX: M1B)Futures.Energy.MicroSingaporeFOBMarineFuelZeroPointFivePercetPlatts
Futures.Energy.MICRO_SINGAPORE_FOB_MARINE_FUEL_ZERO_POINT_FIVE_PERCET_PLATTS
: Micro Singapore FOB Marine Fuel 0.5% (Platts) Futures (NYMEX: S5O)Futures.Energy.MicroSingaporeFuelOil380CSTPlatts
Futures.Energy.MICRO_SINGAPORE_FUEL_OIL_380_CST_PLATTS
: Micro Singapore Fuel Oil 380CST (Platts) Futures (NYMEX: MAF)Futures.Energy.MiniEuropeanThreePointPercentFiveFuelOilBargesPlatts
Futures.Energy.MINI_EUROPEAN_THREE_POINT_PERCENT_FIVE_FUEL_OIL_BARGES_PLATTS
: Mini European 3.5% Fuel Oil Barges FOB Rdam (Platts) Futures (NYMEX: A0D)Futures.Energy.MiniSingaporeFuelOil180CstPlatts
Futures.Energy.MINI_SINGAPORE_FUEL_OIL_180_CST_PLATTS
: Mini Singapore Fuel Oil 180 cst (Platts) Futures (NYMEX: A0F)Futures.Energy.MontBelvieuEthaneOPIS
Futures.Energy.MONT_BELVIEU_ETHANE_OPIS
: Mont Belvieu Ethane (OPIS) Futures (NYMEX: AC0)Futures.Energy.MontBelvieuLDHPropaneOPIS
Futures.Energy.MONT_BELVIEU_LDH_PROPANE_OPIS
: Mont Belvieu LDH Propane (OPIS) Futures (NYMEX: B0)Futures.Energy.MontBelvieuNaturalGasolineOPIS
Futures.Energy.MONT_BELVIEU_NATURAL_GASOLINE_OPIS
: Mont Belvieu Natural Gasoline (OPIS) Futures (NYMEX: A7Q)Futures.Energy.MontBelvieuNormalButaneOPIS
Futures.Energy.MONT_BELVIEU_NORMAL_BUTANE_OPIS
: Mont Belvieu Normal Butane (OPIS) Futures (NYMEX: AD0)Futures.Energy.NaturalGas
Futures.Energy.NATURAL_GAS
: Natural Gas Futures (NYMEX: NG)Futures.Energy.NaturalGasHenryHubLastDayFinancial
Futures.Energy.NATURAL_GAS_HENRY_HUB_LAST_DAY_FINANCIAL
: Natural Gas (Henry Hub) Last-day Financial Futures (NYMEX: HH)Futures.Energy.NaturalGasHenryHubPenultimateFinancial
Futures.Energy.NATURAL_GAS_HENRY_HUB_PENULTIMATE_FINANCIAL
: Natural Gas (Henry Hub) Penultimate Financial Futures (NYMEX: HP)Futures.Energy.OnePercentFuelOilCargoesFOBNWEPlattsVsThreePointFivePercentFuelOilBargesFOBRdamPlatts
Futures.Energy.ONE_PERCENT_FUEL_OIL_CARGOES_FOBNWE_PLATTS_VS_THREE_POINT_FIVE_PERCENT_FUEL_OIL_BARGES_FOB_RDAM_PLATTS
: 1% Fuel Oil Cargoes FOB NWE (Platts) vs. 3.5% Fuel Oil Barges FOB Rdam (Platts) Futures (NYMEX: FSS)Futures.Energy.PremiumUnleadedGasoline10ppmFOBMEDPlatts
Futures.Energy.PREMIUM_UNLEADED_GASOLINE_10_PPM_FOBMED_PLATTS
: Premium Unleaded Gasoline 10 ppm FOB MED (Platts) Futures (NYMEX: A3G)Futures.Energy.PropaneNonLDHMontBelvieuOPIS
Futures.Energy.PROPANE_NON_LDH_MONT_BELVIEU_OPIS
: Propane Non-LDH Mont Belvieu (OPIS) Futures (NYMEX: A1R)Futures.Energy.RBOBGasolineCrackSpread
Futures.Energy.RBOB_GASOLINE_CRACK_SPREAD
: RBOB Gasoline Crack Spread Futures (NYMEX: ARE)Futures.Energy.RBOBGasolineVsEurobobOxyNWEBargesArgusThreeHundredFiftyThousandGallons
Futures.Energy.RBOB_GASOLINE_VS_EUROBOB_OXY_NWE_BARGES_ARGUS_THREE_HUNDRED_FIFTY_THOUSAND_GALLONS
: RBOB Gasoline vs. Euro-bob Oxy NWE Barges (Argus) (350,000 gallons) Futures (NYMEX: EXR)Futures.Energy.SingaporeFuelOil380cstPlattsVsEuropeanThreePointFivePercentFuelOilBargesFOBRdamPlatts
Futures.Energy.SINGAPORE_FUEL_OIL_380_CST_PLATTS_VS_EUROPEAN_THREE_POINT_FIVE_PERCENT_FUEL_OIL_BARGES_FOB_RDAM_PLATTS
: Singapore Fuel Oil 380 cst (Platts) vs. European 3.5% Fuel Oil Barges FOB Rdam (Platts) Futures (NYMEX: EVC)Futures.Energy.SingaporeGasoilPlattsVsLowSulphurGasoilFutures
Futures.Energy.SINGAPORE_GASOIL_PLATTS_VS_LOW_SULPHUR_GASOIL_FUTURES
: Singapore Gasoil (Platts) vs. Low Sulphur Gasoil Futures (NYMEX: AGA)Futures.Energy.SingaporeMogas92UnleadedPlattsBrentCrackSpread
Futures.Energy.SINGAPORE_MOGAS_92_UNLEADED_PLATTS_BRENT_CRACK_SPREAD
: Singapore Mogas 92 Unleaded (Platts) Brent Crack Spread Futures (NYMEX: D1N)Futures.Energy.ThreePointFivePercentFuelOilBargesFOBRdamPlattsCrackSpread
Futures.Energy.THREE_POINT_FIVE_PERCENT_FUEL_OIL_BARGES_FOB_RDAM_PLATTS_CRACK_SPREAD
: 3.5% Fuel Oil Barges FOB Rdam (Platts) Crack Spread Futures (NYMEX: FO)Futures.Energy.ThreePointFivePercentFuelOilBargesFOBRdamPlattsCrackSpread1000mt
Futures.Energy.THREE_POINT_FIVE_PERCENT_FUEL_OIL_BARGES_FOB_RDAM_PLATTS_CRACK_SPREAD_1000_MT
: 3.5% Fuel Oil Barges FOB Rdam (Platts) Crack Spread (1000mt) Futures (NYMEX: BOO)Futures.Energy.WTIBrentFinancial
Futures.Energy.WTI_BRENT_FINANCIAL
: WTI-Brent Financial Futures (NYMEX: BK)Futures.Energy.WTIFinancial
Futures.Energy.WTI_FINANCIAL
: WTI Financial Futures (NYMEX: CSX)Futures.Energy.WTIHoustonArgusVsWTITradeMonth
Futures.Energy.WTI_HOUSTON_ARGUS_VS_WTI_TRADE_MONTH
: WTI Houston (Argus) vs. WTI Trade Month Futures (NYMEX: HTT)Futures.Energy.WTIHoustonCrudeOil
Futures.Energy.WTI_HOUSTON_CRUDE_OIL
: WTI Houston Crude Oil Futures (NYMEX: HCL)Futures.Financials.EuroDollar
Futures.Financials.EURO_DOLLAR
: EuroDollar Futures (CME: GE)Futures.Financials.FiveYearUSDMACSwap
Futures.Financials.FIVE_YEAR_USDMAC_SWAP
: 5-Year USD MAC Swap Futures (CBOT: F1U)Futures.Financials.MicroY10TreasuryNote
Futures.Financials.MICRO_Y_10_TREASURY_NOTE
: Micro 10-Year Yield Futures (CBOT: 10Y)Futures.Financials.MicroY2TreasuryBond
Futures.Financials.MICRO_Y_2_TREASURY_BOND
: Micro 2-Year Yield Futures (CBOT: 2YY)Futures.Financials.MicroY30TreasuryBond
Futures.Financials.MICRO_Y_30_TREASURY_BOND
: Micro 30-Year Yield Futures (CBOT: 30Y)Futures.Financials.MicroY5TreasuryBond
Futures.Financials.MICRO_Y_5_TREASURY_BOND
: Micro 5-Year Yield Futures (CBOT: 5YY)Futures.Financials.UltraTenYearUSTreasuryNote
Futures.Financials.ULTRA_TEN_YEAR_US_TREASURY_NOTE
: Ultra 10-Year U.S. Treasury Note Futures (CBOT: TN)Futures.Financials.UltraUSTreasuryBond
Futures.Financials.ULTRA_US_TREASURY_BOND
: Ultra U.S. Treasury Bond Futures (CBOT: UB)Futures.Financials.Y10TreasuryNote
Futures.Financials.Y_10_TREASURY_NOTE
: 10Y U.S. Treasury Note Futures (CBOT: ZN)Futures.Financials.Y2TreasuryNote
Futures.Financials.Y_2_TREASURY_NOTE
: 2Y U.S. Treasury Note Futures (CBOT: ZT)Futures.Financials.Y30TreasuryBond
Futures.Financials.Y_30_TREASURY_BOND
: 30Y U.S. Treasury Bond Futures (CBOT: ZB)Futures.Financials.Y5TreasuryNote
Futures.Financials.Y_5_TREASURY_NOTE
: 5Y U.S. Treasury Note Futures (CBOT: ZF)Futures.Forestry.Lumber
Futures.Forestry.LUMBER
: Lumber Futures (CME: LBR)Futures.Forestry.RandomLengthLumber
Futures.Forestry.RANDOM_LENGTH_LUMBER
: Random Length Lumber Futures (CME: LBS)Futures.Grains.BlackSeaCornFinanciallySettledPlatts
Futures.Grains.BLACK_SEA_CORN_FINANCIALLY_SETTLED_PLATTS
: Black Sea Corn Financially Settled (Platts) Futures (CBOT: BCF)Futures.Grains.BlackSeaWheatFinanciallySettledPlatts
Futures.Grains.BLACK_SEA_WHEAT_FINANCIALLY_SETTLED_PLATTS
: Black Sea Wheat Financially Settled (Platts) Futures (CBOT: BWF)Futures.Grains.Corn
Futures.Grains.CORN
: Corn Futures (CBOT: ZC)Futures.Grains.HRWWheat
Futures.Grains.HRW_WHEAT
: KC HRW Wheat Futures (CBOT: KE)Futures.Grains.Oats
Futures.Grains.OATS
: Oats Futures (CBOT: ZO)Futures.Grains.Soybeans
Futures.Grains.SOYBEANS
: Soybeans Futures (CBOT: ZS)Futures.Grains.SoybeanMeal
Futures.Grains.SOYBEAN_MEAL
: Soybean Meal Futures (CBOT: ZM)Futures.Grains.SoybeanOil
Futures.Grains.SOYBEAN_OIL
: Soybean Oil Futures (CBOT: ZL)Futures.Grains.Wheat
Futures.Grains.WHEAT
: Default wheat contract is SRWWheat (CBOT: ZW)Futures.Indices.BloombergCommodityIndex
Futures.Indices.BLOOMBERG_COMMODITY_INDEX
: Bloomberg Commodity Index Futures (CBOT: AW)Futures.Indices.Dow30EMini
Futures.Indices.DOW_30_E_MINI
: E-mini Dow Indu 30 Futures (CBOT: YM)Futures.Indices.DowJonesRealEstate
Futures.Indices.DOW_JONES_REAL_ESTATE
: (CME: RX)Futures.Indices.FTSEEmergingEmini
Futures.Indices.FTSE_EMERGING_EMINI
: E-mini FTSE Emerging Index Futures (CME: EI)Futures.Indices.MicroDow30EMini
Futures.Indices.MICRO_DOW_30_E_MINI
: Micro E-mini Dow Jones Industrial Average Index Futures (CBOT: MYM)Futures.Indices.MicroNASDAQ100EMini
Futures.Indices.MICRO_NASDAQ_100_E_MINI
: Micro E-mini Nasdaq-100 Index Futures (CME: MNQ)Futures.Indices.MicroRussell2000EMini
Futures.Indices.MICRO_RUSSELL_2000_E_MINI
: Micro E-mini Russell 2000 Index Futures (CME: M2K)Futures.Indices.MicroSP500EMini
Futures.Indices.MICRO_SP_500_E_MINI
: Micro E-mini S&P 500 Index Futures (CME: MES)Futures.Indices.NASDAQ100BiotechnologyEMini
Futures.Indices.NASDAQ_100_BIOTECHNOLOGY_E_MINI
: E-mini Nasdaq-100 Biotechnology Index Futures (CME: BIO)Futures.Indices.NASDAQ100EMini
Futures.Indices.NASDAQ_100_E_MINI
: E-mini NASDAQ 100 Futures (CME: NQ)Futures.Indices.Nikkei225Dollar
Futures.Indices.NIKKEI_225_DOLLAR
: Nikkei-225 Dollar Futures (CME: NKD)Futures.Indices.Nikkei225YenCME
Futures.Indices.NIKKEI_225_YEN_CME
: Nikkei-225 Yen denominated Futures on CME (CME: NIY)Futures.Indices.Russell1000EMini
Futures.Indices.RUSSELL_1000_E_MINI
: (CME: RS1)Futures.Indices.Russell2000EMini
Futures.Indices.RUSSELL_2000_E_MINI
: E-mini Russell 2000 Futures (CME: RTY)Futures.Indices.SPGSCICommodity
Futures.Indices.SPGSCI_COMMODITY
: S&P-GSCI Commodity Index Futures (CME: GD)Futures.Indices.SP400MidCapEmini
Futures.Indices.SP_400_MID_CAP_EMINI
: E-mini S&P MidCap 400 Futures (CME: EMD)Futures.Indices.SP500AnnualDividendIndex
Futures.Indices.SP_500_ANNUAL_DIVIDEND_INDEX
: (CME: SDA)Futures.Indices.SP500EMini
Futures.Indices.SP_500_E_MINI
: E-mini S&P 500 Futures (CME: ES)Futures.Indices.TOPIXYEN
: (CME: TPY)Futures.Indices.USDDenominatedIbovespa
Futures.Indices.USD_DENOMINATED_IBOVESPA
: USD-Denominated Ibovespa Index Futures (CME: IBV)Futures.Indices.VIX
: CBOE Volatility Index Futures (CFE: VX)Futures.Meats.FeederCattle
Futures.Meats.FEEDER_CATTLE
: Feeder Cattle Futures (CME: GF)Futures.Meats.LeanHogs
Futures.Meats.LEAN_HOGS
: Lean Hogs Futures (CME: HE)Futures.Meats.LiveCattle
Futures.Meats.LIVE_CATTLE
: Live Cattle Futures (CME: LE)Futures.Metals.AluminiumEuropeanPremiumDutyPaidMetalBulletin
Futures.Metals.ALUMINIUM_EUROPEAN_PREMIUM_DUTY_PAID_METAL_BULLETIN
: Aluminium European Premium Duty-Paid (Metal Bulletin) Futures (COMEX: EDP)Futures.Metals.AluminumMWUSTransactionPremiumPlatts25MT
Futures.Metals.ALUMINUM_MWUS_TRANSACTION_PREMIUM_PLATTS_25_MT
: Aluminum MW U.S. Transaction Premium Platts (25MT) Futures (COMEX: AUP)Futures.Metals.Copper
Futures.Metals.COPPER
: Copper Futures (COMEX: HG)Futures.Metals.Gold
Futures.Metals.GOLD
: Gold Futures (COMEX: GC)Futures.Metals.MicroGold
Futures.Metals.MICRO_GOLD
: Micro Gold Futures (COMEX: MGC)Futures.Metals.MicroGoldTAS
Futures.Metals.MICRO_GOLD_TAS
: Micro Gold TAS Futures (COMEX: MGT)Futures.Metals.MicroPalladium
Futures.Metals.MICRO_PALLADIUM
: Micro Palladium Futures (NYMEX: PAM)Futures.Metals.MicroSilver
Futures.Metals.MICRO_SILVER
: Micro Silver Futures (COMEX: SIL)Futures.Metals.Palladium
Futures.Metals.PALLADIUM
: Palladium Futures (NYMEX: PA)Futures.Metals.Platinum
Futures.Metals.PLATINUM
: Platinum Futures (NYMEX: PL)Futures.Metals.Silver
Futures.Metals.SILVER
: Silver Futures (COMEX: SI)Futures.Metals.USMidwestDomesticHotRolledCoilSteelCRUIndex
Futures.Metals.US_MIDWEST_DOMESTIC_HOT_ROLLED_COIL_STEEL_CRU_INDEX
: U.S. Midwest Domestic Hot-Rolled Coil Steel (CRU) Index Futures (NYMEX: HRC)Futures.Softs.Sugar11
Futures.Softs.SUGAR_11
: Sugar #11 Futures ICE (ICE: SB)Futures.Softs.Sugar11CME
Futures.Softs.SUGAR_11_CME
: Sugar #11 Futures CME (NYMEX: YO)
Example Applications
The US Futures Security Master enables you to design strategies harnessing continuous Futures contracts. Examples include the following strategies:
- Trading cyclical patterns in commodity Futures.
- Buying gold Futures as an inflation hedge with automatic contract roll-overs.
- Detecting arbitrage opportunities between index Futures and Equities.
Classic Algorithm Example
The following example algorithm buys the continuous Future contract for Crude Oil when its price rises above its simple moving average. When its price drops below its simple moving average, the algorithm sells the continuous contract. Each time the continuous Future contract rolls over, the algorithm logs the event.
from AlgorithmImports import * from QuantConnect.DataSource import * class USFuturesSecurityMasterDataClassicAlgorithm (QCAlgorithm): # 1% margin to reassure trend direction threshold = 0.01 def initialize(self) -> None: self.set_cash(1000000) self.set_start_date(2019, 2, 1) self.set_end_date(2021, 6, 1) # Setting the continuous contract mapping criteria, the contract with highest open interest provide the best price information for trend estimation self.continuous_contract = self.add_future(Futures.Energies.CRUDE_OIL_WTI, data_normalization_mode = DataNormalizationMode.BACKWARDS_RATIO, data_mapping_mode = DataMappingMode.OPEN_INTEREST, contract_depth_offset = 0) self.continuous_contract_symbol = self.continuous_contract.symbol # Historical data history = self.history(self.continuous_contract_symbol, 500, Resolution.MINUTE) self.debug(f"We got {len(history)} items from our history request") # Set up SMA indicator for trend direction estimator self.sma = self.SMA(self.continuous_contract_symbol, 10, Resolution.DAILY) # Warm up the SMA indicator for its readiness for immediate use if not history.empty: for time, row in history.droplevel(0).loc[self.continuous_contract_symbol].iterrows(): self.sma.update(IndicatorDataPoint(time, row.close)) def on_data(self, slice: Slice) -> None: # Up-to-date handling of switching the mapped contract for trade liquidity for symbol, changed_event in slice.symbol_changed_events.items(): old_symbol = changed_event.old_symbol # Newly mapped contract might not have subscription data for trading yet, so we request its data new_symbol = self.add_future_contract(changed_event.new_symbol).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 != 0: self.market_order(new_symbol, quantity, tag = tag) self.log(tag) mapped_symbol = self.continuous_contract.mapped # Make sure trade decisions are based on newly received data if not (slice.bars.contains_key(self.continuous_contract_symbol) and self.sma.is_ready and mapped_symbol): return # Buy if trend up by threshold to follow the trend if slice.bars[self.continuous_contract_symbol].price > self.sma.current.value * (1+self.threshold) and not self.portfolio[mapped_symbol].is_long: self.market_order(mapped_symbol, 1) # Sell if trend down by threshold to follow the trend elif slice.bars[self.continuous_contract_symbol].price < self.sma.current.value * (1-self.threshold) and not self.portfolio[mapped_symbol].is_short: self.market_order(mapped_symbol, -1)
public class USFuturesSecurityMasterDataClassicAlgorithm : QCAlgorithm { private Future _continuousContract; private Symbol _symbol; // 1% margin to reassure trend direction private decimal _threshold = 0.01m; private SimpleMovingAverage _sma; public override void Initialize() { SetCash(1000000); SetStartDate(2019, 2, 1); SetEndDate(2021, 6, 1); // Setting the continuous contract mapping criteria, the contract with highest open interest provide the best price information for trend estimation _continuousContract = AddFuture(Futures.Energies.CrudeOilWTI, dataNormalizationMode: DataNormalizationMode.BackwardsRatio, dataMappingMode: DataMappingMode.OpenInterest, contractDepthOffset: 0 ); _symbol = _continuousContract.Symbol; // Historical data var history = History(_symbol, 60*24*10, Resolution.Minute); Debug($"We got {history.Count()} items from our history request"); // Set up SMA indicator for trend direction estimator _sma = SMA(_symbol, 10, Resolution.Daily); // Warm up the SMA indicator for its readiness for immediate use foreach (var bar in history) { _sma.Update(new IndicatorDataPoint(bar.Time, bar.Close)); } } public override void OnData(Slice slice) { // Up-to-date handling of switching the mapped contract for trade liquidity foreach (var (symbol, changedEvent) in slice.SymbolChangedEvents) { var oldSymbol = changedEvent.OldSymbol; // Newly mapped contract might not have subscription data for trading yet, so we request its data var newSymbol = AddFutureContract(changedEvent.NewSymbol).Symbol; 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); } var mappedSymbol = _continuousContract.Mapped; // Make sure trade decisions are based on newly received data if (!(slice.Bars.ContainsKey(_symbol) && _sma.IsReady && mappedSymbol != None)) { return; } // Buy if trend up by threshold to follow the trend if (slice.Bars[_symbol].Price > _sma.Current.Value * (1+_threshold) && !Portfolio[mappedSymbol].IsLong) { MarketOrder(mappedSymbol, 1); } // Sell if trend down by threshold to follow the trend else if (slice.Bars[_symbol].Price < _sma.Current.Value * (1-_threshold) && !Portfolio[mappedSymbol].IsShort) { MarketOrder(mappedSymbol, -1); } } }
Framework Algorithm Example
The following example algorithm buys the continuous Future contract for Crude Oil when its price rises above its simple moving average. When its price drops below its simple moving average, the algorithm sells the continuous contract. Each time the contract rolls over, the algorithm logs the event.
from AlgorithmImports import * from QuantConnect.DataSource import * class USFuturesSecurityMasterDataFrameworkAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_cash(1000000) self.set_start_date(2019, 2, 1) self.set_end_date(2021, 6, 1) # Setting the continuous contract mapping criteria, the contract with highest open interest provide the best price information for trend estimation self.add_future(Futures.Energies.CRUDE_OIL_WTI, data_normalization_mode = DataNormalizationMode.BACKWARDS_RATIO, data_mapping_mode = DataMappingMode.OPEN_INTEREST, contract_depth_offset = 0) self.add_alpha(ContinuousFuturesAlphaModel()) self.set_portfolio_construction(SingleSharePortfolioConstructionModel()) class ContinuousFuturesAlphaModel(AlphaModel): # 1% margin to reassure trend direction threshold = 0.01 symbol = None continuous_contract = None def update(self, algorithm: QCAlgorithm, slice: Slice) -> List[Insight]: insights = [] # Check if contract data available, since all trades are based on that only if self.continuous_contract_symbol is None or self.continuous_contract is None: return insights # Up-to-date handling of switching the mapped contract for trade liquidity for symbol, changed_event in slice.symbol_changed_events.items(): old_symbol = changed_event.old_symbol if algorithm.insights.has_active_insights(old_symbol, algorithm.utc_time): new_symbol = changed_event.new_symbol tag = f"Rollover - Symbol changed at {algorithm.time}: {old_symbol} -> {new_symbol}" last_insight = sorted(algorithm.insights[old_symbol], key=lambda x: x.close_time_utc)[-1] insights.append(Insight.price(new_symbol, last_insight.close_time_utc, last_insight.direction, tag= tag)) algorithm.insights.clear([old_symbol]) algorithm.log(tag) mapped_symbol = self.continuous_contract.mapped # Make sure trade decisions are based on newly received data if not slice.bars.contains_key(self.continuous_contract_symbol) or not self.sma.is_ready or not mapped_symbol: return insights direction = None # Long if trend up by threshold to follow the trend if slice.bars[self.continuous_contract_symbol].price > self.sma.current.value * (1+self.threshold) and not algorithm.portfolio[mapped_symbol].is_long: direction = InsightDirection.UP # Short if trend down by threshold to follow the trend elif slice.bars[self.continuous_contract_symbol].price < self.sma.current.value * (1-self.threshold) and not algorithm.portfolio[mapped_symbol].is_short: direction = InsightDirection.DOWN if direction: insights.append(Insight.price(mapped_symbol, timedelta(days=14), direction)) return insights def on_securities_changed(self, algorithm: QCAlgorithm, changes: SecurityChanges) -> None: for security in changes.added_securities: symbol = security.symbol # Set up SMA indicator for trend direction estimator, only for the canonical symbol if symbol.is_canonical(): self.continuous_contract = security self.continuous_contract_symbol = symbol self.sma = algorithm.SMA(self.continuous_contract_symbol, 10, Resolution.DAILY) # Historical data history = algorithm.history(symbol, 60*24*10, Resolution.MINUTE) algorithm.debug(f"We got {len(history)} from our history request for {symbol}") if history.empty: continue # Warm up the SMA indicator for its readiness for immediate use for time, row in history.droplevel(0).loc[self.continuous_contract_symbol].iterrows(): self.sma.update(IndicatorDataPoint(time, row.close)) class SingleSharePortfolioConstructionModel(PortfolioConstructionModel): def create_targets(self, algorithm: QCAlgorithm, insights: List[Insight]) -> List[PortfolioTarget]: targets = [] for insight in insights: if algorithm.securities[insight.symbol].is_tradable: targets.append(PortfolioTarget(insight.symbol, insight.direction)) return targets
public class USFuturesSecurityMasterDataFrameworkAlgorithm : QCAlgorithm { public override void Initialize() { SetCash(1000000); SetStartDate(2019, 2, 1); SetEndDate(2021, 6, 1); // Setting the continuous contract mapping criteria, the contract with highest open interest provide the best price information for trend estimation AddFuture(Futures.Energies.CrudeOilWTI, dataNormalizationMode: DataNormalizationMode.BackwardsRatio, dataMappingMode: DataMappingMode.OpenInterest, contractDepthOffset: 0 ); AddAlpha(new ContinuousFuturesAlphaModel()); SetPortfolioConstruction(new SingleSharePortfolioConstructionModel()); } class ContinuousFuturesAlphaModel : AlphaModel { private Future _continuousContract; private Symbol _symbol; // 1% margin to reassure trend direction private decimal _threshold = 0.01m; private SimpleMovingAverage _sma; public override IEnumerable Update(QCAlgorithm algorithm, Slice slice) { var insights = new List(); // Check if contract data available, since all trades are based on that only if (_symbol == None || _continuousContract == None) { return insights; } // Up-to-date handling of switching the mapped contract for trade liquidity foreach (var (symbol, changedEvent) in slice.SymbolChangedEvents) { var oldSymbol = changedEvent.OldSymbol; if (algorithm.Insights.HasActiveInsights(oldSymbol, algorithm.UtcTime)) { var newSymbol = changedEvent.NewSymbol; var tag = $"Rollover - Symbol changed at {algorithm.Time}: {oldSymbol} -> {newSymbol}"; var lastInsight = algorithm.Insights[oldSymbol].OrderBy(x => x.CloseTimeUtc).LastOrDefault(); insights.Add(Insight.Price(newSymbol, lastInsight.CloseTimeUtc, lastInsight.Direction, tag: tag)); algorithm.Insights.Clear(new Symbol[] { oldSymbol }); algorithm.Log(tag); } } var mappedSymbol = _continuousContract.Mapped; // Make sure trade decisions are based on newly received data if (!slice.Bars.ContainsKey(_symbol) || !_sma.IsReady || mappedSymbol == None) { return insights; } // Long if trend up by threshold to follow the trend if (slice.Bars[_symbol].Price > _sma.Current.Value * (1+_threshold) && !algorithm.Portfolio[mappedSymbol].IsLong) { insights.Add(Insight.Price(mappedSymbol, TimeSpan.FromDays(14), InsightDirection.Up)); } // Short if trend down by threshold to follow the trend else if (slice.Bars[_symbol].Price < _sma.Current.Value * (1-_threshold) && !algorithm.Portfolio[mappedSymbol].IsShort) { insights.Add(Insight.Price(mappedSymbol, TimeSpan.FromDays(14), InsightDirection.Down)); } return insights; } public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes) { foreach (var security in changes.AddedSecurities) { var symbol = security.Symbol; // Set up SMA indicator for trend direction estimator, only for the canonical symbol if (symbol.IsCanonical()) { _continuousContract = (Future)security; _symbol = symbol; _sma = algorithm.SMA(_symbol, 10, Resolution.Daily); // Historical data var history = algorithm.History(symbol, 60*24*10, Resolution.Minute); algorithm.Debug($"We got {history.Count()} from our history request for {symbol}"); // Warm up the SMA indicator for its readiness for immediate use foreach (var bar in history) { _sma.Update(new IndicatorDataPoint(bar.Time, bar.Close)); } } } } } class SingleSharePortfolioConstructionModel : PortfolioConstructionModel { public override IEnumerable CreateTargets(QCAlgorithm algorithm, Insight[] insights) { var targets = new List(); foreach (var insight in insights) { if (algorithm.Securities[insight.Symbol].IsTradable) { targets.Add(new PortfolioTarget(insight.Symbol, (int) insight.Direction)); } } return targets; } } }