ib-underscore-async-underscore-modules-ib-underscore-async-client.md
"""Socket client for communicating with Interactive Brokers."""import asyncioimport ioimport loggingimport mathimport structimport timefrom collections importdequefrom collections.abc importCallablefrom typing importAnyfrom eventkit importEventfrom .connection importConnectionfrom .contract importContractfrom .decoder importDecoderfrom .objects importConnectionStats,WshEventDatafrom .util importUNSET\_DOUBLE,UNSET\_INTEGER,dataclassAsTuple,getLoop,run
[[docs]](../../api.html#ib_async.client.Client)class Client:""" Replacement for ``ibapi.client.EClient`` that uses asyncio. The client is fully asynchronous and has its own event-driven networking code that replaces the networking code of the standard EClient. It also replaces the infinite loop of ``EClient.run()`` with the asyncio event loop. It can be used as a drop-in replacement for the standard EClient as provided by IBAPI. Compared to the standard EClient this client has the following additional features: \* ``client.connect()`` will block until the client is ready to serve requests; It is not necessary to wait for ``nextValidId`` to start requests as the client has already done that. The reqId is directly available with :py:meth:`.getReqId()`. \* ``client.connectAsync()`` is a coroutine for connecting asynchronously. \* When blocking, ``client.connect()`` can be made to time out with the timeout parameter (default 2 seconds). \* Optional ``wrapper.priceSizeTick(reqId, tickType, price, size)`` that combines price and size instead of the two wrapper methods priceTick and sizeTick. \* Automatic request throttling. \* Optional ``wrapper.tcpDataArrived()`` method; If the wrapper has this method it is invoked directly after a network packet has arrived. A possible use is to timestamp all data in the packet with the exact same time. \* Optional ``wrapper.tcpDataProcessed()`` method; If the wrapper has this method it is invoked after the network packet's data has been handled. A possible use is to write or evaluate the newly arrived data in one batch instead of item by item. Parameters: MaxRequests (int): Throttle the number of requests to ``MaxRequests`` per ``RequestsInterval`` seconds. Set to 0 to disable throttling. RequestsInterval (float): Time interval (in seconds) for request throttling. MinClientVersion (int): Client protocol version. MaxClientVersion (int): Client protocol version. Events: \* ``apiStart`` () \* ``apiEnd`` () \* ``apiError`` (errorMsg: str) \* ``throttleStart`` () \* ``throttleEnd`` () """events = ("apiStart", "apiEnd", "apiError", "throttleStart", "throttleEnd")MaxRequests = 45RequestsInterval = 1MinClientVersion = 157MaxClientVersion = 178DISCONNECTED, CONNECTING, CONNECTED = range(3)def \_\_init\_\_(self, wrapper):self.wrapper = wrapperself.decoder = Decoder(wrapper, 0)self.apiStart = Event("apiStart")self.apiEnd = Event("apiEnd")self.apiError = Event("apiError")self.throttleStart = Event("throttleStart")self.throttleEnd = Event("throttleEnd")self.\_logger = logging.getLogger("ib\_async.client")self.conn = Connection()self.conn.hasData += self.\_onSocketHasDataself.conn.disconnected += self.\_onSocketDisconnected# extra optional wrapper methodsself.\_priceSizeTick = getattr(wrapper, "priceSizeTick", None)self.\_tcpDataArrived = getattr(wrapper, "tcpDataArrived", None)self.\_tcpDataProcessed = getattr(wrapper, "tcpDataProcessed", None)self.host = ""self.port = -1self.clientId = -1self.optCapab = ""self.connectOptions = b""self.reset()
[[docs]](../../api.html#ib_async.client.Client.reset)def reset(self):self.connState = Client.DISCONNECTEDself.\_apiReady = Falseself.\_serverVersion = 0self.\_data = b""self.\_hasReqId = Falseself.\_reqIdSeq = 0self.\_accounts = []self.\_startTime = time.time()self.\_numBytesRecv = 0self.\_numMsgRecv = 0self.\_isThrottling = Falseself.\_msgQ: deque[str] = deque()self.\_timeQ: deque[float] = deque()
[[docs]](../../api.html#ib_async.client.Client.serverVersion)def serverVersion(self) -\> int:return self.\_serverVersion
[[docs]](../../api.html#ib_async.client.Client.run)def run(self):loop = getLoop()loop.run\_forever()
[[docs]](../../api.html#ib_async.client.Client.isConnected)def isConnected(self):return self.connState == Client.CONNECTED
[[docs]](../../api.html#ib_async.client.Client.isReady)def isReady(self) -\> bool:"""Is the API connection up and running?"""return self.\_apiReady
[[docs]](../../api.html#ib_async.client.Client.connectionStats)def connectionStats(self) -\> ConnectionStats:"""Get statistics about the connection."""if not self.isReady():raise ConnectionError("Not connected")return ConnectionStats(self.\_startTime,time.time() - self.\_startTime,self.\_numBytesRecv,self.conn.numBytesSent,self.\_numMsgRecv,self.conn.numMsgSent,)
[[docs]](../../api.html#ib_async.client.Client.getReqId)def getReqId(self) -\> int:"""Get new request ID."""if not self.isReady():raise ConnectionError("Not connected")newId = self.\_reqIdSeqself.\_reqIdSeq += 1return newId
[[docs]](../../api.html#ib_async.client.Client.updateReqId)def updateReqId(self, minReqId):"""Update the next reqId to be at least ``minReqId``."""self.\_reqIdSeq = max(self.\_reqIdSeq, minReqId)
[[docs]](../../api.html#ib_async.client.Client.getAccounts)def getAccounts(self) -\> list[str]:"""Get the list of account names that are under management."""if not self.isReady():raise ConnectionError("Not connected")return self.\_accounts
[[docs]](../../api.html#ib_async.client.Client.setConnectOptions)def setConnectOptions(self, connectOptions: str):""" Set additional connect options. Args: connectOptions: Use "+PACEAPI" to use request-pacing built into TWS/gateway 974+ (obsolete). """self.connectOptions = connectOptions.encode()
[[docs]](../../api.html#ib_async.client.Client.connect)def connect(self, host: str, port: int, clientId: int, timeout: float | None = 2.0):""" Connect to a running TWS or IB gateway application. Args: host: Host name or IP address. port: Port number. clientId: ID number to use for this client; must be unique per connection. timeout: If establishing the connection takes longer than ``timeout`` seconds then the ``asyncio.TimeoutError`` exception is raised. Set to 0 to disable timeout. """run(self.connectAsync(host, port, clientId, timeout))
[[docs]](../../api.html#ib_async.client.Client.connectAsync)async def connectAsync(self, host, port, clientId, timeout=2.0):try:self.\_logger.info(f"Connecting to {host}:{port} with clientId {clientId}...")self.host = hostself.port = int(port)self.clientId = int(clientId)self.connState = Client.CONNECTINGtimeout = timeout or Noneawait asyncio.wait\_for(self.conn.connectAsync(host, port), timeout)self.\_logger.info("Connected")msg = b"API\0" + self.\_prefix(b"v%d..%d%s"% (self.MinClientVersion,self.MaxClientVersion,b" " + self.connectOptions if self.connectOptions else b"",))self.conn.sendMsg(msg)await asyncio.wait\_for(self.apiStart, timeout)self.\_logger.info("API connection ready")except BaseException as e:self.disconnect()msg = f"API connection failed: {e!r}"self.\_logger.error(msg)self.apiError.emit(msg)if isinstance(e, ConnectionRefusedError):self.\_logger.error("Make sure API port on TWS/IBG is open")raise
[[docs]](../../api.html#ib_async.client.Client.disconnect)def disconnect(self):"""Disconnect from IB connection."""self.\_logger.info("Disconnecting")self.connState = Client.DISCONNECTEDself.conn.disconnect()self.reset()
[[docs]](../../api.html#ib_async.client.Client.send)def send(self, \*fields, makeEmpty=True):"""Serialize and send the given fields using the IB socket protocol. if 'makeEmpty' is True (default), then the IBKR values representing "no value" become the empty string."""if not self.isConnected():raise ConnectionError("Not connected")# fmt: offFORMAT\_HANDLERS: dict[Any, Callable[[Any], str]] = {# Contracts are formatted in IBKR null delimiter formatContract: lambda c: "\0".join([str(f)for f in (c.conId,c.symbol,c.secType,c.lastTradeDateOrContractMonth,c.strike,c.right,c.multiplier,c.exchange,c.primaryExchange,c.currency,c.localSymbol,c.tradingClass,)]),# Float conversion has 3 stages:# - Convert 'IBKR unset' double to empty (if requested)# - Convert infinity to 'Infinite' string (if appropriate)# - else, convert float to string normallyfloat: lambda f: ""if (makeEmpty and f == UNSET\_DOUBLE)else ("Infinite" if (f == math.inf) else str(f)),# Int conversion has 2 stages:# - Convert 'IBKR unset' to empty (if requested)# - else, convert int to string normallyint: lambda f: "" if makeEmpty and f == UNSET\_INTEGER else str(f),# None is always just an empty string.# (due to a quirk of Python, 'type(None)' is how you properly generate the NoneType value)type(None): lambda \_: "",# Strings are always stringsstr: lambda s: s,# Bools become strings "1" or "0"bool: lambda b: "1" if b else "0",# Lists of tags become semicolon-appended KV pairslist: lambda lst: "".join([f"{v.tag}={v.value};" for v in lst]),}# fmt: on# start of new messagemsg = io.StringIO()for field in fields:# Fetch type converter for this field (falls back to 'str(field)' as a default for unmatched types)# (extra `isinstance()` wrapper needed here because Contract subclasses are their own type, but we want# to only match against the Contract parent class for formatting operations)convert = FORMAT\_HANDLERS.get(Contract if isinstance(field, Contract) else type(field), str)# Convert field to IBKR protocol string parts = convert(field)# Append converted IBKR protocol string to message buffermsg.write(s)msg.write("\0")generated = msg.getvalue()self.sendMsg(generated)
[[docs]](../../api.html#ib_async.client.Client.sendMsg)def sendMsg(self, msg: str):loop = getLoop()t = loop.time()times = self.\_timeQmsgs = self.\_msgQwhile times and t - times[0] \> self.RequestsInterval:times.popleft()if msg:msgs.append(msg)while msgs and (len(times) \< self.MaxRequests or not self.MaxRequests):msg = msgs.popleft()self.conn.sendMsg(self.\_prefix(msg.encode()))times.append(t)if self.\_logger.isEnabledFor(logging.DEBUG):self.\_logger.debug("\>\>\> %s", msg[:-1].replace("\0", ","))if msgs:if not self.\_isThrottling:self.\_isThrottling = Trueself.throttleStart.emit()self.\_logger.debug("Started to throttle requests")loop.call\_at(times[0] + self.RequestsInterval, self.sendMsg, None)else:if self.\_isThrottling:self.\_isThrottling = Falseself.throttleEnd.emit()self.\_logger.debug("Stopped to throttle requests")
def \_prefix(self, msg):# prefix a message with its lengthreturn struct.pack("\>I", len(msg)) + msgdef \_onSocketHasData(self, data):debug = self.\_logger.isEnabledFor(logging.DEBUG)if self.\_tcpDataArrived:self.\_tcpDataArrived()self.\_data += dataself.\_numBytesRecv += len(data)while True:if len(self.\_data) \<= 4:break# 4 byte prefix tells the message lengthmsgEnd = 4 + struct.unpack("\>I", self.\_data[:4])[0]if len(self.\_data) \< msgEnd:# insufficient data for nowbreakmsg = self.\_data[4:msgEnd].decode(errors="backslashreplace")self.\_data = self.\_data[msgEnd:]fields = msg.split("\0")fields.pop()# pop off last empty elementself.\_numMsgRecv += 1if debug:self.\_logger.debug("\<\<\< %s", ",".join(fields))if not self.\_serverVersion and len(fields) == 2:# this concludes the handshakeversion, \_connTime = fieldsself.\_serverVersion = int(version)if self.\_serverVersion \< self.MinClientVersion:self.\_onSocketDisconnected("TWS/gateway version must be \>= 972")returnself.decoder.serverVersion = self.\_serverVersionself.connState = Client.CONNECTEDself.startApi()self.wrapper.connectAck()self.\_logger.info(f"Logged on to server version {self.\_serverVersion}")else:if not self.\_apiReady:# snoop for nextValidId and managedAccounts response,# when both are in then the client is readymsgId = int(fields[0])if msgId == 9:\_, \_, validId = fieldsself.updateReqId(int(validId))self.\_hasReqId = Trueelif msgId == 15:\_, \_, accts = fieldsself.\_accounts = [a for a in accts.split(",") if a]if self.\_hasReqId and self.\_accounts:self.\_apiReady = Trueself.apiStart.emit()# decode and handle the messageself.decoder.interpret(fields)if self.\_tcpDataProcessed:self.\_tcpDataProcessed()def \_onSocketDisconnected(self, msg):wasReady = self.isReady()if not self.isConnected():self.\_logger.info("Disconnected.")elif not msg:msg = "Peer closed connection."if not wasReady:msg += f" clientId {self.clientId} already in use?"if msg:self.\_logger.error(msg)self.apiError.emit(msg)self.wrapper.setEventsDone()if wasReady:self.wrapper.connectionClosed()self.reset()if wasReady:self.apiEnd.emit()# client request methods# the message type id is sent first, often followed by a version number
[[docs]](../../api.html#ib_async.client.Client.reqMktData)def reqMktData(self,reqId,contract,genericTickList,snapshot,regulatorySnapshot,mktDataOptions,):fields = [1, 11, reqId, contract]if contract.secType == "BAG":legs = contract.comboLegs or []fields += [len(legs)]for leg in legs:fields += [leg.conId, leg.ratio, leg.action, leg.exchange]dnc = contract.deltaNeutralContractif dnc:fields += [True, dnc.conId, dnc.delta, dnc.price]else:fields += [False]fields += [genericTickList, snapshot, regulatorySnapshot, mktDataOptions]self.send(\*fields)
[[docs]](../../api.html#ib_async.client.Client.cancelMktData)def cancelMktData(self, reqId):self.send(2, 2, reqId)
[[docs]](../../api.html#ib_async.client.Client.placeOrder)def placeOrder(self, orderId, contract, order):version = self.serverVersion()# IBKR API BUG FIX:# IBKR sometimes back-populates the 'volatility' field into live orders, but then if we try to# modify an order using cached order objects, IBKR rejects modifications because 'volatility'# is not allowed to be set (even though \_they\_ added it to our previously submitted order).# Solution: if an order is NOT a VOL order, delete the 'volatility' value to prevent this error.if not order.orderType.startswith("VOL"):# ONLY volatility orders can have 'volatility' set when sending API data.order.volatility = None# The IBKR API protocol is just a series of in-order arguments denoted by position.# The upstream API parses all fields based on the first value (the message type).fields = [3,# PLACE\_ORDER message typeorderId,contract,contract.secIdType,contract.secId,order.action,order.totalQuantity,order.orderType,order.lmtPrice,order.auxPrice,order.tif,order.ocaGroup,order.account,order.openClose,order.origin,order.orderRef,order.transmit,order.parentId,order.blockOrder,order.sweepToFill,order.displaySize,order.triggerMethod,order.outsideRth,order.hidden,]if contract.secType == "BAG":legs = contract.comboLegs or []fields += [len(legs)]for leg in legs:fields += [leg.conId,leg.ratio,leg.action,leg.exchange,leg.openClose,leg.shortSaleSlot,leg.designatedLocation,leg.exemptCode,]legs = order.orderComboLegs or []fields += [len(legs)]for leg in legs:fields += [leg.price]params = order.smartComboRoutingParams or []fields += [len(params)]for param in params:fields += [param.tag, param.value]fields += ["",order.discretionaryAmt,order.goodAfterTime,order.goodTillDate,order.faGroup,order.faMethod,order.faPercentage,]if version \< 177:fields += [order.faProfile]fields += [order.modelCode,order.shortSaleSlot,order.designatedLocation,order.exemptCode,order.ocaType,order.rule80A,order.settlingFirm,order.allOrNone,order.minQty,order.percentOffset,order.eTradeOnly,# always Falseorder.firmQuoteOnly,# always Falseorder.nbboPriceCap,# always UNSETorder.auctionStrategy,order.startingPrice,order.stockRefPrice,order.delta,order.stockRangeLower,order.stockRangeUpper,order.overridePercentageConstraints,order.volatility,order.volatilityType,order.deltaNeutralOrderType,order.deltaNeutralAuxPrice,]if order.deltaNeutralOrderType:fields += [order.deltaNeutralConId,order.deltaNeutralSettlingFirm,order.deltaNeutralClearingAccount,order.deltaNeutralClearingIntent,order.deltaNeutralOpenClose,order.deltaNeutralShortSale,order.deltaNeutralShortSaleSlot,order.deltaNeutralDesignatedLocation,]fields += [order.continuousUpdate,order.referencePriceType,order.trailStopPrice,order.trailingPercent,order.scaleInitLevelSize,order.scaleSubsLevelSize,order.scalePriceIncrement,]if 0 \< order.scalePriceIncrement \< UNSET\_DOUBLE:fields += [order.scalePriceAdjustValue,order.scalePriceAdjustInterval,order.scaleProfitOffset,order.scaleAutoReset,order.scaleInitPosition,order.scaleInitFillQty,order.scaleRandomPercent,]fields += [order.scaleTable,order.activeStartTime,order.activeStopTime,order.hedgeType,]if order.hedgeType:fields += [order.hedgeParam]fields += [order.optOutSmartRouting,order.clearingAccount,order.clearingIntent,order.notHeld,]dnc = contract.deltaNeutralContractif dnc:fields += [True, dnc.conId, dnc.delta, dnc.price]else:fields += [False]fields += [order.algoStrategy]if order.algoStrategy:params = order.algoParams or []fields += [len(params)]for param in params:fields += [param.tag, param.value]fields += [order.algoId,order.whatIf,order.orderMiscOptions,order.solicited,order.randomizeSize,order.randomizePrice,]if order.orderType in {"PEG BENCH", "PEGBENCH"}:fields += [order.referenceContractId,order.isPeggedChangeAmountDecrease,order.peggedChangeAmount,order.referenceChangeAmount,order.referenceExchangeId,]fields += [len(order.conditions)]if order.conditions:for cond in order.conditions:fields += dataclassAsTuple(cond)fields += [order.conditionsIgnoreRth, order.conditionsCancelOrder]fields += [order.adjustedOrderType,order.triggerPrice,order.lmtPriceOffset,order.adjustedStopPrice,order.adjustedStopLimitPrice,order.adjustedTrailingAmount,order.adjustableTrailingUnit,order.extOperator,order.softDollarTier.name,order.softDollarTier.val,order.cashQty,order.mifid2DecisionMaker,order.mifid2DecisionAlgo,order.mifid2ExecutionTrader,order.mifid2ExecutionAlgo,order.dontUseAutoPriceForHedge,order.isOmsContainer,order.discretionaryUpToLimitPrice,order.usePriceMgmtAlgo,]if version \>= 158:fields += [order.duration]if version \>= 160:fields += [order.postToAts]if version \>= 162:fields += [order.autoCancelParent]if version \>= 166:fields += [order.advancedErrorOverride]if version \>= 169:fields += [order.manualOrderTime]if version \>= 170:if contract.exchange == "IBKRATS":fields += [order.minTradeQty]if order.orderType in {"PEG BEST", "PEGBEST"}:fields += [order.minCompeteSize, order.competeAgainstBestOffset]if order.competeAgainstBestOffset == math.inf:fields += [order.midOffsetAtWhole, order.midOffsetAtHalf]elif order.orderType in {"PEG MID", "PEGMID"}:fields += [order.midOffsetAtWhole, order.midOffsetAtHalf]self.send(\*fields)
[[docs]](../../api.html#ib_async.client.Client.cancelOrder)def cancelOrder(self, orderId, manualCancelOrderTime=""):fields = [4, 1, orderId]if self.serverVersion() \>= 169:fields += [manualCancelOrderTime]self.send(\*fields)
[[docs]](../../api.html#ib_async.client.Client.reqOpenOrders)def reqOpenOrders(self):self.send(5, 1)
[[docs]](../../api.html#ib_async.client.Client.reqAccountUpdates)def reqAccountUpdates(self, subscribe, acctCode):self.send(6, 2, subscribe, acctCode)
[[docs]](../../api.html#ib_async.client.Client.reqExecutions)def reqExecutions(self, reqId, execFilter):self.send(7,3,reqId,execFilter.clientId,execFilter.acctCode,execFilter.time,execFilter.symbol,execFilter.secType,execFilter.exchange,execFilter.side,)
[[docs]](../../api.html#ib_async.client.Client.reqIds)def reqIds(self, numIds):self.send(8, 1, numIds)
[[docs]](../../api.html#ib_async.client.Client.reqContractDetails)def reqContractDetails(self, reqId, contract):fields = [9,8,reqId,contract,contract.includeExpired,contract.secIdType,contract.secId,]if self.serverVersion() \>= 176:fields += [contract.issuerId]self.send(\*fields)
[[docs]](../../api.html#ib_async.client.Client.reqMktDepth)def reqMktDepth(self, reqId, contract, numRows, isSmartDepth, mktDepthOptions):self.send(10,5,reqId,contract.conId,contract.symbol,contract.secType,contract.lastTradeDateOrContractMonth,contract.strike,contract.right,contract.multiplier,contract.exchange,contract.primaryExchange,contract.currency,contract.localSymbol,contract.tradingClass,numRows,isSmartDepth,mktDepthOptions,)
[[docs]](../../api.html#ib_async.client.Client.cancelMktDepth)def cancelMktDepth(self, reqId, isSmartDepth):self.send(11, 1, reqId, isSmartDepth)
[[docs]](../../api.html#ib_async.client.Client.reqNewsBulletins)def reqNewsBulletins(self, allMsgs):self.send(12, 1, allMsgs)
[[docs]](../../api.html#ib_async.client.Client.cancelNewsBulletins)def cancelNewsBulletins(self):self.send(13, 1)
[[docs]](../../api.html#ib_async.client.Client.setServerLogLevel)def setServerLogLevel(self, logLevel):self.send(14, 1, logLevel)
[[docs]](../../api.html#ib_async.client.Client.reqAutoOpenOrders)def reqAutoOpenOrders(self, bAutoBind):self.send(15, 1, bAutoBind)
[[docs]](../../api.html#ib_async.client.Client.reqAllOpenOrders)def reqAllOpenOrders(self):self.send(16, 1)
[[docs]](../../api.html#ib_async.client.Client.reqManagedAccts)def reqManagedAccts(self):self.send(17, 1)
[[docs]](../../api.html#ib_async.client.Client.requestFA)def requestFA(self, faData):self.send(18, 1, faData)
[[docs]](../../api.html#ib_async.client.Client.replaceFA)def replaceFA(self, reqId, faData, cxml):self.send(19, 1, faData, cxml, reqId)
[[docs]](../../api.html#ib_async.client.Client.reqHistoricalData)def reqHistoricalData(self,reqId,contract,endDateTime,durationStr,barSizeSetting,whatToShow,useRTH,formatDate,keepUpToDate,chartOptions,):fields = [20,reqId,contract,contract.includeExpired,endDateTime,barSizeSetting,durationStr,useRTH,whatToShow,formatDate,]if contract.secType == "BAG":legs = contract.comboLegs or []fields += [len(legs)]for leg in legs:fields += [leg.conId, leg.ratio, leg.action, leg.exchange]fields += [keepUpToDate, chartOptions]self.send(\*fields)
[[docs]](../../api.html#ib_async.client.Client.exerciseOptions)def exerciseOptions(self, reqId, contract, exerciseAction, exerciseQuantity, account, override):self.send(21,2,reqId,contract.conId,contract.symbol,contract.secType,contract.lastTradeDateOrContractMonth,contract.strike,contract.right,contract.multiplier,contract.exchange,contract.currency,contract.localSymbol,contract.tradingClass,exerciseAction,exerciseQuantity,account,override,)
[[docs]](../../api.html#ib_async.client.Client.reqScannerSubscription)def reqScannerSubscription(self,reqId,subscription,scannerSubscriptionOptions,scannerSubscriptionFilterOptions,):sub = subscriptionself.send(22,reqId,sub.numberOfRows,sub.instrument,sub.locationCode,sub.scanCode,sub.abovePrice,sub.belowPrice,sub.aboveVolume,sub.marketCapAbove,sub.marketCapBelow,sub.moodyRatingAbove,sub.moodyRatingBelow,sub.spRatingAbove,sub.spRatingBelow,sub.maturityDateAbove,sub.maturityDateBelow,sub.couponRateAbove,sub.couponRateBelow,sub.excludeConvertible,sub.averageOptionVolumeAbove,sub.scannerSettingPairs,sub.stockTypeFilter,scannerSubscriptionFilterOptions,scannerSubscriptionOptions,)
[[docs]](../../api.html#ib_async.client.Client.cancelScannerSubscription)def cancelScannerSubscription(self, reqId):self.send(23, 1, reqId)
[[docs]](../../api.html#ib_async.client.Client.reqScannerParameters)def reqScannerParameters(self):self.send(24, 1)
[[docs]](../../api.html#ib_async.client.Client.cancelHistoricalData)def cancelHistoricalData(self, reqId):self.send(25, 1, reqId)
[[docs]](../../api.html#ib_async.client.Client.reqCurrentTime)def reqCurrentTime(self):self.send(49, 1)
[[docs]](../../api.html#ib_async.client.Client.reqRealTimeBars)def reqRealTimeBars(self, reqId, contract, barSize, whatToShow, useRTH, realTimeBarsOptions):self.send(50, 3, reqId, contract, barSize, whatToShow, useRTH, realTimeBarsOptions)
[[docs]](../../api.html#ib_async.client.Client.cancelRealTimeBars)def cancelRealTimeBars(self, reqId):self.send(51, 1, reqId)
[[docs]](../../api.html#ib_async.client.Client.reqFundamentalData)def reqFundamentalData(self, reqId, contract, reportType, fundamentalDataOptions):options = fundamentalDataOptions or []self.send(52,2,reqId,contract.conId,contract.symbol,contract.secType,contract.exchange,contract.primaryExchange,contract.currency,contract.localSymbol,reportType,len(options),options,)
[[docs]](../../api.html#ib_async.client.Client.cancelFundamentalData)def cancelFundamentalData(self, reqId):self.send(53, 1, reqId)
[[docs]](../../api.html#ib_async.client.Client.calculateImpliedVolatility)def calculateImpliedVolatility(self, reqId, contract, optionPrice, underPrice, implVolOptions):self.send(54,3,reqId,contract,optionPrice,underPrice,len(implVolOptions),implVolOptions,)
[[docs]](../../api.html#ib_async.client.Client.calculateOptionPrice)def calculateOptionPrice(self, reqId, contract, volatility, underPrice, optPrcOptions):self.send(55,3,reqId,contract,volatility,underPrice,len(optPrcOptions),optPrcOptions,)
[[docs]](../../api.html#ib_async.client.Client.cancelCalculateImpliedVolatility)def cancelCalculateImpliedVolatility(self, reqId):self.send(56, 1, reqId)
[[docs]](../../api.html#ib_async.client.Client.cancelCalculateOptionPrice)def cancelCalculateOptionPrice(self, reqId):self.send(57, 1, reqId)
[[docs]](../../api.html#ib_async.client.Client.reqGlobalCancel)def reqGlobalCancel(self):self.send(58, 1)
[[docs]](../../api.html#ib_async.client.Client.reqMarketDataType)def reqMarketDataType(self, marketDataType):self.send(59, 1, marketDataType)
[[docs]](../../api.html#ib_async.client.Client.reqPositions)def reqPositions(self):self.send(61, 1)
[[docs]](../../api.html#ib_async.client.Client.reqAccountSummary)def reqAccountSummary(self, reqId, groupName, tags):self.send(62, 1, reqId, groupName, tags)
[[docs]](../../api.html#ib_async.client.Client.cancelAccountSummary)def cancelAccountSummary(self, reqId):self.send(63, 1, reqId)
[[docs]](../../api.html#ib_async.client.Client.cancelPositions)def cancelPositions(self):self.send(64, 1)
[[docs]](../../api.html#ib_async.client.Client.verifyRequest)def verifyRequest(self, apiName, apiVersion):self.send(65, 1, apiName, apiVersion)
[[docs]](../../api.html#ib_async.client.Client.verifyMessage)def verifyMessage(self, apiData):self.send(66, 1, apiData)
[[docs]](../../api.html#ib_async.client.Client.queryDisplayGroups)def queryDisplayGroups(self, reqId):self.send(67, 1, reqId)
[[docs]](../../api.html#ib_async.client.Client.subscribeToGroupEvents)def subscribeToGroupEvents(self, reqId, groupId):self.send(68, 1, reqId, groupId)
[[docs]](../../api.html#ib_async.client.Client.updateDisplayGroup)def updateDisplayGroup(self, reqId, contractInfo):self.send(69, 1, reqId, contractInfo)
[[docs]](../../api.html#ib_async.client.Client.unsubscribeFromGroupEvents)def unsubscribeFromGroupEvents(self, reqId):self.send(70, 1, reqId)
[[docs]](../../api.html#ib_async.client.Client.startApi)def startApi(self):self.send(71, 2, self.clientId, self.optCapab)
[[docs]](../../api.html#ib_async.client.Client.verifyAndAuthRequest)def verifyAndAuthRequest(self, apiName, apiVersion, opaqueIsvKey):self.send(72, 1, apiName, apiVersion, opaqueIsvKey)
[[docs]](../../api.html#ib_async.client.Client.verifyAndAuthMessage)def verifyAndAuthMessage(self, apiData, xyzResponse):self.send(73, 1, apiData, xyzResponse)
[[docs]](../../api.html#ib_async.client.Client.reqPositionsMulti)def reqPositionsMulti(self, reqId, account, modelCode):self.send(74, 1, reqId, account, modelCode)
[[docs]](../../api.html#ib_async.client.Client.cancelPositionsMulti)def cancelPositionsMulti(self, reqId):self.send(75, 1, reqId)
[[docs]](../../api.html#ib_async.client.Client.reqAccountUpdatesMulti)def reqAccountUpdatesMulti(self, reqId, account, modelCode, ledgerAndNLV):self.send(76, 1, reqId, account, modelCode, ledgerAndNLV)
[[docs]](../../api.html#ib_async.client.Client.cancelAccountUpdatesMulti)def cancelAccountUpdatesMulti(self, reqId):self.send(77, 1, reqId)
[[docs]](../../api.html#ib_async.client.Client.reqSecDefOptParams)def reqSecDefOptParams(self,reqId,underlyingSymbol,futFopExchange,underlyingSecType,underlyingConId,):self.send(78,reqId,underlyingSymbol,futFopExchange,underlyingSecType,underlyingConId,)
[[docs]](../../api.html#ib_async.client.Client.reqSoftDollarTiers)def reqSoftDollarTiers(self, reqId):self.send(79, reqId)
[[docs]](../../api.html#ib_async.client.Client.reqFamilyCodes)def reqFamilyCodes(self):self.send(80)
[[docs]](../../api.html#ib_async.client.Client.reqMatchingSymbols)def reqMatchingSymbols(self, reqId, pattern):self.send(81, reqId, pattern)
[[docs]](../../api.html#ib_async.client.Client.reqMktDepthExchanges)def reqMktDepthExchanges(self):self.send(82)
[[docs]](../../api.html#ib_async.client.Client.reqSmartComponents)def reqSmartComponents(self, reqId, bboExchange):self.send(83, reqId, bboExchange)
[[docs]](../../api.html#ib_async.client.Client.reqNewsArticle)def reqNewsArticle(self, reqId, providerCode, articleId, newsArticleOptions):self.send(84, reqId, providerCode, articleId, newsArticleOptions)
[[docs]](../../api.html#ib_async.client.Client.reqNewsProviders)def reqNewsProviders(self):self.send(85)
[[docs]](../../api.html#ib_async.client.Client.reqHistoricalNews)def reqHistoricalNews(self,reqId,conId,providerCodes,startDateTime,endDateTime,totalResults,historicalNewsOptions,):self.send(86,reqId,conId,providerCodes,startDateTime,endDateTime,totalResults,historicalNewsOptions,)
[[docs]](../../api.html#ib_async.client.Client.reqHeadTimeStamp)def reqHeadTimeStamp(self, reqId, contract, whatToShow, useRTH, formatDate):self.send(87, reqId, contract, contract.includeExpired, useRTH, whatToShow, formatDate)
[[docs]](../../api.html#ib_async.client.Client.reqHistogramData)def reqHistogramData(self, tickerId, contract, useRTH, timePeriod):self.send(88, tickerId, contract, contract.includeExpired, useRTH, timePeriod)
[[docs]](../../api.html#ib_async.client.Client.cancelHistogramData)def cancelHistogramData(self, tickerId):self.send(89, tickerId)
[[docs]](../../api.html#ib_async.client.Client.cancelHeadTimeStamp)def cancelHeadTimeStamp(self, reqId):self.send(90, reqId)
[[docs]](../../api.html#ib_async.client.Client.reqMarketRule)def reqMarketRule(self, marketRuleId):self.send(91, marketRuleId)
[[docs]](../../api.html#ib_async.client.Client.reqPnL)def reqPnL(self, reqId, account, modelCode):self.send(92, reqId, account, modelCode)
[[docs]](../../api.html#ib_async.client.Client.cancelPnL)def cancelPnL(self, reqId):self.send(93, reqId)
[[docs]](../../api.html#ib_async.client.Client.reqPnLSingle)def reqPnLSingle(self, reqId, account, modelCode, conid):self.send(94, reqId, account, modelCode, conid)
[[docs]](../../api.html#ib_async.client.Client.cancelPnLSingle)def cancelPnLSingle(self, reqId):self.send(95, reqId)
[[docs]](../../api.html#ib_async.client.Client.reqHistoricalTicks)def reqHistoricalTicks(self,reqId,contract,startDateTime,endDateTime,numberOfTicks,whatToShow,useRth,ignoreSize,miscOptions,):self.send(96,reqId,contract,contract.includeExpired,startDateTime,endDateTime,numberOfTicks,whatToShow,useRth,ignoreSize,miscOptions,)
[[docs]](../../api.html#ib_async.client.Client.reqTickByTickData)def reqTickByTickData(self, reqId, contract, tickType, numberOfTicks, ignoreSize):self.send(97, reqId, contract, tickType, numberOfTicks, ignoreSize)
[[docs]](../../api.html#ib_async.client.Client.cancelTickByTickData)def cancelTickByTickData(self, reqId):self.send(98, reqId)
[[docs]](../../api.html#ib_async.client.Client.reqCompletedOrders)def reqCompletedOrders(self, apiOnly):self.send(99, apiOnly)
[[docs]](../../api.html#ib_async.client.Client.reqWshMetaData)def reqWshMetaData(self, reqId):self.send(100, reqId)
[[docs]](../../api.html#ib_async.client.Client.cancelWshMetaData)def cancelWshMetaData(self, reqId):self.send(101, reqId)
[[docs]](../../api.html#ib_async.client.Client.reqWshEventData)def reqWshEventData(self, reqId, data: WshEventData):fields = [102, reqId, data.conId]if self.serverVersion() \>= 171:fields += [data.filter,data.fillWatchlist,data.fillPortfolio,data.fillCompetitors,]if self.serverVersion() \>= 173:fields += [data.startDate, data.endDate, data.totalLimit]self.send(\*fields, makeEmpty=False)
[[docs]](../../api.html#ib_async.client.Client.cancelWshEventData)def cancelWshEventData(self, reqId):self.send(103, reqId)
[[docs]](../../api.html#ib_async.client.Client.reqUserInfo)def reqUserInfo(self, reqId):self.send(104, reqId)