Fetch open orders and 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 orders from IB but I get this log “Brokerage connection not available. Cannot fetch IB orders.”. Below is my implementaiton for fetching the orders which were placed outside QC. Please validate below code 


    def fetch_ib_order_details(self):
        """Fetch open orders and holdings directly from Interactive Brokers.

        Any holdings that are present in the brokerage account but missing in the
        ObjectStore will be added so portfolio risk calculations remain accurate.
        Stale entries in the ObjectStore with no corresponding holding will be removed.
        """

        if not self.live_mode:
            self.debug("fetch_ib_order_details: not in live mode – skipped.")
            return

        brokerage = getattr(self, "brokerage", None)
        if brokerage is None:
            self.Debug("Brokerage connection not available. Cannot fetch IB orders.")
            return

        # --- Retrieve open orders ---
        try:
            orders = brokerage.get_open_orders()
        except Exception as e:
            self.Debug(f"Failed to retrieve open orders from brokerage: {str(e)}")
            orders = []

        for order in orders:
            try:
                symbol = order.Symbol.Value if hasattr(order.Symbol, "Value") else str(order.Symbol)
                qty = order.Quantity
                entry_price = getattr(order, "AverageFillPrice", None) or getattr(order, "LimitPrice", None)
                entry_price = entry_price or getattr(order, "StopPrice", None)
                buy_date = getattr(order, "Time", None)
                stop_loss = getattr(order, "StopPrice", None)
                profit_target = getattr(order, "LimitPrice", None)

                self.Debug(
                    f"IB Order - Symbol:{symbol} Qty:{qty} Price:{entry_price} Date:{buy_date} "
                    f"SL:{stop_loss} TP:{profit_target}"
                )
            except Exception as ex:
                self.Debug(f"Error processing IB order: {str(ex)}")

        # --- Retrieve account holdings ---
        try:
            holdings = brokerage.get_account_holdings()
        except Exception as e:
            self.Debug(f"Failed to retrieve account holdings from brokerage: {str(e)}")
            holdings = []

        holdings_symbols = set()
        for holding in holdings:
            try:
                symbol = holding.Symbol.Value if hasattr(holding.Symbol, "Value") else str(holding.Symbol)
                qty = holding.Quantity
                price = holding.AveragePrice
                holdings_symbols.add(symbol)

                self.Debug(f"IB Holding - Symbol:{symbol} Qty:{qty} AvgPrice:{price}")
            except Exception as ex:
                self.Debug(f"Error processing IB holding: {str(ex)}")