Get all existing holdings directly from Interactive Brokers. Any holdings that are present in the brokerage account but missing in the QuantConnect because those trades were place outside QuantConnect. I am trying to use below methods for getting all open holdings from IB which is working fine but I am not sure how to get bracket order info (stop loss and profit limit) for existing positions. For example, I bought 100 GOOGLE with bracket order (profit limit is $210 and stop loss is $180) so I want to get profit limit and stop loss for my existing open positions
brokerage = getattr(self, "brokerage", None)
if self.LiveMode and brokerage is not None:
try:
holdings = brokerage.get_account_holdings()
except Exception as err:
self.Error(f"IB holdings request failed: {err}")
holdings_symbols = set()
for h in holdings:
try:
symbol = getattr(h, "Symbol", None)
qty = getattr(h, "Quantity", 0)
price = getattr(h, "AveragePrice", 0.0)
holdings_symbols.add(str(symbol))
self.Debug(f"IB Holding - {symbol} | Qty {qty} | Avg {price}")
except Exception as inner:
self.Error(f"Unable to parse holding: {inner}")
Mia Alissi
Here’s an improved version of your code that, in addition to getting all holdings from Interactive Brokers, also retrieves open orders and matches each holding to any bracket orders (stop loss and profit limit) that may be present. The relevant stop and limit prices are logged for each holding, and robust error handling is used so you never miss information due to an exception:
Let me know if you’d like to see this as a method or integrated elsewhere!
Sources: Opening Range Breakout for Stocks in Play, Brokerages - QuantConnect.com, Interactive Brokers - QuantConnect.com, Transaction Manager - QuantConnect.com, Holdings - QuantConnect.com
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.
Dharmesh Khalasi
We need to handle the scenario of multiple orders for the same symbol. For example, I have existing GOOGL positions which was bought and it has attach bracket order ( profit and stop price) , In addition I have two GOOGL open order (which is not bought yet but it has buy limit price, profit price and stop price setup). While matching , we need to make sure to match positions with the order which was already bought but not open order
Dharmesh Khalasi
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!