I want to select 20 stocks using the following CoarseSelectionFunction:
def CoarseSelectionFunction(self, coarse):
CoarseWithFundamental = [x for x in coarse if x.HasFundamentalData]
sortedByDollarVolume = sorted(CoarseWithFundamental, key=lambda x: x.DollarVolume, reverse=True)
self.universe = [x.Symbol for x in sortedByDollarVolume[:20]]
return self.universe
When I later check the stock in the self.Securities dictionary using
self.Debug(len(self.Securities.Keys))
for security in self.Securities.Keys:
self.Debug(security)
I get a list of 55 securities. How can that be?
Here is the complete code:
Jared Broad
Securities grow over time as it includes previously selected securities.
The current Active set in the universe can be seen by the ActiveSecurities property.
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.
Filib Uster
Thanks, Thats very helpful.
Follow up question:
Why does the Univere contain only SPY when the rebalance function is triggered for the first time? Is there a timing problem and how can that be fixed?
Jared Broad
SPY is in the universe immediately. You can see its data event in OnData.
def OnData(self, slice): if slice.ContainsKey("SPY"): self.Debug(self.Time) def Rebalance(self): pass
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.
Serge d'Adesky
Ralph,
Your question had me puzzled too. This would be a workaround: iterate over self.universe, not self.Securities.Keys:
class Algorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2012,1,1) #Set Start Date self.SetEndDate(2012,2,5) #Set End Date self.SetCash(100000) #Set Strategy Cash self.UniverseSettings.Resolution = Resolution.Minute self.AddUniverse(self.CoarseSelectionFunction) self.AddEquity('SPY', Resolution.Daily) self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY", 60), Action(self.Rebalance)) self.universe = {} def CoarseSelectionFunction(self, coarse): CoarseWithFundamental = [x for x in coarse if x.HasFundamentalData] sortedByDollarVolume = sorted(CoarseWithFundamental, key=lambda x: x.DollarVolume, reverse=True) self.universe = [x.Symbol for x in sortedByDollarVolume[:20]] self.Debug(len(self.universe)) return self.universe def Rebalance(self): self.Debug(self.Time) self.Debug(len(self.universe)) for security in self.universe: self.Debug(security)
Jared Broad
Thanks Serge; the recommended way to get all securities in all universes is ActiveSecurities; I've added it as a docs section for the new property here.
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.
Filib Uster
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!