def __get_redis(): redis_conf = CONFIG['redis'].dict_props redis_conf['socket_timeout'] = 3 pool = redis.ConnectionPool(**redis_conf) r = redis.Redis(connection_pool=pool) log('start Redis->' + ','.join(redis_conf)) return r
def waiting(): sleep_time = is_trading_time() if sleep_time > 0: log(f'距离开盘仍需{sleep_time}s') sleep(sleep_time) return True else: return False
def init_shares_config(self): filename_exists(self.filename.parent) if not self.filename.exists(): log(f'没有配置需要监控的股票,请配置') speak_thread(f'没有配置需要监控的股票,请配置') sleep(3600 * 24) self.shares = self.filename.lines() self.shares = [i.strip() for i in self.shares]
def __get_redis(): redis_conf = {'socket_timeout': 3} for line in Path('Bin/Config/Redis.txt').lines(): if ':' in line: _key, value = [j.strip() for j in line.split(':')] redis_conf[_key] = value if value != 'None' else None pool = redis.ConnectionPool(**redis_conf) r = redis.Redis(connection_pool=pool) log('start Redis->' + ','.join(redis_conf)) return r
def __get_socket(): socket_conf = CONFIG['socket'].dict_props srv = socket.socket() # 创建一个socket srv.bind((socket_conf['ip'], socket_conf['port'])) srv.listen(5) log(f"socket等待链接") connect_socket, _ = srv.accept() log(f"socket链接成功") return connect_socket
def loop(self): self.welcome() self.init_shares_config() # while 1: # self.run() # sleep(0.2) sleep(3) while 1: try: self.run() except: log(Except()) else: sleep(2)
def __get_socket(): hostname, port = [ i.strip() for i in Path('Bin/Config/Socket.txt').lines() if i.strip() ] port = int(port) srv = socket.socket() # 创建一个socket srv.bind(('', port)) srv.listen(5) log(f"socket等待{hostname}:{port}的链接") connect_socket, addr = srv.accept() print('链接IP', addr) log(f"socket链接成功") return connect_socket
def get_quote(): # 分发 r = Publish().get_tool() # 所有需要订阅的品种,190702增加去重功能 con = CONFIG['contracts'] # 断线重连,断线重连的时候也要判断是否在有效期内 _error = 'No log in' while 1: es2 = log_es2(r, con) if es2 is not None: # 如果es2成功 _error = loop(es2) else: # 如果es2不成功 _error = 'No log in' # 全军覆没 log(f'restart->{_error}') sleep(3)
def log_es2(r, es2_list): # 指定登录账号 es2 = ES('Bin/Data/APP2/9762.exe', r, app_id='2') es2.config_re_login(30) # 重连时间,s es2.config_account(ip=CONFIG['ip'], port=CONFIG['port'], username=CONFIG['username'], password=CONFIG['password'], auth_code=CONFIG['auth_code']) if asset_success(es2, '2'): # 登录成功 log('2开始订阅') for i in es2_list: es2.config_subscribes(i) # 订阅 log('2订阅完毕,等待交易所') return es2 return None
def waiting_trading_time(): now_time = ar.now() if now_time.isoweekday() >= 6: target_time = now_time.shift(days=8 - now_time.isoweekday()).replace( hour=9, minute=30).floor('minute') elif now_time < now_time.replace(hour=9, minute=30).floor('minute'): target_time = now_time.replace(hour=9, minute=30).floor('minute') elif now_time.replace(hour=11, minute=30).floor( 'minute') < now_time < now_time.replace(hour=13).floor('hour'): target_time = now_time.replace(hour=13).floor('hour') elif now_time > now_time.replace(hour=15).floor('hour'): target_time = now_time.shift(days=1).replace( hour=9, minute=30).floor('minute') else: target_time = now_time if target_time > now_time: sleep_time = target_time.float_timestamp - now_time.float_timestamp log(f'当前不是开盘时间,重置shares.lc并等待{sleep_time}s,再行判断') Pth('Bin\\Config\\alter.lc').remove_p() sleep(sleep_time)
def run_my_config(): while 1: button = eg.buttonbox(msg="请选择操作", title="博弈资产自动盯盘V1.0", choices=["盯盘", "配置", "退出"]) if button == "盯盘": share = Shares() share.loop() elif button == "配置": config_file = eg.fileopenbox(msg="请选择需要监控的股票代码\n每行有且仅有一支股票", title="博弈资产自动盯盘V1.0") filename = Pth(".\\Bin\\Config\\Shares.txt") if config_file: filename_exists(filename.parent) Pth(config_file).copyfile(filename) shares = filename.text() log('配置完成') eg.msgbox(f'配置完成\n{shares}') else: log('未输入任何股票代码') eg.msgbox('未输入任何股票代码') else: sys.exit()
def asset_success(es, exe_id='1'): for _ in range(500): es.reading_out() if es.success: print('for_num', _) log(f'{exe_id}登录成功') return True elif es.error_: log(f'{exe_id}登录失败->{es.error_}') return False else: log(f'{exe_id}登录未知') es.kill(f'{exe_id}Kill') return False
def get_price(self, share): data = self.trader.get_last(share) if data: self.load() rate = (data['ask1'] / data['prev_close'] - 1) * 100 pre_rate = self.alter.get(share, 0) if rate >= 3 > pre_rate: log(f'{data["name"]}涨{rate}') rate = int(rate) speak_thread(f'{data["name"]}涨{rate}、涨{rate}') elif rate >= 5 > pre_rate: log(f'{data["name"]}涨{rate}') rate = int(rate) speak_thread(f'{data["name"]}涨{rate}、涨{rate}') elif rate >= 6 > pre_rate: log(f'{data["name"]}涨{rate}') rate = int(rate) speak_thread(f'{data["name"]}涨{rate}、涨{rate}') self.alter[share] = rate self.dump()
def welcome(): log('博弈资产盯盘软件V1.0开始运行') speak_thread('博弈资产盯盘软件V1.0开始运行')