Back

Loop through self.Securities?

I'm trying to access all the objects in my current universe with the following code:

for x in self.Securities:
self.Debug(str(x.Symbol))

However, "x" just ends up being a short string containing the ticker symbol and some other garble, instead of a complete QuantConnect.Securities.Security object, which is what the docs say it should be. So I can't access the needed object properties like Symbol, Price, etc. Does anyone know why I'm getting a string instead of the expected object? Many thanks!

Update Backtest







Not sure if this will help, but it seems ( maybe for this reason ) a lot of people create a global type variabe to hold the symbol object.  So in your init function you would do

self.symbols = []

then inside the universe selection, like coarse universe selection you would update your variable:

def CoarseSelectionFunction(self, coarse):
self.symbols = [ x.Symbol for x in coarse ]

Then later in your code you can loop through the list self.symbols.

Also I have a similar loop but uses the Portfolio class instead of Securities:

for symbol in self.Portfolio.Keys:
#maybe this is a good symbol
0

x is already the security

x.Symbol is the string of the ticker

Securities[stock].Price & self.Securities[stock].Holdings.Quantity  example ->

https://github.com/QuantConnect/Lean/blob/cbd953437f987392f2c2e154adcb7d43d684b7f1/Algorithm.Python/BubbleAlgorithm.py#L110

 

so its :

for security in self.securities: 

:)

1

Some Lean objects are dictionaries where the Key is a Symbol object and the Value is another object (Security, SecurityHolding, etc). You should loop through Securities like this:

for kvp in self.Securities:
symbol = kvp.Key
security = kvp.Value
self.Debug(str(security.Symbol))
# or self.Debug(str(kvp.Key))

and Portfolio:

for kvp in self.Portfolio:
symbol = kvp.Key
holding = kvp.Value
self.Debug(str(holding.Symbol))
# or self.Debug(str(kvp.Key))
2

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.


Alexandre, that's exactly what I needed, thanks! Idk why I didn't realize that when looking at the debug output.

1

Also something useful is using the dir function in python to print out all the attributes of an object.  So if x is some object you can find out all the available attributes and functions like this:

self.Debug(str(dir( x )))
0

Tim De Lise, I guess that's one way to do it, but it seems like an unnecesary extra step, and inneficient use of memory since the self.Securities dictionary already exists just for stuff like this.

0

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