def run(self, sleep_time): while True: try: self.logger.debug("enter run") if self.cal_client.is_trading_day(): if is_trading_time(): t_sleep_time = 1 if not self.subscriber.status(): self.subscriber.start() if 0 == self.init_index_info( ) and 0 == self.init_real_stock_info(): self.init_combination_info() else: self.logger.debug("enter stop subscriber") self.subscriber.stop() else: self.collect_stock_runtime_data() self.collect_combination_runtime_data() self.collect_index_runtime_data() self.animation_client.collect() else: t_sleep_time = sleep_time if self.subscriber.status(): self.subscriber.stop() else: t_sleep_time = sleep_time except Exception as e: #traceback.print_exc() self.logger.error(e) gevent.sleep(t_sleep_time)
def buy_new_stock(self, sleep_time): while True: try: if self.cal_client.is_trading_day(): if is_trading_time(): _today = datetime.now().strftime('%Y-%m-%d') time.sleep(sleep_time) if self.buy_succeed_date != _today: if not self.init(): raise Exception("trader login failed") n_list = self.get_new_stock_list() if len(n_list) == 0: logger.info("no new stock for %s." % _today) self.buy_succeed_date = _today succeed = True for stock in n_list: for i in range(len(self.traders)): ret, amount = self.traders[i].max_amounts(stock[0], stock[1]) if 0 == ret: ret, msg = self.traders[i].deal(stock[0], stock[1], amount, "B") if ret != 0 and ret != ct.ALREADY_BUY: logger.error("buy new stock:%s amount:%s for %s error, msg:%s, ret:%s" % (stock, amount, _today, msg, ret)) succeed = False elif ret == 0: logger.info("buy new stock:%s amount:%s for %s succeed." % (stock, amount, _today)) elif ret == ct.ALREADY_BUY: logger.info("already buy new stock:%s amount:%s for %s, no use to buy more." % (stock, amount, _today)) if True == succeed: self.buy_succeed_date = _today except Exception as e: self.traders = None logger.error(e) traceback.print_exc()
def run(self, sleep_time): while True: try: if self.cal_client.is_trading_day(): if is_trading_time() and not self.subscriber.status(): self.subscriber.start() self.init_combination_info() self.init_real_stock_info() elif is_trading_time() and self.subscriber.status(): self.collect_stock_runtime_data() self.collect_combination_runtime_data() self.animation_client.collect() elif not is_trading_time() and self.subscriber.status(): self.subscriber.stop() except Exception as e: logger.error(e) #traceback.print_exc() time.sleep(sleep_time)
def test_is_trading_time(self): dtime = datetime(2018, 5, 19, 10, 46, 52, 957084) self.assertTrue(common.is_trading_time(dtime)) dtime = datetime(2018, 5, 18, 19, 30, 00, 0) self.assertTrue(not common.is_trading_time(dtime))