For some reason this lesson https://www.quantconnect.com/learning/task/158/Preparing-Indicators-with-History does not make real backtest, in fact there is no results.
I've tried to copy the code from the ‘solution' - the same, no results
In video there are immediately results on backtest. What can be the reason?
Full code:
# region imports
from AlgorithmImports import *
# endregion
from AlgorithmImports import *
class EMAMomentumUniverse(QCAlgorithm):
def Initialize(self):
self.set_start_date(2019, 1, 7)
self.set_end_date(2019, 4, 1)
self.set_cash(100000)
self.universe_settings.resolution = Resolution.DAILY
self.add_universe(self.coarse_selection_function)
self.averages = { }
def coarse_selection_function(self, universe):
selected = []
universe = sorted(universe, key=lambda c: c.dollar_volume, reverse=True)
universe = [c for c in universe if c.price > 10][:100]
for coarse in universe:
symbol = coarse.symbol
if symbol not in self.averages:
# 1. Call history to get an array of 200 days of history data
history = self.history(symbol, 200, Resolution.DAILY)
#2. Adjust SelectionData to pass in the history result
self.averages[symbol] = SelectionData(history)
self.averages[symbol].update(self.time, coarse.adjusted_price)
if self.averages[symbol].is_ready() and self.averages[symbol].fast > self.averages[symbol].slow:
selected.append(symbol)
return selected[:10]
def on_securities_changed(self, changes):
for security in changes.removed_securities:
self.liquidate(security.symbol)
for security in changes.added_securities:
self.set_holdings(security.symbol, 0.10)
class SelectionData():
#3. Update the constructor to accept a history array
def __init__(self, history):
self.slow = ExponentialMovingAverage(200)
self.fast = ExponentialMovingAverage(50)
#4. Loop over the history data and update the indicators
for bar in history.itertuples():
self.update(bar.Index[1], bar.close)
def is_ready(self):
return self.slow.is_ready and self.fast.is_ready
def update(self, time, price):
self.fast.update(time, price)
self.slow.update(time, price)
Mia Alissi
Here is the corrected version of the provided content:
Here are some steps to troubleshoot and potentially resolve the issue with the backtest not producing results:
Correct Code Formatting: Ensure the code follows PEP 8 standards. Here is the corrected code snippet:
Check Universe Selection: Ensure that the
coarse_selection_function
is correctly filtering and selecting symbols. Make sure the universe is not empty after filtering.History Data Retrieval: Confirm that the
history
method is correctly fetching historical data for the symbols. Check if the data is being retrieved and processed correctly in theSelectionData
class.Indicator Initialization: Ensure that the
SelectionData
class is correctly initializing and updating the Exponential Moving Averages with the historical data.Debugging: Add debug statements to print out key variables and states, such as the number of symbols selected, the state of the indicators, and any errors or warnings during execution.
Review Documentation: Refer to the QuantConnect documentation for the
history
method and universe selection to ensure correct usage and implementation. Here are some relevant examples:History Requests: You can request historical data using the
history
method. For example:Universe Selection: Ensure that the universe selection function is correctly implemented and returning a list of symbols.
On Securities Changed: The
on_securities_changed
method should handle changes in the universe correctly by liquidating removed securities and setting holdings for added securities.By following these steps, you should be able to identify and resolve the issue preventing the backtest from producing results.
Sources: Momentum And State Of Market Filters, Momentum Effect In Stocks, Ema Cross Universe Selection Algorithm, Constituents Universe Regression Algorithm, Futures Fast Trend Following, with Trend Strength
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.
Serhii
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!