def run(self) -> None: """Fire-up dataloding function.""" log.debug(f'running {self.func}') try: util.run(self.func()) except Exception as e: log.error(f'ignoring schedule exception: {e}')
def connect(self, host, port, clientId, timeout=2): """ Connect to TWS/IBG at given host and port and with a clientId that is not in use elsewhere. When timeout is not zero, asyncio.TimeoutError is raised if the connection is not established within the timeout period. """ util.run(self.connectAsync(host, port, clientId, timeout))
def run(self) -> None: """Interface method, providing entry point to run simulations.""" log.debug(f'Market initialized with reboot={self._reboot}') log.debug(f'commission levels for instruments: {self.commissions}') log.debug(f'minTicks for instruments: {self.ticks}') try: getattr(self, 'manager') except AttributeError: raise AttributeError('No manager object provided to Market') util.run(self._run()) self.post_mortem()
def waitOnUpdate(self, timeout=0): self._clearPendingTickers() self._waitingOnUpdate = True coro = self._updateEv.wait() if timeout: try: util.run(asyncio.wait_for(coro, timeout)) except asyncio.TimeoutError: pass else: util.run(coro) self._waitingOnUpdate = False self._emitPendingTickers() return True
def connect(self, host: str, port: int, clientId: int, timeout: float = 2): """ 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. """ util.run(self.connectAsync(host, port, clientId, timeout))
def reqMktData(self, contract, what=None): '''Creates a MarketData subscription Params: - contract: a ib.ext.Contract.Contract intance Returns: - a Queue the client can wait on to receive a RTVolume instance ''' # get a ticker/queue for identification/data delivery q = self.getTickerQueue() ticks = '233' # request RTVOLUME tick delivered over tickString if contract.secType in ['CASH', 'CFD']: #self.iscash[tickerId] = True ticks = '' # cash markets do not get RTVOLUME if what == 'ASK': #self.iscash[tickerId] = 2 pass # q.put(None) # to kickstart backfilling # Can request 233 also for cash ... nothing will arrive md = MktData() q_ticks = queue.Queue() util.run(md.update_ticks(self.ib, contract, ticks, q_ticks)) while not q_ticks.empty(): ticker = q_ticks.get() for tick in ticker.ticks: # https://interactivebrokers.github.io/tws-api/tick_types.html if tick != self.last_tick: #last price #print(str(tick.time) +" >> " + str(tick.price)) self.last_tick = tick q.put(tick) return q
def terminate(self): """Terminate TWS/IBG.""" util.run(self.terminateAsync())
def stop(self): """Cleanly shutdown TWS/IBG.""" util.run(self.stopAsync())
def start(self): """Launch TWS/IBG.""" util.run(self.startAsync())
def onStarted(self, *args): log.debug(f'Starting: {args}') util.run(self.func())
def save(self) -> None: util.run(self._save())
def write_to_file(self, data: Dict[str, Any]) -> None: util.run(self._write_to_file(data))