Back

[Python] Question about portfolio and universes

Question 1: is there an easier way to get a list of stocks that I currently own than this:

positions = [i for i in self.Portfolio.Keys if self.Portfolio[i].HoldStock]

I thought that Portfolio only included stocks that you own but apparently that's not the case so I had to implement this list comprehension. Seems there would be an easier way to just get a simple list of all the stocks currently held, but I couldn't find it.

 

Question 2: What is the proper way to get the list of stocks currently selected by your universe?

I have two universes, user-defined ETF's and stocks selected with coarse/fine selection. The two universes of stocks are treated entierly different by my algorithm so it's important that I have a list of a all stocks currently selected by the fundamentals. I don't care about which ones were added/removed today I just need all the ones that currently meet my criteria.

I tried everything mentioned on this thread: 

https://www.quantconnect.com/forum/discussion/1359/working-with-a-universe

with no luck. The only solution that i think worked is to append each stock's symbol to a list during the universe selection. like this:

for x in topFine:
list.Add(x.Symbol)
self.stocks_long.append(str(x.Symbol.Value))
return list

But now when I try and get the securities price I get an error saying that that the stock was not found in securities. If I add it to securities it then becomes part of my user-defined universe which would mess up the logic. I was under the assumption that all stocks selected with the universes were automatically added to securities and and had the data availible.

Update Backtest








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