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

serhii_3_1732699465.jpg

 

In video there are immediately results on backtest. What can be the reason?

 

Full code:

  1. # region imports
  2. from AlgorithmImports import *
  3. # endregion
  4. from AlgorithmImports import *
  5. class EMAMomentumUniverse(QCAlgorithm):
  6. def Initialize(self):
  7. self.set_start_date(2019, 1, 7)
  8. self.set_end_date(2019, 4, 1)
  9. self.set_cash(100000)
  10. self.universe_settings.resolution = Resolution.DAILY
  11. self.add_universe(self.coarse_selection_function)
  12. self.averages = { }
  13. def coarse_selection_function(self, universe):
  14. selected = []
  15. universe = sorted(universe, key=lambda c: c.dollar_volume, reverse=True)
  16. universe = [c for c in universe if c.price > 10][:100]
  17. for coarse in universe:
  18. symbol = coarse.symbol
  19. if symbol not in self.averages:
  20. # 1. Call history to get an array of 200 days of history data
  21. history = self.history(symbol, 200, Resolution.DAILY)
  22. #2. Adjust SelectionData to pass in the history result
  23. self.averages[symbol] = SelectionData(history)
  24. self.averages[symbol].update(self.time, coarse.adjusted_price)
  25. if self.averages[symbol].is_ready() and self.averages[symbol].fast > self.averages[symbol].slow:
  26. selected.append(symbol)
  27. return selected[:10]
  28. def on_securities_changed(self, changes):
  29. for security in changes.removed_securities:
  30. self.liquidate(security.symbol)
  31. for security in changes.added_securities:
  32. self.set_holdings(security.symbol, 0.10)
  33. class SelectionData():
  34. #3. Update the constructor to accept a history array
  35. def __init__(self, history):
  36. self.slow = ExponentialMovingAverage(200)
  37. self.fast = ExponentialMovingAverage(50)
  38. #4. Loop over the history data and update the indicators
  39. for bar in history.itertuples():
  40. self.update(bar.Index[1], bar.close)
  41. def is_ready(self):
  42. return self.slow.is_ready and self.fast.is_ready
  43. def update(self, time, price):
  44. self.fast.update(time, price)
  45. self.slow.update(time, price)
+ Expand

Author

Serhii

November 2024