Back

How to show all the existing positions in my broker account?

I feel like this may be a dumb question as it seems basic...

I am linking my IB account to my QC algo and my IB account already has several positions in it.

I wanted to ask how I can loop through all the existing positions in the code

Something like

foreach (stock in Portfolio){
debug(stock.AveragePrice.ToString());
}

But obviously, the code above does not work and I was wondering what modification I need to make.

 

Thanks a lot!

Update Backtest








What will work for sure is:

Securities class has the securty collection which you subscribed to + you automatically subsrcibe to securities in your portfolio(holdings).

so list of Symbols(class) from securites:  var x = Securities.Keys.ToList();  <- list of symbol class 

Loop through the symbols and check (from documentation):  Portfolio[symbol].Invested

if you are invested.

same way over SubscriptionManager.Subscriptions is possible

API tab in the LAB does not show that information i think ->

Jared Broad 

0

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. (for more datails, checkout the docs).

In order to get all existing open positions, you can use this code snippet:

foreach (var kvp in Portfolio)
{
if (kvp.Value.Invested)
{
Log($"Symbol: {kvp.Key} -> Price: {kvp.Value.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.


Thanks much better

btw kvp.Value.Symbol you get the symbol

0

Yes, we can also use kvp.Value.Symbol. Ideally, we should create variables with meaningful names:

foreach (var kvp in Portfolio)
{
var holdings = kvp.Value;
var symbol = holdings.Symbol; // or kvp.Key;

if (holdings.Invested)
{
Log($"Symbol: {symbol} -> Price: {holdings.AveragePrice}");
}
}

Also note that is a "best practice" to loop through Portfolio than Portfolio.Values.
 

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.


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