ib-api-reloaded / ib_async

Python sync/async framework for Interactive Brokers API (replaces ib_insync)
BSD 2-Clause "Simplified" License
223 stars 37 forks source link

Added functionality reqPositionsMulti #25

Open paramkr2 opened 3 weeks ago

eyalk11 commented 3 weeks ago

Lol implemented it a long ago. Just fixed it. Your implementation doesn't seem to be async (I don't get how it works too)

    async def reqTickersAsync(
            self,genericTickList='', *contracts: Contract):
        s=self._ibsource.ibrem.ib
        futures = []
        tickers = []
        reqIds = []
        contracts = set(list(contracts))
        for contract in contracts:
            reqId = s.client.getReqId()
            reqIds.append(reqId)
            future = s.wrapper.startReq(reqId, contract)
            futures.append(future)
            ticker = s.wrapper.startTicker(reqId, contract, "mktData")
            tickers.append(ticker)
            def kk(reqId,contract,aa):
                logging.debug(('called', reqId))
                #s.cancelMktData(contract)
                s.wrapper._endReq(reqId)

            ticker.updateEvent += partial(kk,reqId,contract)
            s.client.reqMktData(
                reqId, contract,genericTickList=genericTickList,snapshot=False,regulatorySnapshot=False,mktDataOptions=[])

        ls=await asyncio.gather(*futures,return_exceptions=True)
        logging.debug('gathered')
        for w,id in enumerate(ls):
            if isinstance(w,Exception):
                logging.debug(('error',w,id))

        for contract in contracts:
            s.cancelMktData(contract)
            #s.wrapper.endTicker(ticker, "aa")
        logging.debug('ended tickers')
        return tickers
paramkr2 commented 3 weeks ago

@eyalk11 Thansk for looking into it, I was trying to get positions within a specified modelCode , and for me async works fine. I needed functionality for Position Update Subscription by Model, which is not implemented here. I am new to this, so i don't understand how your code achieves this.

eyalk11 commented 2 weeks ago

Hi, I probably didn't understand your use case.