Back

ActiveSecurities Type Error

I'm making a simple test strategy:

class LiquidUniverseSelection(QCAlgorithm):

filteredByPrice = None

def Initialize(self):
self.SetStartDate(2019, 1, 11)
self.SetEndDate(2019, 7, 1)
self.SetCash(100000)
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.coarse_select)

def coarse_select(self, coarse):
fundamental_filter = [x for x in coarse if x.HasFundamentalData]
volume_filter = [x for x in fundamental_filter if x.DollarVolume > 10000000]
price_filter = [x for x in volume_filter if (50 > x.Price > 5)]

return self.active

def OnData(self, data):
num_securities = len(self.ActiveSecurities.Keys)
targets = [PortfolioTarget(x, 1/num_securities) for x in self.ActiveSecurities.Keys]
self.SetHoldings(targets)

This returns this stacktrace:

System.Exception: TypeError : object of type '0, Culture=neutral, PublicKeyToken=null]]' has no len()
at OnData in main.py:line 67 :: num_securities = len(self.ActiveSecurities.Keys)
---> Python.Runtime.PythonException: TypeError : object of type '0, Culture=neutral, PublicKeyToken=null]]' has no len()
at Python.Runtime.PyObject.Invoke (Python.Runtime.PyTuple args, Python.Runtime.PyDict kw) [0x00033] in :0
at Python.Runtime.PyObject.TryInvoke (System.Dynamic.InvokeBinder binder, System.Object[] args, System.Object& result) [0x0001d] in :0
at (wrapper dynamic-method) System.Object.CallSite.Target(System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object,QuantConnect.Data.Slice)
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1] (System.Runtime.CompilerServices.CallSite site, T0 arg0, T1 arg1) [0x00105] in <92922d9bda2f4e1cba9242d052be6d43>:0
at QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper.OnData (QuantConnect.Data.Slice slice) [0x0011f] in <6595d12b90e34d0e8d92c91974132544>:0
at QuantConnect.Lean.Engine.AlgorithmManager.Run (QuantConnect.Packets.AlgorithmNodePacket job, QuantConnect.Interfaces.IAlgorithm algorithm, QuantConnect.Lean.Engine.DataFeeds.ISynchronizer synchronizer, QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler transactions, QuantConnect.Lean.Engine.Results.IResultHandler results, QuantConnect.Lean.Engine.RealTime.IRealTimeHandler realtime, QuantConnect.Lean.Engine.Server.ILeanManager leanManager, QuantConnect.Lean.Engine.Alpha.IAlphaHandler alphas, System.Threading.CancellationToken token) [0x01140] in Lean.Engine.Alpha.IAlphaHandler alphas, System.Threading.CancellationToken token) [0x01140] in <5153497fd8c94879ae659069852668fc>:0
--- End of inner exception stack trace ---

If I replace 'ActiveSecurities' with 'Securities' it works fine.

Update Backtest







0

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.


Hey ErikDains,

There is a subtle reason why len(self.Securities.Keys) works, while len(self.ActiveSecurities.Keys) does not work. self.ActiveSecurities.Keys is an IEnumerable object, which does not have a length property. However self.Securities.Keys, similar to a list, is a ICollection object which does have a length property.

So one way to find the size of self.ActiveSecurities.Keys is to first turn it into a list/Collection and then call len .

num_securities = len(list(self.ActiveSecurities.Keys))

You can also use

num_securities = self.ActiveSecurities.Count

which iterates through the IEnumerable and counts the elements.

You can learn more about the differences between IEnumerable and ICollection in this article.

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed