Hi
I am trying to create a universe of equities which have market cap more than 10 billion. But the backtest always seem to include some which do not have fulfill the condition.
I will attach my sample code here
Am I doing something wrong ?
class FlipCountTradingAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2018, 1, 1) # Set the start date for the algorithm
#self.SetEndDate(datetime.today()) # Set the end date to today
self.SetEndDate(2020,1,1)
self.SetCash(10000000) # Set the initial cash for the algorithm
self.UniverseSettings.Resolution = Resolution.Daily # Set the universe selection resolution to daily
# Add universe selection based on the constituents of SPY ETF
#self.AddUniverse(ETFConstituentUniverse, "SPY", self.UniverseSettings.Resolution, self.CoarseSelectionFunction, self.FineSelectionFunction)
#self.AddUniverseSelection(ETFConstituentsUniverseSelectionModel("SPY"))
self.AddUniverse(self.CoarseSelectionFunction)
#self.add_universe(self.universe.etf("SPY"))
# Dictionary to store stock data including the rolling window
self.data = {}
# Define thresholds
self.buy_threshold = 9
self.sell_threshold = 9
# Lists to track long and short positions
self.long_positions = []
self.short_positions = []
def CoarseSelectionFunction(self, coarse):
# Select all constituents of the ETF
f = [f.Symbol for f in coarse if f.market_cap > 10000000000]
return f
Mia Alissi
Hi Tushar,
The issue you're facing is that the
CoarseFundamental
object does not have amarket_cap
attribute, which is why your current universe selection is not working as expected.However, you can calculate the market cap in the
FineSelectionFunction
using theMarketCap
attribute of theFineFundamental
object. Here is an example of how you can do this:You would need to add this function to your algorithm and use
AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
in yourInitialize
method.Please note that the
MarketCap
attribute is in dollars, so1e10
represents 10 billion.Also, keep in mind that each asset in the universe consumes approximately 5MB of RAM, so you may quickly run out of memory if your universe filter selects many assets. To keep your algorithm fast and efficient, only subscribe to the assets you need source.
I hope this helps! Let me know if you have any other questions.
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.
Tushar Verma
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!