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!
Tim De Lise
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
Michael Manus
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:
:)
Alexandre Catarino
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))
Timothy Hellwig
Alexandre, that's exactly what I needed, thanks! Idk why I didn't realize that when looking at the debug output.
Tim De Lise
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 )))
Timothy Hellwig
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.
Mark Reeve
Thanks Tim De Lise - that dir() trick was gold for me!
Timothy Hellwig
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!