I'm using the continuos futures markets and I have problems with some of them like NG and Euro FX (6E). The continuos future provides me some wrong contracts that not have information and for this reason my backtest doen't work.
self.continuous_futureNG = quant.AddFuture(
Futures.Energies.NaturalGas,
resolution = Resolution.Minute,
extendedMarketHours = True,
dataNormalizationMode = DataNormalizationMode.Raw,
dataMappingMode = DataMappingMode.OpenInterest,
contractDepthOffset = 0
)
self.continuous_futureNG.SetFilter(lambda future_filter_universe: future_filter_universe.StandardsOnly().FrontMonth())
def OnData(self, slice):
# Comprobamos si se produce un cambio de vencimiento
for changedEvent in slice.SymbolChangedEvents.Values:
if changedEvent.Symbol == self.continuous_futureNG.Symbol:
self.Debug(f"Contract rollover from {changedEvent.OldSymbol} to {changedEvent.NewSymbol}")
self.Debug(f"Precio continuo: {self.Securities[self.continuous_futureNG.Symbol].Price}")
self.Debug(f"Precio: {self.Securities[self.ng.continuous_futureNG.Mapped].Price}")
self.contract = self.ng.continuous_futureNG.Mapped
Mia Alissi
From the information provided in the extracts, it seems like you are trying to use continuous futures in your algorithm. Continuous futures in QuantConnect represent a series of separate contracts stitched together to form a continuous price.
The
AddFuture
method returns aFuture
object, which contains the continuous contractSymbol
. Thecontinuous contract Symbol
is the key to access the contracts in the FutureChain that LEAN passes to theOnData
method.When the continuous contract rolls over, LEAN passes a
SymbolChangedEvent
to yourOnData
method, which contains the old contractSymbol
and the new contractSymbol
.In your code, you are correctly checking for the
SymbolChangedEvent
in theOnData
method. However, it's important to note that theSlice
may not contain data for your Future at every time step. To avoid issues, you should check if theSlice
contains the data you want before you index it.Here is an example of how to do this:
If you are still facing issues, it might be due to the specific futures contracts you are trying to access. Some contracts might not have enough data or might not be available in the data set. You might want to check the availability and quality of the data for these contracts.
For more information, you can refer to the QuantConnect documentation on futures and continuous futures.
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.
Victor Vicent Babiloni
That answer doen't resolve my problem
Victor Vicent Babiloni
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!