I'm trying to limit the available stocks within the Universe to stocks with a Close < $10 and Market Cap < $10M. When I run this code, I'm getting stocks with a close above $10, which I've emphasised this within the code block, highlighting stocks with a close above $100
I'm new to QuantConnect, and likely missing something, but I can't work out what it is. Hopefully someone can point me in the right direction.
from AlgorithmImports import *
from Selection import *
class AdaptableApricotRat(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 5, 10)
self.SetEndDate(2022, 6, 10)
self.SetCash(100000)
self.max_price = 10
self.max_market_cap = 10000000
self.AddUniverseSelection(CoarseFundamentalUniverseSelectionModel(self.CoarseSelectionFunction))
self.UniverseSettings.Resolution = Resolution.Daily
def CoarseSelectionFunction(self, coarse):
return [x.Symbol for x in coarse if x.HasFundamentalData and x.Price < self.max_price and x.MarketCap < self.max_market_cap]
def OnData(self, data):
for symbol in data:
value = symbol.Value
if value.Close > 100:
self.Debug('{0}, {1}'.format(self.Time, str(symbol)))
# 2022-05-10 00:00:00, [DBGI XOH3CLWJT4RP, DBGI XOH3CLWJT4RP: O: 573.75 H: 587.25 L: 483 C: 509.5 V: 2248]
# 2022-05-10 00:00:00, [LION XU4WVYMHNAZP, LION XU4WVYMHNAZP: O: 219.23 H: 219.23 L: 219.23 C: 219.23 V: 0]
Mia Alissi
Based on the information provided in the extracts, it seems like you are trying to filter your universe based on certain criteria. In QuantConnect, you can use the CoarseSelectionFunction to filter stocks based on price and other criteria. Here is an example of how you can do it:
In this example,
coarse
is a list of CoarseFundamental objects that contains the stock data. We filter this list to include only the stocks that have a price less than $10 and have fundamental data available.However, the CoarseFundamental object does not contain information about the market cap. To filter based on market cap, you would need to use the FineSelectionFunction, which provides FineFundamental objects that contain more detailed data. Here is an example:
In this example,
fine
is a list of FineFundamental objects. We filter this list to include only the stocks that have a market cap less than $10M.Please note that these functions are just examples and might need to be adjusted to fit your specific needs. For more information, you can refer to the QuantConnect 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.
Pete Dubbelaar
Thanks for the reply, however, when I implement this, I still get the same result.
self.AddUniverseSelection(FineFundamentalUniverseSelectionModel(self.CoarseSelectionFunction, self.FineSelectionFunction))
def CoarseSelectionFunction(self, coarse):
return [x.Symbol for x in coarse if x.HasFundamentalData and x.Price < self.max_price]
def FineSelectionFunction(self, fine):
return [x.Symbol for x in fine if x.MarketCap < self.max_market_cap]
Mia Alissi
Based on the information provided in the extracts, it seems like your code is correct for filtering stocks based on price and market cap. However, there could be a few reasons why you're still seeing stocks with a price above $10.
One possibility is that the price of the stocks increased after they were added to the universe. The universe selection is done once a day at midnight (by default), and the price of the stocks can change throughout the day.
Another possibility is that there's an issue with the
self.max_price
andself.max_market_cap
variables. Make sure these variables are set correctly and that they are not being modified elsewhere in your code.If you're still having issues, it might be helpful to add some debug statements to your code to check the price and market cap of the stocks that are being added to the universe. For example:
This will print a message to the console every time a stock is added to the universe, which can help you identify any issues.
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.
Pete Dubbelaar
Thanks Mia, I've modifed the code, removing my class variables (hardcoding the close and market cap price) and I still get the same issue. I'll try the debug option, but it appears that I'm doing everything as expected.
Mia Alissi
Based on the information provided in the extracts, your code seems correct for filtering stocks based on price and market cap. However, the debug output you provided shows that the close price of some stocks is above $100, which is unexpected.
One possible explanation for this could be related to how QuantConnect handles stock splits and dividends. By default, all price data in QuantConnect is adjusted for splits and dividends. This means that the price you see for a stock might not be its actual price on that day, but rather its price adjusted for future splits and dividends.
For example, if a stock was priced at $10 on a certain day and later had a 10-for-1 split, the adjusted price for that day would be $1. If the stock's price later increased to $100, the adjusted price for the earlier day would still be $1, but the actual price on that day would have been $10.
In your case, it's possible that the stocks with a close price above $100 had splits or dividends that increased their adjusted price.
If you want to work with unadjusted price data, you can set
self.SetDataNormalizationMode(DataNormalizationMode.Raw)
in yourInitialize
method. However, please note that working with unadjusted data can make it more difficult to compare prices over time and can lead to misleading results if not handled correctly.For more information on data normalization in QuantConnect, you can refer to this forum discussion.
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.
Pete Dubbelaar
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!