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!
Michael Manus
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
Alexandre Catarino
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}"); } }
Michael Manus
Thanks much better
btw kvp.Value.Symbol you get the symbol
Alexandre Catarino
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.
Jonas Hasle
Hi,Â
just moved to QuantConnect from Quantopian, so this is very new to me. I´ve tried to find out how to view the current positions in my portfolio at a given time and i found this thread and I´m wondering: Where in the algorithm do you paste the code to see positions in the porfolio? Any help would be much appreciated :-)Â
Â
I´ve attached the algorithm which I´ve been playing around with for the last couple of days.Â
Derek Melchin
Hi Jonas,
We can access the `Portfolio` anywhere we have a reference to the QCAlgorithm object because `Portfolio` is a member of that class.
Refer to the Quantopian Migration and the Securities and Portfolio docs. The attached backtest provides an example.
Best,
Derek Melchin
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.
Jon Has
Hello! Thank you so much for helpful response. This helps a lot. I also found some help from this thread where tickers and quantity can be found in the console using self.Debug:Â
https://www.quantconnect.com/forum/discussion/3938/loop-through-self-securities/p1Best RegardsÂ
Jonas H.
QQQ VVV
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!