def getRealtimeInfo(code_list, entry_time): stocks = {'success': False} if entry_time == 3: return stocks try: LOGGER.info(f"Retry to grab realtime infomation ({entry_time}/2)") stocks = twstock.realtime.get(code_list) except KeyboardInterrupt: os._exit() except ConnectionError: LOGGER.warning("Connection Fail...") stocks = getRealtimeInfo(code_list, entry_time + 1) except: LOGGER.exception("Fail to Get realtime info") return stocks
def main(): LOGGER.info(f"Loading stocks ...") telegram_json = load_telegram_json() reporter = Reporter(telegram_json) df = GoodInfo.output_cold_stocks() codes = df.index.tolist() total = len(codes) LOGGER.info(f"Get {total} Cold stocks") reporter.push_to_telegram(f'從 GoodInfo 抓到 {total} 筆冷門股') while True: if not (TEST_MODE or is_taiwan_stock_opening()): LOGGER.info("非開市時間..") time.sleep(SLEEP_BETWEEN_EPOCH) continue LOGGER.info(f"Monitoring {total} Cold stocks") try: for cs in chunck_slice(codes, chunck_size=20): # stocks = twstock.realtime.get(cs) stocks = getRealtimeInfo(cs, 0) if stocks['success'] == False: print("Fail!!!!!!") continue for code in stocks: print(f'{code} ,', end='') if code not in cs or not stocks[code]['success']: continue if isStockBecomeHot(code, stocks[code], df): realtime = stocks[code]['realtime'] name = stocks[code]['info']['name'] LOGGER.info( f"**********[{name} {code}] is HOT (Price={realtime['latest_trade_price']}, Vol={realtime['accumulate_trade_volume']})!!!!***********" ) reporter.add(stocks[code]) print('') reporter.show() time.sleep(SLEEP_BETWEEN_REQ) except KeyboardInterrupt: os._exit() except: LOGGER.exception("\nSome Error happen...") print(f"\nSleep for {SLEEP_BETWEEN_EPOCH} seconds") time.sleep(SLEEP_BETWEEN_EPOCH)