In the documentation (https://www.quantconnect.com/docs/v2/writing-algorithms/securities/key-concepts) I read:
"The ActiveSecurities property of the algorithm class contains all of the assets currently in your universe ...".
This is however not what I see in my code. I wrote a class Scanner, which is derived from FundamentalSelectionModel, the SelectFine member of which is :
def SelectFine(self, algorithm, fine):
algorithm.Debug('-------------> Starting Fine Selection')
filtered1 = [f for f in fine if f.CompanyReference.PrimaryExchangeID == 'NAS' or f.CompanyReference.PrimaryExchangeID == 'NYS']
filtered2 = [f for f in filtered1 if f.EarningReports.BasicAverageShares.ThreeMonths <= self.maxFloat]
algorithm.Debug('-------------> Fine Selection done')
algorithm.Debug(str([f.Symbol.Value for f in filtered2]))
return [f.Symbol for f in filtered2]
The Algorithm class is:
import AlgorithmImports as aim
import Selection.FundamentalUniverseSelectionModel as sfu
import datetime as dt
import numpy as np
class runScanner(aim.QCAlgorithm):
def Initialize(self):
# self.RelVolLookback = 30
self.SetStartDate(2023, 1, 4) # Set Start Date
self.SetEndDate(2023, 1, 4) # Set End Date
self.AddEquity('MSFT')
self.Schedule.On(self.DateRules.EveryDay('MSFT'), self.TimeRules.AfterMarketOpen('MSFT', 1), self.StartOfDay)
# self.SetWarmUp(dt.timedelta(self.RelVolLookback))
self.UniverseSettings.Resolution = aim.Resolution.Daily
self.AddUniverseSelection(Scanner())
# self.SetExecution(aim.ImmediateExecutionModel())
def OnData(self, data):
pass
def StartOfDay(self):
self.Debug('--------------------> StartOfDay '+ self.Time.strftime('%Y-%m-%d %H:%M:%S'))
self.Debug(str([security.Key.Value for security in self.ActiveSecurities]))
The Debug statements give the following printouts:
11 | 8:35:30: -------------> Starting Fine Selection
12 | 8:35:30: -------------> Fine Selection done
13 | 8:35:30:['AMCI', 'ARQQ', 'CKPT', 'CSTA', 'CVRX', 'FIAC', 'FRXB', 'OPI', 'GRRR', 'JSPR', 'JYNT', 'ATNF', 'LFAC', 'AMPY', 'RSLS', 'ORMP', 'PORT', 'ROC', 'RONI', 'SGH', 'SJT', 'TUSK', 'TWLV']
14 | 8:35:32: --------------------> StartOfDay 2023-01-04 09:31:00
15 | 8:35:32: ['RONI', 'JYNT', 'ARQQ', 'KBLM', 'CSTA', 'LFAC', 'GOV', 'FRXB', 'SGH', 'PORT', 'MPO', 'TUSK', 'AMCI', 'GRRR', 'CKPT', 'OBLN', 'ORMP', 'ROC', 'JSPR', 'FIAC', 'SJT', 'CVRX', 'TWLV', 'VGAC', 'ERC', 'SWBI...
16 | 8:35:32: Algorithm 'e5be5be323ca89d632c9c8d2b10ef5d7' completed
My main question is, why is the printout from the ActiveSecurites property (line 15 above) different from the printout in the SelcetFine function (line 13 above)? Shouldn't it be the same?
A second question is, the printout for ActiveSecurities is different every time I run a backtest, but it never matches ny Universe selection.
What am I doing wrong?
Please let me know if you want the full executable code. I was hoping to save you time by only posting the relevant parts of my code so far.
Jesso joy
it is the same number of instruments, just the ordering is different.
Melanie Schiefele
I apologize, I should have taken that back long ago. The code above is plenty buggy and has the complete wrong idea. You are not supposed to write a class derived from FineFundamentalUniverseSelectionModel, but rather do self.AddUniverseSelection(aim.FineFundamentalUniverseSelectionModel(self.SelectCoarse, self.SelectFine)) in your class derived from QCAlgorithm, and then add the functions SelectCoarse and SelectFine to your QCAlgorithm derived class. Sorry, I just got that wrong in the beginning.
Melanie Schiefele
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!