from AlgorithmImports import *
class AssetGrowth(QCAlgorithm):
def Initialize(self):
#rebalancing should occur in July
self.SetStartDate(2008,6,15) #Set Start Date
self.SetEndDate(2010,7,15) #Set End Date
self.SetCash(1000000) #Set Strategy Cash
self.UniverseSettings.Resolution = Resolution.Daily
self.previous_fine = None
self.filtered_fine = None
self.AddUniverse(self.CoarseSelectionFunction,self.FineSelectionFunction)
self.AddEquity("SPY", Resolution.Daily)
#monthly scheduled event but will only rebalance once a year
self.Schedule.On(self.DateRules.WeekStart("SPY"), self.TimeRules.At(10, 0), self.rebalance)
self.months = -1
self.yearly_rebalance = False
self.SetAlpha(ConstantAlphaModel(InsightType.Price, InsightDirection.Up, timedelta(30)))
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel(lambda time:None))
self.current_month = -1
self.coarse_count = 300
self.fine_count = 10
def CoarseSelectionFunction(self, coarse):
if self.yearly_rebalance:
# drop stocks which have no fundamental data
#filtered_coarse = [x.Symbol for x in coarse if (x.HasFundamentalData) and (x.Market == "usa")]
filtered_coarse = [x.Symbol for x in coarse]
self.Debug("CoarseSelectionFunction")
self.Debug(filtered_coarse)
sortedByDollarVolume = sorted([x for x in coarse if (x.HasFundamentalData)],
key=lambda x: x.DollarVolume, reverse=True)[:self.coarse_count]
filtered_coarse = [i.Symbol for i in sortedByDollarVolume]
self.Debug(filtered_coarse)
return filtered_coarse
else:
return []
terminal output:
4667
|
10:36:28
:
CoarseSelectionFunction
4668
|
10:36:28
:
[, , , ,
4669
|
10:36:28
:
[, , , ,
i am confused ,how can i get stock symbol for all US stock?
Mia Alissi
In your
CoarseSelectionFunction
, you are trying to get the symbols for all US stocks. However, the linefiltered_coarse = [x.Symbol for x in coarse]
is not filtering for US stocks specifically.To filter for US stocks, you should use the
x.Market
property of theCoarseFundamental
object, which should be equal to "usa". Here is how you can modify your code:This code will return the symbols of the stocks that have fundamental data and are in the US market. The
Debug
statements will print these symbols to the console for your reference.For more information on universe selection in QuantConnect, you can refer to the 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.
Zhiwei
thank you,but i still get this, so i need vip or something to get normal return data?
Ashutosh
Hello,
CoarseSelectionFunction is working just fine.
If you need to output the symbols that are filtered out after selection logic you would need to Log the Symbol.Value of each stock.
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.
Zhiwei
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!