import numpy as np
class BasicTemplateAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2010,1,1) #Set Start Date
self.SetEndDate(2010,2,2) #Set End Date
self.SetCash(100000) #Set Strategy Cash
self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
def CoarseSelectionFunction(self, coarse):
sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
return [ x.Symbol for x in sortedByDollarVolume[:500] ]
def FineSelectionFunction(self, fine):
sortedByPeRatio = sorted(fine, key=lambda x: x.ValuationRatios.PERatio, reverse=True)
return [ x.Symbol for x in sortedByPeRatio ]
def OnData(self, data):
symbols = self.CoarseSelectionFunction(self, data)
I have attempted this but receieve the error:
Runtime Error: TypeError : CoarseSelectionFunction() takes 2 positional arguments but 3 were given
at OnData in main.py:line 35
TypeError : CoarseSelectionFunction() takes 2 positional arguments but 3 were given (Open Stacktrace)
How can I call the CoarseSelectionFunction and return the result to OnData?
Jared Broad
You will need to pass it a coarse universe object not the slice object.
Coarse universe is 9k fundamental data points generated once per day
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.
Kieran Flynn
import numpy as np class BasicTemplateAlgorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2010,1,1) #Set Start Date self.SetEndDate(2010,2,2) #Set End Date self.SetCash(100000) #Set Strategy Cash self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction) def CoarseSelectionFunction(self, coarse): sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True) return [ x.Symbol for x in sortedByDollarVolume[:10] ] def FineSelectionFunction(self, fine): sortedByPeRatio = sorted(fine, key=lambda x: x.ValuationRatios.PERatio, reverse=True) return [ x.Symbol for x in sortedByPeRatio ] def OnData(self, data): symbols = self.CoarseSelectionFunction(self, coarse)
This does not work either. Is there some way to return the symbols to my OnData function?
Kieran Flynn
What I am after is a way to access my universe of symbols in my main algorithm. How is this achieved?
Jing Wu
Hi Kieran, to access the symbols in universe selection method, you can save the symbol list in an instance variable for example "self.symbols" then you can use this variable in OnData(). Please refer to this example
Kieran Flynn
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!