There is a problem with the solution code provided in BootCamp 101 / US Equities Lesson 7 200-50 EMA Momentum Universe task 04/04.
When running a backtest on solution code, the backtest generates may errors in the cloud terminal like this:
Backtest Handled Error: 2019-01-07 00:00:00 AMD R735QTJ8XC9X: 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. In live trading this can mean you do not have an active subscription to the asset class you're trying to trade. If using custom data make sure you've set the 'Value' property.
As result of these errors, the backtest does not make any trades.
For reference, here is a backtest report using the sample code provided in the lesson:
https://www.quantconnect.cloud/backtest/4f72d037f8aae6cdc6a3521fb104f93f/?theme=chrome
I solved this error by setting the market price for each security before calling set_holdings in the on_securities_changed method.
for security in changes.added_securities:
# make sure the secuirty has a price before placing the order
price = self.get_last_known_price(security)
security.set_market_price(price)
self.set_holdings(security.symbol, 0.10)
Here is a backtest report with the revised code that runs with no errors:
https://www.quantconnect.cloud/backtest/ec6b3ac264abffe6c6622abb4a23e974/?theme=chrome
P.S. The BootCamp is helpful to introduce concepts. It would be great if someone could review it and make sure everything works properly. For a new user, its hard to know what is casuing errors, and it takes a lot of time and effort to debug errors. This distracts from learning the concepts.
Christopher Roberts
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!