Hello,
I am trying to use data for both WTI and BRENT futures. I am using the following code to iterate between the futures contracts;however, I dont seem a way to correctly get the current symbols for both WTI and BRENT. I need to create two arrays with the closing prices of each asset, but I can only get it for one of them as of now.
self.AddFuture(Futures.Energies.CrudeOilWTI, Resolution.Minute)
self.AddFuture("BZ", Resolution.Minute)
#in OnData
for chain in data.FutureChains:
#retrieving contracts from chain
contracts = [i for i in chain.Value]
if len(contracts) == 0:
continue
sortedByEXContracts = sorted(contracts, key=lambda k : k.Expiry, reverse=True)
self.futurecontracts = sortedByEXContracts[0]
symbol = self.futurecontracts.Symbol
expiry = self.futurecontracts.Expiry
df = self.History([symbol], 10, Resolution.Minute)
df_close = df.loc[(expiry, symbol), "close"]
Shile Wen
Hi Giuliano,
An easier way to get the front month contract is to use future.SetFilter(lambda x: x.FrontMonth().OnlyApplyFilterAtMarketOpen()). Furthermore, we can check the underlying of a futures contract with contract.Symbol.ID.Symbol to know which RollingWindow, a way to store data in a rolling manner, to add the data to. I've shown this in the attached backtest.
Best,
Shile Wen
Giuliano Ciccone
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!