I can find the Symbols in my Portfolio, but I'm unable to print the string of the Symbol to my Log file.
What's wrong with this code?
if hasattr(self, "Portfolio"):
self.Log("Portfolio object DOES exist")
try:
for i in self.Portfolio:
if hasattr(i,"Value"):
self.Log("the object has a Value")
if hasattr(i.Value,"Symbol"):
self.Log("the object.Value has a Symbol")
self.Log(i.Value.Symbol , " is in porfolio" )
except:
self.Log('line 113 An unknown error occurred trying to access self.Portfolio.' + str(sys.exc_info()[0]) )
quit
Alexandre Catarino
If you want all the symbol in your Portfolio, you can get them by the Portfolio keys:
all_symbols = [ x.Value for x in self.Portfolio.Keys ] self.Log("all: " + str(all_symbols))
On the other hand, if you are looking for the symbols that are invested, you need to rely on the Invested property;
invested = [ x.Symbol.Value for x in self.Portfolio.Values if x.Invested ] self.Log("invested: " + str(invested))
Alexandre Catarino
In the docs, we state that
"The Portfolio property is a collection of SecurityHolding objects to provide easy access to the holding properties. The Portfolio class is a Dictionary<Symbol, SecurityHolding> so can be accessed via ticker index: Portfolio["IBM"].IsLong"
It may be not straightfoward to come up with the code that I wrote from that information, but it is there.
We have been receiving new questions that clearly are derived from new members experience from Quantopian.
It looks like your question is about the QuantConnect equivalent of Quantopian's context.portfolio.positions.
Anyway, we are constantly working to improve documentation and add examples.
Jared Broad
The documentation is a wiki and editable. Please feel free to submit an update.
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.
Wei Chian Ong
Hi Alexandre,
Thanks for the documentation. From your code snippet:
all_symbols = [ x.Value for x in self.Portfolio.Keys ]
I am gathering that since x is the key for the enhanced Portfolio dictionary, then x is the symbol itself and not the SecurityHolding (which would be gathered by using self.Portfolio.Values). I'm guessing this is a really beginner question, but then, what does applying the .Value after it do? As far as I'm aware .Value is not a standard property assigned to dictionary keys, so is this a special property that is linked to the "enhanced" dictionary?
Apologies if my question is confusing.
Wei Chian Ong
I think I've figured out the answer to my own question, so I'll answer it here, just in case anyone was following this thread, as it might be helpful to them. I found the answer here: https://www.quantconnect.com/lean/documentation/topic7826.html
The key itself is a symbol class object, which has a public property Value. When the property Value is applied, it returns the symbol as a string. Hence, the original key is a symbol class object, when the Value property is applied, a string is returned.
Gurumeher Sawhney
The property Value retrieves the cached ticker string of the symbol. So x identifies the company, and x.Value is a reference to the company.
Wei Chian Ong
Understood, thanks for the clarification.
Serge d'Adesky
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!