class BrokerConnection(metaclass=Singleton): def __init__(self): self.ib = IB() def connect(self, host, port, client_id, callback=None): self.ib.connect(host, port, clientId=client_id) if callback: self.ib.connectedEvent += callback def disconnect(self): self.ib.disconnect() def isConnected(self): return self.ib.isConnected() def positions(self): return [ pos for pos in self.ib.positions() if pos.contract.secType == 'OPT' ] def reqMatchingSymbols(self, text_to_search): ''' Function IBApi::EClient::reqMatchingSymbols is available to search for stock contracts. The input can be either the first few letters of the ticker symbol, or for longer strings, a character sequence matching a word in the security name. https://interactivebrokers.github.io/tws-api/matching_symbols.html ''' return self.ib.reqMatchingSymbols(text_to_search) def getOptionChainContracts(self, contract): chain = self.ib.reqSecDefOptParams(contract.symbol, contract.exchange, contract.secType, contract.conId) qChain = self.ib.qualifyContracts(chain) #return util.df(qChain) return qChain
# ib.cancelRealTimeBars(bars) # # # from ib_insync import * # # from IPython.display import display, clear_output # # # util.startLoop() # # # ib = IB() # # ib.connect('127.0.0.1', 7497, clientId=1) # # # contract = Stock('AAPL', 'SMART', 'USD') # # # ib.reqHeadTimeStamp(contract, whatToShow='TRADES', useRTH=True) # # # bars = ib.reqHistoricalData( # # contract, # # endDateTime='', # # durationStr='1 D', # # barSizeSetting='5 mins', # # whatToShow='TRADES', # # useRTH=True, # # formatDate=1) # # # df = util.df(bars) # # # display(df.tail()) ib = IB() ib.connect('127.0.0.1', 5001, clientId=50) symbols = ib.reqMatchingSymbols("SFCU1") print(symbols)