I get the following error:
Runtime Error: AttributeError : 'SelectionModel' object has no attribute 'History'
The class I am using however inherits from (FundamentalUniverseSelectionModel).
The reason for this is that I am using the Course and Fine Filters. And I am also trying to implement Momentum in the Fine Filter Function along the lines shown here:
Lean/Algorithm.Python/EmaCrossUniverseSelectionAlgorithm.py
However I note that the EmaCrossUniverseSelectionAlgorithm inherits fromĀ (QCAlgorithm) and adds the Course Filter using:
self.AddUniverse(self.CoarseSelectionFunction)
Is this my mistake? Trying to access History in a class which inherits from FundamentalUniverseSelectionModel?
Jared Broad
Without seeing the code I am guessing you're calling self.History(), you need to access the algorithm parameter which is passed into the selection model. i.e. algorithm.History. The self refers to the current class; if you're in the history API is accessible on the algorithm object.
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.
Alexandre Catarino
Hi Anthony FJ GarnerĀ ,
On the 'AttributeError' issue, we can see that it was fixed after you changedĀ self.History for algorithm.History (line 30).
fine is a list of FineFundamental objects where each item has the AdjustedPrice attribute. If you loop through fine orĀ sortedByDEratio, you should be able to access it:
for f in sortedByDEratio: self.averages[f.Symbol].update(f.EndTime, f.AdjustedPrice)
Ā
Alexandre Catarino
Hi Anthony FJ GarnerĀ ,
Sorry about the misinformation.
Please find in the attached backtest a working version of what you are trying to accomplish.
If you have further questions, do not hesitate to ask. :-)
Nathan
Thanks for the exampleAlexandre CatarinoĀ - this is helpful for me as well.
Anthony FJ Garner
Alexandre
I have been going through your kindly provided code and believe I have spotted an error.Ā The following lines are an unnecessary duplication:
Ā
for symbol, mom in averages.items():
c = self.coarse.pop(symbol, None)
mom.Update(c.EndTime, c.AdjustedPrice)
Ā
In fact, by adding an extra and duplicate line to averages, an incorrect momentum is calculated and passed on to the next stage of the Framework.
The dataframe of prices (created from the call to History) for each stock already includes the lates price and is called each time that the Course/Fine function is called.
Incidentally, as regards Momentum, there is no need to calculate a momentum figure for each day included in history - all we are interested is the latest momentum. It saves a lot of preocessing power and time NOT to have to loop through each line of history. While it does no actual harm of course. Nonethless, perhaps the example code should stand "as is" since for some indicators, it is necessary to calculate for each day - a moving average for instance.
Sorry to be long winded but I thought this might be useful for anyone following.
Alexandre Catarino
Thank you, Anthony FJ GarnerĀ .
In fact, more generally, we would need to check the last (current) indicator value to see its EndTime property before we try to update:
# Update with current data for symbol, mom in averages.items(): c = self.coarse.pop(symbol, None) if c.EndTime > mom.Current.EndTime: mom.Update(c.EndTime, c.AdjustedPrice)
Ā
Anthony FJ Garner
Alexandre
Under what circumstances would:
Ā c.EndTimeĀ
bear a later date than the close in:
df = history[symbol].dropna() ?
The close in df is what is used to update the indicator and it seems odd if the last row of df woul bear a different date to that in c.EndTime?
Alexandre Catarino
Hi Anthony FJ GarnerĀ ,
In some applications, we don't need to make historical data requests for all symbols since the difference between the previous request and the current one is just one data point. So, instead of making an expensive operation that includes the data in CoarseFundamental object, we just use it.
In your algorithm particular case, because of the yearly selection, I think it is better to just request all the data.
Anthony FJ Garner
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!