Back

How many stocks do I hold

My model only holds a subset of the symbols I track.  What is the best way to count the number of symbols currently held? Is there a count function, can I loop through self.Portfolio, or must I loop through all symbols tracked?  I've tried several things but the only thing that works is looping through all symbols tracked but I think there must be a better way.  Thanks for any help.

Update Backtest








Hi Hugh,

To iterate all the stocks in the portfolio, you could use

for kvp in self.Portfolio:
    security_holding = kvp.Value
    symbol = security_holding.Symbol.Value
# Quantity of the security held
    quantity = security_holding.Quantity
# Average price of the security holdings
    price = security_holding.AveragePrice

Or you can get the symbol list invested first

invested = [x.Key for x in self.Portfolio if x.Value.Invested]

Then loop the "invested" to get the properties

for symbol in invested:
    security_holding = self.Portfolio[symbol]
    quantity = security_holding.Quantity
    price = security_holding.AveragePrice
1

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.


Jing Wu - Thanks, you're the best.   You gave me several options that should work.  Really appreciate it.

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