Пример #1
0
def check_for_restart(r_time, IB, port, BQ):
    """Check if present time is within two minutes preceding the IB Gateway
    restart. If so, disconnect and reconnect after restart.

    Args:
        r_time: restart time. Should be in UTC'
        IB: IB Client.
        port: ib port
        BQ: BigQuery Client

    Returns:
        (re)connected IB Client
    """

    now = utc(datetime.now())
    lower_bound_td = timedelta(seconds=-120)
    upper_bound_td = timedelta(seconds=+30)
    lower_bound = now.replace(
        hour=r_time.hour, minute=r_time.minute, second=0,
        microsecond=0) + lower_bound_td
    upper_bound = now.replace(
        hour=r_time.hour, minute=r_time.minute, second=0,
        microsecond=0) + upper_bound_td

    if now > lower_bound and now < upper_bound:
        print(
            f'\n{"#" * 52}\n{"#" * 8} GOING TO SLEEP IN AWAIT OF RESTART {"#" * 8}\n{"#" * 52}\n'
        )
        IB.disconnect()
        util.sleep(420)
        IB = connect_ib(port, BQ)
    return IB
Пример #2
0
 def onDisconnectedEvent(self) -> None:
     """Initiate re-start when external watchdog manages api connection."""
     log.debug('Disconnected!')
     try:
         util.sleep(60)
     except Exception as e:
         log.debug(f'exception caught: {e}')
     log.debug('will attempt reconnection')
     self.connect()
Пример #3
0
 def connect(self) -> None:
     """Establish conection while not using watchdog."""
     log.debug('Connecting....')
     while not self.ib.isConnected():
         try:
             self.ib.connect(self.host, self.port, self.id)
         except ConnectionRefusedError as e:
             log.debug(f'While attepting reconnection: {e}')
             util.sleep(30)
             log.debug(f'post sleep...')
         except Exception as e:
             log.debug(f'Connection error: {e}')
Пример #4
0
 def process_back_data(self, date: Optional[datetime] = None) -> None:
     # flag needed on re-connect
     self.backfill = True
     for counter, bar in enumerate(self.bars[:-1]):
         # date given on reconnect only
         if (date and (bar.date > date)) or not date:
             self.aggregate(bar)
             # prevent from blocking too long
             if counter % 10000 == 0:
                 log.debug(f'releasing control {self.contract.localSymbol}')
                 util.sleep(0)
     log.debug(f'startup data generated for {self.contract.localSymbol}')
     self.backfill = False
     self.clear_buffer()
Пример #5
0
 def __call__(self,
              ib,
              contract: Contract,
              start_date: Optional[datetime] = None) -> None:
     self.ib = ib
     self.contract = contract
     log.debug(f'start_date: {start_date}')
     if start_date:
         # 30s time-window to retrieve data
         self.durationStr = f'{self.date_to_delta(start_date) + 30} S'
     while True:
         log.debug(f'Requesting bars for {self.contract.localSymbol}')
         self.bars = self.get_bars()
         if self.bars is not None:
             break
         else:
             util.sleep(5)
     log.debug(f'Bars received for {self.contract.localSymbol}')
     self.subscribe()
Пример #6
0
 def disconnect(self):
     if self.socket:
         self.socket.transport.close()
         self.socket = None
         util.sleep(0)
Пример #7
0
 def sleep(self, *args):
     # this makes sense only if run in asyncio
     util.sleep()