I'm trying to make a universe that filters stocks based on proximity to an EMA, but when I try to update the indicator it says that the number is too big or too small for a Decimal. I've fixed all the other bugs I've had up to this point on my own, but I have absolutely no clue where to start with this one. This is the code with most of the parts unrelated to the problem stripped out. Any help would be great.
from UniverseTest import EMAProximity
class UglyGreenLeopard(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 12, 9)
self.SetCash(100000)
self.CustomUniverse = EMAProximity(self, 200)
self.AddUniverse(self.CustomUniverse.diffSelectCoarse)
self.AddAlpha(NullAlphaModel())
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.SetExecution(ImmediateExecutionModel())
def OnData(self, data):
pass
class EMAProximity():
def __init__(self, algorithm, EMALength,):
self.algorithm = algorithm
#length of the slow EMA
self.EMALength = EMALength
def diffSelectCoarse(self, coarse):
#This function takes the coarse data and creates an EMA for each stock
#it then checks to se if the close is within the specified pecentage range of the EMA
for c in coarse:
EMA = ExponentialMovingAverage(200, Resolution.Daily)
history = self.algorithm.History(c.Symbol, self.EMALength, Resolution.Daily)
for x in history.itertuples():
EMA.Update(x.Index[1], x.close)
return Universe.Unchanged
Varad Kabade
Hi Simon Koeman,
We were unable to reproduce the error. Please share a backtest Highlighting the error. Please refer to the attached backtest on our attempt at reproducing the error.
We recommend using the following code snippet in your algorithm to resolve the issue.
Â
It may happen that there is not enough past data.
Best,
Varad Kabade
Simon Koeman
I found the problem. The error was caused by putting the Resolution.Daily into the EMA constructor. Still have no idea why that caused the problem it did, but my code works now. Thanks for the backtest it really helped! Also I don't seem to be able to link backtests with errors for some reason, so if you want to reproduce the error you'll have to copy and paste the code from my post.
Varad Kabade
Hi Simon Koeman,
We cannot attach backtests with runtime errors. In this case, we can comment part of the code that produces the runtime error, run the algorithm, attach the backest, and point to the lines that need to be uncommented to reproduce the error.
Best,
Varad Kabade
Simon Koeman
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!