def run(self): self.logger.info("start...") while True: if TradeTime.is_market_open() or datetime.datetime.now( ).minute == 0: try: self.collect_data() except Exception as e: self.logger.error('Trace: ' + traceback.format_exc()) self.logger.error(str(e)) else: time.sleep(1)
def run(self): self.logger.info("start...") while True: if TradeTime.is_market_open(): try: for symbol in self.symbols: self.collect_data(symbol) self.logger.info('Check missing data...') count = self.equity_realtime_dao.add_missing_data_in_real_time( symbol) if count is not None and count > 0: self.logger.info( 'Filled {} records...'.format(count)) except Exception as e: self.logger.error('Trace: ' + traceback.format_exc()) self.logger.error(str(e)) time.sleep(1) else: self.logger.info('market not open...') time.sleep(10)
def listener(strategy_name, market_open_only=True): """ :param strategy_name: :param market_open_only: if you want handle function run every minutes, even if the market is not open, give it false. :return: """ schedule_functions = Container.get_schedule_functions(strategy_name) handle_function = Container.get_handle_function(strategy_name) last_minute = datetime.datetime.now().minute - 1 while True: if Container.context.terminate_p: break now = datetime.datetime.now() if now.minute == last_minute: time.sleep(1) continue else: logger = StrategyRunner.get_logger() # logger.info('check schedule functions...') for schedule_function in schedule_functions: try: schedule_function.run(now) except Exception as e: logger.error('Trace: ' + traceback.format_exc(), False) logger.error('Error: get action arguments failed:' + str(e)) if handle_function is not None: # logger.info('check handle functions...') if market_open_only is False or TradeTime.is_market_open(): try: handle_function() except Exception as e: logger.error('Trace: ' + traceback.format_exc(), False) logger.error( 'Error: get action arguments failed:' + str(e)) else: # logger.info('market not open...') Container.clear_loggers() last_minute = now.minute
def is_market_open(): return TradeTime.is_market_open()