Hello,
I am facing some difficulty trying to figure coarse selection and fine selection universe. I am trying to list all stocks that trade in NYSE.
However, i am noticing that coarse seem to print approx 5K stocks however, fine always shows as an empty field. Could some one let me know what is that i am obviously doing wrong in this code?
This is what my code is:
class CoarseFineFundamentalRegressionAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019,1,3) #Set Start Date
self.SetEndDate(2019,1,4) #Set End Date
self.SetCash(50000) #Set Strategy Cash
self.UniverseSettings.Resolution = Resolution.Minute
# this add universe method accepts two parameters:
# - coarse selection function: accepts an IEnumerable<CoarseFundamental> and returns an IEnumerable<Symbol>
# - fine selection function: accepts an IEnumerable<FineFundamental> and returns an IEnumerable<Symbol>
self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
self.changes = None
# return a list of three fixed symbol objects
def CoarseSelectionFunction(self, coarse):
Coarse_filtered_stocks = filter(lambda x: x.DollarVolume >0,coarse)
Coarse_filtered_stocks = filter(lambda x: x.HasFundamentalData,Coarse_filtered_stocks)
Coarse_filtered_stocks = filter(lambda x: x.Price >0,Coarse_filtered_stocks)
count = 0
for stock in Coarse_filtered_stocks:
count += 1
self.Debug(' coarse # of stocks {}'.format(count))
return [stock.symbol for stock in Coarse_filtered_stocks]
# sort the data based on what trades on NYSE
def FineSelectionFunction(self, fine):
count1 = 0
for stock in fine:
count1 += 1
self.Debug(' fine # of stocks {}'.format(count1))
filtered_fine = filter(lambda x: x.CompanyReference.PrimaryExchangeID == "NYS",fine)
return [ x.Symbol for x in filtered_fine]
Daniel Chen
Hi Narayanan,
Thank you for your question. Basically, there are two reasons led to this issue: 1) it should be stock.Symbol (not stock.symbol) in line 24; 2) we cannot get the elements in _Coarse_filltered_stocks_ using for stock in Coarse_filtered_stocks when Coarse_filtered_stocks is not a list. In Python3, filter() returns an Iterator instead of a List, so we recommend you not use filter(). In the following backtest, please let me show you another way to achieve this.
Based on your idea, I directly rewrite your backtest. The results show that there are 5476 stocks in Coarse Universe and 1847 stocks in Fine Universe (i.e. 1847 stocks in NYSE), which is reasonable. I hope this will solve your problem. For more information on Universe Selection, please see this page. We look forward to your good news!
Narayanan Doraiswamy
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!