# region imports
from AlgorithmImports import *
# endregion
from datetime import datetime
import numpy as np
class FuturesBasis(QCAlgorithm):
def Initialize(self):
self.SetStartDate(datetime(2011, 1, 1))
self.SetEndDate(datetime(2022, 5, 1))
self.SetCash(100000)
self.futureSP500 = self.AddFuture(Futures.Indices.SP500EMini,Resolution.Daily)
def OnData(self, data):
self.symbol = self.futureSP500.Symbol
self.current_contract= self.Securities[self.futureSP500.Mapped].Symbol
if not self.Portfolio.Invested:
self.MarketOrder(self.current_contract, 1)
if self.Portfolio.Invested and data.SymbolChangedEvents.ContainsKey(self.symbol):
changed_event = data.SymbolChangedEvents[self.symbol]
old_symbol = changed_event.OldSymbol
new_symbol = changed_event.NewSymbol
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)
self.MarketOrder(new_symbol, quantity)
Backtest Handled Error: ES W8Z0KK3PQJ9D: The security does not have an accurate price as it has not yet received a bar of data. Before placing a trade (or using SetHoldings) warm up your algorithm with SetWarmup, or use slice.Contains(symbol) to confirm the Slice object has price before using the data. Data does not necessarily all arrive at the same time so your algorithm should confirm the data is ready before using it.
The backtest is running,but getting the above error.I tried the above two solution,but still getting the error
Louis Szeto
Hi Jesso
Refer to this template example for Future rolling over.
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.
Jesso joy
HI louis,
Thanks
Jesso joy
hi louis , i found this example too , do this perform rollover too?
Louis Szeto
Hi Jesso
It is the same link I’ve shared with you. Maybe you posted the wrong url?
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.
Jesso joy
sorry i meant this
Louis Szeto
Hi Jesso
Nope, it does not rollover the future holdings. That was an example on how to access the mapped contract only.
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.
Yardley
Louis Szeto how do I access the 890 public algorithms that's listed under your profile? I'm new to QuantConnect and still trying to figure out where everything is.
Nico Xenox
Hey Yardley,
you could take a look at the numerous algos on GITHUB. They posted a lot of different examples. Here's the link
Best,
Nico
Jesso joy
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!