I'm trying to view the symbols of stocks added to the universe on every day using Log function, and I'm noticing the following issues:
- dates are not matching the set start and finish dates.
- symbols are not 10 every day.
- i'm not sure whether the problem is in the selected universe or in using the log function.
- how to fix the above and add 10 stocks every day to the universe based on the criteria (top 10 stocks by dollar volume, has fundamental data, and price is between $5-$100 and print those stocks for every day between start and finish dates?
class UniverseSelection(QCAlgorithm):
filteredByPrice = None
def Initialize(self):
self.SetStartDate(2021, 11, 1) # Set Start Date
self.SetStartDate(2021, 11, 30) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.AddUniverse(self.CoarseSelectionFilter)
self.UniverseSettings.Resolution = Resolution.Daily
def CoarseSelectionFilter(self, coarse):
sortedByDollarVolume = sorted(coarse, key=lambda c: c.DollarVolume, reverse=True)
filteredByPrice = [c.Symbol for c in sortedByDollarVolume if (c.HasFundamentalData and c.Price > 5 and c.Price < 500)]
selectedStocks = filteredByPrice[:10]
return selectedStocks
def OnSecuritiesChanged(self, changes):
for security in changes.AddedSecurities:
self.Debug(security.Symbol)
Fred Painchaud
Hi Mostafa,
1- You wrote SetStartData twice. So the second call overwrites the first.
2- Your coarse filter should always return 10 securities. Note you wrote < 500 instead of < 100 (as you wrote in the post).
3- In OnSecuritiesChanged, you log added securities, those are new securities, which were not in the universe before. That's why you do not always see 10 securities as some of them are already in there.
4- It's already ok.
Cheers,
Fred
Mostafa Omran
Thanks Fred. I fixed the End date mistake, and used the (ActiveSecurities) instead of (AddedSecurities), and now I have also updated the code to include both Coarse and Fine Filters.
Now I have the following questions:
Thanks,
Mostafa
Mak K
Hi Mostafa,
I am going to answer the rest when I get more time but for now the easy ones;
These are the CoarseFundamentals not listed in the morningstar library
These are the FineFundamentals not listed in the morningstar library;
Here you can see how you can schedule your universe selection at your desired time interval;
https://www.quantconnect.com/docs/algorithm-framework/universe-selection#:~:text=%23%20Selection%20will%20run,function%20in%20algorithm.
Shares Float isn't available in QC but you can follow this posts explanation on how to calculate them yourselves or use shares outstanding instead;
https://www.quantconnect.com/forum/discussion/3936/looking-for-floating-stock-shares-number-on-equities/p1
You can check this posts on how to get the short float;
https://www.quantconnect.com/forum/discussion/10513/short-interest-data/p1
Rest when I have more time, thanks!
Mostafa Omran
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!