If I have a list of symbols that I always want included in my universe, how can I append them to the end of the fine universe selection to ensure that they're included in my universe? I've tried appending the symbols themselves to the list that I return to the fine universe selection function but receive an error since it looks like the fine selection filter has it's own identifier for stocks and doesn't recognize a normal stock symbol. For exmple, the identifier for TLT is 'TLT: some type of box 135.4'. Is there a place where I can find the universe identifiers to append to my universe? Or is there a better way of going about this? Thanks!
Frank Giardina
Nathan,
I was looking for something similar check out this thread it says removing but there is a piece of code that appends symbols at the end of the fine filter is that what you tried?
Â
https://www.quantconnect.com/forum/discussion/4278/removing-tickers-from-manual-universe/p1Frank
Ryan Riordon
Do the symbols have to be added directly to the fine universe? In order of your universe they are passed like this: Coarse -> Fine -> OnSecuritiesChanged -> OnData/Sceduled event. Is it a list of symbols you just want to be available for trading along with those selected by the FineSelectionFunction?
Nathan Miller
Frank Giardina - Thanks for the link. I've gone through and retried this with success! The difference that solved it for me was usingÂ
self.spy = Symbol.Create('SPY', SecurityType.Equity, Market.USA)Â
rather than the usualÂ
self.spy = self.AddEquity("SPY", Resolution.Minute)
For anyone coming across this post in the future, here is a more complete example of a solution to refer to than the one in the thread linked above:
class MomentumEffAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1) # Set Start Date
self.SetEndDate(2020, 11, 1) # Set End Date
self.SetCash(100000) # Set Strategy Cash
# Create your symbols here
self.agg = Symbol.Create('AGG', SecurityType.Equity, Market.USA)
self.tlt = Symbol.Create('TLT', SecurityType.Equity, Market.USA)
self.ief = Symbol.Create('IEF', SecurityType.Equity, Market.USA)
self.iei = Symbol.Create('IEI', SecurityType.Equity, Market.USA)
# Add your symbols to a list
self.addedsymbols = [self.agg, self.tlt, self.ief, self.iei]
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
def CoarseSelectionFunction(self, coarse):
self.selected = sorted([x for x in coarse if x.HasFundamentalData and x.Price > 5],
key=lambda x: x.DollarVolume, reverse=True)
self.coarselist = [x.Symbol for x in self.selected[:100]]
for symbol in self.addedsymbols:
self.coarselist.append(symbol)
return self.coarselist
def FineSelectionFunction(self, fine):
fine = [f for f in fine if f.ValuationRatios.PERatio > 0
and f.EarningReports.BasicEPS.TwelveMonths > 0
and f.EarningReports.BasicAverageShares.ThreeMonths > 0
]
self.selectedfine = sorted(fine,
key=lambda f: f.ValuationRatios.PERatio *
f.EarningReports.BasicEPS.TwelveMonths *
f.EarningReports.BasicAverageShares.ThreeMonths,
reverse=True)
self.finesymbols = [x.Symbol for x in self.selectedfine[:50]]
for symbol in self.addedsymbols:
self.finesymbols.append(symbol)
return self.finesymbols
No updates to OnSecurities changed or OnData are necessary. The symbols are there!
*Note* - not sure why the formatting is screwed up, I'll repost it below with the correct indentation.
Nathan Miller
class MomentumEffAlgorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2020, 1, 1) # Set Start Date self.SetEndDate(2020, 11, 1) # Set End Date self.SetCash(100000) # Set Strategy Cash # Create your symbols here self.agg = Symbol.Create('AGG', SecurityType.Equity, Market.USA) self.tlt = Symbol.Create('TLT', SecurityType.Equity, Market.USA) self.ief = Symbol.Create('IEF', SecurityType.Equity, Market.USA) self.iei = Symbol.Create('IEI', SecurityType.Equity, Market.USA) # Add your symbols to a list self.addedsymbols = [self.agg, self.tlt, self.ief, self.iei] self.UniverseSettings.Resolution = Resolution.Daily self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction) def CoarseSelectionFunction(self, coarse): self.selected = sorted([x for x in coarse if x.HasFundamentalData and x.Price > 5], key=lambda x: x.DollarVolume, reverse=True) self.coarselist = [x.Symbol for x in self.selected[:100]] for symbol in self.addedsymbols: self.coarselist.append(symbol) return self.coarselist def FineSelectionFunction(self, fine): fine = [f for f in fine if f.ValuationRatios.PERatio > 0 and f.EarningReports.BasicEPS.TwelveMonths > 0 and f.EarningReports.BasicAverageShares.ThreeMonths > 0 ] self.selectedfine = sorted(fine, key=lambda f: f.ValuationRatios.PERatio * f.EarningReports.BasicEPS.TwelveMonths * f.EarningReports.BasicAverageShares.ThreeMonths, reverse=True) self.finesymbols = [x.Symbol for x in self.selectedfine[:50]] for symbol in self.addedsymbols: self.finesymbols.append(symbol) return self.finesymbols
Â
Bryson Long
I am curious on how I would go about creating an algorithm that deals with a manual list of stocks. So say I created a list of APPL, MSFT and TSLA but only wanted to invest in the stock with the lowest pe ratio out of the three listed. How would I create an algo that picks only the security with the lowest pe ratio. Without distributing the portfolio equally so lets say MSFT has the lowest pe ratio the algo would select microsoft and Invest the whole portfolio in MSFT leaving the other securities untouched.
Varad Kabade
Hi Bryson Long,
To receive fundamental data during a backtest, we must use a fine universe filter. Just return the 3 assets during universe selection in this case. We now use OnData to scan for signal
Fundamental data can then be accessed through the stock's Security object. See the source code for reference. Refer to the attached backtest.
Best,
Varad Kabade
Bryson Long
Thanks Varad, but how do you make it trade?
Varad Kabade
Hi Bryson Long,
Refer to the following algorithm where we buy the security with the highest ratio.
Best,
Varad Kabade
Nathan Miller
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!