I cloned and am trying to run this example code but it throws an error “name Fundamental not defined”. Is there a module I need to import?
from AlgorithmImports import *
class MorningStarDataAlgorithm(QCAlgorithm):
def Initialize(self) -> None:
self.SetStartDate(2021, 1, 1)
self.SetEndDate(2021, 7, 1)
self.SetCash(100000)
# Requesting data
self.AddUniverse(self.FundamentalSelectionFunction)
self.num_coarse_symbols = 100
self.num_fine_symbols = 10
self.UniverseSettings.Resolution = Resolution.Daily
def FundamentalSelectionFunction(self, fundamental: List[Fundamental]) -> List[Symbol]:
selected = [f for f in fundamental if f.HasFundamentalData and f.Price > 1]
sorted_by_dollar_volume = sorted(selected, key=lambda f: f.DollarVolume, reverse=True)[:self.num_coarse_symbols]
sorted_by_pe_ratio = sorted(sorted_by_dollar_volume, key=lambda f: f.ValuationRatios.PERatio, reverse=False)[:self.num_fine_symbols]
return [ f.Symbol for f in sorted_by_pe_ratio ]
def OnData(self, slice: Slice) -> None:
# if we have no changes, do nothing
if self._changes is None: return
# liquidate removed securities
for security in self._changes.RemovedSecurities:
if security.Invested:
self.Liquidate(security.Symbol)
# we want 1/N allocation in each security in our universe
for security in self._changes.AddedSecurities:
self.SetHoldings(security.Symbol, 1 / self.num_fine_symbols)
self._changes = None
def OnSecuritiesChanged(self, changes: SecurityChanges) -> None:
self._changes = changes
for security in changes.AddedSecurities:
# Historical data
history = self.History(security.Symbol, 7, Resolution.Daily)
self.Debug(f"We got {len(history)} from our history request for {security.Symbol}")
Mia Alissi
The error "name Fundamental not defined" is due to the fact that the
Fundamental
class is not imported in your code. You need to import it fromQuantConnect.Data.Fundamental
module.However, based on the extracts provided, it seems you are trying to use the
FineFundamental
objects for your universe selection function. TheFineFundamental
objects are used to access fundamental data in QuantConnect.Here is how you can modify your code:
This should resolve the error you are facing.
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.
Levi Freedman
Now it says
Runtime Error: 'CoarseFundamental' object has no attribute 'ValuationRatios'
at <lambda>
sorted_by_pe_ratio = sorted(sorted_by_dollar_volume in main.py: line 20
at FundamentalSelectionFunction
sorted_by_pe_ratio = sorted(sorted_by_dollar_volume in main.py: line 20
Mia Alissi
The error message "Runtime Error: 'CoarseFundamental' object has no attribute 'ValuationRatios'" is indicating that the
CoarseFundamental
object does not have aValuationRatios
attribute.In QuantConnect,
CoarseFundamental
objects are used for coarse universe selection and they do not contain detailed fundamental data. They only have the following attributes: Market, DollarVolume, Volume, HasFundamentalData, PriceFactor, SplitFactor, PriceScaleFactor, AdjustedPrice, EndTime, Price, DataType, IsFillForward, and Time.The
ValuationRatios
attribute is part of theFineFundamental
object, which is used for fine universe selection and contains detailed fundamental data.You should separate your universe selection into two stages: coarse selection and fine selection. In the coarse selection, you can filter the universe based on attributes like DollarVolume and HasFundamentalData. In the fine selection, you can filter the remaining securities based on fundamental data like ValuationRatios.
Here is an example of how you can modify your code:
This should resolve the error you are facing.
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.
Levi Freedman
There is a new Universe framework / method i'm trying to use; course/fine is the old method.
Mia Alissi
Yes, you're correct. The Algorithm Framework in QuantConnect provides a more modular way to design algorithms, including universe selection. You can use pre-built models or create your own custom models.
In the Algorithm Framework, you can add one or more Universe Selection models to your algorithm. If you add multiple Universe Selection models, the algorithm subscribes to the constituents of all the models.
Here is an example of how to add multiple Universe Selection models to an algorithm:
If you want to create your own Universe Selection model, you should extend the
UniverseSelectionModel
class and implement theCreateUniverses
method, which receives analgorithm
object and returns an array ofUniverse
objects. Here is an example:When the Universe Selection model adjusts the universe constituents, your algorithm will be notified through the
OnSecuritiesChanged
event handler on your other framework components.For more details, you can refer to the Algorithm Framework documentation.
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.
Levi Freedman
I'm trying to run this code
And i get an error
During the algorithm initialization, the following exception has occurred: cannot instantiate abstract class
at Initialize
self.AddUniverseSelection(FundamentalUniverseSelectionModel(self.FundamentalFilterFunction))
at Python.Runtime.PythonException.ThrowLastAsClrException() at D:\QuantConnect\MyLean\pythonnet\src\runtime\PythonException.cs:line 53
at Python.Runtime.PyObject.Invoke(PyTuple args in main.py: line 10
cannot instantiate abstract class
Mia Alissi
The error message "cannot instantiate abstract class" is indicating that you're trying to create an instance of an abstract class, which is not allowed.
In your code, you're trying to create an instance of
FundamentalUniverseSelectionModel
which is an abstract class in QuantConnect. This class is meant to be extended, not instantiated directly.However, based on the information provided in the extracts, it seems that you might be trying to use the
FineFundamental
objects for your universe selection function. TheFineFundamental
objects are used to access fundamental data in QuantConnect.Here is how you can modify your code:
This should resolve the error you are facing.
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.
Levi Freedman
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!