Back

How to access History?

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?

Update Backtest







0

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.


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.

0

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.


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)

 

0

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.


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. :-)

0

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.


Thanks for the exampleAlexandre Catarino - this is helpful for me as well.

0

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.

0

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)

 

0

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

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?

0

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.

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed