Hi there,
One thing that is not clear to me is how to access indicators with multiple continuous futures contracts.
I understand how the SMA indicator is being accessed when accessing the history of one continuous futures contract like in this example:
# region imports
from AlgorithmImports import *
# endregion
class USFuturesSecurityMasterDataClassicAlgorithm (QCAlgorithm):
threshold = 0.01 # 1%
def Initialize(self) -> None:
self.SetCash(1000000)
self.SetStartDate(2022, 1, 1)
self.SetEndDate(2022, 6, 1)
# Requesting data
self.continuous_contract = self.AddFuture(Futures.Energies.CrudeOilWTI,
dataNormalizationMode = DataNormalizationMode.BackwardsRatio,
dataMappingMode = DataMappingMode.OpenInterest,
contractDepthOffset = 0)
self.symbol = self.continuous_contract.Symbol
# Historical data
history = self.History(self.symbol, 500, Resolution.Minute)
self.Debug(f"We got {len(history)} items from our history request")
self.sma = self.SMA(self.symbol, 10, Resolution.Daily)
if not history.empty:
for time, row in history.droplevel(0).loc[self.symbol].iterrows():
self.sma.Update(IndicatorDataPoint(time, row.close))
self.Log(history)
def OnData(self, slice: Slice) -> None:
# Accessing data
for changed_event in slice.SymbolChangedEvents.Values:
if changed_event.Symbol == self.symbol:
self.Log(f"SymbolChanged event at {self.Time}: {changed_event}")
mapped_symbol = self.continuous_contract.Mapped
if not (slice.Bars.ContainsKey(self.symbol) and self.sma.IsReady and mapped_symbol):
return
if slice.Bars[self.symbol].Price > self.sma.Current.Value * (1+self.threshold) and not self.Portfolio[mapped_symbol].IsLong:
self.MarketOrder(mapped_symbol, 1)
elif slice.Bars[self.symbol].Price < self.sma.Current.Value * (1-self.threshold) and not self.Portfolio[mapped_symbol].IsShort:
self.MarketOrder(mapped_symbol, -1)
But what if I want to access the SMA indicator for an array of continuous futures whose history is accessed by something like this:
# Requesting data
self.CL = self.AddFuture(Futures.Energies.CrudeOilWTI,
dataNormalizationMode = DataNormalizationMode.BackwardsRatio,
dataMappingMode = DataMappingMode.OpenInterest,
contractDepthOffset = 0)
self.ES = self.AddFuture(Futures.Indices.SP500EMini,
dataNormalizationMode = DataNormalizationMode.BackwardsRatio,
dataMappingMode = DataMappingMode.OpenInterest,
contractDepthOffset = 0)
self.symbols = [
self.CL.Symbol,
self.ES.Symbol
]
# Get historical data
data = self.History(self.symbols, period, Resolution.Daily)
How would I iterate using:
for symbol in self.symbols:
to generate SMA for each symbol so I could determine whether or not to buy/sell that symbol based on comparing price to the SMA?
Thanks in advance,
David
Louis Szeto
Hi David
It is not possible to instantiate an automatic updating indicator with a canonical symbol (e.g. continuous future symbol). Instead, use a manual updating one, save the reference in a dictionary and update it with:
Best
Louis
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.
David Lelong
Thanks, Louis. I'm still having some issues with the indicators, and I don't think I'm feeding them the historical data properly.
Please have a look:
Thanks in advance,
David
Louis Szeto
Hi David
The historical data is not fed to warm up the indicators in your snippets. Refer to this doc to see how to do it.
Best
Louis
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.
David Lelong
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!